1457c8996SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2e1069839SBorislav Petkov /*
3e1069839SBorislav Petkov * Per core/cpu state
4e1069839SBorislav Petkov *
5e1069839SBorislav Petkov * Used to coordinate shared registers between HT threads or
6e1069839SBorislav Petkov * among events on a single PMU.
7e1069839SBorislav Petkov */
8e1069839SBorislav Petkov
9e1069839SBorislav Petkov #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10e1069839SBorislav Petkov
11e1069839SBorislav Petkov #include <linux/stddef.h>
12e1069839SBorislav Petkov #include <linux/types.h>
13e1069839SBorislav Petkov #include <linux/init.h>
14e1069839SBorislav Petkov #include <linux/slab.h>
15e1069839SBorislav Petkov #include <linux/export.h>
16e1069839SBorislav Petkov #include <linux/nmi.h>
178183a538SLike Xu #include <linux/kvm_host.h>
18e1069839SBorislav Petkov
19e1069839SBorislav Petkov #include <asm/cpufeature.h>
20e1069839SBorislav Petkov #include <asm/hardirq.h>
21ef5f9f47SDave Hansen #include <asm/intel-family.h>
2242880f72SAlexander Shishkin #include <asm/intel_pt.h>
23e1069839SBorislav Petkov #include <asm/apic.h>
249b545c04SAndi Kleen #include <asm/cpu_device_id.h>
25e1069839SBorislav Petkov
2627f6d22bSBorislav Petkov #include "../perf_event.h"
27e1069839SBorislav Petkov
28e1069839SBorislav Petkov /*
29e1069839SBorislav Petkov * Intel PerfMon, used on Core and later.
30e1069839SBorislav Petkov */
31e1069839SBorislav Petkov static u64 intel_perfmon_event_map[PERF_COUNT_HW_MAX] __read_mostly =
32e1069839SBorislav Petkov {
33e1069839SBorislav Petkov [PERF_COUNT_HW_CPU_CYCLES] = 0x003c,
34e1069839SBorislav Petkov [PERF_COUNT_HW_INSTRUCTIONS] = 0x00c0,
35e1069839SBorislav Petkov [PERF_COUNT_HW_CACHE_REFERENCES] = 0x4f2e,
36e1069839SBorislav Petkov [PERF_COUNT_HW_CACHE_MISSES] = 0x412e,
37e1069839SBorislav Petkov [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = 0x00c4,
38e1069839SBorislav Petkov [PERF_COUNT_HW_BRANCH_MISSES] = 0x00c5,
39e1069839SBorislav Petkov [PERF_COUNT_HW_BUS_CYCLES] = 0x013c,
40e1069839SBorislav Petkov [PERF_COUNT_HW_REF_CPU_CYCLES] = 0x0300, /* pseudo-encoding */
41e1069839SBorislav Petkov };
42e1069839SBorislav Petkov
43e1069839SBorislav Petkov static struct event_constraint intel_core_event_constraints[] __read_mostly =
44e1069839SBorislav Petkov {
45e1069839SBorislav Petkov INTEL_EVENT_CONSTRAINT(0x11, 0x2), /* FP_ASSIST */
46e1069839SBorislav Petkov INTEL_EVENT_CONSTRAINT(0x12, 0x2), /* MUL */
47e1069839SBorislav Petkov INTEL_EVENT_CONSTRAINT(0x13, 0x2), /* DIV */
48e1069839SBorislav Petkov INTEL_EVENT_CONSTRAINT(0x14, 0x1), /* CYCLES_DIV_BUSY */
49e1069839SBorislav Petkov INTEL_EVENT_CONSTRAINT(0x19, 0x2), /* DELAYED_BYPASS */
50e1069839SBorislav Petkov INTEL_EVENT_CONSTRAINT(0xc1, 0x1), /* FP_COMP_INSTR_RET */
51e1069839SBorislav Petkov EVENT_CONSTRAINT_END
52e1069839SBorislav Petkov };
53e1069839SBorislav Petkov
54e1069839SBorislav Petkov static struct event_constraint intel_core2_event_constraints[] __read_mostly =
55e1069839SBorislav Petkov {
56e1069839SBorislav Petkov FIXED_EVENT_CONSTRAINT(0x00c0, 0), /* INST_RETIRED.ANY */
57e1069839SBorislav Petkov FIXED_EVENT_CONSTRAINT(0x003c, 1), /* CPU_CLK_UNHALTED.CORE */
58e1069839SBorislav Petkov FIXED_EVENT_CONSTRAINT(0x0300, 2), /* CPU_CLK_UNHALTED.REF */
59e1069839SBorislav Petkov INTEL_EVENT_CONSTRAINT(0x10, 0x1), /* FP_COMP_OPS_EXE */
60e1069839SBorislav Petkov INTEL_EVENT_CONSTRAINT(0x11, 0x2), /* FP_ASSIST */
61e1069839SBorislav Petkov INTEL_EVENT_CONSTRAINT(0x12, 0x2), /* MUL */
62e1069839SBorislav Petkov INTEL_EVENT_CONSTRAINT(0x13, 0x2), /* DIV */
63e1069839SBorislav Petkov INTEL_EVENT_CONSTRAINT(0x14, 0x1), /* CYCLES_DIV_BUSY */
64e1069839SBorislav Petkov INTEL_EVENT_CONSTRAINT(0x18, 0x1), /* IDLE_DURING_DIV */
65e1069839SBorislav Petkov INTEL_EVENT_CONSTRAINT(0x19, 0x2), /* DELAYED_BYPASS */
66e1069839SBorislav Petkov INTEL_EVENT_CONSTRAINT(0xa1, 0x1), /* RS_UOPS_DISPATCH_CYCLES */
67e1069839SBorislav Petkov INTEL_EVENT_CONSTRAINT(0xc9, 0x1), /* ITLB_MISS_RETIRED (T30-9) */
68e1069839SBorislav Petkov INTEL_EVENT_CONSTRAINT(0xcb, 0x1), /* MEM_LOAD_RETIRED */
69e1069839SBorislav Petkov EVENT_CONSTRAINT_END
70e1069839SBorislav Petkov };
71e1069839SBorislav Petkov
72e1069839SBorislav Petkov static struct event_constraint intel_nehalem_event_constraints[] __read_mostly =
73e1069839SBorislav Petkov {
74e1069839SBorislav Petkov FIXED_EVENT_CONSTRAINT(0x00c0, 0), /* INST_RETIRED.ANY */
75e1069839SBorislav Petkov FIXED_EVENT_CONSTRAINT(0x003c, 1), /* CPU_CLK_UNHALTED.CORE */
76e1069839SBorislav Petkov FIXED_EVENT_CONSTRAINT(0x0300, 2), /* CPU_CLK_UNHALTED.REF */
77e1069839SBorislav Petkov INTEL_EVENT_CONSTRAINT(0x40, 0x3), /* L1D_CACHE_LD */
78e1069839SBorislav Petkov INTEL_EVENT_CONSTRAINT(0x41, 0x3), /* L1D_CACHE_ST */
79e1069839SBorislav Petkov INTEL_EVENT_CONSTRAINT(0x42, 0x3), /* L1D_CACHE_LOCK */
80e1069839SBorislav Petkov INTEL_EVENT_CONSTRAINT(0x43, 0x3), /* L1D_ALL_REF */
81e1069839SBorislav Petkov INTEL_EVENT_CONSTRAINT(0x48, 0x3), /* L1D_PEND_MISS */
82e1069839SBorislav Petkov INTEL_EVENT_CONSTRAINT(0x4e, 0x3), /* L1D_PREFETCH */
83e1069839SBorislav Petkov INTEL_EVENT_CONSTRAINT(0x51, 0x3), /* L1D */
84e1069839SBorislav Petkov INTEL_EVENT_CONSTRAINT(0x63, 0x3), /* CACHE_LOCK_CYCLES */
85e1069839SBorislav Petkov EVENT_CONSTRAINT_END
86e1069839SBorislav Petkov };
87e1069839SBorislav Petkov
88e1069839SBorislav Petkov static struct extra_reg intel_nehalem_extra_regs[] __read_mostly =
89e1069839SBorislav Petkov {
90e1069839SBorislav Petkov /* must define OFFCORE_RSP_X first, see intel_fixup_er() */
91e1069839SBorislav Petkov INTEL_UEVENT_EXTRA_REG(0x01b7, MSR_OFFCORE_RSP_0, 0xffff, RSP_0),
92e1069839SBorislav Petkov INTEL_UEVENT_PEBS_LDLAT_EXTRA_REG(0x100b),
93e1069839SBorislav Petkov EVENT_EXTRA_END
94e1069839SBorislav Petkov };
95e1069839SBorislav Petkov
96e1069839SBorislav Petkov static struct event_constraint intel_westmere_event_constraints[] __read_mostly =
97e1069839SBorislav Petkov {
98e1069839SBorislav Petkov FIXED_EVENT_CONSTRAINT(0x00c0, 0), /* INST_RETIRED.ANY */
99e1069839SBorislav Petkov FIXED_EVENT_CONSTRAINT(0x003c, 1), /* CPU_CLK_UNHALTED.CORE */
100e1069839SBorislav Petkov FIXED_EVENT_CONSTRAINT(0x0300, 2), /* CPU_CLK_UNHALTED.REF */
101e1069839SBorislav Petkov INTEL_EVENT_CONSTRAINT(0x51, 0x3), /* L1D */
102e1069839SBorislav Petkov INTEL_EVENT_CONSTRAINT(0x60, 0x1), /* OFFCORE_REQUESTS_OUTSTANDING */
103e1069839SBorislav Petkov INTEL_EVENT_CONSTRAINT(0x63, 0x3), /* CACHE_LOCK_CYCLES */
104e1069839SBorislav Petkov INTEL_EVENT_CONSTRAINT(0xb3, 0x1), /* SNOOPQ_REQUEST_OUTSTANDING */
105e1069839SBorislav Petkov EVENT_CONSTRAINT_END
106e1069839SBorislav Petkov };
107e1069839SBorislav Petkov
108e1069839SBorislav Petkov static struct event_constraint intel_snb_event_constraints[] __read_mostly =
109e1069839SBorislav Petkov {
110e1069839SBorislav Petkov FIXED_EVENT_CONSTRAINT(0x00c0, 0), /* INST_RETIRED.ANY */
111e1069839SBorislav Petkov FIXED_EVENT_CONSTRAINT(0x003c, 1), /* CPU_CLK_UNHALTED.CORE */
112e1069839SBorislav Petkov FIXED_EVENT_CONSTRAINT(0x0300, 2), /* CPU_CLK_UNHALTED.REF */
113e1069839SBorislav Petkov INTEL_UEVENT_CONSTRAINT(0x04a3, 0xf), /* CYCLE_ACTIVITY.CYCLES_NO_DISPATCH */
114e1069839SBorislav Petkov INTEL_UEVENT_CONSTRAINT(0x05a3, 0xf), /* CYCLE_ACTIVITY.STALLS_L2_PENDING */
115e1069839SBorislav Petkov INTEL_UEVENT_CONSTRAINT(0x02a3, 0x4), /* CYCLE_ACTIVITY.CYCLES_L1D_PENDING */
116e1069839SBorislav Petkov INTEL_UEVENT_CONSTRAINT(0x06a3, 0x4), /* CYCLE_ACTIVITY.STALLS_L1D_PENDING */
117e1069839SBorislav Petkov INTEL_EVENT_CONSTRAINT(0x48, 0x4), /* L1D_PEND_MISS.PENDING */
118e1069839SBorislav Petkov INTEL_UEVENT_CONSTRAINT(0x01c0, 0x2), /* INST_RETIRED.PREC_DIST */
119e1069839SBorislav Petkov INTEL_EVENT_CONSTRAINT(0xcd, 0x8), /* MEM_TRANS_RETIRED.LOAD_LATENCY */
120e1069839SBorislav Petkov INTEL_UEVENT_CONSTRAINT(0x04a3, 0xf), /* CYCLE_ACTIVITY.CYCLES_NO_DISPATCH */
121e1069839SBorislav Petkov INTEL_UEVENT_CONSTRAINT(0x02a3, 0x4), /* CYCLE_ACTIVITY.CYCLES_L1D_PENDING */
122e1069839SBorislav Petkov
1239010ae4aSStephane Eranian /*
1249010ae4aSStephane Eranian * When HT is off these events can only run on the bottom 4 counters
1259010ae4aSStephane Eranian * When HT is on, they are impacted by the HT bug and require EXCL access
1269010ae4aSStephane Eranian */
127e1069839SBorislav Petkov INTEL_EXCLEVT_CONSTRAINT(0xd0, 0xf), /* MEM_UOPS_RETIRED.* */
128e1069839SBorislav Petkov INTEL_EXCLEVT_CONSTRAINT(0xd1, 0xf), /* MEM_LOAD_UOPS_RETIRED.* */
129e1069839SBorislav Petkov INTEL_EXCLEVT_CONSTRAINT(0xd2, 0xf), /* MEM_LOAD_UOPS_LLC_HIT_RETIRED.* */
130e1069839SBorislav Petkov INTEL_EXCLEVT_CONSTRAINT(0xd3, 0xf), /* MEM_LOAD_UOPS_LLC_MISS_RETIRED.* */
131e1069839SBorislav Petkov
132e1069839SBorislav Petkov EVENT_CONSTRAINT_END
133e1069839SBorislav Petkov };
134e1069839SBorislav Petkov
135e1069839SBorislav Petkov static struct event_constraint intel_ivb_event_constraints[] __read_mostly =
136e1069839SBorislav Petkov {
137e1069839SBorislav Petkov FIXED_EVENT_CONSTRAINT(0x00c0, 0), /* INST_RETIRED.ANY */
138e1069839SBorislav Petkov FIXED_EVENT_CONSTRAINT(0x003c, 1), /* CPU_CLK_UNHALTED.CORE */
139e1069839SBorislav Petkov FIXED_EVENT_CONSTRAINT(0x0300, 2), /* CPU_CLK_UNHALTED.REF */
140e1069839SBorislav Petkov INTEL_UEVENT_CONSTRAINT(0x0148, 0x4), /* L1D_PEND_MISS.PENDING */
141d9f6e12fSIngo Molnar INTEL_UEVENT_CONSTRAINT(0x0279, 0xf), /* IDQ.EMPTY */
142e1069839SBorislav Petkov INTEL_UEVENT_CONSTRAINT(0x019c, 0xf), /* IDQ_UOPS_NOT_DELIVERED.CORE */
143e1069839SBorislav Petkov INTEL_UEVENT_CONSTRAINT(0x02a3, 0xf), /* CYCLE_ACTIVITY.CYCLES_LDM_PENDING */
144e1069839SBorislav Petkov INTEL_UEVENT_CONSTRAINT(0x04a3, 0xf), /* CYCLE_ACTIVITY.CYCLES_NO_EXECUTE */
145e1069839SBorislav Petkov INTEL_UEVENT_CONSTRAINT(0x05a3, 0xf), /* CYCLE_ACTIVITY.STALLS_L2_PENDING */
146e1069839SBorislav Petkov INTEL_UEVENT_CONSTRAINT(0x06a3, 0xf), /* CYCLE_ACTIVITY.STALLS_LDM_PENDING */
147e1069839SBorislav Petkov INTEL_UEVENT_CONSTRAINT(0x08a3, 0x4), /* CYCLE_ACTIVITY.CYCLES_L1D_PENDING */
148e1069839SBorislav Petkov INTEL_UEVENT_CONSTRAINT(0x0ca3, 0x4), /* CYCLE_ACTIVITY.STALLS_L1D_PENDING */
149e1069839SBorislav Petkov INTEL_UEVENT_CONSTRAINT(0x01c0, 0x2), /* INST_RETIRED.PREC_DIST */
150e1069839SBorislav Petkov
1519010ae4aSStephane Eranian /*
1529010ae4aSStephane Eranian * When HT is off these events can only run on the bottom 4 counters
1539010ae4aSStephane Eranian * When HT is on, they are impacted by the HT bug and require EXCL access
1549010ae4aSStephane Eranian */
155e1069839SBorislav Petkov INTEL_EXCLEVT_CONSTRAINT(0xd0, 0xf), /* MEM_UOPS_RETIRED.* */
156e1069839SBorislav Petkov INTEL_EXCLEVT_CONSTRAINT(0xd1, 0xf), /* MEM_LOAD_UOPS_RETIRED.* */
157e1069839SBorislav Petkov INTEL_EXCLEVT_CONSTRAINT(0xd2, 0xf), /* MEM_LOAD_UOPS_LLC_HIT_RETIRED.* */
158e1069839SBorislav Petkov INTEL_EXCLEVT_CONSTRAINT(0xd3, 0xf), /* MEM_LOAD_UOPS_LLC_MISS_RETIRED.* */
159e1069839SBorislav Petkov
160e1069839SBorislav Petkov EVENT_CONSTRAINT_END
161e1069839SBorislav Petkov };
162e1069839SBorislav Petkov
163e1069839SBorislav Petkov static struct extra_reg intel_westmere_extra_regs[] __read_mostly =
164e1069839SBorislav Petkov {
165e1069839SBorislav Petkov /* must define OFFCORE_RSP_X first, see intel_fixup_er() */
166e1069839SBorislav Petkov INTEL_UEVENT_EXTRA_REG(0x01b7, MSR_OFFCORE_RSP_0, 0xffff, RSP_0),
167e1069839SBorislav Petkov INTEL_UEVENT_EXTRA_REG(0x01bb, MSR_OFFCORE_RSP_1, 0xffff, RSP_1),
168e1069839SBorislav Petkov INTEL_UEVENT_PEBS_LDLAT_EXTRA_REG(0x100b),
169e1069839SBorislav Petkov EVENT_EXTRA_END
170e1069839SBorislav Petkov };
171e1069839SBorislav Petkov
172e1069839SBorislav Petkov static struct event_constraint intel_v1_event_constraints[] __read_mostly =
173e1069839SBorislav Petkov {
174e1069839SBorislav Petkov EVENT_CONSTRAINT_END
175e1069839SBorislav Petkov };
176e1069839SBorislav Petkov
177e1069839SBorislav Petkov static struct event_constraint intel_gen_event_constraints[] __read_mostly =
178e1069839SBorislav Petkov {
179e1069839SBorislav Petkov FIXED_EVENT_CONSTRAINT(0x00c0, 0), /* INST_RETIRED.ANY */
180e1069839SBorislav Petkov FIXED_EVENT_CONSTRAINT(0x003c, 1), /* CPU_CLK_UNHALTED.CORE */
181e1069839SBorislav Petkov FIXED_EVENT_CONSTRAINT(0x0300, 2), /* CPU_CLK_UNHALTED.REF */
182e1069839SBorislav Petkov EVENT_CONSTRAINT_END
183e1069839SBorislav Petkov };
184e1069839SBorislav Petkov
185ee28855aSKan Liang static struct event_constraint intel_v5_gen_event_constraints[] __read_mostly =
186ee28855aSKan Liang {
187ee28855aSKan Liang FIXED_EVENT_CONSTRAINT(0x00c0, 0), /* INST_RETIRED.ANY */
188ee28855aSKan Liang FIXED_EVENT_CONSTRAINT(0x003c, 1), /* CPU_CLK_UNHALTED.CORE */
189ee28855aSKan Liang FIXED_EVENT_CONSTRAINT(0x0300, 2), /* CPU_CLK_UNHALTED.REF */
190ee28855aSKan Liang FIXED_EVENT_CONSTRAINT(0x0400, 3), /* SLOTS */
191ee28855aSKan Liang FIXED_EVENT_CONSTRAINT(0x0500, 4),
192ee28855aSKan Liang FIXED_EVENT_CONSTRAINT(0x0600, 5),
193ee28855aSKan Liang FIXED_EVENT_CONSTRAINT(0x0700, 6),
194ee28855aSKan Liang FIXED_EVENT_CONSTRAINT(0x0800, 7),
195ee28855aSKan Liang FIXED_EVENT_CONSTRAINT(0x0900, 8),
196ee28855aSKan Liang FIXED_EVENT_CONSTRAINT(0x0a00, 9),
197ee28855aSKan Liang FIXED_EVENT_CONSTRAINT(0x0b00, 10),
198ee28855aSKan Liang FIXED_EVENT_CONSTRAINT(0x0c00, 11),
199ee28855aSKan Liang FIXED_EVENT_CONSTRAINT(0x0d00, 12),
200ee28855aSKan Liang FIXED_EVENT_CONSTRAINT(0x0e00, 13),
201ee28855aSKan Liang FIXED_EVENT_CONSTRAINT(0x0f00, 14),
202ee28855aSKan Liang FIXED_EVENT_CONSTRAINT(0x1000, 15),
203ee28855aSKan Liang EVENT_CONSTRAINT_END
204ee28855aSKan Liang };
205ee28855aSKan Liang
206e1069839SBorislav Petkov static struct event_constraint intel_slm_event_constraints[] __read_mostly =
207e1069839SBorislav Petkov {
208e1069839SBorislav Petkov FIXED_EVENT_CONSTRAINT(0x00c0, 0), /* INST_RETIRED.ANY */
209e1069839SBorislav Petkov FIXED_EVENT_CONSTRAINT(0x003c, 1), /* CPU_CLK_UNHALTED.CORE */
210e1069839SBorislav Petkov FIXED_EVENT_CONSTRAINT(0x0300, 2), /* pseudo CPU_CLK_UNHALTED.REF */
211e1069839SBorislav Petkov EVENT_CONSTRAINT_END
212e1069839SBorislav Petkov };
213e1069839SBorislav Petkov
21420f36278SLukasz Odzioba static struct event_constraint intel_skl_event_constraints[] = {
215e1069839SBorislav Petkov FIXED_EVENT_CONSTRAINT(0x00c0, 0), /* INST_RETIRED.ANY */
216e1069839SBorislav Petkov FIXED_EVENT_CONSTRAINT(0x003c, 1), /* CPU_CLK_UNHALTED.CORE */
217e1069839SBorislav Petkov FIXED_EVENT_CONSTRAINT(0x0300, 2), /* CPU_CLK_UNHALTED.REF */
218e1069839SBorislav Petkov INTEL_UEVENT_CONSTRAINT(0x1c0, 0x2), /* INST_RETIRED.PREC_DIST */
2199010ae4aSStephane Eranian
2209010ae4aSStephane Eranian /*
2219010ae4aSStephane Eranian * when HT is off, these can only run on the bottom 4 counters
2229010ae4aSStephane Eranian */
2239010ae4aSStephane Eranian INTEL_EVENT_CONSTRAINT(0xd0, 0xf), /* MEM_INST_RETIRED.* */
2249010ae4aSStephane Eranian INTEL_EVENT_CONSTRAINT(0xd1, 0xf), /* MEM_LOAD_RETIRED.* */
2259010ae4aSStephane Eranian INTEL_EVENT_CONSTRAINT(0xd2, 0xf), /* MEM_LOAD_L3_HIT_RETIRED.* */
2269010ae4aSStephane Eranian INTEL_EVENT_CONSTRAINT(0xcd, 0xf), /* MEM_TRANS_RETIRED.* */
2279010ae4aSStephane Eranian INTEL_EVENT_CONSTRAINT(0xc6, 0xf), /* FRONTEND_RETIRED.* */
2289010ae4aSStephane Eranian
229e1069839SBorislav Petkov EVENT_CONSTRAINT_END
230e1069839SBorislav Petkov };
231e1069839SBorislav Petkov
232e1069839SBorislav Petkov static struct extra_reg intel_knl_extra_regs[] __read_mostly = {
2339c489fceSLukasz Odzioba INTEL_UEVENT_EXTRA_REG(0x01b7, MSR_OFFCORE_RSP_0, 0x799ffbb6e7ull, RSP_0),
2349c489fceSLukasz Odzioba INTEL_UEVENT_EXTRA_REG(0x02b7, MSR_OFFCORE_RSP_1, 0x399ffbffe7ull, RSP_1),
235e1069839SBorislav Petkov EVENT_EXTRA_END
236e1069839SBorislav Petkov };
237e1069839SBorislav Petkov
238e1069839SBorislav Petkov static struct extra_reg intel_snb_extra_regs[] __read_mostly = {
239e1069839SBorislav Petkov /* must define OFFCORE_RSP_X first, see intel_fixup_er() */
240e1069839SBorislav Petkov INTEL_UEVENT_EXTRA_REG(0x01b7, MSR_OFFCORE_RSP_0, 0x3f807f8fffull, RSP_0),
241e1069839SBorislav Petkov INTEL_UEVENT_EXTRA_REG(0x01bb, MSR_OFFCORE_RSP_1, 0x3f807f8fffull, RSP_1),
242e1069839SBorislav Petkov INTEL_UEVENT_PEBS_LDLAT_EXTRA_REG(0x01cd),
243e1069839SBorislav Petkov EVENT_EXTRA_END
244e1069839SBorislav Petkov };
245e1069839SBorislav Petkov
246e1069839SBorislav Petkov static struct extra_reg intel_snbep_extra_regs[] __read_mostly = {
247e1069839SBorislav Petkov /* must define OFFCORE_RSP_X first, see intel_fixup_er() */
248e1069839SBorislav Petkov INTEL_UEVENT_EXTRA_REG(0x01b7, MSR_OFFCORE_RSP_0, 0x3fffff8fffull, RSP_0),
249e1069839SBorislav Petkov INTEL_UEVENT_EXTRA_REG(0x01bb, MSR_OFFCORE_RSP_1, 0x3fffff8fffull, RSP_1),
250e1069839SBorislav Petkov INTEL_UEVENT_PEBS_LDLAT_EXTRA_REG(0x01cd),
251e1069839SBorislav Petkov EVENT_EXTRA_END
252e1069839SBorislav Petkov };
253e1069839SBorislav Petkov
254e1069839SBorislav Petkov static struct extra_reg intel_skl_extra_regs[] __read_mostly = {
255e1069839SBorislav Petkov INTEL_UEVENT_EXTRA_REG(0x01b7, MSR_OFFCORE_RSP_0, 0x3fffff8fffull, RSP_0),
256e1069839SBorislav Petkov INTEL_UEVENT_EXTRA_REG(0x01bb, MSR_OFFCORE_RSP_1, 0x3fffff8fffull, RSP_1),
257e1069839SBorislav Petkov INTEL_UEVENT_PEBS_LDLAT_EXTRA_REG(0x01cd),
258e1069839SBorislav Petkov /*
259e1069839SBorislav Petkov * Note the low 8 bits eventsel code is not a continuous field, containing
260e1069839SBorislav Petkov * some #GPing bits. These are masked out.
261e1069839SBorislav Petkov */
262e1069839SBorislav Petkov INTEL_UEVENT_EXTRA_REG(0x01c6, MSR_PEBS_FRONTEND, 0x7fff17, FE),
263e1069839SBorislav Petkov EVENT_EXTRA_END
264e1069839SBorislav Petkov };
265e1069839SBorislav Petkov
26660176089SKan Liang static struct event_constraint intel_icl_event_constraints[] = {
26760176089SKan Liang FIXED_EVENT_CONSTRAINT(0x00c0, 0), /* INST_RETIRED.ANY */
2682de71ee1SStephane Eranian FIXED_EVENT_CONSTRAINT(0x01c0, 0), /* old INST_RETIRED.PREC_DIST */
2692de71ee1SStephane Eranian FIXED_EVENT_CONSTRAINT(0x0100, 0), /* INST_RETIRED.PREC_DIST */
27060176089SKan Liang FIXED_EVENT_CONSTRAINT(0x003c, 1), /* CPU_CLK_UNHALTED.CORE */
27160176089SKan Liang FIXED_EVENT_CONSTRAINT(0x0300, 2), /* CPU_CLK_UNHALTED.REF */
27260176089SKan Liang FIXED_EVENT_CONSTRAINT(0x0400, 3), /* SLOTS */
27359a854e2SKan Liang METRIC_EVENT_CONSTRAINT(INTEL_TD_METRIC_RETIRING, 0),
27459a854e2SKan Liang METRIC_EVENT_CONSTRAINT(INTEL_TD_METRIC_BAD_SPEC, 1),
27559a854e2SKan Liang METRIC_EVENT_CONSTRAINT(INTEL_TD_METRIC_FE_BOUND, 2),
27659a854e2SKan Liang METRIC_EVENT_CONSTRAINT(INTEL_TD_METRIC_BE_BOUND, 3),
27760176089SKan Liang INTEL_EVENT_CONSTRAINT_RANGE(0x03, 0x0a, 0xf),
27860176089SKan Liang INTEL_EVENT_CONSTRAINT_RANGE(0x1f, 0x28, 0xf),
27960176089SKan Liang INTEL_EVENT_CONSTRAINT(0x32, 0xf), /* SW_PREFETCH_ACCESS.* */
28086dca369SKan Liang INTEL_EVENT_CONSTRAINT_RANGE(0x48, 0x56, 0xf),
28160176089SKan Liang INTEL_EVENT_CONSTRAINT_RANGE(0x60, 0x8b, 0xf),
28260176089SKan Liang INTEL_UEVENT_CONSTRAINT(0x04a3, 0xff), /* CYCLE_ACTIVITY.STALLS_TOTAL */
283306e3e91SKan Liang INTEL_UEVENT_CONSTRAINT(0x10a3, 0xff), /* CYCLE_ACTIVITY.CYCLES_MEM_ANY */
284306e3e91SKan Liang INTEL_UEVENT_CONSTRAINT(0x14a3, 0xff), /* CYCLE_ACTIVITY.STALLS_MEM_ANY */
28560176089SKan Liang INTEL_EVENT_CONSTRAINT(0xa3, 0xf), /* CYCLE_ACTIVITY.* */
28660176089SKan Liang INTEL_EVENT_CONSTRAINT_RANGE(0xa8, 0xb0, 0xf),
28760176089SKan Liang INTEL_EVENT_CONSTRAINT_RANGE(0xb7, 0xbd, 0xf),
28860176089SKan Liang INTEL_EVENT_CONSTRAINT_RANGE(0xd0, 0xe6, 0xf),
289ecc2123eSKan Liang INTEL_EVENT_CONSTRAINT(0xef, 0xf),
29060176089SKan Liang INTEL_EVENT_CONSTRAINT_RANGE(0xf0, 0xf4, 0xf),
29160176089SKan Liang EVENT_CONSTRAINT_END
29260176089SKan Liang };
29360176089SKan Liang
29460176089SKan Liang static struct extra_reg intel_icl_extra_regs[] __read_mostly = {
2953b238a64SYunying Sun INTEL_UEVENT_EXTRA_REG(0x01b7, MSR_OFFCORE_RSP_0, 0x3fffffbfffull, RSP_0),
2963b238a64SYunying Sun INTEL_UEVENT_EXTRA_REG(0x01bb, MSR_OFFCORE_RSP_1, 0x3fffffbfffull, RSP_1),
29760176089SKan Liang INTEL_UEVENT_PEBS_LDLAT_EXTRA_REG(0x01cd),
29860176089SKan Liang INTEL_UEVENT_EXTRA_REG(0x01c6, MSR_PEBS_FRONTEND, 0x7fff17, FE),
29960176089SKan Liang EVENT_EXTRA_END
30060176089SKan Liang };
30160176089SKan Liang
30261b985e3SKan Liang static struct extra_reg intel_spr_extra_regs[] __read_mostly = {
30361b985e3SKan Liang INTEL_UEVENT_EXTRA_REG(0x012a, MSR_OFFCORE_RSP_0, 0x3fffffffffull, RSP_0),
30461b985e3SKan Liang INTEL_UEVENT_EXTRA_REG(0x012b, MSR_OFFCORE_RSP_1, 0x3fffffffffull, RSP_1),
30561b985e3SKan Liang INTEL_UEVENT_PEBS_LDLAT_EXTRA_REG(0x01cd),
306e590928dSKan Liang INTEL_UEVENT_EXTRA_REG(0x01c6, MSR_PEBS_FRONTEND, 0x7fff1f, FE),
307d18216faSKan Liang INTEL_UEVENT_EXTRA_REG(0x40ad, MSR_PEBS_FRONTEND, 0x7, FE),
308d18216faSKan Liang INTEL_UEVENT_EXTRA_REG(0x04c2, MSR_PEBS_FRONTEND, 0x8, FE),
30961b985e3SKan Liang EVENT_EXTRA_END
31061b985e3SKan Liang };
31161b985e3SKan Liang
31261b985e3SKan Liang static struct event_constraint intel_spr_event_constraints[] = {
31361b985e3SKan Liang FIXED_EVENT_CONSTRAINT(0x00c0, 0), /* INST_RETIRED.ANY */
3142de71ee1SStephane Eranian FIXED_EVENT_CONSTRAINT(0x0100, 0), /* INST_RETIRED.PREC_DIST */
31561b985e3SKan Liang FIXED_EVENT_CONSTRAINT(0x003c, 1), /* CPU_CLK_UNHALTED.CORE */
31661b985e3SKan Liang FIXED_EVENT_CONSTRAINT(0x0300, 2), /* CPU_CLK_UNHALTED.REF */
31761b985e3SKan Liang FIXED_EVENT_CONSTRAINT(0x0400, 3), /* SLOTS */
31861b985e3SKan Liang METRIC_EVENT_CONSTRAINT(INTEL_TD_METRIC_RETIRING, 0),
31961b985e3SKan Liang METRIC_EVENT_CONSTRAINT(INTEL_TD_METRIC_BAD_SPEC, 1),
32061b985e3SKan Liang METRIC_EVENT_CONSTRAINT(INTEL_TD_METRIC_FE_BOUND, 2),
32161b985e3SKan Liang METRIC_EVENT_CONSTRAINT(INTEL_TD_METRIC_BE_BOUND, 3),
32261b985e3SKan Liang METRIC_EVENT_CONSTRAINT(INTEL_TD_METRIC_HEAVY_OPS, 4),
32361b985e3SKan Liang METRIC_EVENT_CONSTRAINT(INTEL_TD_METRIC_BR_MISPREDICT, 5),
32461b985e3SKan Liang METRIC_EVENT_CONSTRAINT(INTEL_TD_METRIC_FETCH_LAT, 6),
32561b985e3SKan Liang METRIC_EVENT_CONSTRAINT(INTEL_TD_METRIC_MEM_BOUND, 7),
32661b985e3SKan Liang
32761b985e3SKan Liang INTEL_EVENT_CONSTRAINT(0x2e, 0xff),
32861b985e3SKan Liang INTEL_EVENT_CONSTRAINT(0x3c, 0xff),
32961b985e3SKan Liang /*
33061b985e3SKan Liang * Generally event codes < 0x90 are restricted to counters 0-3.
33161b985e3SKan Liang * The 0x2E and 0x3C are exception, which has no restriction.
33261b985e3SKan Liang */
33361b985e3SKan Liang INTEL_EVENT_CONSTRAINT_RANGE(0x01, 0x8f, 0xf),
33461b985e3SKan Liang
33561b985e3SKan Liang INTEL_UEVENT_CONSTRAINT(0x01a3, 0xf),
33661b985e3SKan Liang INTEL_UEVENT_CONSTRAINT(0x02a3, 0xf),
33761b985e3SKan Liang INTEL_UEVENT_CONSTRAINT(0x08a3, 0xf),
33861b985e3SKan Liang INTEL_UEVENT_CONSTRAINT(0x04a4, 0x1),
33961b985e3SKan Liang INTEL_UEVENT_CONSTRAINT(0x08a4, 0x1),
34061b985e3SKan Liang INTEL_UEVENT_CONSTRAINT(0x02cd, 0x1),
34161b985e3SKan Liang INTEL_EVENT_CONSTRAINT(0xce, 0x1),
34261b985e3SKan Liang INTEL_EVENT_CONSTRAINT_RANGE(0xd0, 0xdf, 0xf),
34361b985e3SKan Liang /*
34461b985e3SKan Liang * Generally event codes >= 0x90 are likely to have no restrictions.
34561b985e3SKan Liang * The exception are defined as above.
34661b985e3SKan Liang */
34761b985e3SKan Liang INTEL_EVENT_CONSTRAINT_RANGE(0x90, 0xfe, 0xff),
34861b985e3SKan Liang
34961b985e3SKan Liang EVENT_CONSTRAINT_END
35061b985e3SKan Liang };
35161b985e3SKan Liang
352a6742cb9SKan Liang static struct extra_reg intel_gnr_extra_regs[] __read_mostly = {
353a6742cb9SKan Liang INTEL_UEVENT_EXTRA_REG(0x012a, MSR_OFFCORE_RSP_0, 0x3fffffffffull, RSP_0),
354a6742cb9SKan Liang INTEL_UEVENT_EXTRA_REG(0x012b, MSR_OFFCORE_RSP_1, 0x3fffffffffull, RSP_1),
355a6742cb9SKan Liang INTEL_UEVENT_PEBS_LDLAT_EXTRA_REG(0x01cd),
356a6742cb9SKan Liang INTEL_UEVENT_EXTRA_REG(0x02c6, MSR_PEBS_FRONTEND, 0x9, FE),
357a6742cb9SKan Liang INTEL_UEVENT_EXTRA_REG(0x03c6, MSR_PEBS_FRONTEND, 0x7fff1f, FE),
358a6742cb9SKan Liang INTEL_UEVENT_EXTRA_REG(0x40ad, MSR_PEBS_FRONTEND, 0x7, FE),
359a6742cb9SKan Liang INTEL_UEVENT_EXTRA_REG(0x04c2, MSR_PEBS_FRONTEND, 0x8, FE),
360a6742cb9SKan Liang EVENT_EXTRA_END
361a6742cb9SKan Liang };
36261b985e3SKan Liang
363e1069839SBorislav Petkov EVENT_ATTR_STR(mem-loads, mem_ld_nhm, "event=0x0b,umask=0x10,ldlat=3");
364e1069839SBorislav Petkov EVENT_ATTR_STR(mem-loads, mem_ld_snb, "event=0xcd,umask=0x1,ldlat=3");
365e1069839SBorislav Petkov EVENT_ATTR_STR(mem-stores, mem_st_snb, "event=0xcd,umask=0x2");
366e1069839SBorislav Petkov
367d4ae5529SJiri Olsa static struct attribute *nhm_mem_events_attrs[] = {
368e1069839SBorislav Petkov EVENT_PTR(mem_ld_nhm),
369e1069839SBorislav Petkov NULL,
370e1069839SBorislav Petkov };
371e1069839SBorislav Petkov
372a39fcae7SAndi Kleen /*
373a39fcae7SAndi Kleen * topdown events for Intel Core CPUs.
374a39fcae7SAndi Kleen *
375a39fcae7SAndi Kleen * The events are all in slots, which is a free slot in a 4 wide
376a39fcae7SAndi Kleen * pipeline. Some events are already reported in slots, for cycle
377a39fcae7SAndi Kleen * events we multiply by the pipeline width (4).
378a39fcae7SAndi Kleen *
379a39fcae7SAndi Kleen * With Hyper Threading on, topdown metrics are either summed or averaged
380a39fcae7SAndi Kleen * between the threads of a core: (count_t0 + count_t1).
381a39fcae7SAndi Kleen *
382a39fcae7SAndi Kleen * For the average case the metric is always scaled to pipeline width,
383a39fcae7SAndi Kleen * so we use factor 2 ((count_t0 + count_t1) / 2 * 4)
384a39fcae7SAndi Kleen */
385a39fcae7SAndi Kleen
386a39fcae7SAndi Kleen EVENT_ATTR_STR_HT(topdown-total-slots, td_total_slots,
387a39fcae7SAndi Kleen "event=0x3c,umask=0x0", /* cpu_clk_unhalted.thread */
388a39fcae7SAndi Kleen "event=0x3c,umask=0x0,any=1"); /* cpu_clk_unhalted.thread_any */
389a39fcae7SAndi Kleen EVENT_ATTR_STR_HT(topdown-total-slots.scale, td_total_slots_scale, "4", "2");
390a39fcae7SAndi Kleen EVENT_ATTR_STR(topdown-slots-issued, td_slots_issued,
391a39fcae7SAndi Kleen "event=0xe,umask=0x1"); /* uops_issued.any */
392a39fcae7SAndi Kleen EVENT_ATTR_STR(topdown-slots-retired, td_slots_retired,
393a39fcae7SAndi Kleen "event=0xc2,umask=0x2"); /* uops_retired.retire_slots */
394a39fcae7SAndi Kleen EVENT_ATTR_STR(topdown-fetch-bubbles, td_fetch_bubbles,
395a39fcae7SAndi Kleen "event=0x9c,umask=0x1"); /* idq_uops_not_delivered_core */
396a39fcae7SAndi Kleen EVENT_ATTR_STR_HT(topdown-recovery-bubbles, td_recovery_bubbles,
397a39fcae7SAndi Kleen "event=0xd,umask=0x3,cmask=1", /* int_misc.recovery_cycles */
398a39fcae7SAndi Kleen "event=0xd,umask=0x3,cmask=1,any=1"); /* int_misc.recovery_cycles_any */
399a39fcae7SAndi Kleen EVENT_ATTR_STR_HT(topdown-recovery-bubbles.scale, td_recovery_bubbles_scale,
400a39fcae7SAndi Kleen "4", "2");
401a39fcae7SAndi Kleen
40259a854e2SKan Liang EVENT_ATTR_STR(slots, slots, "event=0x00,umask=0x4");
40359a854e2SKan Liang EVENT_ATTR_STR(topdown-retiring, td_retiring, "event=0x00,umask=0x80");
40459a854e2SKan Liang EVENT_ATTR_STR(topdown-bad-spec, td_bad_spec, "event=0x00,umask=0x81");
40559a854e2SKan Liang EVENT_ATTR_STR(topdown-fe-bound, td_fe_bound, "event=0x00,umask=0x82");
40659a854e2SKan Liang EVENT_ATTR_STR(topdown-be-bound, td_be_bound, "event=0x00,umask=0x83");
40761b985e3SKan Liang EVENT_ATTR_STR(topdown-heavy-ops, td_heavy_ops, "event=0x00,umask=0x84");
40861b985e3SKan Liang EVENT_ATTR_STR(topdown-br-mispredict, td_br_mispredict, "event=0x00,umask=0x85");
40961b985e3SKan Liang EVENT_ATTR_STR(topdown-fetch-lat, td_fetch_lat, "event=0x00,umask=0x86");
41061b985e3SKan Liang EVENT_ATTR_STR(topdown-mem-bound, td_mem_bound, "event=0x00,umask=0x87");
41159a854e2SKan Liang
41220f36278SLukasz Odzioba static struct attribute *snb_events_attrs[] = {
413a39fcae7SAndi Kleen EVENT_PTR(td_slots_issued),
414a39fcae7SAndi Kleen EVENT_PTR(td_slots_retired),
415a39fcae7SAndi Kleen EVENT_PTR(td_fetch_bubbles),
416a39fcae7SAndi Kleen EVENT_PTR(td_total_slots),
417a39fcae7SAndi Kleen EVENT_PTR(td_total_slots_scale),
418a39fcae7SAndi Kleen EVENT_PTR(td_recovery_bubbles),
419a39fcae7SAndi Kleen EVENT_PTR(td_recovery_bubbles_scale),
420e1069839SBorislav Petkov NULL,
421e1069839SBorislav Petkov };
422e1069839SBorislav Petkov
423d4ae5529SJiri Olsa static struct attribute *snb_mem_events_attrs[] = {
424d4ae5529SJiri Olsa EVENT_PTR(mem_ld_snb),
425d4ae5529SJiri Olsa EVENT_PTR(mem_st_snb),
426d4ae5529SJiri Olsa NULL,
427d4ae5529SJiri Olsa };
428d4ae5529SJiri Olsa
429e1069839SBorislav Petkov static struct event_constraint intel_hsw_event_constraints[] = {
430e1069839SBorislav Petkov FIXED_EVENT_CONSTRAINT(0x00c0, 0), /* INST_RETIRED.ANY */
431e1069839SBorislav Petkov FIXED_EVENT_CONSTRAINT(0x003c, 1), /* CPU_CLK_UNHALTED.CORE */
432e1069839SBorislav Petkov FIXED_EVENT_CONSTRAINT(0x0300, 2), /* CPU_CLK_UNHALTED.REF */
433e1069839SBorislav Petkov INTEL_UEVENT_CONSTRAINT(0x148, 0x4), /* L1D_PEND_MISS.PENDING */
434e1069839SBorislav Petkov INTEL_UEVENT_CONSTRAINT(0x01c0, 0x2), /* INST_RETIRED.PREC_DIST */
435e1069839SBorislav Petkov INTEL_EVENT_CONSTRAINT(0xcd, 0x8), /* MEM_TRANS_RETIRED.LOAD_LATENCY */
436e1069839SBorislav Petkov /* CYCLE_ACTIVITY.CYCLES_L1D_PENDING */
437e1069839SBorislav Petkov INTEL_UEVENT_CONSTRAINT(0x08a3, 0x4),
438e1069839SBorislav Petkov /* CYCLE_ACTIVITY.STALLS_L1D_PENDING */
439e1069839SBorislav Petkov INTEL_UEVENT_CONSTRAINT(0x0ca3, 0x4),
440e1069839SBorislav Petkov /* CYCLE_ACTIVITY.CYCLES_NO_EXECUTE */
441e1069839SBorislav Petkov INTEL_UEVENT_CONSTRAINT(0x04a3, 0xf),
442e1069839SBorislav Petkov
4439010ae4aSStephane Eranian /*
4449010ae4aSStephane Eranian * When HT is off these events can only run on the bottom 4 counters
4459010ae4aSStephane Eranian * When HT is on, they are impacted by the HT bug and require EXCL access
4469010ae4aSStephane Eranian */
447e1069839SBorislav Petkov INTEL_EXCLEVT_CONSTRAINT(0xd0, 0xf), /* MEM_UOPS_RETIRED.* */
448e1069839SBorislav Petkov INTEL_EXCLEVT_CONSTRAINT(0xd1, 0xf), /* MEM_LOAD_UOPS_RETIRED.* */
449e1069839SBorislav Petkov INTEL_EXCLEVT_CONSTRAINT(0xd2, 0xf), /* MEM_LOAD_UOPS_LLC_HIT_RETIRED.* */
450e1069839SBorislav Petkov INTEL_EXCLEVT_CONSTRAINT(0xd3, 0xf), /* MEM_LOAD_UOPS_LLC_MISS_RETIRED.* */
451e1069839SBorislav Petkov
452e1069839SBorislav Petkov EVENT_CONSTRAINT_END
453e1069839SBorislav Petkov };
454e1069839SBorislav Petkov
45520f36278SLukasz Odzioba static struct event_constraint intel_bdw_event_constraints[] = {
456e1069839SBorislav Petkov FIXED_EVENT_CONSTRAINT(0x00c0, 0), /* INST_RETIRED.ANY */
457e1069839SBorislav Petkov FIXED_EVENT_CONSTRAINT(0x003c, 1), /* CPU_CLK_UNHALTED.CORE */
458e1069839SBorislav Petkov FIXED_EVENT_CONSTRAINT(0x0300, 2), /* CPU_CLK_UNHALTED.REF */
459e1069839SBorislav Petkov INTEL_UEVENT_CONSTRAINT(0x148, 0x4), /* L1D_PEND_MISS.PENDING */
460e1069839SBorislav Petkov INTEL_UBIT_EVENT_CONSTRAINT(0x8a3, 0x4), /* CYCLE_ACTIVITY.CYCLES_L1D_MISS */
4619010ae4aSStephane Eranian /*
4629010ae4aSStephane Eranian * when HT is off, these can only run on the bottom 4 counters
4639010ae4aSStephane Eranian */
4649010ae4aSStephane Eranian INTEL_EVENT_CONSTRAINT(0xd0, 0xf), /* MEM_INST_RETIRED.* */
4659010ae4aSStephane Eranian INTEL_EVENT_CONSTRAINT(0xd1, 0xf), /* MEM_LOAD_RETIRED.* */
4669010ae4aSStephane Eranian INTEL_EVENT_CONSTRAINT(0xd2, 0xf), /* MEM_LOAD_L3_HIT_RETIRED.* */
4679010ae4aSStephane Eranian INTEL_EVENT_CONSTRAINT(0xcd, 0xf), /* MEM_TRANS_RETIRED.* */
468e1069839SBorislav Petkov EVENT_CONSTRAINT_END
469e1069839SBorislav Petkov };
470e1069839SBorislav Petkov
intel_pmu_event_map(int hw_event)471e1069839SBorislav Petkov static u64 intel_pmu_event_map(int hw_event)
472e1069839SBorislav Petkov {
473e1069839SBorislav Petkov return intel_perfmon_event_map[hw_event];
474e1069839SBorislav Petkov }
475e1069839SBorislav Petkov
47661b985e3SKan Liang static __initconst const u64 spr_hw_cache_event_ids
47761b985e3SKan Liang [PERF_COUNT_HW_CACHE_MAX]
47861b985e3SKan Liang [PERF_COUNT_HW_CACHE_OP_MAX]
47961b985e3SKan Liang [PERF_COUNT_HW_CACHE_RESULT_MAX] =
48061b985e3SKan Liang {
48161b985e3SKan Liang [ C(L1D ) ] = {
48261b985e3SKan Liang [ C(OP_READ) ] = {
48361b985e3SKan Liang [ C(RESULT_ACCESS) ] = 0x81d0,
48461b985e3SKan Liang [ C(RESULT_MISS) ] = 0xe124,
48561b985e3SKan Liang },
48661b985e3SKan Liang [ C(OP_WRITE) ] = {
48761b985e3SKan Liang [ C(RESULT_ACCESS) ] = 0x82d0,
48861b985e3SKan Liang },
48961b985e3SKan Liang },
49061b985e3SKan Liang [ C(L1I ) ] = {
49161b985e3SKan Liang [ C(OP_READ) ] = {
49261b985e3SKan Liang [ C(RESULT_MISS) ] = 0xe424,
49361b985e3SKan Liang },
49461b985e3SKan Liang [ C(OP_WRITE) ] = {
49561b985e3SKan Liang [ C(RESULT_ACCESS) ] = -1,
49661b985e3SKan Liang [ C(RESULT_MISS) ] = -1,
49761b985e3SKan Liang },
49861b985e3SKan Liang },
49961b985e3SKan Liang [ C(LL ) ] = {
50061b985e3SKan Liang [ C(OP_READ) ] = {
50161b985e3SKan Liang [ C(RESULT_ACCESS) ] = 0x12a,
50261b985e3SKan Liang [ C(RESULT_MISS) ] = 0x12a,
50361b985e3SKan Liang },
50461b985e3SKan Liang [ C(OP_WRITE) ] = {
50561b985e3SKan Liang [ C(RESULT_ACCESS) ] = 0x12a,
50661b985e3SKan Liang [ C(RESULT_MISS) ] = 0x12a,
50761b985e3SKan Liang },
50861b985e3SKan Liang },
50961b985e3SKan Liang [ C(DTLB) ] = {
51061b985e3SKan Liang [ C(OP_READ) ] = {
51161b985e3SKan Liang [ C(RESULT_ACCESS) ] = 0x81d0,
51261b985e3SKan Liang [ C(RESULT_MISS) ] = 0xe12,
51361b985e3SKan Liang },
51461b985e3SKan Liang [ C(OP_WRITE) ] = {
51561b985e3SKan Liang [ C(RESULT_ACCESS) ] = 0x82d0,
51661b985e3SKan Liang [ C(RESULT_MISS) ] = 0xe13,
51761b985e3SKan Liang },
51861b985e3SKan Liang },
51961b985e3SKan Liang [ C(ITLB) ] = {
52061b985e3SKan Liang [ C(OP_READ) ] = {
52161b985e3SKan Liang [ C(RESULT_ACCESS) ] = -1,
52261b985e3SKan Liang [ C(RESULT_MISS) ] = 0xe11,
52361b985e3SKan Liang },
52461b985e3SKan Liang [ C(OP_WRITE) ] = {
52561b985e3SKan Liang [ C(RESULT_ACCESS) ] = -1,
52661b985e3SKan Liang [ C(RESULT_MISS) ] = -1,
52761b985e3SKan Liang },
52861b985e3SKan Liang [ C(OP_PREFETCH) ] = {
52961b985e3SKan Liang [ C(RESULT_ACCESS) ] = -1,
53061b985e3SKan Liang [ C(RESULT_MISS) ] = -1,
53161b985e3SKan Liang },
53261b985e3SKan Liang },
53361b985e3SKan Liang [ C(BPU ) ] = {
53461b985e3SKan Liang [ C(OP_READ) ] = {
53561b985e3SKan Liang [ C(RESULT_ACCESS) ] = 0x4c4,
53661b985e3SKan Liang [ C(RESULT_MISS) ] = 0x4c5,
53761b985e3SKan Liang },
53861b985e3SKan Liang [ C(OP_WRITE) ] = {
53961b985e3SKan Liang [ C(RESULT_ACCESS) ] = -1,
54061b985e3SKan Liang [ C(RESULT_MISS) ] = -1,
54161b985e3SKan Liang },
54261b985e3SKan Liang [ C(OP_PREFETCH) ] = {
54361b985e3SKan Liang [ C(RESULT_ACCESS) ] = -1,
54461b985e3SKan Liang [ C(RESULT_MISS) ] = -1,
54561b985e3SKan Liang },
54661b985e3SKan Liang },
54761b985e3SKan Liang [ C(NODE) ] = {
54861b985e3SKan Liang [ C(OP_READ) ] = {
54961b985e3SKan Liang [ C(RESULT_ACCESS) ] = 0x12a,
55061b985e3SKan Liang [ C(RESULT_MISS) ] = 0x12a,
55161b985e3SKan Liang },
55261b985e3SKan Liang },
55361b985e3SKan Liang };
55461b985e3SKan Liang
55561b985e3SKan Liang static __initconst const u64 spr_hw_cache_extra_regs
55661b985e3SKan Liang [PERF_COUNT_HW_CACHE_MAX]
55761b985e3SKan Liang [PERF_COUNT_HW_CACHE_OP_MAX]
55861b985e3SKan Liang [PERF_COUNT_HW_CACHE_RESULT_MAX] =
55961b985e3SKan Liang {
56061b985e3SKan Liang [ C(LL ) ] = {
56161b985e3SKan Liang [ C(OP_READ) ] = {
56261b985e3SKan Liang [ C(RESULT_ACCESS) ] = 0x10001,
56361b985e3SKan Liang [ C(RESULT_MISS) ] = 0x3fbfc00001,
56461b985e3SKan Liang },
56561b985e3SKan Liang [ C(OP_WRITE) ] = {
56661b985e3SKan Liang [ C(RESULT_ACCESS) ] = 0x3f3ffc0002,
56761b985e3SKan Liang [ C(RESULT_MISS) ] = 0x3f3fc00002,
56861b985e3SKan Liang },
56961b985e3SKan Liang },
57061b985e3SKan Liang [ C(NODE) ] = {
57161b985e3SKan Liang [ C(OP_READ) ] = {
57261b985e3SKan Liang [ C(RESULT_ACCESS) ] = 0x10c000001,
57361b985e3SKan Liang [ C(RESULT_MISS) ] = 0x3fb3000001,
57461b985e3SKan Liang },
57561b985e3SKan Liang },
57661b985e3SKan Liang };
57761b985e3SKan Liang
578e1069839SBorislav Petkov /*
579e1069839SBorislav Petkov * Notes on the events:
580e1069839SBorislav Petkov * - data reads do not include code reads (comparable to earlier tables)
581e1069839SBorislav Petkov * - data counts include speculative execution (except L1 write, dtlb, bpu)
582e1069839SBorislav Petkov * - remote node access includes remote memory, remote cache, remote mmio.
583e1069839SBorislav Petkov * - prefetches are not included in the counts.
584e1069839SBorislav Petkov * - icache miss does not include decoded icache
585e1069839SBorislav Petkov */
586e1069839SBorislav Petkov
587e1069839SBorislav Petkov #define SKL_DEMAND_DATA_RD BIT_ULL(0)
588e1069839SBorislav Petkov #define SKL_DEMAND_RFO BIT_ULL(1)
589e1069839SBorislav Petkov #define SKL_ANY_RESPONSE BIT_ULL(16)
590e1069839SBorislav Petkov #define SKL_SUPPLIER_NONE BIT_ULL(17)
591e1069839SBorislav Petkov #define SKL_L3_MISS_LOCAL_DRAM BIT_ULL(26)
592e1069839SBorislav Petkov #define SKL_L3_MISS_REMOTE_HOP0_DRAM BIT_ULL(27)
593e1069839SBorislav Petkov #define SKL_L3_MISS_REMOTE_HOP1_DRAM BIT_ULL(28)
594e1069839SBorislav Petkov #define SKL_L3_MISS_REMOTE_HOP2P_DRAM BIT_ULL(29)
595e1069839SBorislav Petkov #define SKL_L3_MISS (SKL_L3_MISS_LOCAL_DRAM| \
596e1069839SBorislav Petkov SKL_L3_MISS_REMOTE_HOP0_DRAM| \
597e1069839SBorislav Petkov SKL_L3_MISS_REMOTE_HOP1_DRAM| \
598e1069839SBorislav Petkov SKL_L3_MISS_REMOTE_HOP2P_DRAM)
599e1069839SBorislav Petkov #define SKL_SPL_HIT BIT_ULL(30)
600e1069839SBorislav Petkov #define SKL_SNOOP_NONE BIT_ULL(31)
601e1069839SBorislav Petkov #define SKL_SNOOP_NOT_NEEDED BIT_ULL(32)
602e1069839SBorislav Petkov #define SKL_SNOOP_MISS BIT_ULL(33)
603e1069839SBorislav Petkov #define SKL_SNOOP_HIT_NO_FWD BIT_ULL(34)
604e1069839SBorislav Petkov #define SKL_SNOOP_HIT_WITH_FWD BIT_ULL(35)
605e1069839SBorislav Petkov #define SKL_SNOOP_HITM BIT_ULL(36)
606e1069839SBorislav Petkov #define SKL_SNOOP_NON_DRAM BIT_ULL(37)
607e1069839SBorislav Petkov #define SKL_ANY_SNOOP (SKL_SPL_HIT|SKL_SNOOP_NONE| \
608e1069839SBorislav Petkov SKL_SNOOP_NOT_NEEDED|SKL_SNOOP_MISS| \
609e1069839SBorislav Petkov SKL_SNOOP_HIT_NO_FWD|SKL_SNOOP_HIT_WITH_FWD| \
610e1069839SBorislav Petkov SKL_SNOOP_HITM|SKL_SNOOP_NON_DRAM)
611e1069839SBorislav Petkov #define SKL_DEMAND_READ SKL_DEMAND_DATA_RD
612e1069839SBorislav Petkov #define SKL_SNOOP_DRAM (SKL_SNOOP_NONE| \
613e1069839SBorislav Petkov SKL_SNOOP_NOT_NEEDED|SKL_SNOOP_MISS| \
614e1069839SBorislav Petkov SKL_SNOOP_HIT_NO_FWD|SKL_SNOOP_HIT_WITH_FWD| \
615e1069839SBorislav Petkov SKL_SNOOP_HITM|SKL_SPL_HIT)
616e1069839SBorislav Petkov #define SKL_DEMAND_WRITE SKL_DEMAND_RFO
617e1069839SBorislav Petkov #define SKL_LLC_ACCESS SKL_ANY_RESPONSE
618e1069839SBorislav Petkov #define SKL_L3_MISS_REMOTE (SKL_L3_MISS_REMOTE_HOP0_DRAM| \
619e1069839SBorislav Petkov SKL_L3_MISS_REMOTE_HOP1_DRAM| \
620e1069839SBorislav Petkov SKL_L3_MISS_REMOTE_HOP2P_DRAM)
621e1069839SBorislav Petkov
622e1069839SBorislav Petkov static __initconst const u64 skl_hw_cache_event_ids
623e1069839SBorislav Petkov [PERF_COUNT_HW_CACHE_MAX]
624e1069839SBorislav Petkov [PERF_COUNT_HW_CACHE_OP_MAX]
625e1069839SBorislav Petkov [PERF_COUNT_HW_CACHE_RESULT_MAX] =
626e1069839SBorislav Petkov {
627e1069839SBorislav Petkov [ C(L1D ) ] = {
628e1069839SBorislav Petkov [ C(OP_READ) ] = {
629e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = 0x81d0, /* MEM_INST_RETIRED.ALL_LOADS */
630e1069839SBorislav Petkov [ C(RESULT_MISS) ] = 0x151, /* L1D.REPLACEMENT */
631e1069839SBorislav Petkov },
632e1069839SBorislav Petkov [ C(OP_WRITE) ] = {
633e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = 0x82d0, /* MEM_INST_RETIRED.ALL_STORES */
634e1069839SBorislav Petkov [ C(RESULT_MISS) ] = 0x0,
635e1069839SBorislav Petkov },
636e1069839SBorislav Petkov [ C(OP_PREFETCH) ] = {
637e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = 0x0,
638e1069839SBorislav Petkov [ C(RESULT_MISS) ] = 0x0,
639e1069839SBorislav Petkov },
640e1069839SBorislav Petkov },
641e1069839SBorislav Petkov [ C(L1I ) ] = {
642e1069839SBorislav Petkov [ C(OP_READ) ] = {
643e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = 0x0,
644e1069839SBorislav Petkov [ C(RESULT_MISS) ] = 0x283, /* ICACHE_64B.MISS */
645e1069839SBorislav Petkov },
646e1069839SBorislav Petkov [ C(OP_WRITE) ] = {
647e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = -1,
648e1069839SBorislav Petkov [ C(RESULT_MISS) ] = -1,
649e1069839SBorislav Petkov },
650e1069839SBorislav Petkov [ C(OP_PREFETCH) ] = {
651e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = 0x0,
652e1069839SBorislav Petkov [ C(RESULT_MISS) ] = 0x0,
653e1069839SBorislav Petkov },
654e1069839SBorislav Petkov },
655e1069839SBorislav Petkov [ C(LL ) ] = {
656e1069839SBorislav Petkov [ C(OP_READ) ] = {
657e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = 0x1b7, /* OFFCORE_RESPONSE */
658e1069839SBorislav Petkov [ C(RESULT_MISS) ] = 0x1b7, /* OFFCORE_RESPONSE */
659e1069839SBorislav Petkov },
660e1069839SBorislav Petkov [ C(OP_WRITE) ] = {
661e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = 0x1b7, /* OFFCORE_RESPONSE */
662e1069839SBorislav Petkov [ C(RESULT_MISS) ] = 0x1b7, /* OFFCORE_RESPONSE */
663e1069839SBorislav Petkov },
664e1069839SBorislav Petkov [ C(OP_PREFETCH) ] = {
665e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = 0x0,
666e1069839SBorislav Petkov [ C(RESULT_MISS) ] = 0x0,
667e1069839SBorislav Petkov },
668e1069839SBorislav Petkov },
669e1069839SBorislav Petkov [ C(DTLB) ] = {
670e1069839SBorislav Petkov [ C(OP_READ) ] = {
671e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = 0x81d0, /* MEM_INST_RETIRED.ALL_LOADS */
672fb3a5055SKan Liang [ C(RESULT_MISS) ] = 0xe08, /* DTLB_LOAD_MISSES.WALK_COMPLETED */
673e1069839SBorislav Petkov },
674e1069839SBorislav Petkov [ C(OP_WRITE) ] = {
675e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = 0x82d0, /* MEM_INST_RETIRED.ALL_STORES */
676fb3a5055SKan Liang [ C(RESULT_MISS) ] = 0xe49, /* DTLB_STORE_MISSES.WALK_COMPLETED */
677e1069839SBorislav Petkov },
678e1069839SBorislav Petkov [ C(OP_PREFETCH) ] = {
679e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = 0x0,
680e1069839SBorislav Petkov [ C(RESULT_MISS) ] = 0x0,
681e1069839SBorislav Petkov },
682e1069839SBorislav Petkov },
683e1069839SBorislav Petkov [ C(ITLB) ] = {
684e1069839SBorislav Petkov [ C(OP_READ) ] = {
685e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = 0x2085, /* ITLB_MISSES.STLB_HIT */
686e1069839SBorislav Petkov [ C(RESULT_MISS) ] = 0xe85, /* ITLB_MISSES.WALK_COMPLETED */
687e1069839SBorislav Petkov },
688e1069839SBorislav Petkov [ C(OP_WRITE) ] = {
689e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = -1,
690e1069839SBorislav Petkov [ C(RESULT_MISS) ] = -1,
691e1069839SBorislav Petkov },
692e1069839SBorislav Petkov [ C(OP_PREFETCH) ] = {
693e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = -1,
694e1069839SBorislav Petkov [ C(RESULT_MISS) ] = -1,
695e1069839SBorislav Petkov },
696e1069839SBorislav Petkov },
697e1069839SBorislav Petkov [ C(BPU ) ] = {
698e1069839SBorislav Petkov [ C(OP_READ) ] = {
699e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = 0xc4, /* BR_INST_RETIRED.ALL_BRANCHES */
700e1069839SBorislav Petkov [ C(RESULT_MISS) ] = 0xc5, /* BR_MISP_RETIRED.ALL_BRANCHES */
701e1069839SBorislav Petkov },
702e1069839SBorislav Petkov [ C(OP_WRITE) ] = {
703e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = -1,
704e1069839SBorislav Petkov [ C(RESULT_MISS) ] = -1,
705e1069839SBorislav Petkov },
706e1069839SBorislav Petkov [ C(OP_PREFETCH) ] = {
707e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = -1,
708e1069839SBorislav Petkov [ C(RESULT_MISS) ] = -1,
709e1069839SBorislav Petkov },
710e1069839SBorislav Petkov },
711e1069839SBorislav Petkov [ C(NODE) ] = {
712e1069839SBorislav Petkov [ C(OP_READ) ] = {
713e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = 0x1b7, /* OFFCORE_RESPONSE */
714e1069839SBorislav Petkov [ C(RESULT_MISS) ] = 0x1b7, /* OFFCORE_RESPONSE */
715e1069839SBorislav Petkov },
716e1069839SBorislav Petkov [ C(OP_WRITE) ] = {
717e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = 0x1b7, /* OFFCORE_RESPONSE */
718e1069839SBorislav Petkov [ C(RESULT_MISS) ] = 0x1b7, /* OFFCORE_RESPONSE */
719e1069839SBorislav Petkov },
720e1069839SBorislav Petkov [ C(OP_PREFETCH) ] = {
721e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = 0x0,
722e1069839SBorislav Petkov [ C(RESULT_MISS) ] = 0x0,
723e1069839SBorislav Petkov },
724e1069839SBorislav Petkov },
725e1069839SBorislav Petkov };
726e1069839SBorislav Petkov
727e1069839SBorislav Petkov static __initconst const u64 skl_hw_cache_extra_regs
728e1069839SBorislav Petkov [PERF_COUNT_HW_CACHE_MAX]
729e1069839SBorislav Petkov [PERF_COUNT_HW_CACHE_OP_MAX]
730e1069839SBorislav Petkov [PERF_COUNT_HW_CACHE_RESULT_MAX] =
731e1069839SBorislav Petkov {
732e1069839SBorislav Petkov [ C(LL ) ] = {
733e1069839SBorislav Petkov [ C(OP_READ) ] = {
734e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = SKL_DEMAND_READ|
735e1069839SBorislav Petkov SKL_LLC_ACCESS|SKL_ANY_SNOOP,
736e1069839SBorislav Petkov [ C(RESULT_MISS) ] = SKL_DEMAND_READ|
737e1069839SBorislav Petkov SKL_L3_MISS|SKL_ANY_SNOOP|
738e1069839SBorislav Petkov SKL_SUPPLIER_NONE,
739e1069839SBorislav Petkov },
740e1069839SBorislav Petkov [ C(OP_WRITE) ] = {
741e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = SKL_DEMAND_WRITE|
742e1069839SBorislav Petkov SKL_LLC_ACCESS|SKL_ANY_SNOOP,
743e1069839SBorislav Petkov [ C(RESULT_MISS) ] = SKL_DEMAND_WRITE|
744e1069839SBorislav Petkov SKL_L3_MISS|SKL_ANY_SNOOP|
745e1069839SBorislav Petkov SKL_SUPPLIER_NONE,
746e1069839SBorislav Petkov },
747e1069839SBorislav Petkov [ C(OP_PREFETCH) ] = {
748e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = 0x0,
749e1069839SBorislav Petkov [ C(RESULT_MISS) ] = 0x0,
750e1069839SBorislav Petkov },
751e1069839SBorislav Petkov },
752e1069839SBorislav Petkov [ C(NODE) ] = {
753e1069839SBorislav Petkov [ C(OP_READ) ] = {
754e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = SKL_DEMAND_READ|
755e1069839SBorislav Petkov SKL_L3_MISS_LOCAL_DRAM|SKL_SNOOP_DRAM,
756e1069839SBorislav Petkov [ C(RESULT_MISS) ] = SKL_DEMAND_READ|
757e1069839SBorislav Petkov SKL_L3_MISS_REMOTE|SKL_SNOOP_DRAM,
758e1069839SBorislav Petkov },
759e1069839SBorislav Petkov [ C(OP_WRITE) ] = {
760e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = SKL_DEMAND_WRITE|
761e1069839SBorislav Petkov SKL_L3_MISS_LOCAL_DRAM|SKL_SNOOP_DRAM,
762e1069839SBorislav Petkov [ C(RESULT_MISS) ] = SKL_DEMAND_WRITE|
763e1069839SBorislav Petkov SKL_L3_MISS_REMOTE|SKL_SNOOP_DRAM,
764e1069839SBorislav Petkov },
765e1069839SBorislav Petkov [ C(OP_PREFETCH) ] = {
766e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = 0x0,
767e1069839SBorislav Petkov [ C(RESULT_MISS) ] = 0x0,
768e1069839SBorislav Petkov },
769e1069839SBorislav Petkov },
770e1069839SBorislav Petkov };
771e1069839SBorislav Petkov
772e1069839SBorislav Petkov #define SNB_DMND_DATA_RD (1ULL << 0)
773e1069839SBorislav Petkov #define SNB_DMND_RFO (1ULL << 1)
774e1069839SBorislav Petkov #define SNB_DMND_IFETCH (1ULL << 2)
775e1069839SBorislav Petkov #define SNB_DMND_WB (1ULL << 3)
776e1069839SBorislav Petkov #define SNB_PF_DATA_RD (1ULL << 4)
777e1069839SBorislav Petkov #define SNB_PF_RFO (1ULL << 5)
778e1069839SBorislav Petkov #define SNB_PF_IFETCH (1ULL << 6)
779e1069839SBorislav Petkov #define SNB_LLC_DATA_RD (1ULL << 7)
780e1069839SBorislav Petkov #define SNB_LLC_RFO (1ULL << 8)
781e1069839SBorislav Petkov #define SNB_LLC_IFETCH (1ULL << 9)
782e1069839SBorislav Petkov #define SNB_BUS_LOCKS (1ULL << 10)
783e1069839SBorislav Petkov #define SNB_STRM_ST (1ULL << 11)
784e1069839SBorislav Petkov #define SNB_OTHER (1ULL << 15)
785e1069839SBorislav Petkov #define SNB_RESP_ANY (1ULL << 16)
786e1069839SBorislav Petkov #define SNB_NO_SUPP (1ULL << 17)
787e1069839SBorislav Petkov #define SNB_LLC_HITM (1ULL << 18)
788e1069839SBorislav Petkov #define SNB_LLC_HITE (1ULL << 19)
789e1069839SBorislav Petkov #define SNB_LLC_HITS (1ULL << 20)
790e1069839SBorislav Petkov #define SNB_LLC_HITF (1ULL << 21)
791e1069839SBorislav Petkov #define SNB_LOCAL (1ULL << 22)
792e1069839SBorislav Petkov #define SNB_REMOTE (0xffULL << 23)
793e1069839SBorislav Petkov #define SNB_SNP_NONE (1ULL << 31)
794e1069839SBorislav Petkov #define SNB_SNP_NOT_NEEDED (1ULL << 32)
795e1069839SBorislav Petkov #define SNB_SNP_MISS (1ULL << 33)
796e1069839SBorislav Petkov #define SNB_NO_FWD (1ULL << 34)
797e1069839SBorislav Petkov #define SNB_SNP_FWD (1ULL << 35)
798e1069839SBorislav Petkov #define SNB_HITM (1ULL << 36)
799e1069839SBorislav Petkov #define SNB_NON_DRAM (1ULL << 37)
800e1069839SBorislav Petkov
801e1069839SBorislav Petkov #define SNB_DMND_READ (SNB_DMND_DATA_RD|SNB_LLC_DATA_RD)
802e1069839SBorislav Petkov #define SNB_DMND_WRITE (SNB_DMND_RFO|SNB_LLC_RFO)
803e1069839SBorislav Petkov #define SNB_DMND_PREFETCH (SNB_PF_DATA_RD|SNB_PF_RFO)
804e1069839SBorislav Petkov
805e1069839SBorislav Petkov #define SNB_SNP_ANY (SNB_SNP_NONE|SNB_SNP_NOT_NEEDED| \
806e1069839SBorislav Petkov SNB_SNP_MISS|SNB_NO_FWD|SNB_SNP_FWD| \
807e1069839SBorislav Petkov SNB_HITM)
808e1069839SBorislav Petkov
809e1069839SBorislav Petkov #define SNB_DRAM_ANY (SNB_LOCAL|SNB_REMOTE|SNB_SNP_ANY)
810e1069839SBorislav Petkov #define SNB_DRAM_REMOTE (SNB_REMOTE|SNB_SNP_ANY)
811e1069839SBorislav Petkov
812e1069839SBorislav Petkov #define SNB_L3_ACCESS SNB_RESP_ANY
813e1069839SBorislav Petkov #define SNB_L3_MISS (SNB_DRAM_ANY|SNB_NON_DRAM)
814e1069839SBorislav Petkov
815e1069839SBorislav Petkov static __initconst const u64 snb_hw_cache_extra_regs
816e1069839SBorislav Petkov [PERF_COUNT_HW_CACHE_MAX]
817e1069839SBorislav Petkov [PERF_COUNT_HW_CACHE_OP_MAX]
818e1069839SBorislav Petkov [PERF_COUNT_HW_CACHE_RESULT_MAX] =
819e1069839SBorislav Petkov {
820e1069839SBorislav Petkov [ C(LL ) ] = {
821e1069839SBorislav Petkov [ C(OP_READ) ] = {
822e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = SNB_DMND_READ|SNB_L3_ACCESS,
823e1069839SBorislav Petkov [ C(RESULT_MISS) ] = SNB_DMND_READ|SNB_L3_MISS,
824e1069839SBorislav Petkov },
825e1069839SBorislav Petkov [ C(OP_WRITE) ] = {
826e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = SNB_DMND_WRITE|SNB_L3_ACCESS,
827e1069839SBorislav Petkov [ C(RESULT_MISS) ] = SNB_DMND_WRITE|SNB_L3_MISS,
828e1069839SBorislav Petkov },
829e1069839SBorislav Petkov [ C(OP_PREFETCH) ] = {
830e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = SNB_DMND_PREFETCH|SNB_L3_ACCESS,
831e1069839SBorislav Petkov [ C(RESULT_MISS) ] = SNB_DMND_PREFETCH|SNB_L3_MISS,
832e1069839SBorislav Petkov },
833e1069839SBorislav Petkov },
834e1069839SBorislav Petkov [ C(NODE) ] = {
835e1069839SBorislav Petkov [ C(OP_READ) ] = {
836e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = SNB_DMND_READ|SNB_DRAM_ANY,
837e1069839SBorislav Petkov [ C(RESULT_MISS) ] = SNB_DMND_READ|SNB_DRAM_REMOTE,
838e1069839SBorislav Petkov },
839e1069839SBorislav Petkov [ C(OP_WRITE) ] = {
840e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = SNB_DMND_WRITE|SNB_DRAM_ANY,
841e1069839SBorislav Petkov [ C(RESULT_MISS) ] = SNB_DMND_WRITE|SNB_DRAM_REMOTE,
842e1069839SBorislav Petkov },
843e1069839SBorislav Petkov [ C(OP_PREFETCH) ] = {
844e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = SNB_DMND_PREFETCH|SNB_DRAM_ANY,
845e1069839SBorislav Petkov [ C(RESULT_MISS) ] = SNB_DMND_PREFETCH|SNB_DRAM_REMOTE,
846e1069839SBorislav Petkov },
847e1069839SBorislav Petkov },
848e1069839SBorislav Petkov };
849e1069839SBorislav Petkov
850e1069839SBorislav Petkov static __initconst const u64 snb_hw_cache_event_ids
851e1069839SBorislav Petkov [PERF_COUNT_HW_CACHE_MAX]
852e1069839SBorislav Petkov [PERF_COUNT_HW_CACHE_OP_MAX]
853e1069839SBorislav Petkov [PERF_COUNT_HW_CACHE_RESULT_MAX] =
854e1069839SBorislav Petkov {
855e1069839SBorislav Petkov [ C(L1D) ] = {
856e1069839SBorislav Petkov [ C(OP_READ) ] = {
857e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = 0xf1d0, /* MEM_UOP_RETIRED.LOADS */
858e1069839SBorislav Petkov [ C(RESULT_MISS) ] = 0x0151, /* L1D.REPLACEMENT */
859e1069839SBorislav Petkov },
860e1069839SBorislav Petkov [ C(OP_WRITE) ] = {
861e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = 0xf2d0, /* MEM_UOP_RETIRED.STORES */
862e1069839SBorislav Petkov [ C(RESULT_MISS) ] = 0x0851, /* L1D.ALL_M_REPLACEMENT */
863e1069839SBorislav Petkov },
864e1069839SBorislav Petkov [ C(OP_PREFETCH) ] = {
865e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = 0x0,
866e1069839SBorislav Petkov [ C(RESULT_MISS) ] = 0x024e, /* HW_PRE_REQ.DL1_MISS */
867e1069839SBorislav Petkov },
868e1069839SBorislav Petkov },
869e1069839SBorislav Petkov [ C(L1I ) ] = {
870e1069839SBorislav Petkov [ C(OP_READ) ] = {
871e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = 0x0,
872e1069839SBorislav Petkov [ C(RESULT_MISS) ] = 0x0280, /* ICACHE.MISSES */
873e1069839SBorislav Petkov },
874e1069839SBorislav Petkov [ C(OP_WRITE) ] = {
875e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = -1,
876e1069839SBorislav Petkov [ C(RESULT_MISS) ] = -1,
877e1069839SBorislav Petkov },
878e1069839SBorislav Petkov [ C(OP_PREFETCH) ] = {
879e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = 0x0,
880e1069839SBorislav Petkov [ C(RESULT_MISS) ] = 0x0,
881e1069839SBorislav Petkov },
882e1069839SBorislav Petkov },
883e1069839SBorislav Petkov [ C(LL ) ] = {
884e1069839SBorislav Petkov [ C(OP_READ) ] = {
885e1069839SBorislav Petkov /* OFFCORE_RESPONSE.ANY_DATA.LOCAL_CACHE */
886e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = 0x01b7,
887e1069839SBorislav Petkov /* OFFCORE_RESPONSE.ANY_DATA.ANY_LLC_MISS */
888e1069839SBorislav Petkov [ C(RESULT_MISS) ] = 0x01b7,
889e1069839SBorislav Petkov },
890e1069839SBorislav Petkov [ C(OP_WRITE) ] = {
891e1069839SBorislav Petkov /* OFFCORE_RESPONSE.ANY_RFO.LOCAL_CACHE */
892e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = 0x01b7,
893e1069839SBorislav Petkov /* OFFCORE_RESPONSE.ANY_RFO.ANY_LLC_MISS */
894e1069839SBorislav Petkov [ C(RESULT_MISS) ] = 0x01b7,
895e1069839SBorislav Petkov },
896e1069839SBorislav Petkov [ C(OP_PREFETCH) ] = {
897e1069839SBorislav Petkov /* OFFCORE_RESPONSE.PREFETCH.LOCAL_CACHE */
898e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = 0x01b7,
899e1069839SBorislav Petkov /* OFFCORE_RESPONSE.PREFETCH.ANY_LLC_MISS */
900e1069839SBorislav Petkov [ C(RESULT_MISS) ] = 0x01b7,
901e1069839SBorislav Petkov },
902e1069839SBorislav Petkov },
903e1069839SBorislav Petkov [ C(DTLB) ] = {
904e1069839SBorislav Petkov [ C(OP_READ) ] = {
905e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = 0x81d0, /* MEM_UOP_RETIRED.ALL_LOADS */
906e1069839SBorislav Petkov [ C(RESULT_MISS) ] = 0x0108, /* DTLB_LOAD_MISSES.CAUSES_A_WALK */
907e1069839SBorislav Petkov },
908e1069839SBorislav Petkov [ C(OP_WRITE) ] = {
909e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = 0x82d0, /* MEM_UOP_RETIRED.ALL_STORES */
910e1069839SBorislav Petkov [ C(RESULT_MISS) ] = 0x0149, /* DTLB_STORE_MISSES.MISS_CAUSES_A_WALK */
911e1069839SBorislav Petkov },
912e1069839SBorislav Petkov [ C(OP_PREFETCH) ] = {
913e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = 0x0,
914e1069839SBorislav Petkov [ C(RESULT_MISS) ] = 0x0,
915e1069839SBorislav Petkov },
916e1069839SBorislav Petkov },
917e1069839SBorislav Petkov [ C(ITLB) ] = {
918e1069839SBorislav Petkov [ C(OP_READ) ] = {
919e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = 0x1085, /* ITLB_MISSES.STLB_HIT */
920e1069839SBorislav Petkov [ C(RESULT_MISS) ] = 0x0185, /* ITLB_MISSES.CAUSES_A_WALK */
921e1069839SBorislav Petkov },
922e1069839SBorislav Petkov [ C(OP_WRITE) ] = {
923e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = -1,
924e1069839SBorislav Petkov [ C(RESULT_MISS) ] = -1,
925e1069839SBorislav Petkov },
926e1069839SBorislav Petkov [ C(OP_PREFETCH) ] = {
927e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = -1,
928e1069839SBorislav Petkov [ C(RESULT_MISS) ] = -1,
929e1069839SBorislav Petkov },
930e1069839SBorislav Petkov },
931e1069839SBorislav Petkov [ C(BPU ) ] = {
932e1069839SBorislav Petkov [ C(OP_READ) ] = {
933e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = 0x00c4, /* BR_INST_RETIRED.ALL_BRANCHES */
934e1069839SBorislav Petkov [ C(RESULT_MISS) ] = 0x00c5, /* BR_MISP_RETIRED.ALL_BRANCHES */
935e1069839SBorislav Petkov },
936e1069839SBorislav Petkov [ C(OP_WRITE) ] = {
937e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = -1,
938e1069839SBorislav Petkov [ C(RESULT_MISS) ] = -1,
939e1069839SBorislav Petkov },
940e1069839SBorislav Petkov [ C(OP_PREFETCH) ] = {
941e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = -1,
942e1069839SBorislav Petkov [ C(RESULT_MISS) ] = -1,
943e1069839SBorislav Petkov },
944e1069839SBorislav Petkov },
945e1069839SBorislav Petkov [ C(NODE) ] = {
946e1069839SBorislav Petkov [ C(OP_READ) ] = {
947e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = 0x01b7,
948e1069839SBorislav Petkov [ C(RESULT_MISS) ] = 0x01b7,
949e1069839SBorislav Petkov },
950e1069839SBorislav Petkov [ C(OP_WRITE) ] = {
951e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = 0x01b7,
952e1069839SBorislav Petkov [ C(RESULT_MISS) ] = 0x01b7,
953e1069839SBorislav Petkov },
954e1069839SBorislav Petkov [ C(OP_PREFETCH) ] = {
955e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = 0x01b7,
956e1069839SBorislav Petkov [ C(RESULT_MISS) ] = 0x01b7,
957e1069839SBorislav Petkov },
958e1069839SBorislav Petkov },
959e1069839SBorislav Petkov
960e1069839SBorislav Petkov };
961e1069839SBorislav Petkov
962e1069839SBorislav Petkov /*
963e1069839SBorislav Petkov * Notes on the events:
964e1069839SBorislav Petkov * - data reads do not include code reads (comparable to earlier tables)
965e1069839SBorislav Petkov * - data counts include speculative execution (except L1 write, dtlb, bpu)
966e1069839SBorislav Petkov * - remote node access includes remote memory, remote cache, remote mmio.
967e1069839SBorislav Petkov * - prefetches are not included in the counts because they are not
968e1069839SBorislav Petkov * reliably counted.
969e1069839SBorislav Petkov */
970e1069839SBorislav Petkov
971e1069839SBorislav Petkov #define HSW_DEMAND_DATA_RD BIT_ULL(0)
972e1069839SBorislav Petkov #define HSW_DEMAND_RFO BIT_ULL(1)
973e1069839SBorislav Petkov #define HSW_ANY_RESPONSE BIT_ULL(16)
974e1069839SBorislav Petkov #define HSW_SUPPLIER_NONE BIT_ULL(17)
975e1069839SBorislav Petkov #define HSW_L3_MISS_LOCAL_DRAM BIT_ULL(22)
976e1069839SBorislav Petkov #define HSW_L3_MISS_REMOTE_HOP0 BIT_ULL(27)
977e1069839SBorislav Petkov #define HSW_L3_MISS_REMOTE_HOP1 BIT_ULL(28)
978e1069839SBorislav Petkov #define HSW_L3_MISS_REMOTE_HOP2P BIT_ULL(29)
979e1069839SBorislav Petkov #define HSW_L3_MISS (HSW_L3_MISS_LOCAL_DRAM| \
980e1069839SBorislav Petkov HSW_L3_MISS_REMOTE_HOP0|HSW_L3_MISS_REMOTE_HOP1| \
981e1069839SBorislav Petkov HSW_L3_MISS_REMOTE_HOP2P)
982e1069839SBorislav Petkov #define HSW_SNOOP_NONE BIT_ULL(31)
983e1069839SBorislav Petkov #define HSW_SNOOP_NOT_NEEDED BIT_ULL(32)
984e1069839SBorislav Petkov #define HSW_SNOOP_MISS BIT_ULL(33)
985e1069839SBorislav Petkov #define HSW_SNOOP_HIT_NO_FWD BIT_ULL(34)
986e1069839SBorislav Petkov #define HSW_SNOOP_HIT_WITH_FWD BIT_ULL(35)
987e1069839SBorislav Petkov #define HSW_SNOOP_HITM BIT_ULL(36)
988e1069839SBorislav Petkov #define HSW_SNOOP_NON_DRAM BIT_ULL(37)
989e1069839SBorislav Petkov #define HSW_ANY_SNOOP (HSW_SNOOP_NONE| \
990e1069839SBorislav Petkov HSW_SNOOP_NOT_NEEDED|HSW_SNOOP_MISS| \
991e1069839SBorislav Petkov HSW_SNOOP_HIT_NO_FWD|HSW_SNOOP_HIT_WITH_FWD| \
992e1069839SBorislav Petkov HSW_SNOOP_HITM|HSW_SNOOP_NON_DRAM)
993e1069839SBorislav Petkov #define HSW_SNOOP_DRAM (HSW_ANY_SNOOP & ~HSW_SNOOP_NON_DRAM)
994e1069839SBorislav Petkov #define HSW_DEMAND_READ HSW_DEMAND_DATA_RD
995e1069839SBorislav Petkov #define HSW_DEMAND_WRITE HSW_DEMAND_RFO
996e1069839SBorislav Petkov #define HSW_L3_MISS_REMOTE (HSW_L3_MISS_REMOTE_HOP0|\
997e1069839SBorislav Petkov HSW_L3_MISS_REMOTE_HOP1|HSW_L3_MISS_REMOTE_HOP2P)
998e1069839SBorislav Petkov #define HSW_LLC_ACCESS HSW_ANY_RESPONSE
999e1069839SBorislav Petkov
1000e1069839SBorislav Petkov #define BDW_L3_MISS_LOCAL BIT(26)
1001e1069839SBorislav Petkov #define BDW_L3_MISS (BDW_L3_MISS_LOCAL| \
1002e1069839SBorislav Petkov HSW_L3_MISS_REMOTE_HOP0|HSW_L3_MISS_REMOTE_HOP1| \
1003e1069839SBorislav Petkov HSW_L3_MISS_REMOTE_HOP2P)
1004e1069839SBorislav Petkov
1005e1069839SBorislav Petkov
1006e1069839SBorislav Petkov static __initconst const u64 hsw_hw_cache_event_ids
1007e1069839SBorislav Petkov [PERF_COUNT_HW_CACHE_MAX]
1008e1069839SBorislav Petkov [PERF_COUNT_HW_CACHE_OP_MAX]
1009e1069839SBorislav Petkov [PERF_COUNT_HW_CACHE_RESULT_MAX] =
1010e1069839SBorislav Petkov {
1011e1069839SBorislav Petkov [ C(L1D ) ] = {
1012e1069839SBorislav Petkov [ C(OP_READ) ] = {
1013e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = 0x81d0, /* MEM_UOPS_RETIRED.ALL_LOADS */
1014e1069839SBorislav Petkov [ C(RESULT_MISS) ] = 0x151, /* L1D.REPLACEMENT */
1015e1069839SBorislav Petkov },
1016e1069839SBorislav Petkov [ C(OP_WRITE) ] = {
1017e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = 0x82d0, /* MEM_UOPS_RETIRED.ALL_STORES */
1018e1069839SBorislav Petkov [ C(RESULT_MISS) ] = 0x0,
1019e1069839SBorislav Petkov },
1020e1069839SBorislav Petkov [ C(OP_PREFETCH) ] = {
1021e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = 0x0,
1022e1069839SBorislav Petkov [ C(RESULT_MISS) ] = 0x0,
1023e1069839SBorislav Petkov },
1024e1069839SBorislav Petkov },
1025e1069839SBorislav Petkov [ C(L1I ) ] = {
1026e1069839SBorislav Petkov [ C(OP_READ) ] = {
1027e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = 0x0,
1028e1069839SBorislav Petkov [ C(RESULT_MISS) ] = 0x280, /* ICACHE.MISSES */
1029e1069839SBorislav Petkov },
1030e1069839SBorislav Petkov [ C(OP_WRITE) ] = {
1031e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = -1,
1032e1069839SBorislav Petkov [ C(RESULT_MISS) ] = -1,
1033e1069839SBorislav Petkov },
1034e1069839SBorislav Petkov [ C(OP_PREFETCH) ] = {
1035e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = 0x0,
1036e1069839SBorislav Petkov [ C(RESULT_MISS) ] = 0x0,
1037e1069839SBorislav Petkov },
1038e1069839SBorislav Petkov },
1039e1069839SBorislav Petkov [ C(LL ) ] = {
1040e1069839SBorislav Petkov [ C(OP_READ) ] = {
1041e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = 0x1b7, /* OFFCORE_RESPONSE */
1042e1069839SBorislav Petkov [ C(RESULT_MISS) ] = 0x1b7, /* OFFCORE_RESPONSE */
1043e1069839SBorislav Petkov },
1044e1069839SBorislav Petkov [ C(OP_WRITE) ] = {
1045e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = 0x1b7, /* OFFCORE_RESPONSE */
1046e1069839SBorislav Petkov [ C(RESULT_MISS) ] = 0x1b7, /* OFFCORE_RESPONSE */
1047e1069839SBorislav Petkov },
1048e1069839SBorislav Petkov [ C(OP_PREFETCH) ] = {
1049e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = 0x0,
1050e1069839SBorislav Petkov [ C(RESULT_MISS) ] = 0x0,
1051e1069839SBorislav Petkov },
1052e1069839SBorislav Petkov },
1053e1069839SBorislav Petkov [ C(DTLB) ] = {
1054e1069839SBorislav Petkov [ C(OP_READ) ] = {
1055e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = 0x81d0, /* MEM_UOPS_RETIRED.ALL_LOADS */
1056e1069839SBorislav Petkov [ C(RESULT_MISS) ] = 0x108, /* DTLB_LOAD_MISSES.MISS_CAUSES_A_WALK */
1057e1069839SBorislav Petkov },
1058e1069839SBorislav Petkov [ C(OP_WRITE) ] = {
1059e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = 0x82d0, /* MEM_UOPS_RETIRED.ALL_STORES */
1060e1069839SBorislav Petkov [ C(RESULT_MISS) ] = 0x149, /* DTLB_STORE_MISSES.MISS_CAUSES_A_WALK */
1061e1069839SBorislav Petkov },
1062e1069839SBorislav Petkov [ C(OP_PREFETCH) ] = {
1063e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = 0x0,
1064e1069839SBorislav Petkov [ C(RESULT_MISS) ] = 0x0,
1065e1069839SBorislav Petkov },
1066e1069839SBorislav Petkov },
1067e1069839SBorislav Petkov [ C(ITLB) ] = {
1068e1069839SBorislav Petkov [ C(OP_READ) ] = {
1069e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = 0x6085, /* ITLB_MISSES.STLB_HIT */
1070e1069839SBorislav Petkov [ C(RESULT_MISS) ] = 0x185, /* ITLB_MISSES.MISS_CAUSES_A_WALK */
1071e1069839SBorislav Petkov },
1072e1069839SBorislav Petkov [ C(OP_WRITE) ] = {
1073e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = -1,
1074e1069839SBorislav Petkov [ C(RESULT_MISS) ] = -1,
1075e1069839SBorislav Petkov },
1076e1069839SBorislav Petkov [ C(OP_PREFETCH) ] = {
1077e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = -1,
1078e1069839SBorislav Petkov [ C(RESULT_MISS) ] = -1,
1079e1069839SBorislav Petkov },
1080e1069839SBorislav Petkov },
1081e1069839SBorislav Petkov [ C(BPU ) ] = {
1082e1069839SBorislav Petkov [ C(OP_READ) ] = {
1083e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = 0xc4, /* BR_INST_RETIRED.ALL_BRANCHES */
1084e1069839SBorislav Petkov [ C(RESULT_MISS) ] = 0xc5, /* BR_MISP_RETIRED.ALL_BRANCHES */
1085e1069839SBorislav Petkov },
1086e1069839SBorislav Petkov [ C(OP_WRITE) ] = {
1087e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = -1,
1088e1069839SBorislav Petkov [ C(RESULT_MISS) ] = -1,
1089e1069839SBorislav Petkov },
1090e1069839SBorislav Petkov [ C(OP_PREFETCH) ] = {
1091e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = -1,
1092e1069839SBorislav Petkov [ C(RESULT_MISS) ] = -1,
1093e1069839SBorislav Petkov },
1094e1069839SBorislav Petkov },
1095e1069839SBorislav Petkov [ C(NODE) ] = {
1096e1069839SBorislav Petkov [ C(OP_READ) ] = {
1097e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = 0x1b7, /* OFFCORE_RESPONSE */
1098e1069839SBorislav Petkov [ C(RESULT_MISS) ] = 0x1b7, /* OFFCORE_RESPONSE */
1099e1069839SBorislav Petkov },
1100e1069839SBorislav Petkov [ C(OP_WRITE) ] = {
1101e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = 0x1b7, /* OFFCORE_RESPONSE */
1102e1069839SBorislav Petkov [ C(RESULT_MISS) ] = 0x1b7, /* OFFCORE_RESPONSE */
1103e1069839SBorislav Petkov },
1104e1069839SBorislav Petkov [ C(OP_PREFETCH) ] = {
1105e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = 0x0,
1106e1069839SBorislav Petkov [ C(RESULT_MISS) ] = 0x0,
1107e1069839SBorislav Petkov },
1108e1069839SBorislav Petkov },
1109e1069839SBorislav Petkov };
1110e1069839SBorislav Petkov
1111e1069839SBorislav Petkov static __initconst const u64 hsw_hw_cache_extra_regs
1112e1069839SBorislav Petkov [PERF_COUNT_HW_CACHE_MAX]
1113e1069839SBorislav Petkov [PERF_COUNT_HW_CACHE_OP_MAX]
1114e1069839SBorislav Petkov [PERF_COUNT_HW_CACHE_RESULT_MAX] =
1115e1069839SBorislav Petkov {
1116e1069839SBorislav Petkov [ C(LL ) ] = {
1117e1069839SBorislav Petkov [ C(OP_READ) ] = {
1118e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = HSW_DEMAND_READ|
1119e1069839SBorislav Petkov HSW_LLC_ACCESS,
1120e1069839SBorislav Petkov [ C(RESULT_MISS) ] = HSW_DEMAND_READ|
1121e1069839SBorislav Petkov HSW_L3_MISS|HSW_ANY_SNOOP,
1122e1069839SBorislav Petkov },
1123e1069839SBorislav Petkov [ C(OP_WRITE) ] = {
1124e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = HSW_DEMAND_WRITE|
1125e1069839SBorislav Petkov HSW_LLC_ACCESS,
1126e1069839SBorislav Petkov [ C(RESULT_MISS) ] = HSW_DEMAND_WRITE|
1127e1069839SBorislav Petkov HSW_L3_MISS|HSW_ANY_SNOOP,
1128e1069839SBorislav Petkov },
1129e1069839SBorislav Petkov [ C(OP_PREFETCH) ] = {
1130e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = 0x0,
1131e1069839SBorislav Petkov [ C(RESULT_MISS) ] = 0x0,
1132e1069839SBorislav Petkov },
1133e1069839SBorislav Petkov },
1134e1069839SBorislav Petkov [ C(NODE) ] = {
1135e1069839SBorislav Petkov [ C(OP_READ) ] = {
1136e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = HSW_DEMAND_READ|
1137e1069839SBorislav Petkov HSW_L3_MISS_LOCAL_DRAM|
1138e1069839SBorislav Petkov HSW_SNOOP_DRAM,
1139e1069839SBorislav Petkov [ C(RESULT_MISS) ] = HSW_DEMAND_READ|
1140e1069839SBorislav Petkov HSW_L3_MISS_REMOTE|
1141e1069839SBorislav Petkov HSW_SNOOP_DRAM,
1142e1069839SBorislav Petkov },
1143e1069839SBorislav Petkov [ C(OP_WRITE) ] = {
1144e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = HSW_DEMAND_WRITE|
1145e1069839SBorislav Petkov HSW_L3_MISS_LOCAL_DRAM|
1146e1069839SBorislav Petkov HSW_SNOOP_DRAM,
1147e1069839SBorislav Petkov [ C(RESULT_MISS) ] = HSW_DEMAND_WRITE|
1148e1069839SBorislav Petkov HSW_L3_MISS_REMOTE|
1149e1069839SBorislav Petkov HSW_SNOOP_DRAM,
1150e1069839SBorislav Petkov },
1151e1069839SBorislav Petkov [ C(OP_PREFETCH) ] = {
1152e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = 0x0,
1153e1069839SBorislav Petkov [ C(RESULT_MISS) ] = 0x0,
1154e1069839SBorislav Petkov },
1155e1069839SBorislav Petkov },
1156e1069839SBorislav Petkov };
1157e1069839SBorislav Petkov
1158e1069839SBorislav Petkov static __initconst const u64 westmere_hw_cache_event_ids
1159e1069839SBorislav Petkov [PERF_COUNT_HW_CACHE_MAX]
1160e1069839SBorislav Petkov [PERF_COUNT_HW_CACHE_OP_MAX]
1161e1069839SBorislav Petkov [PERF_COUNT_HW_CACHE_RESULT_MAX] =
1162e1069839SBorislav Petkov {
1163e1069839SBorislav Petkov [ C(L1D) ] = {
1164e1069839SBorislav Petkov [ C(OP_READ) ] = {
1165e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = 0x010b, /* MEM_INST_RETIRED.LOADS */
1166e1069839SBorislav Petkov [ C(RESULT_MISS) ] = 0x0151, /* L1D.REPL */
1167e1069839SBorislav Petkov },
1168e1069839SBorislav Petkov [ C(OP_WRITE) ] = {
1169e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = 0x020b, /* MEM_INST_RETURED.STORES */
1170e1069839SBorislav Petkov [ C(RESULT_MISS) ] = 0x0251, /* L1D.M_REPL */
1171e1069839SBorislav Petkov },
1172e1069839SBorislav Petkov [ C(OP_PREFETCH) ] = {
1173e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = 0x014e, /* L1D_PREFETCH.REQUESTS */
1174e1069839SBorislav Petkov [ C(RESULT_MISS) ] = 0x024e, /* L1D_PREFETCH.MISS */
1175e1069839SBorislav Petkov },
1176e1069839SBorislav Petkov },
1177e1069839SBorislav Petkov [ C(L1I ) ] = {
1178e1069839SBorislav Petkov [ C(OP_READ) ] = {
1179e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = 0x0380, /* L1I.READS */
1180e1069839SBorislav Petkov [ C(RESULT_MISS) ] = 0x0280, /* L1I.MISSES */
1181e1069839SBorislav Petkov },
1182e1069839SBorislav Petkov [ C(OP_WRITE) ] = {
1183e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = -1,
1184e1069839SBorislav Petkov [ C(RESULT_MISS) ] = -1,
1185e1069839SBorislav Petkov },
1186e1069839SBorislav Petkov [ C(OP_PREFETCH) ] = {
1187e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = 0x0,
1188e1069839SBorislav Petkov [ C(RESULT_MISS) ] = 0x0,
1189e1069839SBorislav Petkov },
1190e1069839SBorislav Petkov },
1191e1069839SBorislav Petkov [ C(LL ) ] = {
1192e1069839SBorislav Petkov [ C(OP_READ) ] = {
1193e1069839SBorislav Petkov /* OFFCORE_RESPONSE.ANY_DATA.LOCAL_CACHE */
1194e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = 0x01b7,
1195e1069839SBorislav Petkov /* OFFCORE_RESPONSE.ANY_DATA.ANY_LLC_MISS */
1196e1069839SBorislav Petkov [ C(RESULT_MISS) ] = 0x01b7,
1197e1069839SBorislav Petkov },
1198e1069839SBorislav Petkov /*
1199e1069839SBorislav Petkov * Use RFO, not WRITEBACK, because a write miss would typically occur
1200e1069839SBorislav Petkov * on RFO.
1201e1069839SBorislav Petkov */
1202e1069839SBorislav Petkov [ C(OP_WRITE) ] = {
1203e1069839SBorislav Petkov /* OFFCORE_RESPONSE.ANY_RFO.LOCAL_CACHE */
1204e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = 0x01b7,
1205e1069839SBorislav Petkov /* OFFCORE_RESPONSE.ANY_RFO.ANY_LLC_MISS */
1206e1069839SBorislav Petkov [ C(RESULT_MISS) ] = 0x01b7,
1207e1069839SBorislav Petkov },
1208e1069839SBorislav Petkov [ C(OP_PREFETCH) ] = {
1209e1069839SBorislav Petkov /* OFFCORE_RESPONSE.PREFETCH.LOCAL_CACHE */
1210e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = 0x01b7,
1211e1069839SBorislav Petkov /* OFFCORE_RESPONSE.PREFETCH.ANY_LLC_MISS */
1212e1069839SBorislav Petkov [ C(RESULT_MISS) ] = 0x01b7,
1213e1069839SBorislav Petkov },
1214e1069839SBorislav Petkov },
1215e1069839SBorislav Petkov [ C(DTLB) ] = {
1216e1069839SBorislav Petkov [ C(OP_READ) ] = {
1217e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = 0x010b, /* MEM_INST_RETIRED.LOADS */
1218e1069839SBorislav Petkov [ C(RESULT_MISS) ] = 0x0108, /* DTLB_LOAD_MISSES.ANY */
1219e1069839SBorislav Petkov },
1220e1069839SBorislav Petkov [ C(OP_WRITE) ] = {
1221e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = 0x020b, /* MEM_INST_RETURED.STORES */
1222e1069839SBorislav Petkov [ C(RESULT_MISS) ] = 0x010c, /* MEM_STORE_RETIRED.DTLB_MISS */
1223e1069839SBorislav Petkov },
1224e1069839SBorislav Petkov [ C(OP_PREFETCH) ] = {
1225e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = 0x0,
1226e1069839SBorislav Petkov [ C(RESULT_MISS) ] = 0x0,
1227e1069839SBorislav Petkov },
1228e1069839SBorislav Petkov },
1229e1069839SBorislav Petkov [ C(ITLB) ] = {
1230e1069839SBorislav Petkov [ C(OP_READ) ] = {
1231e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = 0x01c0, /* INST_RETIRED.ANY_P */
1232e1069839SBorislav Petkov [ C(RESULT_MISS) ] = 0x0185, /* ITLB_MISSES.ANY */
1233e1069839SBorislav Petkov },
1234e1069839SBorislav Petkov [ C(OP_WRITE) ] = {
1235e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = -1,
1236e1069839SBorislav Petkov [ C(RESULT_MISS) ] = -1,
1237e1069839SBorislav Petkov },
1238e1069839SBorislav Petkov [ C(OP_PREFETCH) ] = {
1239e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = -1,
1240e1069839SBorislav Petkov [ C(RESULT_MISS) ] = -1,
1241e1069839SBorislav Petkov },
1242e1069839SBorislav Petkov },
1243e1069839SBorislav Petkov [ C(BPU ) ] = {
1244e1069839SBorislav Petkov [ C(OP_READ) ] = {
1245e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = 0x00c4, /* BR_INST_RETIRED.ALL_BRANCHES */
1246e1069839SBorislav Petkov [ C(RESULT_MISS) ] = 0x03e8, /* BPU_CLEARS.ANY */
1247e1069839SBorislav Petkov },
1248e1069839SBorislav Petkov [ C(OP_WRITE) ] = {
1249e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = -1,
1250e1069839SBorislav Petkov [ C(RESULT_MISS) ] = -1,
1251e1069839SBorislav Petkov },
1252e1069839SBorislav Petkov [ C(OP_PREFETCH) ] = {
1253e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = -1,
1254e1069839SBorislav Petkov [ C(RESULT_MISS) ] = -1,
1255e1069839SBorislav Petkov },
1256e1069839SBorislav Petkov },
1257e1069839SBorislav Petkov [ C(NODE) ] = {
1258e1069839SBorislav Petkov [ C(OP_READ) ] = {
1259e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = 0x01b7,
1260e1069839SBorislav Petkov [ C(RESULT_MISS) ] = 0x01b7,
1261e1069839SBorislav Petkov },
1262e1069839SBorislav Petkov [ C(OP_WRITE) ] = {
1263e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = 0x01b7,
1264e1069839SBorislav Petkov [ C(RESULT_MISS) ] = 0x01b7,
1265e1069839SBorislav Petkov },
1266e1069839SBorislav Petkov [ C(OP_PREFETCH) ] = {
1267e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = 0x01b7,
1268e1069839SBorislav Petkov [ C(RESULT_MISS) ] = 0x01b7,
1269e1069839SBorislav Petkov },
1270e1069839SBorislav Petkov },
1271e1069839SBorislav Petkov };
1272e1069839SBorislav Petkov
1273e1069839SBorislav Petkov /*
1274e1069839SBorislav Petkov * Nehalem/Westmere MSR_OFFCORE_RESPONSE bits;
1275e1069839SBorislav Petkov * See IA32 SDM Vol 3B 30.6.1.3
1276e1069839SBorislav Petkov */
1277e1069839SBorislav Petkov
1278e1069839SBorislav Petkov #define NHM_DMND_DATA_RD (1 << 0)
1279e1069839SBorislav Petkov #define NHM_DMND_RFO (1 << 1)
1280e1069839SBorislav Petkov #define NHM_DMND_IFETCH (1 << 2)
1281e1069839SBorislav Petkov #define NHM_DMND_WB (1 << 3)
1282e1069839SBorislav Petkov #define NHM_PF_DATA_RD (1 << 4)
1283e1069839SBorislav Petkov #define NHM_PF_DATA_RFO (1 << 5)
1284e1069839SBorislav Petkov #define NHM_PF_IFETCH (1 << 6)
1285e1069839SBorislav Petkov #define NHM_OFFCORE_OTHER (1 << 7)
1286e1069839SBorislav Petkov #define NHM_UNCORE_HIT (1 << 8)
1287e1069839SBorislav Petkov #define NHM_OTHER_CORE_HIT_SNP (1 << 9)
1288e1069839SBorislav Petkov #define NHM_OTHER_CORE_HITM (1 << 10)
1289e1069839SBorislav Petkov /* reserved */
1290e1069839SBorislav Petkov #define NHM_REMOTE_CACHE_FWD (1 << 12)
1291e1069839SBorislav Petkov #define NHM_REMOTE_DRAM (1 << 13)
1292e1069839SBorislav Petkov #define NHM_LOCAL_DRAM (1 << 14)
1293e1069839SBorislav Petkov #define NHM_NON_DRAM (1 << 15)
1294e1069839SBorislav Petkov
1295e1069839SBorislav Petkov #define NHM_LOCAL (NHM_LOCAL_DRAM|NHM_REMOTE_CACHE_FWD)
1296e1069839SBorislav Petkov #define NHM_REMOTE (NHM_REMOTE_DRAM)
1297e1069839SBorislav Petkov
1298e1069839SBorislav Petkov #define NHM_DMND_READ (NHM_DMND_DATA_RD)
1299e1069839SBorislav Petkov #define NHM_DMND_WRITE (NHM_DMND_RFO|NHM_DMND_WB)
1300e1069839SBorislav Petkov #define NHM_DMND_PREFETCH (NHM_PF_DATA_RD|NHM_PF_DATA_RFO)
1301e1069839SBorislav Petkov
1302e1069839SBorislav Petkov #define NHM_L3_HIT (NHM_UNCORE_HIT|NHM_OTHER_CORE_HIT_SNP|NHM_OTHER_CORE_HITM)
1303e1069839SBorislav Petkov #define NHM_L3_MISS (NHM_NON_DRAM|NHM_LOCAL_DRAM|NHM_REMOTE_DRAM|NHM_REMOTE_CACHE_FWD)
1304e1069839SBorislav Petkov #define NHM_L3_ACCESS (NHM_L3_HIT|NHM_L3_MISS)
1305e1069839SBorislav Petkov
1306e1069839SBorislav Petkov static __initconst const u64 nehalem_hw_cache_extra_regs
1307e1069839SBorislav Petkov [PERF_COUNT_HW_CACHE_MAX]
1308e1069839SBorislav Petkov [PERF_COUNT_HW_CACHE_OP_MAX]
1309e1069839SBorislav Petkov [PERF_COUNT_HW_CACHE_RESULT_MAX] =
1310e1069839SBorislav Petkov {
1311e1069839SBorislav Petkov [ C(LL ) ] = {
1312e1069839SBorislav Petkov [ C(OP_READ) ] = {
1313e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = NHM_DMND_READ|NHM_L3_ACCESS,
1314e1069839SBorislav Petkov [ C(RESULT_MISS) ] = NHM_DMND_READ|NHM_L3_MISS,
1315e1069839SBorislav Petkov },
1316e1069839SBorislav Petkov [ C(OP_WRITE) ] = {
1317e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = NHM_DMND_WRITE|NHM_L3_ACCESS,
1318e1069839SBorislav Petkov [ C(RESULT_MISS) ] = NHM_DMND_WRITE|NHM_L3_MISS,
1319e1069839SBorislav Petkov },
1320e1069839SBorislav Petkov [ C(OP_PREFETCH) ] = {
1321e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = NHM_DMND_PREFETCH|NHM_L3_ACCESS,
1322e1069839SBorislav Petkov [ C(RESULT_MISS) ] = NHM_DMND_PREFETCH|NHM_L3_MISS,
1323e1069839SBorislav Petkov },
1324e1069839SBorislav Petkov },
1325e1069839SBorislav Petkov [ C(NODE) ] = {
1326e1069839SBorislav Petkov [ C(OP_READ) ] = {
1327e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = NHM_DMND_READ|NHM_LOCAL|NHM_REMOTE,
1328e1069839SBorislav Petkov [ C(RESULT_MISS) ] = NHM_DMND_READ|NHM_REMOTE,
1329e1069839SBorislav Petkov },
1330e1069839SBorislav Petkov [ C(OP_WRITE) ] = {
1331e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = NHM_DMND_WRITE|NHM_LOCAL|NHM_REMOTE,
1332e1069839SBorislav Petkov [ C(RESULT_MISS) ] = NHM_DMND_WRITE|NHM_REMOTE,
1333e1069839SBorislav Petkov },
1334e1069839SBorislav Petkov [ C(OP_PREFETCH) ] = {
1335e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = NHM_DMND_PREFETCH|NHM_LOCAL|NHM_REMOTE,
1336e1069839SBorislav Petkov [ C(RESULT_MISS) ] = NHM_DMND_PREFETCH|NHM_REMOTE,
1337e1069839SBorislav Petkov },
1338e1069839SBorislav Petkov },
1339e1069839SBorislav Petkov };
1340e1069839SBorislav Petkov
1341e1069839SBorislav Petkov static __initconst const u64 nehalem_hw_cache_event_ids
1342e1069839SBorislav Petkov [PERF_COUNT_HW_CACHE_MAX]
1343e1069839SBorislav Petkov [PERF_COUNT_HW_CACHE_OP_MAX]
1344e1069839SBorislav Petkov [PERF_COUNT_HW_CACHE_RESULT_MAX] =
1345e1069839SBorislav Petkov {
1346e1069839SBorislav Petkov [ C(L1D) ] = {
1347e1069839SBorislav Petkov [ C(OP_READ) ] = {
1348e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = 0x010b, /* MEM_INST_RETIRED.LOADS */
1349e1069839SBorislav Petkov [ C(RESULT_MISS) ] = 0x0151, /* L1D.REPL */
1350e1069839SBorislav Petkov },
1351e1069839SBorislav Petkov [ C(OP_WRITE) ] = {
1352e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = 0x020b, /* MEM_INST_RETURED.STORES */
1353e1069839SBorislav Petkov [ C(RESULT_MISS) ] = 0x0251, /* L1D.M_REPL */
1354e1069839SBorislav Petkov },
1355e1069839SBorislav Petkov [ C(OP_PREFETCH) ] = {
1356e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = 0x014e, /* L1D_PREFETCH.REQUESTS */
1357e1069839SBorislav Petkov [ C(RESULT_MISS) ] = 0x024e, /* L1D_PREFETCH.MISS */
1358e1069839SBorislav Petkov },
1359e1069839SBorislav Petkov },
1360e1069839SBorislav Petkov [ C(L1I ) ] = {
1361e1069839SBorislav Petkov [ C(OP_READ) ] = {
1362e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = 0x0380, /* L1I.READS */
1363e1069839SBorislav Petkov [ C(RESULT_MISS) ] = 0x0280, /* L1I.MISSES */
1364e1069839SBorislav Petkov },
1365e1069839SBorislav Petkov [ C(OP_WRITE) ] = {
1366e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = -1,
1367e1069839SBorislav Petkov [ C(RESULT_MISS) ] = -1,
1368e1069839SBorislav Petkov },
1369e1069839SBorislav Petkov [ C(OP_PREFETCH) ] = {
1370e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = 0x0,
1371e1069839SBorislav Petkov [ C(RESULT_MISS) ] = 0x0,
1372e1069839SBorislav Petkov },
1373e1069839SBorislav Petkov },
1374e1069839SBorislav Petkov [ C(LL ) ] = {
1375e1069839SBorislav Petkov [ C(OP_READ) ] = {
1376e1069839SBorislav Petkov /* OFFCORE_RESPONSE.ANY_DATA.LOCAL_CACHE */
1377e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = 0x01b7,
1378e1069839SBorislav Petkov /* OFFCORE_RESPONSE.ANY_DATA.ANY_LLC_MISS */
1379e1069839SBorislav Petkov [ C(RESULT_MISS) ] = 0x01b7,
1380e1069839SBorislav Petkov },
1381e1069839SBorislav Petkov /*
1382e1069839SBorislav Petkov * Use RFO, not WRITEBACK, because a write miss would typically occur
1383e1069839SBorislav Petkov * on RFO.
1384e1069839SBorislav Petkov */
1385e1069839SBorislav Petkov [ C(OP_WRITE) ] = {
1386e1069839SBorislav Petkov /* OFFCORE_RESPONSE.ANY_RFO.LOCAL_CACHE */
1387e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = 0x01b7,
1388e1069839SBorislav Petkov /* OFFCORE_RESPONSE.ANY_RFO.ANY_LLC_MISS */
1389e1069839SBorislav Petkov [ C(RESULT_MISS) ] = 0x01b7,
1390e1069839SBorislav Petkov },
1391e1069839SBorislav Petkov [ C(OP_PREFETCH) ] = {
1392e1069839SBorislav Petkov /* OFFCORE_RESPONSE.PREFETCH.LOCAL_CACHE */
1393e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = 0x01b7,
1394e1069839SBorislav Petkov /* OFFCORE_RESPONSE.PREFETCH.ANY_LLC_MISS */
1395e1069839SBorislav Petkov [ C(RESULT_MISS) ] = 0x01b7,
1396e1069839SBorislav Petkov },
1397e1069839SBorislav Petkov },
1398e1069839SBorislav Petkov [ C(DTLB) ] = {
1399e1069839SBorislav Petkov [ C(OP_READ) ] = {
1400e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = 0x0f40, /* L1D_CACHE_LD.MESI (alias) */
1401e1069839SBorislav Petkov [ C(RESULT_MISS) ] = 0x0108, /* DTLB_LOAD_MISSES.ANY */
1402e1069839SBorislav Petkov },
1403e1069839SBorislav Petkov [ C(OP_WRITE) ] = {
1404e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = 0x0f41, /* L1D_CACHE_ST.MESI (alias) */
1405e1069839SBorislav Petkov [ C(RESULT_MISS) ] = 0x010c, /* MEM_STORE_RETIRED.DTLB_MISS */
1406e1069839SBorislav Petkov },
1407e1069839SBorislav Petkov [ C(OP_PREFETCH) ] = {
1408e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = 0x0,
1409e1069839SBorislav Petkov [ C(RESULT_MISS) ] = 0x0,
1410e1069839SBorislav Petkov },
1411e1069839SBorislav Petkov },
1412e1069839SBorislav Petkov [ C(ITLB) ] = {
1413e1069839SBorislav Petkov [ C(OP_READ) ] = {
1414e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = 0x01c0, /* INST_RETIRED.ANY_P */
1415e1069839SBorislav Petkov [ C(RESULT_MISS) ] = 0x20c8, /* ITLB_MISS_RETIRED */
1416e1069839SBorislav Petkov },
1417e1069839SBorislav Petkov [ C(OP_WRITE) ] = {
1418e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = -1,
1419e1069839SBorislav Petkov [ C(RESULT_MISS) ] = -1,
1420e1069839SBorislav Petkov },
1421e1069839SBorislav Petkov [ C(OP_PREFETCH) ] = {
1422e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = -1,
1423e1069839SBorislav Petkov [ C(RESULT_MISS) ] = -1,
1424e1069839SBorislav Petkov },
1425e1069839SBorislav Petkov },
1426e1069839SBorislav Petkov [ C(BPU ) ] = {
1427e1069839SBorislav Petkov [ C(OP_READ) ] = {
1428e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = 0x00c4, /* BR_INST_RETIRED.ALL_BRANCHES */
1429e1069839SBorislav Petkov [ C(RESULT_MISS) ] = 0x03e8, /* BPU_CLEARS.ANY */
1430e1069839SBorislav Petkov },
1431e1069839SBorislav Petkov [ C(OP_WRITE) ] = {
1432e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = -1,
1433e1069839SBorislav Petkov [ C(RESULT_MISS) ] = -1,
1434e1069839SBorislav Petkov },
1435e1069839SBorislav Petkov [ C(OP_PREFETCH) ] = {
1436e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = -1,
1437e1069839SBorislav Petkov [ C(RESULT_MISS) ] = -1,
1438e1069839SBorislav Petkov },
1439e1069839SBorislav Petkov },
1440e1069839SBorislav Petkov [ C(NODE) ] = {
1441e1069839SBorislav Petkov [ C(OP_READ) ] = {
1442e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = 0x01b7,
1443e1069839SBorislav Petkov [ C(RESULT_MISS) ] = 0x01b7,
1444e1069839SBorislav Petkov },
1445e1069839SBorislav Petkov [ C(OP_WRITE) ] = {
1446e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = 0x01b7,
1447e1069839SBorislav Petkov [ C(RESULT_MISS) ] = 0x01b7,
1448e1069839SBorislav Petkov },
1449e1069839SBorislav Petkov [ C(OP_PREFETCH) ] = {
1450e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = 0x01b7,
1451e1069839SBorislav Petkov [ C(RESULT_MISS) ] = 0x01b7,
1452e1069839SBorislav Petkov },
1453e1069839SBorislav Petkov },
1454e1069839SBorislav Petkov };
1455e1069839SBorislav Petkov
1456e1069839SBorislav Petkov static __initconst const u64 core2_hw_cache_event_ids
1457e1069839SBorislav Petkov [PERF_COUNT_HW_CACHE_MAX]
1458e1069839SBorislav Petkov [PERF_COUNT_HW_CACHE_OP_MAX]
1459e1069839SBorislav Petkov [PERF_COUNT_HW_CACHE_RESULT_MAX] =
1460e1069839SBorislav Petkov {
1461e1069839SBorislav Petkov [ C(L1D) ] = {
1462e1069839SBorislav Petkov [ C(OP_READ) ] = {
1463e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = 0x0f40, /* L1D_CACHE_LD.MESI */
1464e1069839SBorislav Petkov [ C(RESULT_MISS) ] = 0x0140, /* L1D_CACHE_LD.I_STATE */
1465e1069839SBorislav Petkov },
1466e1069839SBorislav Petkov [ C(OP_WRITE) ] = {
1467e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = 0x0f41, /* L1D_CACHE_ST.MESI */
1468e1069839SBorislav Petkov [ C(RESULT_MISS) ] = 0x0141, /* L1D_CACHE_ST.I_STATE */
1469e1069839SBorislav Petkov },
1470e1069839SBorislav Petkov [ C(OP_PREFETCH) ] = {
1471e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = 0x104e, /* L1D_PREFETCH.REQUESTS */
1472e1069839SBorislav Petkov [ C(RESULT_MISS) ] = 0,
1473e1069839SBorislav Petkov },
1474e1069839SBorislav Petkov },
1475e1069839SBorislav Petkov [ C(L1I ) ] = {
1476e1069839SBorislav Petkov [ C(OP_READ) ] = {
1477e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = 0x0080, /* L1I.READS */
1478e1069839SBorislav Petkov [ C(RESULT_MISS) ] = 0x0081, /* L1I.MISSES */
1479e1069839SBorislav Petkov },
1480e1069839SBorislav Petkov [ C(OP_WRITE) ] = {
1481e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = -1,
1482e1069839SBorislav Petkov [ C(RESULT_MISS) ] = -1,
1483e1069839SBorislav Petkov },
1484e1069839SBorislav Petkov [ C(OP_PREFETCH) ] = {
1485e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = 0,
1486e1069839SBorislav Petkov [ C(RESULT_MISS) ] = 0,
1487e1069839SBorislav Petkov },
1488e1069839SBorislav Petkov },
1489e1069839SBorislav Petkov [ C(LL ) ] = {
1490e1069839SBorislav Petkov [ C(OP_READ) ] = {
1491e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = 0x4f29, /* L2_LD.MESI */
1492e1069839SBorislav Petkov [ C(RESULT_MISS) ] = 0x4129, /* L2_LD.ISTATE */
1493e1069839SBorislav Petkov },
1494e1069839SBorislav Petkov [ C(OP_WRITE) ] = {
1495e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = 0x4f2A, /* L2_ST.MESI */
1496e1069839SBorislav Petkov [ C(RESULT_MISS) ] = 0x412A, /* L2_ST.ISTATE */
1497e1069839SBorislav Petkov },
1498e1069839SBorislav Petkov [ C(OP_PREFETCH) ] = {
1499e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = 0,
1500e1069839SBorislav Petkov [ C(RESULT_MISS) ] = 0,
1501e1069839SBorislav Petkov },
1502e1069839SBorislav Petkov },
1503e1069839SBorislav Petkov [ C(DTLB) ] = {
1504e1069839SBorislav Petkov [ C(OP_READ) ] = {
1505e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = 0x0f40, /* L1D_CACHE_LD.MESI (alias) */
1506e1069839SBorislav Petkov [ C(RESULT_MISS) ] = 0x0208, /* DTLB_MISSES.MISS_LD */
1507e1069839SBorislav Petkov },
1508e1069839SBorislav Petkov [ C(OP_WRITE) ] = {
1509e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = 0x0f41, /* L1D_CACHE_ST.MESI (alias) */
1510e1069839SBorislav Petkov [ C(RESULT_MISS) ] = 0x0808, /* DTLB_MISSES.MISS_ST */
1511e1069839SBorislav Petkov },
1512e1069839SBorislav Petkov [ C(OP_PREFETCH) ] = {
1513e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = 0,
1514e1069839SBorislav Petkov [ C(RESULT_MISS) ] = 0,
1515e1069839SBorislav Petkov },
1516e1069839SBorislav Petkov },
1517e1069839SBorislav Petkov [ C(ITLB) ] = {
1518e1069839SBorislav Petkov [ C(OP_READ) ] = {
1519e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = 0x00c0, /* INST_RETIRED.ANY_P */
1520e1069839SBorislav Petkov [ C(RESULT_MISS) ] = 0x1282, /* ITLBMISSES */
1521e1069839SBorislav Petkov },
1522e1069839SBorislav Petkov [ C(OP_WRITE) ] = {
1523e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = -1,
1524e1069839SBorislav Petkov [ C(RESULT_MISS) ] = -1,
1525e1069839SBorislav Petkov },
1526e1069839SBorislav Petkov [ C(OP_PREFETCH) ] = {
1527e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = -1,
1528e1069839SBorislav Petkov [ C(RESULT_MISS) ] = -1,
1529e1069839SBorislav Petkov },
1530e1069839SBorislav Petkov },
1531e1069839SBorislav Petkov [ C(BPU ) ] = {
1532e1069839SBorislav Petkov [ C(OP_READ) ] = {
1533e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = 0x00c4, /* BR_INST_RETIRED.ANY */
1534e1069839SBorislav Petkov [ C(RESULT_MISS) ] = 0x00c5, /* BP_INST_RETIRED.MISPRED */
1535e1069839SBorislav Petkov },
1536e1069839SBorislav Petkov [ C(OP_WRITE) ] = {
1537e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = -1,
1538e1069839SBorislav Petkov [ C(RESULT_MISS) ] = -1,
1539e1069839SBorislav Petkov },
1540e1069839SBorislav Petkov [ C(OP_PREFETCH) ] = {
1541e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = -1,
1542e1069839SBorislav Petkov [ C(RESULT_MISS) ] = -1,
1543e1069839SBorislav Petkov },
1544e1069839SBorislav Petkov },
1545e1069839SBorislav Petkov };
1546e1069839SBorislav Petkov
1547e1069839SBorislav Petkov static __initconst const u64 atom_hw_cache_event_ids
1548e1069839SBorislav Petkov [PERF_COUNT_HW_CACHE_MAX]
1549e1069839SBorislav Petkov [PERF_COUNT_HW_CACHE_OP_MAX]
1550e1069839SBorislav Petkov [PERF_COUNT_HW_CACHE_RESULT_MAX] =
1551e1069839SBorislav Petkov {
1552e1069839SBorislav Petkov [ C(L1D) ] = {
1553e1069839SBorislav Petkov [ C(OP_READ) ] = {
1554e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = 0x2140, /* L1D_CACHE.LD */
1555e1069839SBorislav Petkov [ C(RESULT_MISS) ] = 0,
1556e1069839SBorislav Petkov },
1557e1069839SBorislav Petkov [ C(OP_WRITE) ] = {
1558e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = 0x2240, /* L1D_CACHE.ST */
1559e1069839SBorislav Petkov [ C(RESULT_MISS) ] = 0,
1560e1069839SBorislav Petkov },
1561e1069839SBorislav Petkov [ C(OP_PREFETCH) ] = {
1562e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = 0x0,
1563e1069839SBorislav Petkov [ C(RESULT_MISS) ] = 0,
1564e1069839SBorislav Petkov },
1565e1069839SBorislav Petkov },
1566e1069839SBorislav Petkov [ C(L1I ) ] = {
1567e1069839SBorislav Petkov [ C(OP_READ) ] = {
1568e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = 0x0380, /* L1I.READS */
1569e1069839SBorislav Petkov [ C(RESULT_MISS) ] = 0x0280, /* L1I.MISSES */
1570e1069839SBorislav Petkov },
1571e1069839SBorislav Petkov [ C(OP_WRITE) ] = {
1572e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = -1,
1573e1069839SBorislav Petkov [ C(RESULT_MISS) ] = -1,
1574e1069839SBorislav Petkov },
1575e1069839SBorislav Petkov [ C(OP_PREFETCH) ] = {
1576e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = 0,
1577e1069839SBorislav Petkov [ C(RESULT_MISS) ] = 0,
1578e1069839SBorislav Petkov },
1579e1069839SBorislav Petkov },
1580e1069839SBorislav Petkov [ C(LL ) ] = {
1581e1069839SBorislav Petkov [ C(OP_READ) ] = {
1582e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = 0x4f29, /* L2_LD.MESI */
1583e1069839SBorislav Petkov [ C(RESULT_MISS) ] = 0x4129, /* L2_LD.ISTATE */
1584e1069839SBorislav Petkov },
1585e1069839SBorislav Petkov [ C(OP_WRITE) ] = {
1586e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = 0x4f2A, /* L2_ST.MESI */
1587e1069839SBorislav Petkov [ C(RESULT_MISS) ] = 0x412A, /* L2_ST.ISTATE */
1588e1069839SBorislav Petkov },
1589e1069839SBorislav Petkov [ C(OP_PREFETCH) ] = {
1590e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = 0,
1591e1069839SBorislav Petkov [ C(RESULT_MISS) ] = 0,
1592e1069839SBorislav Petkov },
1593e1069839SBorislav Petkov },
1594e1069839SBorislav Petkov [ C(DTLB) ] = {
1595e1069839SBorislav Petkov [ C(OP_READ) ] = {
1596e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = 0x2140, /* L1D_CACHE_LD.MESI (alias) */
1597e1069839SBorislav Petkov [ C(RESULT_MISS) ] = 0x0508, /* DTLB_MISSES.MISS_LD */
1598e1069839SBorislav Petkov },
1599e1069839SBorislav Petkov [ C(OP_WRITE) ] = {
1600e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = 0x2240, /* L1D_CACHE_ST.MESI (alias) */
1601e1069839SBorislav Petkov [ C(RESULT_MISS) ] = 0x0608, /* DTLB_MISSES.MISS_ST */
1602e1069839SBorislav Petkov },
1603e1069839SBorislav Petkov [ C(OP_PREFETCH) ] = {
1604e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = 0,
1605e1069839SBorislav Petkov [ C(RESULT_MISS) ] = 0,
1606e1069839SBorislav Petkov },
1607e1069839SBorislav Petkov },
1608e1069839SBorislav Petkov [ C(ITLB) ] = {
1609e1069839SBorislav Petkov [ C(OP_READ) ] = {
1610e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = 0x00c0, /* INST_RETIRED.ANY_P */
1611e1069839SBorislav Petkov [ C(RESULT_MISS) ] = 0x0282, /* ITLB.MISSES */
1612e1069839SBorislav Petkov },
1613e1069839SBorislav Petkov [ C(OP_WRITE) ] = {
1614e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = -1,
1615e1069839SBorislav Petkov [ C(RESULT_MISS) ] = -1,
1616e1069839SBorislav Petkov },
1617e1069839SBorislav Petkov [ C(OP_PREFETCH) ] = {
1618e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = -1,
1619e1069839SBorislav Petkov [ C(RESULT_MISS) ] = -1,
1620e1069839SBorislav Petkov },
1621e1069839SBorislav Petkov },
1622e1069839SBorislav Petkov [ C(BPU ) ] = {
1623e1069839SBorislav Petkov [ C(OP_READ) ] = {
1624e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = 0x00c4, /* BR_INST_RETIRED.ANY */
1625e1069839SBorislav Petkov [ C(RESULT_MISS) ] = 0x00c5, /* BP_INST_RETIRED.MISPRED */
1626e1069839SBorislav Petkov },
1627e1069839SBorislav Petkov [ C(OP_WRITE) ] = {
1628e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = -1,
1629e1069839SBorislav Petkov [ C(RESULT_MISS) ] = -1,
1630e1069839SBorislav Petkov },
1631e1069839SBorislav Petkov [ C(OP_PREFETCH) ] = {
1632e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = -1,
1633e1069839SBorislav Petkov [ C(RESULT_MISS) ] = -1,
1634e1069839SBorislav Petkov },
1635e1069839SBorislav Petkov },
1636e1069839SBorislav Petkov };
1637e1069839SBorislav Petkov
1638eb12b8ecSAndi Kleen EVENT_ATTR_STR(topdown-total-slots, td_total_slots_slm, "event=0x3c");
1639eb12b8ecSAndi Kleen EVENT_ATTR_STR(topdown-total-slots.scale, td_total_slots_scale_slm, "2");
1640eb12b8ecSAndi Kleen /* no_alloc_cycles.not_delivered */
1641eb12b8ecSAndi Kleen EVENT_ATTR_STR(topdown-fetch-bubbles, td_fetch_bubbles_slm,
1642eb12b8ecSAndi Kleen "event=0xca,umask=0x50");
1643eb12b8ecSAndi Kleen EVENT_ATTR_STR(topdown-fetch-bubbles.scale, td_fetch_bubbles_scale_slm, "2");
1644eb12b8ecSAndi Kleen /* uops_retired.all */
1645eb12b8ecSAndi Kleen EVENT_ATTR_STR(topdown-slots-issued, td_slots_issued_slm,
1646eb12b8ecSAndi Kleen "event=0xc2,umask=0x10");
1647eb12b8ecSAndi Kleen /* uops_retired.all */
1648eb12b8ecSAndi Kleen EVENT_ATTR_STR(topdown-slots-retired, td_slots_retired_slm,
1649eb12b8ecSAndi Kleen "event=0xc2,umask=0x10");
1650eb12b8ecSAndi Kleen
1651eb12b8ecSAndi Kleen static struct attribute *slm_events_attrs[] = {
1652eb12b8ecSAndi Kleen EVENT_PTR(td_total_slots_slm),
1653eb12b8ecSAndi Kleen EVENT_PTR(td_total_slots_scale_slm),
1654eb12b8ecSAndi Kleen EVENT_PTR(td_fetch_bubbles_slm),
1655eb12b8ecSAndi Kleen EVENT_PTR(td_fetch_bubbles_scale_slm),
1656eb12b8ecSAndi Kleen EVENT_PTR(td_slots_issued_slm),
1657eb12b8ecSAndi Kleen EVENT_PTR(td_slots_retired_slm),
1658eb12b8ecSAndi Kleen NULL
1659eb12b8ecSAndi Kleen };
1660eb12b8ecSAndi Kleen
1661e1069839SBorislav Petkov static struct extra_reg intel_slm_extra_regs[] __read_mostly =
1662e1069839SBorislav Petkov {
1663e1069839SBorislav Petkov /* must define OFFCORE_RSP_X first, see intel_fixup_er() */
1664e1069839SBorislav Petkov INTEL_UEVENT_EXTRA_REG(0x01b7, MSR_OFFCORE_RSP_0, 0x768005ffffull, RSP_0),
1665e1069839SBorislav Petkov INTEL_UEVENT_EXTRA_REG(0x02b7, MSR_OFFCORE_RSP_1, 0x368005ffffull, RSP_1),
1666e1069839SBorislav Petkov EVENT_EXTRA_END
1667e1069839SBorislav Petkov };
1668e1069839SBorislav Petkov
1669e1069839SBorislav Petkov #define SLM_DMND_READ SNB_DMND_DATA_RD
1670e1069839SBorislav Petkov #define SLM_DMND_WRITE SNB_DMND_RFO
1671e1069839SBorislav Petkov #define SLM_DMND_PREFETCH (SNB_PF_DATA_RD|SNB_PF_RFO)
1672e1069839SBorislav Petkov
1673e1069839SBorislav Petkov #define SLM_SNP_ANY (SNB_SNP_NONE|SNB_SNP_MISS|SNB_NO_FWD|SNB_HITM)
1674e1069839SBorislav Petkov #define SLM_LLC_ACCESS SNB_RESP_ANY
1675e1069839SBorislav Petkov #define SLM_LLC_MISS (SLM_SNP_ANY|SNB_NON_DRAM)
1676e1069839SBorislav Petkov
1677e1069839SBorislav Petkov static __initconst const u64 slm_hw_cache_extra_regs
1678e1069839SBorislav Petkov [PERF_COUNT_HW_CACHE_MAX]
1679e1069839SBorislav Petkov [PERF_COUNT_HW_CACHE_OP_MAX]
1680e1069839SBorislav Petkov [PERF_COUNT_HW_CACHE_RESULT_MAX] =
1681e1069839SBorislav Petkov {
1682e1069839SBorislav Petkov [ C(LL ) ] = {
1683e1069839SBorislav Petkov [ C(OP_READ) ] = {
1684e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = SLM_DMND_READ|SLM_LLC_ACCESS,
1685e1069839SBorislav Petkov [ C(RESULT_MISS) ] = 0,
1686e1069839SBorislav Petkov },
1687e1069839SBorislav Petkov [ C(OP_WRITE) ] = {
1688e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = SLM_DMND_WRITE|SLM_LLC_ACCESS,
1689e1069839SBorislav Petkov [ C(RESULT_MISS) ] = SLM_DMND_WRITE|SLM_LLC_MISS,
1690e1069839SBorislav Petkov },
1691e1069839SBorislav Petkov [ C(OP_PREFETCH) ] = {
1692e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = SLM_DMND_PREFETCH|SLM_LLC_ACCESS,
1693e1069839SBorislav Petkov [ C(RESULT_MISS) ] = SLM_DMND_PREFETCH|SLM_LLC_MISS,
1694e1069839SBorislav Petkov },
1695e1069839SBorislav Petkov },
1696e1069839SBorislav Petkov };
1697e1069839SBorislav Petkov
1698e1069839SBorislav Petkov static __initconst const u64 slm_hw_cache_event_ids
1699e1069839SBorislav Petkov [PERF_COUNT_HW_CACHE_MAX]
1700e1069839SBorislav Petkov [PERF_COUNT_HW_CACHE_OP_MAX]
1701e1069839SBorislav Petkov [PERF_COUNT_HW_CACHE_RESULT_MAX] =
1702e1069839SBorislav Petkov {
1703e1069839SBorislav Petkov [ C(L1D) ] = {
1704e1069839SBorislav Petkov [ C(OP_READ) ] = {
1705e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = 0,
1706e1069839SBorislav Petkov [ C(RESULT_MISS) ] = 0x0104, /* LD_DCU_MISS */
1707e1069839SBorislav Petkov },
1708e1069839SBorislav Petkov [ C(OP_WRITE) ] = {
1709e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = 0,
1710e1069839SBorislav Petkov [ C(RESULT_MISS) ] = 0,
1711e1069839SBorislav Petkov },
1712e1069839SBorislav Petkov [ C(OP_PREFETCH) ] = {
1713e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = 0,
1714e1069839SBorislav Petkov [ C(RESULT_MISS) ] = 0,
1715e1069839SBorislav Petkov },
1716e1069839SBorislav Petkov },
1717e1069839SBorislav Petkov [ C(L1I ) ] = {
1718e1069839SBorislav Petkov [ C(OP_READ) ] = {
1719e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = 0x0380, /* ICACHE.ACCESSES */
1720e1069839SBorislav Petkov [ C(RESULT_MISS) ] = 0x0280, /* ICACGE.MISSES */
1721e1069839SBorislav Petkov },
1722e1069839SBorislav Petkov [ C(OP_WRITE) ] = {
1723e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = -1,
1724e1069839SBorislav Petkov [ C(RESULT_MISS) ] = -1,
1725e1069839SBorislav Petkov },
1726e1069839SBorislav Petkov [ C(OP_PREFETCH) ] = {
1727e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = 0,
1728e1069839SBorislav Petkov [ C(RESULT_MISS) ] = 0,
1729e1069839SBorislav Petkov },
1730e1069839SBorislav Petkov },
1731e1069839SBorislav Petkov [ C(LL ) ] = {
1732e1069839SBorislav Petkov [ C(OP_READ) ] = {
1733e1069839SBorislav Petkov /* OFFCORE_RESPONSE.ANY_DATA.LOCAL_CACHE */
1734e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = 0x01b7,
1735e1069839SBorislav Petkov [ C(RESULT_MISS) ] = 0,
1736e1069839SBorislav Petkov },
1737e1069839SBorislav Petkov [ C(OP_WRITE) ] = {
1738e1069839SBorislav Petkov /* OFFCORE_RESPONSE.ANY_RFO.LOCAL_CACHE */
1739e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = 0x01b7,
1740e1069839SBorislav Petkov /* OFFCORE_RESPONSE.ANY_RFO.ANY_LLC_MISS */
1741e1069839SBorislav Petkov [ C(RESULT_MISS) ] = 0x01b7,
1742e1069839SBorislav Petkov },
1743e1069839SBorislav Petkov [ C(OP_PREFETCH) ] = {
1744e1069839SBorislav Petkov /* OFFCORE_RESPONSE.PREFETCH.LOCAL_CACHE */
1745e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = 0x01b7,
1746e1069839SBorislav Petkov /* OFFCORE_RESPONSE.PREFETCH.ANY_LLC_MISS */
1747e1069839SBorislav Petkov [ C(RESULT_MISS) ] = 0x01b7,
1748e1069839SBorislav Petkov },
1749e1069839SBorislav Petkov },
1750e1069839SBorislav Petkov [ C(DTLB) ] = {
1751e1069839SBorislav Petkov [ C(OP_READ) ] = {
1752e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = 0,
1753e1069839SBorislav Petkov [ C(RESULT_MISS) ] = 0x0804, /* LD_DTLB_MISS */
1754e1069839SBorislav Petkov },
1755e1069839SBorislav Petkov [ C(OP_WRITE) ] = {
1756e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = 0,
1757e1069839SBorislav Petkov [ C(RESULT_MISS) ] = 0,
1758e1069839SBorislav Petkov },
1759e1069839SBorislav Petkov [ C(OP_PREFETCH) ] = {
1760e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = 0,
1761e1069839SBorislav Petkov [ C(RESULT_MISS) ] = 0,
1762e1069839SBorislav Petkov },
1763e1069839SBorislav Petkov },
1764e1069839SBorislav Petkov [ C(ITLB) ] = {
1765e1069839SBorislav Petkov [ C(OP_READ) ] = {
1766e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = 0x00c0, /* INST_RETIRED.ANY_P */
1767e1069839SBorislav Petkov [ C(RESULT_MISS) ] = 0x40205, /* PAGE_WALKS.I_SIDE_WALKS */
1768e1069839SBorislav Petkov },
1769e1069839SBorislav Petkov [ C(OP_WRITE) ] = {
1770e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = -1,
1771e1069839SBorislav Petkov [ C(RESULT_MISS) ] = -1,
1772e1069839SBorislav Petkov },
1773e1069839SBorislav Petkov [ C(OP_PREFETCH) ] = {
1774e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = -1,
1775e1069839SBorislav Petkov [ C(RESULT_MISS) ] = -1,
1776e1069839SBorislav Petkov },
1777e1069839SBorislav Petkov },
1778e1069839SBorislav Petkov [ C(BPU ) ] = {
1779e1069839SBorislav Petkov [ C(OP_READ) ] = {
1780e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = 0x00c4, /* BR_INST_RETIRED.ANY */
1781e1069839SBorislav Petkov [ C(RESULT_MISS) ] = 0x00c5, /* BP_INST_RETIRED.MISPRED */
1782e1069839SBorislav Petkov },
1783e1069839SBorislav Petkov [ C(OP_WRITE) ] = {
1784e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = -1,
1785e1069839SBorislav Petkov [ C(RESULT_MISS) ] = -1,
1786e1069839SBorislav Petkov },
1787e1069839SBorislav Petkov [ C(OP_PREFETCH) ] = {
1788e1069839SBorislav Petkov [ C(RESULT_ACCESS) ] = -1,
1789e1069839SBorislav Petkov [ C(RESULT_MISS) ] = -1,
1790e1069839SBorislav Petkov },
1791e1069839SBorislav Petkov },
1792e1069839SBorislav Petkov };
1793e1069839SBorislav Petkov
1794ed827adbSKan Liang EVENT_ATTR_STR(topdown-total-slots, td_total_slots_glm, "event=0x3c");
1795ed827adbSKan Liang EVENT_ATTR_STR(topdown-total-slots.scale, td_total_slots_scale_glm, "3");
1796ed827adbSKan Liang /* UOPS_NOT_DELIVERED.ANY */
1797ed827adbSKan Liang EVENT_ATTR_STR(topdown-fetch-bubbles, td_fetch_bubbles_glm, "event=0x9c");
1798ed827adbSKan Liang /* ISSUE_SLOTS_NOT_CONSUMED.RECOVERY */
1799ed827adbSKan Liang EVENT_ATTR_STR(topdown-recovery-bubbles, td_recovery_bubbles_glm, "event=0xca,umask=0x02");
1800ed827adbSKan Liang /* UOPS_RETIRED.ANY */
1801ed827adbSKan Liang EVENT_ATTR_STR(topdown-slots-retired, td_slots_retired_glm, "event=0xc2");
1802ed827adbSKan Liang /* UOPS_ISSUED.ANY */
1803ed827adbSKan Liang EVENT_ATTR_STR(topdown-slots-issued, td_slots_issued_glm, "event=0x0e");
1804ed827adbSKan Liang
1805ed827adbSKan Liang static struct attribute *glm_events_attrs[] = {
1806ed827adbSKan Liang EVENT_PTR(td_total_slots_glm),
1807ed827adbSKan Liang EVENT_PTR(td_total_slots_scale_glm),
1808ed827adbSKan Liang EVENT_PTR(td_fetch_bubbles_glm),
1809ed827adbSKan Liang EVENT_PTR(td_recovery_bubbles_glm),
1810ed827adbSKan Liang EVENT_PTR(td_slots_issued_glm),
1811ed827adbSKan Liang EVENT_PTR(td_slots_retired_glm),
1812ed827adbSKan Liang NULL
1813ed827adbSKan Liang };
1814ed827adbSKan Liang
18158b92c3a7SKan Liang static struct extra_reg intel_glm_extra_regs[] __read_mostly = {
18168b92c3a7SKan Liang /* must define OFFCORE_RSP_X first, see intel_fixup_er() */
18178b92c3a7SKan Liang INTEL_UEVENT_EXTRA_REG(0x01b7, MSR_OFFCORE_RSP_0, 0x760005ffbfull, RSP_0),
18188b92c3a7SKan Liang INTEL_UEVENT_EXTRA_REG(0x02b7, MSR_OFFCORE_RSP_1, 0x360005ffbfull, RSP_1),
18198b92c3a7SKan Liang EVENT_EXTRA_END
18208b92c3a7SKan Liang };
18218b92c3a7SKan Liang
18228b92c3a7SKan Liang #define GLM_DEMAND_DATA_RD BIT_ULL(0)
18238b92c3a7SKan Liang #define GLM_DEMAND_RFO BIT_ULL(1)
18248b92c3a7SKan Liang #define GLM_ANY_RESPONSE BIT_ULL(16)
18258b92c3a7SKan Liang #define GLM_SNP_NONE_OR_MISS BIT_ULL(33)
18268b92c3a7SKan Liang #define GLM_DEMAND_READ GLM_DEMAND_DATA_RD
18278b92c3a7SKan Liang #define GLM_DEMAND_WRITE GLM_DEMAND_RFO
18288b92c3a7SKan Liang #define GLM_DEMAND_PREFETCH (SNB_PF_DATA_RD|SNB_PF_RFO)
18298b92c3a7SKan Liang #define GLM_LLC_ACCESS GLM_ANY_RESPONSE
18308b92c3a7SKan Liang #define GLM_SNP_ANY (GLM_SNP_NONE_OR_MISS|SNB_NO_FWD|SNB_HITM)
18318b92c3a7SKan Liang #define GLM_LLC_MISS (GLM_SNP_ANY|SNB_NON_DRAM)
18328b92c3a7SKan Liang
18338b92c3a7SKan Liang static __initconst const u64 glm_hw_cache_event_ids
18348b92c3a7SKan Liang [PERF_COUNT_HW_CACHE_MAX]
18358b92c3a7SKan Liang [PERF_COUNT_HW_CACHE_OP_MAX]
18368b92c3a7SKan Liang [PERF_COUNT_HW_CACHE_RESULT_MAX] = {
18378b92c3a7SKan Liang [C(L1D)] = {
18388b92c3a7SKan Liang [C(OP_READ)] = {
18398b92c3a7SKan Liang [C(RESULT_ACCESS)] = 0x81d0, /* MEM_UOPS_RETIRED.ALL_LOADS */
18408b92c3a7SKan Liang [C(RESULT_MISS)] = 0x0,
18418b92c3a7SKan Liang },
18428b92c3a7SKan Liang [C(OP_WRITE)] = {
18438b92c3a7SKan Liang [C(RESULT_ACCESS)] = 0x82d0, /* MEM_UOPS_RETIRED.ALL_STORES */
18448b92c3a7SKan Liang [C(RESULT_MISS)] = 0x0,
18458b92c3a7SKan Liang },
18468b92c3a7SKan Liang [C(OP_PREFETCH)] = {
18478b92c3a7SKan Liang [C(RESULT_ACCESS)] = 0x0,
18488b92c3a7SKan Liang [C(RESULT_MISS)] = 0x0,
18498b92c3a7SKan Liang },
18508b92c3a7SKan Liang },
18518b92c3a7SKan Liang [C(L1I)] = {
18528b92c3a7SKan Liang [C(OP_READ)] = {
18538b92c3a7SKan Liang [C(RESULT_ACCESS)] = 0x0380, /* ICACHE.ACCESSES */
18548b92c3a7SKan Liang [C(RESULT_MISS)] = 0x0280, /* ICACHE.MISSES */
18558b92c3a7SKan Liang },
18568b92c3a7SKan Liang [C(OP_WRITE)] = {
18578b92c3a7SKan Liang [C(RESULT_ACCESS)] = -1,
18588b92c3a7SKan Liang [C(RESULT_MISS)] = -1,
18598b92c3a7SKan Liang },
18608b92c3a7SKan Liang [C(OP_PREFETCH)] = {
18618b92c3a7SKan Liang [C(RESULT_ACCESS)] = 0x0,
18628b92c3a7SKan Liang [C(RESULT_MISS)] = 0x0,
18638b92c3a7SKan Liang },
18648b92c3a7SKan Liang },
18658b92c3a7SKan Liang [C(LL)] = {
18668b92c3a7SKan Liang [C(OP_READ)] = {
18678b92c3a7SKan Liang [C(RESULT_ACCESS)] = 0x1b7, /* OFFCORE_RESPONSE */
18688b92c3a7SKan Liang [C(RESULT_MISS)] = 0x1b7, /* OFFCORE_RESPONSE */
18698b92c3a7SKan Liang },
18708b92c3a7SKan Liang [C(OP_WRITE)] = {
18718b92c3a7SKan Liang [C(RESULT_ACCESS)] = 0x1b7, /* OFFCORE_RESPONSE */
18728b92c3a7SKan Liang [C(RESULT_MISS)] = 0x1b7, /* OFFCORE_RESPONSE */
18738b92c3a7SKan Liang },
18748b92c3a7SKan Liang [C(OP_PREFETCH)] = {
18758b92c3a7SKan Liang [C(RESULT_ACCESS)] = 0x1b7, /* OFFCORE_RESPONSE */
18768b92c3a7SKan Liang [C(RESULT_MISS)] = 0x1b7, /* OFFCORE_RESPONSE */
18778b92c3a7SKan Liang },
18788b92c3a7SKan Liang },
18798b92c3a7SKan Liang [C(DTLB)] = {
18808b92c3a7SKan Liang [C(OP_READ)] = {
18818b92c3a7SKan Liang [C(RESULT_ACCESS)] = 0x81d0, /* MEM_UOPS_RETIRED.ALL_LOADS */
18828b92c3a7SKan Liang [C(RESULT_MISS)] = 0x0,
18838b92c3a7SKan Liang },
18848b92c3a7SKan Liang [C(OP_WRITE)] = {
18858b92c3a7SKan Liang [C(RESULT_ACCESS)] = 0x82d0, /* MEM_UOPS_RETIRED.ALL_STORES */
18868b92c3a7SKan Liang [C(RESULT_MISS)] = 0x0,
18878b92c3a7SKan Liang },
18888b92c3a7SKan Liang [C(OP_PREFETCH)] = {
18898b92c3a7SKan Liang [C(RESULT_ACCESS)] = 0x0,
18908b92c3a7SKan Liang [C(RESULT_MISS)] = 0x0,
18918b92c3a7SKan Liang },
18928b92c3a7SKan Liang },
18938b92c3a7SKan Liang [C(ITLB)] = {
18948b92c3a7SKan Liang [C(OP_READ)] = {
18958b92c3a7SKan Liang [C(RESULT_ACCESS)] = 0x00c0, /* INST_RETIRED.ANY_P */
18968b92c3a7SKan Liang [C(RESULT_MISS)] = 0x0481, /* ITLB.MISS */
18978b92c3a7SKan Liang },
18988b92c3a7SKan Liang [C(OP_WRITE)] = {
18998b92c3a7SKan Liang [C(RESULT_ACCESS)] = -1,
19008b92c3a7SKan Liang [C(RESULT_MISS)] = -1,
19018b92c3a7SKan Liang },
19028b92c3a7SKan Liang [C(OP_PREFETCH)] = {
19038b92c3a7SKan Liang [C(RESULT_ACCESS)] = -1,
19048b92c3a7SKan Liang [C(RESULT_MISS)] = -1,
19058b92c3a7SKan Liang },
19068b92c3a7SKan Liang },
19078b92c3a7SKan Liang [C(BPU)] = {
19088b92c3a7SKan Liang [C(OP_READ)] = {
19098b92c3a7SKan Liang [C(RESULT_ACCESS)] = 0x00c4, /* BR_INST_RETIRED.ALL_BRANCHES */
19108b92c3a7SKan Liang [C(RESULT_MISS)] = 0x00c5, /* BR_MISP_RETIRED.ALL_BRANCHES */
19118b92c3a7SKan Liang },
19128b92c3a7SKan Liang [C(OP_WRITE)] = {
19138b92c3a7SKan Liang [C(RESULT_ACCESS)] = -1,
19148b92c3a7SKan Liang [C(RESULT_MISS)] = -1,
19158b92c3a7SKan Liang },
19168b92c3a7SKan Liang [C(OP_PREFETCH)] = {
19178b92c3a7SKan Liang [C(RESULT_ACCESS)] = -1,
19188b92c3a7SKan Liang [C(RESULT_MISS)] = -1,
19198b92c3a7SKan Liang },
19208b92c3a7SKan Liang },
19218b92c3a7SKan Liang };
19228b92c3a7SKan Liang
19238b92c3a7SKan Liang static __initconst const u64 glm_hw_cache_extra_regs
19248b92c3a7SKan Liang [PERF_COUNT_HW_CACHE_MAX]
19258b92c3a7SKan Liang [PERF_COUNT_HW_CACHE_OP_MAX]
19268b92c3a7SKan Liang [PERF_COUNT_HW_CACHE_RESULT_MAX] = {
19278b92c3a7SKan Liang [C(LL)] = {
19288b92c3a7SKan Liang [C(OP_READ)] = {
19298b92c3a7SKan Liang [C(RESULT_ACCESS)] = GLM_DEMAND_READ|
19308b92c3a7SKan Liang GLM_LLC_ACCESS,
19318b92c3a7SKan Liang [C(RESULT_MISS)] = GLM_DEMAND_READ|
19328b92c3a7SKan Liang GLM_LLC_MISS,
19338b92c3a7SKan Liang },
19348b92c3a7SKan Liang [C(OP_WRITE)] = {
19358b92c3a7SKan Liang [C(RESULT_ACCESS)] = GLM_DEMAND_WRITE|
19368b92c3a7SKan Liang GLM_LLC_ACCESS,
19378b92c3a7SKan Liang [C(RESULT_MISS)] = GLM_DEMAND_WRITE|
19388b92c3a7SKan Liang GLM_LLC_MISS,
19398b92c3a7SKan Liang },
19408b92c3a7SKan Liang [C(OP_PREFETCH)] = {
19418b92c3a7SKan Liang [C(RESULT_ACCESS)] = GLM_DEMAND_PREFETCH|
19428b92c3a7SKan Liang GLM_LLC_ACCESS,
19438b92c3a7SKan Liang [C(RESULT_MISS)] = GLM_DEMAND_PREFETCH|
19448b92c3a7SKan Liang GLM_LLC_MISS,
19458b92c3a7SKan Liang },
19468b92c3a7SKan Liang },
19478b92c3a7SKan Liang };
19488b92c3a7SKan Liang
1949dd0b06b5SKan Liang static __initconst const u64 glp_hw_cache_event_ids
1950dd0b06b5SKan Liang [PERF_COUNT_HW_CACHE_MAX]
1951dd0b06b5SKan Liang [PERF_COUNT_HW_CACHE_OP_MAX]
1952dd0b06b5SKan Liang [PERF_COUNT_HW_CACHE_RESULT_MAX] = {
1953dd0b06b5SKan Liang [C(L1D)] = {
1954dd0b06b5SKan Liang [C(OP_READ)] = {
1955dd0b06b5SKan Liang [C(RESULT_ACCESS)] = 0x81d0, /* MEM_UOPS_RETIRED.ALL_LOADS */
1956dd0b06b5SKan Liang [C(RESULT_MISS)] = 0x0,
1957dd0b06b5SKan Liang },
1958dd0b06b5SKan Liang [C(OP_WRITE)] = {
1959dd0b06b5SKan Liang [C(RESULT_ACCESS)] = 0x82d0, /* MEM_UOPS_RETIRED.ALL_STORES */
1960dd0b06b5SKan Liang [C(RESULT_MISS)] = 0x0,
1961dd0b06b5SKan Liang },
1962dd0b06b5SKan Liang [C(OP_PREFETCH)] = {
1963dd0b06b5SKan Liang [C(RESULT_ACCESS)] = 0x0,
1964dd0b06b5SKan Liang [C(RESULT_MISS)] = 0x0,
1965dd0b06b5SKan Liang },
1966dd0b06b5SKan Liang },
1967dd0b06b5SKan Liang [C(L1I)] = {
1968dd0b06b5SKan Liang [C(OP_READ)] = {
1969dd0b06b5SKan Liang [C(RESULT_ACCESS)] = 0x0380, /* ICACHE.ACCESSES */
1970dd0b06b5SKan Liang [C(RESULT_MISS)] = 0x0280, /* ICACHE.MISSES */
1971dd0b06b5SKan Liang },
1972dd0b06b5SKan Liang [C(OP_WRITE)] = {
1973dd0b06b5SKan Liang [C(RESULT_ACCESS)] = -1,
1974dd0b06b5SKan Liang [C(RESULT_MISS)] = -1,
1975dd0b06b5SKan Liang },
1976dd0b06b5SKan Liang [C(OP_PREFETCH)] = {
1977dd0b06b5SKan Liang [C(RESULT_ACCESS)] = 0x0,
1978dd0b06b5SKan Liang [C(RESULT_MISS)] = 0x0,
1979dd0b06b5SKan Liang },
1980dd0b06b5SKan Liang },
1981dd0b06b5SKan Liang [C(LL)] = {
1982dd0b06b5SKan Liang [C(OP_READ)] = {
1983dd0b06b5SKan Liang [C(RESULT_ACCESS)] = 0x1b7, /* OFFCORE_RESPONSE */
1984dd0b06b5SKan Liang [C(RESULT_MISS)] = 0x1b7, /* OFFCORE_RESPONSE */
1985dd0b06b5SKan Liang },
1986dd0b06b5SKan Liang [C(OP_WRITE)] = {
1987dd0b06b5SKan Liang [C(RESULT_ACCESS)] = 0x1b7, /* OFFCORE_RESPONSE */
1988dd0b06b5SKan Liang [C(RESULT_MISS)] = 0x1b7, /* OFFCORE_RESPONSE */
1989dd0b06b5SKan Liang },
1990dd0b06b5SKan Liang [C(OP_PREFETCH)] = {
1991dd0b06b5SKan Liang [C(RESULT_ACCESS)] = 0x0,
1992dd0b06b5SKan Liang [C(RESULT_MISS)] = 0x0,
1993dd0b06b5SKan Liang },
1994dd0b06b5SKan Liang },
1995dd0b06b5SKan Liang [C(DTLB)] = {
1996dd0b06b5SKan Liang [C(OP_READ)] = {
1997dd0b06b5SKan Liang [C(RESULT_ACCESS)] = 0x81d0, /* MEM_UOPS_RETIRED.ALL_LOADS */
1998dd0b06b5SKan Liang [C(RESULT_MISS)] = 0xe08, /* DTLB_LOAD_MISSES.WALK_COMPLETED */
1999dd0b06b5SKan Liang },
2000dd0b06b5SKan Liang [C(OP_WRITE)] = {
2001dd0b06b5SKan Liang [C(RESULT_ACCESS)] = 0x82d0, /* MEM_UOPS_RETIRED.ALL_STORES */
2002dd0b06b5SKan Liang [C(RESULT_MISS)] = 0xe49, /* DTLB_STORE_MISSES.WALK_COMPLETED */
2003dd0b06b5SKan Liang },
2004dd0b06b5SKan Liang [C(OP_PREFETCH)] = {
2005dd0b06b5SKan Liang [C(RESULT_ACCESS)] = 0x0,
2006dd0b06b5SKan Liang [C(RESULT_MISS)] = 0x0,
2007dd0b06b5SKan Liang },
2008dd0b06b5SKan Liang },
2009dd0b06b5SKan Liang [C(ITLB)] = {
2010dd0b06b5SKan Liang [C(OP_READ)] = {
2011dd0b06b5SKan Liang [C(RESULT_ACCESS)] = 0x00c0, /* INST_RETIRED.ANY_P */
2012dd0b06b5SKan Liang [C(RESULT_MISS)] = 0x0481, /* ITLB.MISS */
2013dd0b06b5SKan Liang },
2014dd0b06b5SKan Liang [C(OP_WRITE)] = {
2015dd0b06b5SKan Liang [C(RESULT_ACCESS)] = -1,
2016dd0b06b5SKan Liang [C(RESULT_MISS)] = -1,
2017dd0b06b5SKan Liang },
2018dd0b06b5SKan Liang [C(OP_PREFETCH)] = {
2019dd0b06b5SKan Liang [C(RESULT_ACCESS)] = -1,
2020dd0b06b5SKan Liang [C(RESULT_MISS)] = -1,
2021dd0b06b5SKan Liang },
2022dd0b06b5SKan Liang },
2023dd0b06b5SKan Liang [C(BPU)] = {
2024dd0b06b5SKan Liang [C(OP_READ)] = {
2025dd0b06b5SKan Liang [C(RESULT_ACCESS)] = 0x00c4, /* BR_INST_RETIRED.ALL_BRANCHES */
2026dd0b06b5SKan Liang [C(RESULT_MISS)] = 0x00c5, /* BR_MISP_RETIRED.ALL_BRANCHES */
2027dd0b06b5SKan Liang },
2028dd0b06b5SKan Liang [C(OP_WRITE)] = {
2029dd0b06b5SKan Liang [C(RESULT_ACCESS)] = -1,
2030dd0b06b5SKan Liang [C(RESULT_MISS)] = -1,
2031dd0b06b5SKan Liang },
2032dd0b06b5SKan Liang [C(OP_PREFETCH)] = {
2033dd0b06b5SKan Liang [C(RESULT_ACCESS)] = -1,
2034dd0b06b5SKan Liang [C(RESULT_MISS)] = -1,
2035dd0b06b5SKan Liang },
2036dd0b06b5SKan Liang },
2037dd0b06b5SKan Liang };
2038dd0b06b5SKan Liang
2039dd0b06b5SKan Liang static __initconst const u64 glp_hw_cache_extra_regs
2040dd0b06b5SKan Liang [PERF_COUNT_HW_CACHE_MAX]
2041dd0b06b5SKan Liang [PERF_COUNT_HW_CACHE_OP_MAX]
2042dd0b06b5SKan Liang [PERF_COUNT_HW_CACHE_RESULT_MAX] = {
2043dd0b06b5SKan Liang [C(LL)] = {
2044dd0b06b5SKan Liang [C(OP_READ)] = {
2045dd0b06b5SKan Liang [C(RESULT_ACCESS)] = GLM_DEMAND_READ|
2046dd0b06b5SKan Liang GLM_LLC_ACCESS,
2047dd0b06b5SKan Liang [C(RESULT_MISS)] = GLM_DEMAND_READ|
2048dd0b06b5SKan Liang GLM_LLC_MISS,
2049dd0b06b5SKan Liang },
2050dd0b06b5SKan Liang [C(OP_WRITE)] = {
2051dd0b06b5SKan Liang [C(RESULT_ACCESS)] = GLM_DEMAND_WRITE|
2052dd0b06b5SKan Liang GLM_LLC_ACCESS,
2053dd0b06b5SKan Liang [C(RESULT_MISS)] = GLM_DEMAND_WRITE|
2054dd0b06b5SKan Liang GLM_LLC_MISS,
2055dd0b06b5SKan Liang },
2056dd0b06b5SKan Liang [C(OP_PREFETCH)] = {
2057dd0b06b5SKan Liang [C(RESULT_ACCESS)] = 0x0,
2058dd0b06b5SKan Liang [C(RESULT_MISS)] = 0x0,
2059dd0b06b5SKan Liang },
2060dd0b06b5SKan Liang },
2061dd0b06b5SKan Liang };
2062dd0b06b5SKan Liang
20636daeb873SKan Liang #define TNT_LOCAL_DRAM BIT_ULL(26)
20646daeb873SKan Liang #define TNT_DEMAND_READ GLM_DEMAND_DATA_RD
20656daeb873SKan Liang #define TNT_DEMAND_WRITE GLM_DEMAND_RFO
20666daeb873SKan Liang #define TNT_LLC_ACCESS GLM_ANY_RESPONSE
20676daeb873SKan Liang #define TNT_SNP_ANY (SNB_SNP_NOT_NEEDED|SNB_SNP_MISS| \
20686daeb873SKan Liang SNB_NO_FWD|SNB_SNP_FWD|SNB_HITM)
20696daeb873SKan Liang #define TNT_LLC_MISS (TNT_SNP_ANY|SNB_NON_DRAM|TNT_LOCAL_DRAM)
20706daeb873SKan Liang
20716daeb873SKan Liang static __initconst const u64 tnt_hw_cache_extra_regs
20726daeb873SKan Liang [PERF_COUNT_HW_CACHE_MAX]
20736daeb873SKan Liang [PERF_COUNT_HW_CACHE_OP_MAX]
20746daeb873SKan Liang [PERF_COUNT_HW_CACHE_RESULT_MAX] = {
20756daeb873SKan Liang [C(LL)] = {
20766daeb873SKan Liang [C(OP_READ)] = {
20776daeb873SKan Liang [C(RESULT_ACCESS)] = TNT_DEMAND_READ|
20786daeb873SKan Liang TNT_LLC_ACCESS,
20796daeb873SKan Liang [C(RESULT_MISS)] = TNT_DEMAND_READ|
20806daeb873SKan Liang TNT_LLC_MISS,
20816daeb873SKan Liang },
20826daeb873SKan Liang [C(OP_WRITE)] = {
20836daeb873SKan Liang [C(RESULT_ACCESS)] = TNT_DEMAND_WRITE|
20846daeb873SKan Liang TNT_LLC_ACCESS,
20856daeb873SKan Liang [C(RESULT_MISS)] = TNT_DEMAND_WRITE|
20866daeb873SKan Liang TNT_LLC_MISS,
20876daeb873SKan Liang },
20886daeb873SKan Liang [C(OP_PREFETCH)] = {
20896daeb873SKan Liang [C(RESULT_ACCESS)] = 0x0,
20906daeb873SKan Liang [C(RESULT_MISS)] = 0x0,
20916daeb873SKan Liang },
20926daeb873SKan Liang },
20936daeb873SKan Liang };
20946daeb873SKan Liang
2095c2208046SKan Liang EVENT_ATTR_STR(topdown-fe-bound, td_fe_bound_tnt, "event=0x71,umask=0x0");
2096c2208046SKan Liang EVENT_ATTR_STR(topdown-retiring, td_retiring_tnt, "event=0xc2,umask=0x0");
2097c2208046SKan Liang EVENT_ATTR_STR(topdown-bad-spec, td_bad_spec_tnt, "event=0x73,umask=0x6");
2098c2208046SKan Liang EVENT_ATTR_STR(topdown-be-bound, td_be_bound_tnt, "event=0x74,umask=0x0");
2099c2208046SKan Liang
2100c2208046SKan Liang static struct attribute *tnt_events_attrs[] = {
2101c2208046SKan Liang EVENT_PTR(td_fe_bound_tnt),
2102c2208046SKan Liang EVENT_PTR(td_retiring_tnt),
2103c2208046SKan Liang EVENT_PTR(td_bad_spec_tnt),
2104c2208046SKan Liang EVENT_PTR(td_be_bound_tnt),
2105c2208046SKan Liang NULL,
2106c2208046SKan Liang };
2107c2208046SKan Liang
21086daeb873SKan Liang static struct extra_reg intel_tnt_extra_regs[] __read_mostly = {
21096daeb873SKan Liang /* must define OFFCORE_RSP_X first, see intel_fixup_er() */
21100813c405SKan Liang INTEL_UEVENT_EXTRA_REG(0x01b7, MSR_OFFCORE_RSP_0, 0x800ff0ffffff9fffull, RSP_0),
21110813c405SKan Liang INTEL_UEVENT_EXTRA_REG(0x02b7, MSR_OFFCORE_RSP_1, 0xff0ffffff9fffull, RSP_1),
21126daeb873SKan Liang EVENT_EXTRA_END
21136daeb873SKan Liang };
21146daeb873SKan Liang
211524919fdeSKan Liang EVENT_ATTR_STR(mem-loads, mem_ld_grt, "event=0xd0,umask=0x5,ldlat=3");
211624919fdeSKan Liang EVENT_ATTR_STR(mem-stores, mem_st_grt, "event=0xd0,umask=0x6");
211724919fdeSKan Liang
211824919fdeSKan Liang static struct attribute *grt_mem_attrs[] = {
211924919fdeSKan Liang EVENT_PTR(mem_ld_grt),
212024919fdeSKan Liang EVENT_PTR(mem_st_grt),
212124919fdeSKan Liang NULL
212224919fdeSKan Liang };
212324919fdeSKan Liang
2124f83d2f91SKan Liang static struct extra_reg intel_grt_extra_regs[] __read_mostly = {
2125f83d2f91SKan Liang /* must define OFFCORE_RSP_X first, see intel_fixup_er() */
2126f83d2f91SKan Liang INTEL_UEVENT_EXTRA_REG(0x01b7, MSR_OFFCORE_RSP_0, 0x3fffffffffull, RSP_0),
2127f83d2f91SKan Liang INTEL_UEVENT_EXTRA_REG(0x02b7, MSR_OFFCORE_RSP_1, 0x3fffffffffull, RSP_1),
2128f83d2f91SKan Liang INTEL_UEVENT_PEBS_LDLAT_EXTRA_REG(0x5d0),
2129f83d2f91SKan Liang EVENT_EXTRA_END
2130f83d2f91SKan Liang };
2131f83d2f91SKan Liang
2132a430021fSKan Liang EVENT_ATTR_STR(topdown-retiring, td_retiring_cmt, "event=0x72,umask=0x0");
2133a430021fSKan Liang EVENT_ATTR_STR(topdown-bad-spec, td_bad_spec_cmt, "event=0x73,umask=0x0");
2134a430021fSKan Liang
2135a430021fSKan Liang static struct attribute *cmt_events_attrs[] = {
2136a430021fSKan Liang EVENT_PTR(td_fe_bound_tnt),
2137a430021fSKan Liang EVENT_PTR(td_retiring_cmt),
2138a430021fSKan Liang EVENT_PTR(td_bad_spec_cmt),
2139a430021fSKan Liang EVENT_PTR(td_be_bound_tnt),
2140a430021fSKan Liang NULL
2141a430021fSKan Liang };
2142a430021fSKan Liang
214338aaf921SKan Liang static struct extra_reg intel_cmt_extra_regs[] __read_mostly = {
214438aaf921SKan Liang /* must define OFFCORE_RSP_X first, see intel_fixup_er() */
214538aaf921SKan Liang INTEL_UEVENT_EXTRA_REG(0x01b7, MSR_OFFCORE_RSP_0, 0x800ff3ffffffffffull, RSP_0),
214638aaf921SKan Liang INTEL_UEVENT_EXTRA_REG(0x02b7, MSR_OFFCORE_RSP_1, 0xff3ffffffffffull, RSP_1),
214738aaf921SKan Liang INTEL_UEVENT_PEBS_LDLAT_EXTRA_REG(0x5d0),
214838aaf921SKan Liang INTEL_UEVENT_EXTRA_REG(0x0127, MSR_SNOOP_RSP_0, 0xffffffffffffffffull, SNOOP_0),
214938aaf921SKan Liang INTEL_UEVENT_EXTRA_REG(0x0227, MSR_SNOOP_RSP_1, 0xffffffffffffffffull, SNOOP_1),
215038aaf921SKan Liang EVENT_EXTRA_END
215138aaf921SKan Liang };
215238aaf921SKan Liang
2153e1069839SBorislav Petkov #define KNL_OT_L2_HITE BIT_ULL(19) /* Other Tile L2 Hit */
2154e1069839SBorislav Petkov #define KNL_OT_L2_HITF BIT_ULL(20) /* Other Tile L2 Hit */
2155e1069839SBorislav Petkov #define KNL_MCDRAM_LOCAL BIT_ULL(21)
2156e1069839SBorislav Petkov #define KNL_MCDRAM_FAR BIT_ULL(22)
2157e1069839SBorislav Petkov #define KNL_DDR_LOCAL BIT_ULL(23)
2158e1069839SBorislav Petkov #define KNL_DDR_FAR BIT_ULL(24)
2159e1069839SBorislav Petkov #define KNL_DRAM_ANY (KNL_MCDRAM_LOCAL | KNL_MCDRAM_FAR | \
2160e1069839SBorislav Petkov KNL_DDR_LOCAL | KNL_DDR_FAR)
2161e1069839SBorislav Petkov #define KNL_L2_READ SLM_DMND_READ
2162e1069839SBorislav Petkov #define KNL_L2_WRITE SLM_DMND_WRITE
2163e1069839SBorislav Petkov #define KNL_L2_PREFETCH SLM_DMND_PREFETCH
2164e1069839SBorislav Petkov #define KNL_L2_ACCESS SLM_LLC_ACCESS
2165e1069839SBorislav Petkov #define KNL_L2_MISS (KNL_OT_L2_HITE | KNL_OT_L2_HITF | \
2166e1069839SBorislav Petkov KNL_DRAM_ANY | SNB_SNP_ANY | \
2167e1069839SBorislav Petkov SNB_NON_DRAM)
2168e1069839SBorislav Petkov
2169e1069839SBorislav Petkov static __initconst const u64 knl_hw_cache_extra_regs
2170e1069839SBorislav Petkov [PERF_COUNT_HW_CACHE_MAX]
2171e1069839SBorislav Petkov [PERF_COUNT_HW_CACHE_OP_MAX]
2172e1069839SBorislav Petkov [PERF_COUNT_HW_CACHE_RESULT_MAX] = {
2173e1069839SBorislav Petkov [C(LL)] = {
2174e1069839SBorislav Petkov [C(OP_READ)] = {
2175e1069839SBorislav Petkov [C(RESULT_ACCESS)] = KNL_L2_READ | KNL_L2_ACCESS,
2176e1069839SBorislav Petkov [C(RESULT_MISS)] = 0,
2177e1069839SBorislav Petkov },
2178e1069839SBorislav Petkov [C(OP_WRITE)] = {
2179e1069839SBorislav Petkov [C(RESULT_ACCESS)] = KNL_L2_WRITE | KNL_L2_ACCESS,
2180e1069839SBorislav Petkov [C(RESULT_MISS)] = KNL_L2_WRITE | KNL_L2_MISS,
2181e1069839SBorislav Petkov },
2182e1069839SBorislav Petkov [C(OP_PREFETCH)] = {
2183e1069839SBorislav Petkov [C(RESULT_ACCESS)] = KNL_L2_PREFETCH | KNL_L2_ACCESS,
2184e1069839SBorislav Petkov [C(RESULT_MISS)] = KNL_L2_PREFETCH | KNL_L2_MISS,
2185e1069839SBorislav Petkov },
2186e1069839SBorislav Petkov },
2187e1069839SBorislav Petkov };
2188e1069839SBorislav Petkov
2189e1069839SBorislav Petkov /*
2190c3d266c8SKan Liang * Used from PMIs where the LBRs are already disabled.
2191c3d266c8SKan Liang *
2192c3d266c8SKan Liang * This function could be called consecutively. It is required to remain in
2193c3d266c8SKan Liang * disabled state if called consecutively.
2194c3d266c8SKan Liang *
2195c3d266c8SKan Liang * During consecutive calls, the same disable value will be written to related
2196cecf6235SAlexander Shishkin * registers, so the PMU state remains unchanged.
2197cecf6235SAlexander Shishkin *
2198cecf6235SAlexander Shishkin * intel_bts events don't coexist with intel PMU's BTS events because of
2199cecf6235SAlexander Shishkin * x86_add_exclusive(x86_lbr_exclusive_lbr); there's no need to keep them
2200cecf6235SAlexander Shishkin * disabled around intel PMU's event batching etc, only inside the PMI handler.
22016c1c07b3SKan Liang *
22026c1c07b3SKan Liang * Avoid PEBS_ENABLE MSR access in PMIs.
22036c1c07b3SKan Liang * The GLOBAL_CTRL has been disabled. All the counters do not count anymore.
22046c1c07b3SKan Liang * It doesn't matter if the PEBS is enabled or not.
22056c1c07b3SKan Liang * Usually, the PEBS status are not changed in PMIs. It's unnecessary to
22066c1c07b3SKan Liang * access PEBS_ENABLE MSR in disable_all()/enable_all().
22076c1c07b3SKan Liang * However, there are some cases which may change PEBS status, e.g. PMI
22086c1c07b3SKan Liang * throttle. The PEBS_ENABLE should be updated where the status changes.
2209e1069839SBorislav Petkov */
__intel_pmu_disable_all(bool bts)2210c22ac2a3SSong Liu static __always_inline void __intel_pmu_disable_all(bool bts)
2211e1069839SBorislav Petkov {
2212e1069839SBorislav Petkov struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
2213e1069839SBorislav Petkov
2214e1069839SBorislav Petkov wrmsrl(MSR_CORE_PERF_GLOBAL_CTRL, 0);
2215e1069839SBorislav Petkov
2216c22ac2a3SSong Liu if (bts && test_bit(INTEL_PMC_IDX_FIXED_BTS, cpuc->active_mask))
2217e1069839SBorislav Petkov intel_pmu_disable_bts();
2218e1069839SBorislav Petkov }
2219e1069839SBorislav Petkov
intel_pmu_disable_all(void)2220c22ac2a3SSong Liu static __always_inline void intel_pmu_disable_all(void)
2221e1069839SBorislav Petkov {
2222c22ac2a3SSong Liu __intel_pmu_disable_all(true);
22236c1c07b3SKan Liang intel_pmu_pebs_disable_all();
2224e1069839SBorislav Petkov intel_pmu_lbr_disable_all();
2225e1069839SBorislav Petkov }
2226e1069839SBorislav Petkov
__intel_pmu_enable_all(int added,bool pmi)2227e1069839SBorislav Petkov static void __intel_pmu_enable_all(int added, bool pmi)
2228e1069839SBorislav Petkov {
2229e1069839SBorislav Petkov struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
2230fc4b8fcaSKan Liang u64 intel_ctrl = hybrid(cpuc->pmu, intel_ctrl);
2231e1069839SBorislav Petkov
2232e1069839SBorislav Petkov intel_pmu_lbr_enable_all(pmi);
2233fae9ebdeSKan Liang
2234fae9ebdeSKan Liang if (cpuc->fixed_ctrl_val != cpuc->active_fixed_ctrl_val) {
2235fae9ebdeSKan Liang wrmsrl(MSR_ARCH_PERFMON_FIXED_CTR_CTRL, cpuc->fixed_ctrl_val);
2236fae9ebdeSKan Liang cpuc->active_fixed_ctrl_val = cpuc->fixed_ctrl_val;
2237fae9ebdeSKan Liang }
2238fae9ebdeSKan Liang
2239e1069839SBorislav Petkov wrmsrl(MSR_CORE_PERF_GLOBAL_CTRL,
2240fc4b8fcaSKan Liang intel_ctrl & ~cpuc->intel_ctrl_guest_mask);
2241e1069839SBorislav Petkov
2242e1069839SBorislav Petkov if (test_bit(INTEL_PMC_IDX_FIXED_BTS, cpuc->active_mask)) {
2243e1069839SBorislav Petkov struct perf_event *event =
2244e1069839SBorislav Petkov cpuc->events[INTEL_PMC_IDX_FIXED_BTS];
2245e1069839SBorislav Petkov
2246e1069839SBorislav Petkov if (WARN_ON_ONCE(!event))
2247e1069839SBorislav Petkov return;
2248e1069839SBorislav Petkov
2249e1069839SBorislav Petkov intel_pmu_enable_bts(event->hw.config);
2250cecf6235SAlexander Shishkin }
2251e1069839SBorislav Petkov }
2252e1069839SBorislav Petkov
intel_pmu_enable_all(int added)2253e1069839SBorislav Petkov static void intel_pmu_enable_all(int added)
2254e1069839SBorislav Petkov {
22556c1c07b3SKan Liang intel_pmu_pebs_enable_all();
2256e1069839SBorislav Petkov __intel_pmu_enable_all(added, false);
2257e1069839SBorislav Petkov }
2258e1069839SBorislav Petkov
2259c22ac2a3SSong Liu static noinline int
__intel_pmu_snapshot_branch_stack(struct perf_branch_entry * entries,unsigned int cnt,unsigned long flags)2260c22ac2a3SSong Liu __intel_pmu_snapshot_branch_stack(struct perf_branch_entry *entries,
2261c22ac2a3SSong Liu unsigned int cnt, unsigned long flags)
2262c22ac2a3SSong Liu {
2263c22ac2a3SSong Liu struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
2264c22ac2a3SSong Liu
2265c22ac2a3SSong Liu intel_pmu_lbr_read();
2266c22ac2a3SSong Liu cnt = min_t(unsigned int, cnt, x86_pmu.lbr_nr);
2267c22ac2a3SSong Liu
2268c22ac2a3SSong Liu memcpy(entries, cpuc->lbr_entries, sizeof(struct perf_branch_entry) * cnt);
2269c22ac2a3SSong Liu intel_pmu_enable_all(0);
2270c22ac2a3SSong Liu local_irq_restore(flags);
2271c22ac2a3SSong Liu return cnt;
2272c22ac2a3SSong Liu }
2273c22ac2a3SSong Liu
2274c22ac2a3SSong Liu static int
intel_pmu_snapshot_branch_stack(struct perf_branch_entry * entries,unsigned int cnt)2275c22ac2a3SSong Liu intel_pmu_snapshot_branch_stack(struct perf_branch_entry *entries, unsigned int cnt)
2276c22ac2a3SSong Liu {
2277c22ac2a3SSong Liu unsigned long flags;
2278c22ac2a3SSong Liu
2279c22ac2a3SSong Liu /* must not have branches... */
2280c22ac2a3SSong Liu local_irq_save(flags);
2281c22ac2a3SSong Liu __intel_pmu_disable_all(false); /* we don't care about BTS */
2282c22ac2a3SSong Liu __intel_pmu_lbr_disable();
2283c22ac2a3SSong Liu /* ... until here */
2284c22ac2a3SSong Liu return __intel_pmu_snapshot_branch_stack(entries, cnt, flags);
2285c22ac2a3SSong Liu }
2286c22ac2a3SSong Liu
2287c22ac2a3SSong Liu static int
intel_pmu_snapshot_arch_branch_stack(struct perf_branch_entry * entries,unsigned int cnt)2288c22ac2a3SSong Liu intel_pmu_snapshot_arch_branch_stack(struct perf_branch_entry *entries, unsigned int cnt)
2289c22ac2a3SSong Liu {
2290c22ac2a3SSong Liu unsigned long flags;
2291c22ac2a3SSong Liu
2292c22ac2a3SSong Liu /* must not have branches... */
2293c22ac2a3SSong Liu local_irq_save(flags);
2294c22ac2a3SSong Liu __intel_pmu_disable_all(false); /* we don't care about BTS */
2295c22ac2a3SSong Liu __intel_pmu_arch_lbr_disable();
2296c22ac2a3SSong Liu /* ... until here */
2297c22ac2a3SSong Liu return __intel_pmu_snapshot_branch_stack(entries, cnt, flags);
2298c22ac2a3SSong Liu }
2299c22ac2a3SSong Liu
2300e1069839SBorislav Petkov /*
2301e1069839SBorislav Petkov * Workaround for:
2302e1069839SBorislav Petkov * Intel Errata AAK100 (model 26)
2303e1069839SBorislav Petkov * Intel Errata AAP53 (model 30)
2304e1069839SBorislav Petkov * Intel Errata BD53 (model 44)
2305e1069839SBorislav Petkov *
2306e1069839SBorislav Petkov * The official story:
2307e1069839SBorislav Petkov * These chips need to be 'reset' when adding counters by programming the
2308e1069839SBorislav Petkov * magic three (non-counting) events 0x4300B5, 0x4300D2, and 0x4300B1 either
2309e1069839SBorislav Petkov * in sequence on the same PMC or on different PMCs.
2310e1069839SBorislav Petkov *
2311d9f6e12fSIngo Molnar * In practice it appears some of these events do in fact count, and
2312a97673a1SIngo Molnar * we need to program all 4 events.
2313e1069839SBorislav Petkov */
intel_pmu_nhm_workaround(void)2314e1069839SBorislav Petkov static void intel_pmu_nhm_workaround(void)
2315e1069839SBorislav Petkov {
2316e1069839SBorislav Petkov struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
2317e1069839SBorislav Petkov static const unsigned long nhm_magic[4] = {
2318e1069839SBorislav Petkov 0x4300B5,
2319e1069839SBorislav Petkov 0x4300D2,
2320e1069839SBorislav Petkov 0x4300B1,
2321e1069839SBorislav Petkov 0x4300B1
2322e1069839SBorislav Petkov };
2323e1069839SBorislav Petkov struct perf_event *event;
2324e1069839SBorislav Petkov int i;
2325e1069839SBorislav Petkov
2326e1069839SBorislav Petkov /*
2327e1069839SBorislav Petkov * The Errata requires below steps:
2328e1069839SBorislav Petkov * 1) Clear MSR_IA32_PEBS_ENABLE and MSR_CORE_PERF_GLOBAL_CTRL;
2329e1069839SBorislav Petkov * 2) Configure 4 PERFEVTSELx with the magic events and clear
2330e1069839SBorislav Petkov * the corresponding PMCx;
2331e1069839SBorislav Petkov * 3) set bit0~bit3 of MSR_CORE_PERF_GLOBAL_CTRL;
2332e1069839SBorislav Petkov * 4) Clear MSR_CORE_PERF_GLOBAL_CTRL;
2333e1069839SBorislav Petkov * 5) Clear 4 pairs of ERFEVTSELx and PMCx;
2334e1069839SBorislav Petkov */
2335e1069839SBorislav Petkov
2336e1069839SBorislav Petkov /*
2337e1069839SBorislav Petkov * The real steps we choose are a little different from above.
2338e1069839SBorislav Petkov * A) To reduce MSR operations, we don't run step 1) as they
2339e1069839SBorislav Petkov * are already cleared before this function is called;
2340e1069839SBorislav Petkov * B) Call x86_perf_event_update to save PMCx before configuring
2341e1069839SBorislav Petkov * PERFEVTSELx with magic number;
2342e1069839SBorislav Petkov * C) With step 5), we do clear only when the PERFEVTSELx is
2343e1069839SBorislav Petkov * not used currently.
2344e1069839SBorislav Petkov * D) Call x86_perf_event_set_period to restore PMCx;
2345e1069839SBorislav Petkov */
2346e1069839SBorislav Petkov
2347e1069839SBorislav Petkov /* We always operate 4 pairs of PERF Counters */
2348e1069839SBorislav Petkov for (i = 0; i < 4; i++) {
2349e1069839SBorislav Petkov event = cpuc->events[i];
2350e1069839SBorislav Petkov if (event)
2351e577bb17SPeter Zijlstra static_call(x86_pmu_update)(event);
2352e1069839SBorislav Petkov }
2353e1069839SBorislav Petkov
2354e1069839SBorislav Petkov for (i = 0; i < 4; i++) {
2355e1069839SBorislav Petkov wrmsrl(MSR_ARCH_PERFMON_EVENTSEL0 + i, nhm_magic[i]);
2356e1069839SBorislav Petkov wrmsrl(MSR_ARCH_PERFMON_PERFCTR0 + i, 0x0);
2357e1069839SBorislav Petkov }
2358e1069839SBorislav Petkov
2359e1069839SBorislav Petkov wrmsrl(MSR_CORE_PERF_GLOBAL_CTRL, 0xf);
2360e1069839SBorislav Petkov wrmsrl(MSR_CORE_PERF_GLOBAL_CTRL, 0x0);
2361e1069839SBorislav Petkov
2362e1069839SBorislav Petkov for (i = 0; i < 4; i++) {
2363e1069839SBorislav Petkov event = cpuc->events[i];
2364e1069839SBorislav Petkov
2365e1069839SBorislav Petkov if (event) {
2366e577bb17SPeter Zijlstra static_call(x86_pmu_set_period)(event);
2367e1069839SBorislav Petkov __x86_pmu_enable_event(&event->hw,
2368e1069839SBorislav Petkov ARCH_PERFMON_EVENTSEL_ENABLE);
2369e1069839SBorislav Petkov } else
2370e1069839SBorislav Petkov wrmsrl(MSR_ARCH_PERFMON_EVENTSEL0 + i, 0x0);
2371e1069839SBorislav Petkov }
2372e1069839SBorislav Petkov }
2373e1069839SBorislav Petkov
intel_pmu_nhm_enable_all(int added)2374e1069839SBorislav Petkov static void intel_pmu_nhm_enable_all(int added)
2375e1069839SBorislav Petkov {
2376e1069839SBorislav Petkov if (added)
2377e1069839SBorislav Petkov intel_pmu_nhm_workaround();
2378e1069839SBorislav Petkov intel_pmu_enable_all(added);
2379e1069839SBorislav Petkov }
2380e1069839SBorislav Petkov
intel_set_tfa(struct cpu_hw_events * cpuc,bool on)2381400816f6SPeter Zijlstra (Intel) static void intel_set_tfa(struct cpu_hw_events *cpuc, bool on)
2382400816f6SPeter Zijlstra (Intel) {
2383400816f6SPeter Zijlstra (Intel) u64 val = on ? MSR_TFA_RTM_FORCE_ABORT : 0;
2384400816f6SPeter Zijlstra (Intel)
2385400816f6SPeter Zijlstra (Intel) if (cpuc->tfa_shadow != val) {
2386400816f6SPeter Zijlstra (Intel) cpuc->tfa_shadow = val;
2387400816f6SPeter Zijlstra (Intel) wrmsrl(MSR_TSX_FORCE_ABORT, val);
2388400816f6SPeter Zijlstra (Intel) }
2389400816f6SPeter Zijlstra (Intel) }
2390400816f6SPeter Zijlstra (Intel)
intel_tfa_commit_scheduling(struct cpu_hw_events * cpuc,int idx,int cntr)2391400816f6SPeter Zijlstra (Intel) static void intel_tfa_commit_scheduling(struct cpu_hw_events *cpuc, int idx, int cntr)
2392400816f6SPeter Zijlstra (Intel) {
2393400816f6SPeter Zijlstra (Intel) /*
2394400816f6SPeter Zijlstra (Intel) * We're going to use PMC3, make sure TFA is set before we touch it.
2395400816f6SPeter Zijlstra (Intel) */
23961a81542aSPeter Zijlstra if (cntr == 3)
2397400816f6SPeter Zijlstra (Intel) intel_set_tfa(cpuc, true);
2398400816f6SPeter Zijlstra (Intel) }
2399400816f6SPeter Zijlstra (Intel)
intel_tfa_pmu_enable_all(int added)2400400816f6SPeter Zijlstra (Intel) static void intel_tfa_pmu_enable_all(int added)
2401400816f6SPeter Zijlstra (Intel) {
2402400816f6SPeter Zijlstra (Intel) struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
2403400816f6SPeter Zijlstra (Intel)
2404400816f6SPeter Zijlstra (Intel) /*
2405400816f6SPeter Zijlstra (Intel) * If we find PMC3 is no longer used when we enable the PMU, we can
2406400816f6SPeter Zijlstra (Intel) * clear TFA.
2407400816f6SPeter Zijlstra (Intel) */
2408400816f6SPeter Zijlstra (Intel) if (!test_bit(3, cpuc->active_mask))
2409400816f6SPeter Zijlstra (Intel) intel_set_tfa(cpuc, false);
2410400816f6SPeter Zijlstra (Intel)
2411400816f6SPeter Zijlstra (Intel) intel_pmu_enable_all(added);
2412400816f6SPeter Zijlstra (Intel) }
2413400816f6SPeter Zijlstra (Intel)
intel_pmu_get_status(void)2414e1069839SBorislav Petkov static inline u64 intel_pmu_get_status(void)
2415e1069839SBorislav Petkov {
2416e1069839SBorislav Petkov u64 status;
2417e1069839SBorislav Petkov
2418e1069839SBorislav Petkov rdmsrl(MSR_CORE_PERF_GLOBAL_STATUS, status);
2419e1069839SBorislav Petkov
2420e1069839SBorislav Petkov return status;
2421e1069839SBorislav Petkov }
2422e1069839SBorislav Petkov
intel_pmu_ack_status(u64 ack)2423e1069839SBorislav Petkov static inline void intel_pmu_ack_status(u64 ack)
2424e1069839SBorislav Petkov {
2425e1069839SBorislav Petkov wrmsrl(MSR_CORE_PERF_GLOBAL_OVF_CTRL, ack);
2426e1069839SBorislav Petkov }
2427e1069839SBorislav Petkov
event_is_checkpointed(struct perf_event * event)2428027440b5SLike Xu static inline bool event_is_checkpointed(struct perf_event *event)
2429e1069839SBorislav Petkov {
2430027440b5SLike Xu return unlikely(event->hw.config & HSW_IN_TX_CHECKPOINTED) != 0;
2431027440b5SLike Xu }
2432027440b5SLike Xu
intel_set_masks(struct perf_event * event,int idx)2433027440b5SLike Xu static inline void intel_set_masks(struct perf_event *event, int idx)
2434027440b5SLike Xu {
2435027440b5SLike Xu struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
2436027440b5SLike Xu
2437027440b5SLike Xu if (event->attr.exclude_host)
2438027440b5SLike Xu __set_bit(idx, (unsigned long *)&cpuc->intel_ctrl_guest_mask);
2439027440b5SLike Xu if (event->attr.exclude_guest)
2440027440b5SLike Xu __set_bit(idx, (unsigned long *)&cpuc->intel_ctrl_host_mask);
2441027440b5SLike Xu if (event_is_checkpointed(event))
2442027440b5SLike Xu __set_bit(idx, (unsigned long *)&cpuc->intel_cp_status);
2443027440b5SLike Xu }
2444027440b5SLike Xu
intel_clear_masks(struct perf_event * event,int idx)2445027440b5SLike Xu static inline void intel_clear_masks(struct perf_event *event, int idx)
2446027440b5SLike Xu {
2447027440b5SLike Xu struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
2448027440b5SLike Xu
2449027440b5SLike Xu __clear_bit(idx, (unsigned long *)&cpuc->intel_ctrl_guest_mask);
2450027440b5SLike Xu __clear_bit(idx, (unsigned long *)&cpuc->intel_ctrl_host_mask);
2451027440b5SLike Xu __clear_bit(idx, (unsigned long *)&cpuc->intel_cp_status);
2452027440b5SLike Xu }
2453027440b5SLike Xu
intel_pmu_disable_fixed(struct perf_event * event)2454027440b5SLike Xu static void intel_pmu_disable_fixed(struct perf_event *event)
2455027440b5SLike Xu {
2456fae9ebdeSKan Liang struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
2457027440b5SLike Xu struct hw_perf_event *hwc = &event->hw;
24587b2c05a1SKan Liang int idx = hwc->idx;
2459fae9ebdeSKan Liang u64 mask;
2460e1069839SBorislav Petkov
24617b2c05a1SKan Liang if (is_topdown_idx(idx)) {
24627b2c05a1SKan Liang struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
2463e1069839SBorislav Petkov
24647b2c05a1SKan Liang /*
24657b2c05a1SKan Liang * When there are other active TopDown events,
24667b2c05a1SKan Liang * don't disable the fixed counter 3.
24677b2c05a1SKan Liang */
24687b2c05a1SKan Liang if (*(u64 *)cpuc->active_mask & INTEL_PMC_OTHER_TOPDOWN_BITS(idx))
24697b2c05a1SKan Liang return;
24707b2c05a1SKan Liang idx = INTEL_PMC_IDX_FIXED_SLOTS;
24717b2c05a1SKan Liang }
24727b2c05a1SKan Liang
24737b2c05a1SKan Liang intel_clear_masks(event, idx);
24747b2c05a1SKan Liang
247510d95a31SDapeng Mi mask = intel_fixed_bits_by_idx(idx - INTEL_PMC_IDX_FIXED, INTEL_FIXED_BITS_MASK);
2476fae9ebdeSKan Liang cpuc->fixed_ctrl_val &= ~mask;
2477e1069839SBorislav Petkov }
2478e1069839SBorislav Petkov
intel_pmu_disable_event(struct perf_event * event)2479e1069839SBorislav Petkov static void intel_pmu_disable_event(struct perf_event *event)
2480e1069839SBorislav Petkov {
2481e1069839SBorislav Petkov struct hw_perf_event *hwc = &event->hw;
2482027440b5SLike Xu int idx = hwc->idx;
2483e1069839SBorislav Petkov
248458da7dbeSKan Liang switch (idx) {
248558da7dbeSKan Liang case 0 ... INTEL_PMC_IDX_FIXED - 1:
2486027440b5SLike Xu intel_clear_masks(event, idx);
2487027440b5SLike Xu x86_pmu_disable_event(event);
248858da7dbeSKan Liang break;
248958da7dbeSKan Liang case INTEL_PMC_IDX_FIXED ... INTEL_PMC_IDX_FIXED_BTS - 1:
24907b2c05a1SKan Liang case INTEL_PMC_IDX_METRIC_BASE ... INTEL_PMC_IDX_METRIC_END:
2491027440b5SLike Xu intel_pmu_disable_fixed(event);
249258da7dbeSKan Liang break;
249358da7dbeSKan Liang case INTEL_PMC_IDX_FIXED_BTS:
2494e1069839SBorislav Petkov intel_pmu_disable_bts();
2495e1069839SBorislav Petkov intel_pmu_drain_bts_buffer();
249658da7dbeSKan Liang return;
249758da7dbeSKan Liang case INTEL_PMC_IDX_FIXED_VLBR:
2498e1ad1ac2SLike Xu intel_clear_masks(event, idx);
249958da7dbeSKan Liang break;
250058da7dbeSKan Liang default:
250158da7dbeSKan Liang intel_clear_masks(event, idx);
250258da7dbeSKan Liang pr_warn("Failed to disable the event with invalid index %d\n",
250358da7dbeSKan Liang idx);
250458da7dbeSKan Liang return;
250558da7dbeSKan Liang }
2506e1069839SBorislav Petkov
25076f55967aSJiri Olsa /*
25086f55967aSJiri Olsa * Needs to be called after x86_pmu_disable_event,
25096f55967aSJiri Olsa * so we don't trigger the event without PEBS bit set.
25106f55967aSJiri Olsa */
25116f55967aSJiri Olsa if (unlikely(event->attr.precise_ip))
25126f55967aSJiri Olsa intel_pmu_pebs_disable(event);
2513e1069839SBorislav Petkov }
2514e1069839SBorislav Petkov
intel_pmu_assign_event(struct perf_event * event,int idx)25158b8ff8ccSAdrian Hunter static void intel_pmu_assign_event(struct perf_event *event, int idx)
25168b8ff8ccSAdrian Hunter {
25178b8ff8ccSAdrian Hunter if (is_pebs_pt(event))
25188b8ff8ccSAdrian Hunter perf_report_aux_output_id(event, idx);
25198b8ff8ccSAdrian Hunter }
25208b8ff8ccSAdrian Hunter
intel_pmu_del_event(struct perf_event * event)252168f7082fSPeter Zijlstra static void intel_pmu_del_event(struct perf_event *event)
252268f7082fSPeter Zijlstra {
252368f7082fSPeter Zijlstra if (needs_branch_stack(event))
252468f7082fSPeter Zijlstra intel_pmu_lbr_del(event);
252568f7082fSPeter Zijlstra if (event->attr.precise_ip)
252668f7082fSPeter Zijlstra intel_pmu_pebs_del(event);
252768f7082fSPeter Zijlstra }
252868f7082fSPeter Zijlstra
icl_set_topdown_event_period(struct perf_event * event)252959a854e2SKan Liang static int icl_set_topdown_event_period(struct perf_event *event)
253059a854e2SKan Liang {
253159a854e2SKan Liang struct hw_perf_event *hwc = &event->hw;
253259a854e2SKan Liang s64 left = local64_read(&hwc->period_left);
253359a854e2SKan Liang
253459a854e2SKan Liang /*
253559a854e2SKan Liang * The values in PERF_METRICS MSR are derived from fixed counter 3.
253659a854e2SKan Liang * Software should start both registers, PERF_METRICS and fixed
253759a854e2SKan Liang * counter 3, from zero.
253859a854e2SKan Liang * Clear PERF_METRICS and Fixed counter 3 in initialization.
253959a854e2SKan Liang * After that, both MSRs will be cleared for each read.
254059a854e2SKan Liang * Don't need to clear them again.
254159a854e2SKan Liang */
254259a854e2SKan Liang if (left == x86_pmu.max_period) {
254359a854e2SKan Liang wrmsrl(MSR_CORE_PERF_FIXED_CTR3, 0);
254459a854e2SKan Liang wrmsrl(MSR_PERF_METRICS, 0);
25452cb5383bSKan Liang hwc->saved_slots = 0;
25462cb5383bSKan Liang hwc->saved_metric = 0;
25472cb5383bSKan Liang }
25482cb5383bSKan Liang
25492cb5383bSKan Liang if ((hwc->saved_slots) && is_slots_event(event)) {
25502cb5383bSKan Liang wrmsrl(MSR_CORE_PERF_FIXED_CTR3, hwc->saved_slots);
25512cb5383bSKan Liang wrmsrl(MSR_PERF_METRICS, hwc->saved_metric);
255259a854e2SKan Liang }
255359a854e2SKan Liang
255459a854e2SKan Liang perf_event_update_userpage(event);
255559a854e2SKan Liang
255659a854e2SKan Liang return 0;
255759a854e2SKan Liang }
255859a854e2SKan Liang
adl_set_topdown_event_period(struct perf_event * event)2559f83d2f91SKan Liang static int adl_set_topdown_event_period(struct perf_event *event)
2560f83d2f91SKan Liang {
2561f83d2f91SKan Liang struct x86_hybrid_pmu *pmu = hybrid_pmu(event->pmu);
2562f83d2f91SKan Liang
2563f83d2f91SKan Liang if (pmu->cpu_type != hybrid_big)
2564f83d2f91SKan Liang return 0;
2565f83d2f91SKan Liang
2566f83d2f91SKan Liang return icl_set_topdown_event_period(event);
2567f83d2f91SKan Liang }
2568f83d2f91SKan Liang
256923685167SPeter Zijlstra DEFINE_STATIC_CALL(intel_pmu_set_topdown_event_period, x86_perf_event_set_period);
257023685167SPeter Zijlstra
icl_get_metrics_event_value(u64 metric,u64 slots,int idx)257159a854e2SKan Liang static inline u64 icl_get_metrics_event_value(u64 metric, u64 slots, int idx)
257259a854e2SKan Liang {
257359a854e2SKan Liang u32 val;
257459a854e2SKan Liang
257559a854e2SKan Liang /*
257659a854e2SKan Liang * The metric is reported as an 8bit integer fraction
2577d9f6e12fSIngo Molnar * summing up to 0xff.
257859a854e2SKan Liang * slots-in-metric = (Metric / 0xff) * slots
257959a854e2SKan Liang */
258059a854e2SKan Liang val = (metric >> ((idx - INTEL_PMC_IDX_METRIC_BASE) * 8)) & 0xff;
258159a854e2SKan Liang return mul_u64_u32_div(slots, val, 0xff);
258259a854e2SKan Liang }
258359a854e2SKan Liang
icl_get_topdown_value(struct perf_event * event,u64 slots,u64 metrics)25842cb5383bSKan Liang static u64 icl_get_topdown_value(struct perf_event *event,
258559a854e2SKan Liang u64 slots, u64 metrics)
258659a854e2SKan Liang {
258759a854e2SKan Liang int idx = event->hw.idx;
258859a854e2SKan Liang u64 delta;
258959a854e2SKan Liang
259059a854e2SKan Liang if (is_metric_idx(idx))
259159a854e2SKan Liang delta = icl_get_metrics_event_value(metrics, slots, idx);
259259a854e2SKan Liang else
259359a854e2SKan Liang delta = slots;
259459a854e2SKan Liang
25952cb5383bSKan Liang return delta;
25962cb5383bSKan Liang }
25972cb5383bSKan Liang
__icl_update_topdown_event(struct perf_event * event,u64 slots,u64 metrics,u64 last_slots,u64 last_metrics)25982cb5383bSKan Liang static void __icl_update_topdown_event(struct perf_event *event,
25992cb5383bSKan Liang u64 slots, u64 metrics,
26002cb5383bSKan Liang u64 last_slots, u64 last_metrics)
26012cb5383bSKan Liang {
26022cb5383bSKan Liang u64 delta, last = 0;
26032cb5383bSKan Liang
26042cb5383bSKan Liang delta = icl_get_topdown_value(event, slots, metrics);
26052cb5383bSKan Liang if (last_slots)
26062cb5383bSKan Liang last = icl_get_topdown_value(event, last_slots, last_metrics);
26072cb5383bSKan Liang
26082cb5383bSKan Liang /*
26092cb5383bSKan Liang * The 8bit integer fraction of metric may be not accurate,
26102cb5383bSKan Liang * especially when the changes is very small.
26112cb5383bSKan Liang * For example, if only a few bad_spec happens, the fraction
26122cb5383bSKan Liang * may be reduced from 1 to 0. If so, the bad_spec event value
26132cb5383bSKan Liang * will be 0 which is definitely less than the last value.
26142cb5383bSKan Liang * Avoid update event->count for this case.
26152cb5383bSKan Liang */
26162cb5383bSKan Liang if (delta > last) {
26172cb5383bSKan Liang delta -= last;
261859a854e2SKan Liang local64_add(delta, &event->count);
261959a854e2SKan Liang }
26202cb5383bSKan Liang }
26212cb5383bSKan Liang
update_saved_topdown_regs(struct perf_event * event,u64 slots,u64 metrics,int metric_end)2622628d923aSKan Liang static void update_saved_topdown_regs(struct perf_event *event, u64 slots,
2623628d923aSKan Liang u64 metrics, int metric_end)
26242cb5383bSKan Liang {
26252cb5383bSKan Liang struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
26262cb5383bSKan Liang struct perf_event *other;
26272cb5383bSKan Liang int idx;
26282cb5383bSKan Liang
26292cb5383bSKan Liang event->hw.saved_slots = slots;
26302cb5383bSKan Liang event->hw.saved_metric = metrics;
26312cb5383bSKan Liang
2632628d923aSKan Liang for_each_set_bit(idx, cpuc->active_mask, metric_end + 1) {
26332cb5383bSKan Liang if (!is_topdown_idx(idx))
26342cb5383bSKan Liang continue;
26352cb5383bSKan Liang other = cpuc->events[idx];
26362cb5383bSKan Liang other->hw.saved_slots = slots;
26372cb5383bSKan Liang other->hw.saved_metric = metrics;
26382cb5383bSKan Liang }
26392cb5383bSKan Liang }
264059a854e2SKan Liang
264159a854e2SKan Liang /*
264259a854e2SKan Liang * Update all active Topdown events.
264359a854e2SKan Liang *
264459a854e2SKan Liang * The PERF_METRICS and Fixed counter 3 are read separately. The values may be
264559a854e2SKan Liang * modify by a NMI. PMU has to be disabled before calling this function.
264659a854e2SKan Liang */
2647628d923aSKan Liang
intel_update_topdown_event(struct perf_event * event,int metric_end)2648628d923aSKan Liang static u64 intel_update_topdown_event(struct perf_event *event, int metric_end)
264959a854e2SKan Liang {
265059a854e2SKan Liang struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
265159a854e2SKan Liang struct perf_event *other;
265259a854e2SKan Liang u64 slots, metrics;
26532cb5383bSKan Liang bool reset = true;
265459a854e2SKan Liang int idx;
265559a854e2SKan Liang
265659a854e2SKan Liang /* read Fixed counter 3 */
265759a854e2SKan Liang rdpmcl((3 | INTEL_PMC_FIXED_RDPMC_BASE), slots);
265859a854e2SKan Liang if (!slots)
265959a854e2SKan Liang return 0;
266059a854e2SKan Liang
266159a854e2SKan Liang /* read PERF_METRICS */
266259a854e2SKan Liang rdpmcl(INTEL_PMC_FIXED_RDPMC_METRICS, metrics);
266359a854e2SKan Liang
2664628d923aSKan Liang for_each_set_bit(idx, cpuc->active_mask, metric_end + 1) {
266559a854e2SKan Liang if (!is_topdown_idx(idx))
266659a854e2SKan Liang continue;
266759a854e2SKan Liang other = cpuc->events[idx];
26682cb5383bSKan Liang __icl_update_topdown_event(other, slots, metrics,
26692cb5383bSKan Liang event ? event->hw.saved_slots : 0,
26702cb5383bSKan Liang event ? event->hw.saved_metric : 0);
267159a854e2SKan Liang }
267259a854e2SKan Liang
267359a854e2SKan Liang /*
267459a854e2SKan Liang * Check and update this event, which may have been cleared
267559a854e2SKan Liang * in active_mask e.g. x86_pmu_stop()
267659a854e2SKan Liang */
26772cb5383bSKan Liang if (event && !test_bit(event->hw.idx, cpuc->active_mask)) {
26782cb5383bSKan Liang __icl_update_topdown_event(event, slots, metrics,
26792cb5383bSKan Liang event->hw.saved_slots,
26802cb5383bSKan Liang event->hw.saved_metric);
268159a854e2SKan Liang
26822cb5383bSKan Liang /*
26832cb5383bSKan Liang * In x86_pmu_stop(), the event is cleared in active_mask first,
26842cb5383bSKan Liang * then drain the delta, which indicates context switch for
26852cb5383bSKan Liang * counting.
26862cb5383bSKan Liang * Save metric and slots for context switch.
26872cb5383bSKan Liang * Don't need to reset the PERF_METRICS and Fixed counter 3.
26882cb5383bSKan Liang * Because the values will be restored in next schedule in.
26892cb5383bSKan Liang */
2690628d923aSKan Liang update_saved_topdown_regs(event, slots, metrics, metric_end);
26912cb5383bSKan Liang reset = false;
26922cb5383bSKan Liang }
26932cb5383bSKan Liang
26942cb5383bSKan Liang if (reset) {
269559a854e2SKan Liang /* The fixed counter 3 has to be written before the PERF_METRICS. */
269659a854e2SKan Liang wrmsrl(MSR_CORE_PERF_FIXED_CTR3, 0);
269759a854e2SKan Liang wrmsrl(MSR_PERF_METRICS, 0);
26982cb5383bSKan Liang if (event)
2699628d923aSKan Liang update_saved_topdown_regs(event, 0, 0, metric_end);
27002cb5383bSKan Liang }
270159a854e2SKan Liang
270259a854e2SKan Liang return slots;
270359a854e2SKan Liang }
270459a854e2SKan Liang
icl_update_topdown_event(struct perf_event * event)2705628d923aSKan Liang static u64 icl_update_topdown_event(struct perf_event *event)
2706628d923aSKan Liang {
27071ab5f235SKan Liang return intel_update_topdown_event(event, INTEL_PMC_IDX_METRIC_BASE +
27081ab5f235SKan Liang x86_pmu.num_topdown_events - 1);
2709628d923aSKan Liang }
2710628d923aSKan Liang
adl_update_topdown_event(struct perf_event * event)2711f83d2f91SKan Liang static u64 adl_update_topdown_event(struct perf_event *event)
2712f83d2f91SKan Liang {
2713f83d2f91SKan Liang struct x86_hybrid_pmu *pmu = hybrid_pmu(event->pmu);
2714f83d2f91SKan Liang
2715f83d2f91SKan Liang if (pmu->cpu_type != hybrid_big)
2716f83d2f91SKan Liang return 0;
2717f83d2f91SKan Liang
2718f83d2f91SKan Liang return icl_update_topdown_event(event);
2719f83d2f91SKan Liang }
2720f83d2f91SKan Liang
27211acab2e0SPeter Zijlstra DEFINE_STATIC_CALL(intel_pmu_update_topdown_event, x86_perf_event_update);
2722f83d2f91SKan Liang
intel_pmu_read_topdown_event(struct perf_event * event)27237b2c05a1SKan Liang static void intel_pmu_read_topdown_event(struct perf_event *event)
27247b2c05a1SKan Liang {
27257b2c05a1SKan Liang struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
27267b2c05a1SKan Liang
27277b2c05a1SKan Liang /* Only need to call update_topdown_event() once for group read. */
27287b2c05a1SKan Liang if ((cpuc->txn_flags & PERF_PMU_TXN_READ) &&
27297b2c05a1SKan Liang !is_slots_event(event))
27307b2c05a1SKan Liang return;
27317b2c05a1SKan Liang
27327b2c05a1SKan Liang perf_pmu_disable(event->pmu);
27331acab2e0SPeter Zijlstra static_call(intel_pmu_update_topdown_event)(event);
27347b2c05a1SKan Liang perf_pmu_enable(event->pmu);
27357b2c05a1SKan Liang }
27367b2c05a1SKan Liang
intel_pmu_read_event(struct perf_event * event)2737ceb90d9eSKan Liang static void intel_pmu_read_event(struct perf_event *event)
2738ceb90d9eSKan Liang {
2739ceb90d9eSKan Liang if (event->hw.flags & PERF_X86_EVENT_AUTO_RELOAD)
2740ceb90d9eSKan Liang intel_pmu_auto_reload_read(event);
27411acab2e0SPeter Zijlstra else if (is_topdown_count(event))
27427b2c05a1SKan Liang intel_pmu_read_topdown_event(event);
2743ceb90d9eSKan Liang else
2744ceb90d9eSKan Liang x86_perf_event_update(event);
2745ceb90d9eSKan Liang }
2746ceb90d9eSKan Liang
intel_pmu_enable_fixed(struct perf_event * event)27474f08b625SKan Liang static void intel_pmu_enable_fixed(struct perf_event *event)
2748e1069839SBorislav Petkov {
2749fae9ebdeSKan Liang struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
27504f08b625SKan Liang struct hw_perf_event *hwc = &event->hw;
2751fae9ebdeSKan Liang u64 mask, bits = 0;
27527b2c05a1SKan Liang int idx = hwc->idx;
27537b2c05a1SKan Liang
27547b2c05a1SKan Liang if (is_topdown_idx(idx)) {
27557b2c05a1SKan Liang struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
27567b2c05a1SKan Liang /*
27577b2c05a1SKan Liang * When there are other active TopDown events,
27587b2c05a1SKan Liang * don't enable the fixed counter 3 again.
27597b2c05a1SKan Liang */
27607b2c05a1SKan Liang if (*(u64 *)cpuc->active_mask & INTEL_PMC_OTHER_TOPDOWN_BITS(idx))
27617b2c05a1SKan Liang return;
27627b2c05a1SKan Liang
27637b2c05a1SKan Liang idx = INTEL_PMC_IDX_FIXED_SLOTS;
27647b2c05a1SKan Liang }
27657b2c05a1SKan Liang
27667b2c05a1SKan Liang intel_set_masks(event, idx);
2767e1069839SBorislav Petkov
2768e1069839SBorislav Petkov /*
27694f08b625SKan Liang * Enable IRQ generation (0x8), if not PEBS,
2770e1069839SBorislav Petkov * and enable ring-3 counting (0x2) and ring-0 counting (0x1)
2771e1069839SBorislav Petkov * if requested:
2772e1069839SBorislav Petkov */
27734f08b625SKan Liang if (!event->attr.precise_ip)
277410d95a31SDapeng Mi bits |= INTEL_FIXED_0_ENABLE_PMI;
2775e1069839SBorislav Petkov if (hwc->config & ARCH_PERFMON_EVENTSEL_USR)
277610d95a31SDapeng Mi bits |= INTEL_FIXED_0_USER;
2777e1069839SBorislav Petkov if (hwc->config & ARCH_PERFMON_EVENTSEL_OS)
277810d95a31SDapeng Mi bits |= INTEL_FIXED_0_KERNEL;
2779e1069839SBorislav Petkov
2780e1069839SBorislav Petkov /*
2781e1069839SBorislav Petkov * ANY bit is supported in v3 and up
2782e1069839SBorislav Petkov */
2783e1069839SBorislav Petkov if (x86_pmu.version > 2 && hwc->config & ARCH_PERFMON_EVENTSEL_ANY)
278410d95a31SDapeng Mi bits |= INTEL_FIXED_0_ANYTHREAD;
2785e1069839SBorislav Petkov
27867b2c05a1SKan Liang idx -= INTEL_PMC_IDX_FIXED;
278710d95a31SDapeng Mi bits = intel_fixed_bits_by_idx(idx, bits);
278810d95a31SDapeng Mi mask = intel_fixed_bits_by_idx(idx, INTEL_FIXED_BITS_MASK);
2789e1069839SBorislav Petkov
2790c22497f5SKan Liang if (x86_pmu.intel_cap.pebs_baseline && event->attr.precise_ip) {
279110d95a31SDapeng Mi bits |= intel_fixed_bits_by_idx(idx, ICL_FIXED_0_ADAPTIVE);
279210d95a31SDapeng Mi mask |= intel_fixed_bits_by_idx(idx, ICL_FIXED_0_ADAPTIVE);
2793c22497f5SKan Liang }
2794c22497f5SKan Liang
2795fae9ebdeSKan Liang cpuc->fixed_ctrl_val &= ~mask;
2796fae9ebdeSKan Liang cpuc->fixed_ctrl_val |= bits;
2797e1069839SBorislav Petkov }
2798e1069839SBorislav Petkov
intel_pmu_enable_event(struct perf_event * event)2799e1069839SBorislav Petkov static void intel_pmu_enable_event(struct perf_event *event)
2800e1069839SBorislav Petkov {
2801e1069839SBorislav Petkov struct hw_perf_event *hwc = &event->hw;
2802027440b5SLike Xu int idx = hwc->idx;
2803e1069839SBorislav Petkov
2804e1069839SBorislav Petkov if (unlikely(event->attr.precise_ip))
2805e1069839SBorislav Petkov intel_pmu_pebs_enable(event);
2806e1069839SBorislav Petkov
280758da7dbeSKan Liang switch (idx) {
280858da7dbeSKan Liang case 0 ... INTEL_PMC_IDX_FIXED - 1:
2809027440b5SLike Xu intel_set_masks(event, idx);
2810e1069839SBorislav Petkov __x86_pmu_enable_event(hwc, ARCH_PERFMON_EVENTSEL_ENABLE);
281158da7dbeSKan Liang break;
281258da7dbeSKan Liang case INTEL_PMC_IDX_FIXED ... INTEL_PMC_IDX_FIXED_BTS - 1:
28137b2c05a1SKan Liang case INTEL_PMC_IDX_METRIC_BASE ... INTEL_PMC_IDX_METRIC_END:
2814027440b5SLike Xu intel_pmu_enable_fixed(event);
281558da7dbeSKan Liang break;
281658da7dbeSKan Liang case INTEL_PMC_IDX_FIXED_BTS:
2817027440b5SLike Xu if (!__this_cpu_read(cpu_hw_events.enabled))
2818027440b5SLike Xu return;
2819027440b5SLike Xu intel_pmu_enable_bts(hwc->config);
282058da7dbeSKan Liang break;
282158da7dbeSKan Liang case INTEL_PMC_IDX_FIXED_VLBR:
2822e1ad1ac2SLike Xu intel_set_masks(event, idx);
282358da7dbeSKan Liang break;
282458da7dbeSKan Liang default:
282558da7dbeSKan Liang pr_warn("Failed to enable the event with invalid index %d\n",
282658da7dbeSKan Liang idx);
282758da7dbeSKan Liang }
2828e1069839SBorislav Petkov }
2829e1069839SBorislav Petkov
intel_pmu_add_event(struct perf_event * event)283068f7082fSPeter Zijlstra static void intel_pmu_add_event(struct perf_event *event)
283168f7082fSPeter Zijlstra {
283268f7082fSPeter Zijlstra if (event->attr.precise_ip)
283368f7082fSPeter Zijlstra intel_pmu_pebs_add(event);
283468f7082fSPeter Zijlstra if (needs_branch_stack(event))
283568f7082fSPeter Zijlstra intel_pmu_lbr_add(event);
283668f7082fSPeter Zijlstra }
283768f7082fSPeter Zijlstra
2838e1069839SBorislav Petkov /*
2839e1069839SBorislav Petkov * Save and restart an expired event. Called by NMI contexts,
2840e1069839SBorislav Petkov * so it has to be careful about preempting normal event ops:
2841e1069839SBorislav Petkov */
intel_pmu_save_and_restart(struct perf_event * event)2842e1069839SBorislav Petkov int intel_pmu_save_and_restart(struct perf_event *event)
2843e1069839SBorislav Petkov {
2844e577bb17SPeter Zijlstra static_call(x86_pmu_update)(event);
2845e1069839SBorislav Petkov /*
2846e1069839SBorislav Petkov * For a checkpointed counter always reset back to 0. This
2847e1069839SBorislav Petkov * avoids a situation where the counter overflows, aborts the
2848e1069839SBorislav Petkov * transaction and is then set back to shortly before the
2849e1069839SBorislav Petkov * overflow, and overflows and aborts again.
2850e1069839SBorislav Petkov */
2851e1069839SBorislav Petkov if (unlikely(event_is_checkpointed(event))) {
2852e1069839SBorislav Petkov /* No race with NMIs because the counter should not be armed */
2853e1069839SBorislav Petkov wrmsrl(event->hw.event_base, 0);
2854e1069839SBorislav Petkov local64_set(&event->hw.prev_count, 0);
2855e1069839SBorislav Petkov }
2856e577bb17SPeter Zijlstra return static_call(x86_pmu_set_period)(event);
2857e577bb17SPeter Zijlstra }
2858e577bb17SPeter Zijlstra
intel_pmu_set_period(struct perf_event * event)2859e577bb17SPeter Zijlstra static int intel_pmu_set_period(struct perf_event *event)
2860e577bb17SPeter Zijlstra {
286123685167SPeter Zijlstra if (unlikely(is_topdown_count(event)))
286223685167SPeter Zijlstra return static_call(intel_pmu_set_topdown_event_period)(event);
2863e577bb17SPeter Zijlstra
2864e1069839SBorislav Petkov return x86_perf_event_set_period(event);
2865e1069839SBorislav Petkov }
2866e1069839SBorislav Petkov
intel_pmu_update(struct perf_event * event)2867e577bb17SPeter Zijlstra static u64 intel_pmu_update(struct perf_event *event)
2868e577bb17SPeter Zijlstra {
28691acab2e0SPeter Zijlstra if (unlikely(is_topdown_count(event)))
28701acab2e0SPeter Zijlstra return static_call(intel_pmu_update_topdown_event)(event);
2871e577bb17SPeter Zijlstra
2872e577bb17SPeter Zijlstra return x86_perf_event_update(event);
2873e577bb17SPeter Zijlstra }
2874e577bb17SPeter Zijlstra
intel_pmu_reset(void)2875e1069839SBorislav Petkov static void intel_pmu_reset(void)
2876e1069839SBorislav Petkov {
2877e1069839SBorislav Petkov struct debug_store *ds = __this_cpu_read(cpu_hw_events.ds);
2878fc4b8fcaSKan Liang struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
2879d4b294bfSKan Liang int num_counters_fixed = hybrid(cpuc->pmu, num_counters_fixed);
2880d4b294bfSKan Liang int num_counters = hybrid(cpuc->pmu, num_counters);
2881e1069839SBorislav Petkov unsigned long flags;
2882e1069839SBorislav Petkov int idx;
2883e1069839SBorislav Petkov
2884d4b294bfSKan Liang if (!num_counters)
2885e1069839SBorislav Petkov return;
2886e1069839SBorislav Petkov
2887e1069839SBorislav Petkov local_irq_save(flags);
2888e1069839SBorislav Petkov
2889e1069839SBorislav Petkov pr_info("clearing PMU state on CPU#%d\n", smp_processor_id());
2890e1069839SBorislav Petkov
2891d4b294bfSKan Liang for (idx = 0; idx < num_counters; idx++) {
2892e1069839SBorislav Petkov wrmsrl_safe(x86_pmu_config_addr(idx), 0ull);
2893e1069839SBorislav Petkov wrmsrl_safe(x86_pmu_event_addr(idx), 0ull);
2894e1069839SBorislav Petkov }
2895d4b294bfSKan Liang for (idx = 0; idx < num_counters_fixed; idx++) {
2896fc4b8fcaSKan Liang if (fixed_counter_disabled(idx, cpuc->pmu))
289732451614SKan Liang continue;
2898e1069839SBorislav Petkov wrmsrl_safe(MSR_ARCH_PERFMON_FIXED_CTR0 + idx, 0ull);
289932451614SKan Liang }
2900e1069839SBorislav Petkov
2901e1069839SBorislav Petkov if (ds)
2902e1069839SBorislav Petkov ds->bts_index = ds->bts_buffer_base;
2903e1069839SBorislav Petkov
2904e1069839SBorislav Petkov /* Ack all overflows and disable fixed counters */
2905e1069839SBorislav Petkov if (x86_pmu.version >= 2) {
2906e1069839SBorislav Petkov intel_pmu_ack_status(intel_pmu_get_status());
2907e1069839SBorislav Petkov wrmsrl(MSR_CORE_PERF_GLOBAL_CTRL, 0);
2908e1069839SBorislav Petkov }
2909e1069839SBorislav Petkov
2910e1069839SBorislav Petkov /* Reset LBRs and LBR freezing */
2911e1069839SBorislav Petkov if (x86_pmu.lbr_nr) {
2912e1069839SBorislav Petkov update_debugctlmsr(get_debugctlmsr() &
2913e1069839SBorislav Petkov ~(DEBUGCTLMSR_FREEZE_LBRS_ON_PMI|DEBUGCTLMSR_LBR));
2914e1069839SBorislav Petkov }
2915e1069839SBorislav Petkov
2916e1069839SBorislav Petkov local_irq_restore(flags);
2917e1069839SBorislav Petkov }
2918e1069839SBorislav Petkov
291969e575ddSLike Xu /*
292069e575ddSLike Xu * We may be running with guest PEBS events created by KVM, and the
292169e575ddSLike Xu * PEBS records are logged into the guest's DS and invisible to host.
292269e575ddSLike Xu *
292369e575ddSLike Xu * In the case of guest PEBS overflow, we only trigger a fake event
292469e575ddSLike Xu * to emulate the PEBS overflow PMI for guest PEBS counters in KVM.
292569e575ddSLike Xu * The guest will then vm-entry and check the guest DS area to read
292669e575ddSLike Xu * the guest PEBS records.
292769e575ddSLike Xu *
292869e575ddSLike Xu * The contents and other behavior of the guest event do not matter.
292969e575ddSLike Xu */
x86_pmu_handle_guest_pebs(struct pt_regs * regs,struct perf_sample_data * data)293069e575ddSLike Xu static void x86_pmu_handle_guest_pebs(struct pt_regs *regs,
293169e575ddSLike Xu struct perf_sample_data *data)
293269e575ddSLike Xu {
293369e575ddSLike Xu struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
293469e575ddSLike Xu u64 guest_pebs_idxs = cpuc->pebs_enabled & ~cpuc->intel_ctrl_host_mask;
293569e575ddSLike Xu struct perf_event *event = NULL;
293669e575ddSLike Xu int bit;
293769e575ddSLike Xu
293869e575ddSLike Xu if (!unlikely(perf_guest_state()))
293969e575ddSLike Xu return;
294069e575ddSLike Xu
294169e575ddSLike Xu if (!x86_pmu.pebs_ept || !x86_pmu.pebs_active ||
294269e575ddSLike Xu !guest_pebs_idxs)
294369e575ddSLike Xu return;
294469e575ddSLike Xu
294569e575ddSLike Xu for_each_set_bit(bit, (unsigned long *)&guest_pebs_idxs,
294669e575ddSLike Xu INTEL_PMC_IDX_FIXED + x86_pmu.num_counters_fixed) {
294769e575ddSLike Xu event = cpuc->events[bit];
294869e575ddSLike Xu if (!event->attr.precise_ip)
294969e575ddSLike Xu continue;
295069e575ddSLike Xu
295169e575ddSLike Xu perf_sample_data_init(data, 0, event->hw.last_period);
295269e575ddSLike Xu if (perf_event_overflow(event, data, regs))
295369e575ddSLike Xu x86_pmu_stop(event, 0);
295469e575ddSLike Xu
295569e575ddSLike Xu /* Inject one fake event is enough. */
295669e575ddSLike Xu break;
295769e575ddSLike Xu }
295869e575ddSLike Xu }
295969e575ddSLike Xu
handle_pmi_common(struct pt_regs * regs,u64 status)2960ba12d20eSKan Liang static int handle_pmi_common(struct pt_regs *regs, u64 status)
2961e1069839SBorislav Petkov {
2962e1069839SBorislav Petkov struct perf_sample_data data;
2963ba12d20eSKan Liang struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
2964ba12d20eSKan Liang int bit;
2965ba12d20eSKan Liang int handled = 0;
2966fc4b8fcaSKan Liang u64 intel_ctrl = hybrid(cpuc->pmu, intel_ctrl);
2967e1069839SBorislav Petkov
2968e1069839SBorislav Petkov inc_irq_stat(apic_perf_irqs);
2969e1069839SBorislav Petkov
2970e1069839SBorislav Petkov /*
2971e1069839SBorislav Petkov * Ignore a range of extra bits in status that do not indicate
2972e1069839SBorislav Petkov * overflow by themselves.
2973e1069839SBorislav Petkov */
2974e1069839SBorislav Petkov status &= ~(GLOBAL_STATUS_COND_CHG |
2975e1069839SBorislav Petkov GLOBAL_STATUS_ASIF |
2976e1069839SBorislav Petkov GLOBAL_STATUS_LBRS_FROZEN);
2977e1069839SBorislav Petkov if (!status)
2978ba12d20eSKan Liang return 0;
2979daa864b8SStephane Eranian /*
2980daa864b8SStephane Eranian * In case multiple PEBS events are sampled at the same time,
2981daa864b8SStephane Eranian * it is possible to have GLOBAL_STATUS bit 62 set indicating
2982daa864b8SStephane Eranian * PEBS buffer overflow and also seeing at most 3 PEBS counters
2983daa864b8SStephane Eranian * having their bits set in the status register. This is a sign
2984daa864b8SStephane Eranian * that there was at least one PEBS record pending at the time
2985daa864b8SStephane Eranian * of the PMU interrupt. PEBS counters must only be processed
2986daa864b8SStephane Eranian * via the drain_pebs() calls and not via the regular sample
2987daa864b8SStephane Eranian * processing loop coming after that the function, otherwise
2988daa864b8SStephane Eranian * phony regular samples may be generated in the sampling buffer
2989daa864b8SStephane Eranian * not marked with the EXACT tag. Another possibility is to have
2990daa864b8SStephane Eranian * one PEBS event and at least one non-PEBS event which overflows
2991daa864b8SStephane Eranian * while PEBS has armed. In this case, bit 62 of GLOBAL_STATUS will
2992daa864b8SStephane Eranian * not be set, yet the overflow status bit for the PEBS counter will
2993daa864b8SStephane Eranian * be on Skylake.
2994daa864b8SStephane Eranian *
2995daa864b8SStephane Eranian * To avoid this problem, we systematically ignore the PEBS-enabled
2996daa864b8SStephane Eranian * counters from the GLOBAL_STATUS mask and we always process PEBS
2997daa864b8SStephane Eranian * events via drain_pebs().
2998daa864b8SStephane Eranian */
29990d23dc34SPeter Zijlstra (Intel) status &= ~(cpuc->pebs_enabled & x86_pmu.pebs_capable);
3000e1069839SBorislav Petkov
3001e1069839SBorislav Petkov /*
3002e1069839SBorislav Petkov * PEBS overflow sets bit 62 in the global status register
3003e1069839SBorislav Petkov */
300460a2a271SKan Liang if (__test_and_clear_bit(GLOBAL_STATUS_BUFFER_OVF_BIT, (unsigned long *)&status)) {
30056c1c07b3SKan Liang u64 pebs_enabled = cpuc->pebs_enabled;
30066c1c07b3SKan Liang
3007e1069839SBorislav Petkov handled++;
300869e575ddSLike Xu x86_pmu_handle_guest_pebs(regs, &data);
30099dfa9a5cSPeter Zijlstra x86_pmu.drain_pebs(regs, &data);
3010fc4b8fcaSKan Liang status &= intel_ctrl | GLOBAL_STATUS_TRACE_TOPAPMI;
30116c1c07b3SKan Liang
30126c1c07b3SKan Liang /*
30136c1c07b3SKan Liang * PMI throttle may be triggered, which stops the PEBS event.
30146c1c07b3SKan Liang * Although cpuc->pebs_enabled is updated accordingly, the
30156c1c07b3SKan Liang * MSR_IA32_PEBS_ENABLE is not updated. Because the
30166c1c07b3SKan Liang * cpuc->enabled has been forced to 0 in PMI.
30176c1c07b3SKan Liang * Update the MSR if pebs_enabled is changed.
30186c1c07b3SKan Liang */
30196c1c07b3SKan Liang if (pebs_enabled != cpuc->pebs_enabled)
30206c1c07b3SKan Liang wrmsrl(MSR_IA32_PEBS_ENABLE, cpuc->pebs_enabled);
3021e1069839SBorislav Petkov }
3022e1069839SBorislav Petkov
3023e1069839SBorislav Petkov /*
3024e1069839SBorislav Petkov * Intel PT
3025e1069839SBorislav Petkov */
302660a2a271SKan Liang if (__test_and_clear_bit(GLOBAL_STATUS_TRACE_TOPAPMI_BIT, (unsigned long *)&status)) {
3027e1069839SBorislav Petkov handled++;
30281c343051SSean Christopherson if (!perf_guest_handle_intel_pt_intr())
3029e1069839SBorislav Petkov intel_pt_interrupt();
3030e1069839SBorislav Petkov }
3031e1069839SBorislav Petkov
3032e1069839SBorislav Petkov /*
3033d9f6e12fSIngo Molnar * Intel Perf metrics
30347b2c05a1SKan Liang */
30357b2c05a1SKan Liang if (__test_and_clear_bit(GLOBAL_STATUS_PERF_METRICS_OVF_BIT, (unsigned long *)&status)) {
30367b2c05a1SKan Liang handled++;
30371acab2e0SPeter Zijlstra static_call(intel_pmu_update_topdown_event)(NULL);
30387b2c05a1SKan Liang }
30397b2c05a1SKan Liang
30407b2c05a1SKan Liang /*
3041e1069839SBorislav Petkov * Checkpointed counters can lead to 'spurious' PMIs because the
3042e1069839SBorislav Petkov * rollback caused by the PMI will have cleared the overflow status
3043e1069839SBorislav Petkov * bit. Therefore always force probe these counters.
3044e1069839SBorislav Petkov */
3045e1069839SBorislav Petkov status |= cpuc->intel_cp_status;
3046e1069839SBorislav Petkov
3047e1069839SBorislav Petkov for_each_set_bit(bit, (unsigned long *)&status, X86_PMC_IDX_MAX) {
3048e1069839SBorislav Petkov struct perf_event *event = cpuc->events[bit];
3049e1069839SBorislav Petkov
3050e1069839SBorislav Petkov handled++;
3051e1069839SBorislav Petkov
3052e1069839SBorislav Petkov if (!test_bit(bit, cpuc->active_mask))
3053e1069839SBorislav Petkov continue;
3054e1069839SBorislav Petkov
3055e1069839SBorislav Petkov if (!intel_pmu_save_and_restart(event))
3056e1069839SBorislav Petkov continue;
3057e1069839SBorislav Petkov
3058e1069839SBorislav Petkov perf_sample_data_init(&data, 0, event->hw.last_period);
3059e1069839SBorislav Petkov
3060eb55b455SNamhyung Kim if (has_branch_stack(event))
3061eb55b455SNamhyung Kim perf_sample_save_brstack(&data, event, &cpuc->lbr_stack);
3062e1069839SBorislav Petkov
3063e1069839SBorislav Petkov if (perf_event_overflow(event, &data, regs))
3064e1069839SBorislav Petkov x86_pmu_stop(event, 0);
3065e1069839SBorislav Petkov }
3066e1069839SBorislav Petkov
3067ba12d20eSKan Liang return handled;
3068ba12d20eSKan Liang }
3069ba12d20eSKan Liang
3070ba12d20eSKan Liang /*
3071ba12d20eSKan Liang * This handler is triggered by the local APIC, so the APIC IRQ handling
3072ba12d20eSKan Liang * rules apply:
3073ba12d20eSKan Liang */
intel_pmu_handle_irq(struct pt_regs * regs)3074ba12d20eSKan Liang static int intel_pmu_handle_irq(struct pt_regs *regs)
3075ba12d20eSKan Liang {
3076acade637SKan Liang struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
3077acade637SKan Liang bool late_ack = hybrid_bit(cpuc->pmu, late_ack);
3078acade637SKan Liang bool mid_ack = hybrid_bit(cpuc->pmu, mid_ack);
3079ba12d20eSKan Liang int loops;
3080ba12d20eSKan Liang u64 status;
3081ba12d20eSKan Liang int handled;
3082ba12d20eSKan Liang int pmu_enabled;
3083ba12d20eSKan Liang
3084ba12d20eSKan Liang /*
3085ba12d20eSKan Liang * Save the PMU state.
3086ba12d20eSKan Liang * It needs to be restored when leaving the handler.
3087ba12d20eSKan Liang */
3088ba12d20eSKan Liang pmu_enabled = cpuc->enabled;
3089ba12d20eSKan Liang /*
3090acade637SKan Liang * In general, the early ACK is only applied for old platforms.
3091acade637SKan Liang * For the big core starts from Haswell, the late ACK should be
3092acade637SKan Liang * applied.
3093acade637SKan Liang * For the small core after Tremont, we have to do the ACK right
3094acade637SKan Liang * before re-enabling counters, which is in the middle of the
3095acade637SKan Liang * NMI handler.
3096ba12d20eSKan Liang */
3097acade637SKan Liang if (!late_ack && !mid_ack)
3098ba12d20eSKan Liang apic_write(APIC_LVTPC, APIC_DM_NMI);
3099ba12d20eSKan Liang intel_bts_disable_local();
3100ba12d20eSKan Liang cpuc->enabled = 0;
3101c22ac2a3SSong Liu __intel_pmu_disable_all(true);
3102ba12d20eSKan Liang handled = intel_pmu_drain_bts_buffer();
3103ba12d20eSKan Liang handled += intel_bts_interrupt();
3104ba12d20eSKan Liang status = intel_pmu_get_status();
3105ba12d20eSKan Liang if (!status)
3106ba12d20eSKan Liang goto done;
3107ba12d20eSKan Liang
3108ba12d20eSKan Liang loops = 0;
3109ba12d20eSKan Liang again:
3110ba12d20eSKan Liang intel_pmu_lbr_read();
3111ba12d20eSKan Liang intel_pmu_ack_status(status);
3112ba12d20eSKan Liang if (++loops > 100) {
3113ba12d20eSKan Liang static bool warned;
3114ba12d20eSKan Liang
3115ba12d20eSKan Liang if (!warned) {
3116ba12d20eSKan Liang WARN(1, "perfevents: irq loop stuck!\n");
3117ba12d20eSKan Liang perf_event_print_debug();
3118ba12d20eSKan Liang warned = true;
3119ba12d20eSKan Liang }
3120ba12d20eSKan Liang intel_pmu_reset();
3121ba12d20eSKan Liang goto done;
3122ba12d20eSKan Liang }
3123ba12d20eSKan Liang
3124ba12d20eSKan Liang handled += handle_pmi_common(regs, status);
3125ba12d20eSKan Liang
3126e1069839SBorislav Petkov /*
3127e1069839SBorislav Petkov * Repeat if there is more work to be done:
3128e1069839SBorislav Petkov */
3129e1069839SBorislav Petkov status = intel_pmu_get_status();
3130e1069839SBorislav Petkov if (status)
3131e1069839SBorislav Petkov goto again;
3132e1069839SBorislav Petkov
3133e1069839SBorislav Petkov done:
3134acade637SKan Liang if (mid_ack)
3135acade637SKan Liang apic_write(APIC_LVTPC, APIC_DM_NMI);
3136c3d266c8SKan Liang /* Only restore PMU state when it's active. See x86_pmu_disable(). */
313782d71ed0SKan Liang cpuc->enabled = pmu_enabled;
313882d71ed0SKan Liang if (pmu_enabled)
3139e1069839SBorislav Petkov __intel_pmu_enable_all(0, true);
3140cecf6235SAlexander Shishkin intel_bts_enable_local();
3141c3d266c8SKan Liang
3142e1069839SBorislav Petkov /*
3143e1069839SBorislav Petkov * Only unmask the NMI after the overflow counters
3144e1069839SBorislav Petkov * have been reset. This avoids spurious NMIs on
3145e1069839SBorislav Petkov * Haswell CPUs.
3146e1069839SBorislav Petkov */
3147acade637SKan Liang if (late_ack)
3148e1069839SBorislav Petkov apic_write(APIC_LVTPC, APIC_DM_NMI);
3149e1069839SBorislav Petkov return handled;
3150e1069839SBorislav Petkov }
3151e1069839SBorislav Petkov
3152e1069839SBorislav Petkov static struct event_constraint *
intel_bts_constraints(struct perf_event * event)3153e1069839SBorislav Petkov intel_bts_constraints(struct perf_event *event)
3154e1069839SBorislav Petkov {
315567266c10SJiri Olsa if (unlikely(intel_pmu_has_bts(event)))
3156e1069839SBorislav Petkov return &bts_constraint;
3157e1069839SBorislav Petkov
3158e1069839SBorislav Petkov return NULL;
3159e1069839SBorislav Petkov }
3160e1069839SBorislav Petkov
3161097e4311SLike Xu /*
3162097e4311SLike Xu * Note: matches a fake event, like Fixed2.
3163097e4311SLike Xu */
3164097e4311SLike Xu static struct event_constraint *
intel_vlbr_constraints(struct perf_event * event)3165097e4311SLike Xu intel_vlbr_constraints(struct perf_event *event)
3166097e4311SLike Xu {
3167097e4311SLike Xu struct event_constraint *c = &vlbr_constraint;
3168097e4311SLike Xu
316958637025SLike Xu if (unlikely(constraint_match(c, event->hw.config))) {
317058637025SLike Xu event->hw.flags |= c->flags;
3171097e4311SLike Xu return c;
317258637025SLike Xu }
3173097e4311SLike Xu
3174097e4311SLike Xu return NULL;
3175097e4311SLike Xu }
3176097e4311SLike Xu
intel_alt_er(struct cpu_hw_events * cpuc,int idx,u64 config)3177183af736SKan Liang static int intel_alt_er(struct cpu_hw_events *cpuc,
3178183af736SKan Liang int idx, u64 config)
3179e1069839SBorislav Petkov {
3180183af736SKan Liang struct extra_reg *extra_regs = hybrid(cpuc->pmu, extra_regs);
3181e1069839SBorislav Petkov int alt_idx = idx;
3182e1069839SBorislav Petkov
3183e1069839SBorislav Petkov if (!(x86_pmu.flags & PMU_FL_HAS_RSP_1))
3184e1069839SBorislav Petkov return idx;
3185e1069839SBorislav Petkov
3186e1069839SBorislav Petkov if (idx == EXTRA_REG_RSP_0)
3187e1069839SBorislav Petkov alt_idx = EXTRA_REG_RSP_1;
3188e1069839SBorislav Petkov
3189e1069839SBorislav Petkov if (idx == EXTRA_REG_RSP_1)
3190e1069839SBorislav Petkov alt_idx = EXTRA_REG_RSP_0;
3191e1069839SBorislav Petkov
3192183af736SKan Liang if (config & ~extra_regs[alt_idx].valid_mask)
3193e1069839SBorislav Petkov return idx;
3194e1069839SBorislav Petkov
3195e1069839SBorislav Petkov return alt_idx;
3196e1069839SBorislav Petkov }
3197e1069839SBorislav Petkov
intel_fixup_er(struct perf_event * event,int idx)3198e1069839SBorislav Petkov static void intel_fixup_er(struct perf_event *event, int idx)
3199e1069839SBorislav Petkov {
3200183af736SKan Liang struct extra_reg *extra_regs = hybrid(event->pmu, extra_regs);
3201e1069839SBorislav Petkov event->hw.extra_reg.idx = idx;
3202e1069839SBorislav Petkov
3203e1069839SBorislav Petkov if (idx == EXTRA_REG_RSP_0) {
3204e1069839SBorislav Petkov event->hw.config &= ~INTEL_ARCH_EVENT_MASK;
3205183af736SKan Liang event->hw.config |= extra_regs[EXTRA_REG_RSP_0].event;
3206e1069839SBorislav Petkov event->hw.extra_reg.reg = MSR_OFFCORE_RSP_0;
3207e1069839SBorislav Petkov } else if (idx == EXTRA_REG_RSP_1) {
3208e1069839SBorislav Petkov event->hw.config &= ~INTEL_ARCH_EVENT_MASK;
3209183af736SKan Liang event->hw.config |= extra_regs[EXTRA_REG_RSP_1].event;
3210e1069839SBorislav Petkov event->hw.extra_reg.reg = MSR_OFFCORE_RSP_1;
3211e1069839SBorislav Petkov }
3212e1069839SBorislav Petkov }
3213e1069839SBorislav Petkov
3214e1069839SBorislav Petkov /*
3215e1069839SBorislav Petkov * manage allocation of shared extra msr for certain events
3216e1069839SBorislav Petkov *
3217e1069839SBorislav Petkov * sharing can be:
3218e1069839SBorislav Petkov * per-cpu: to be shared between the various events on a single PMU
3219e1069839SBorislav Petkov * per-core: per-cpu + shared by HT threads
3220e1069839SBorislav Petkov */
3221e1069839SBorislav Petkov static struct event_constraint *
__intel_shared_reg_get_constraints(struct cpu_hw_events * cpuc,struct perf_event * event,struct hw_perf_event_extra * reg)3222e1069839SBorislav Petkov __intel_shared_reg_get_constraints(struct cpu_hw_events *cpuc,
3223e1069839SBorislav Petkov struct perf_event *event,
3224e1069839SBorislav Petkov struct hw_perf_event_extra *reg)
3225e1069839SBorislav Petkov {
3226e1069839SBorislav Petkov struct event_constraint *c = &emptyconstraint;
3227e1069839SBorislav Petkov struct er_account *era;
3228e1069839SBorislav Petkov unsigned long flags;
3229e1069839SBorislav Petkov int idx = reg->idx;
3230e1069839SBorislav Petkov
3231e1069839SBorislav Petkov /*
3232e1069839SBorislav Petkov * reg->alloc can be set due to existing state, so for fake cpuc we
3233e1069839SBorislav Petkov * need to ignore this, otherwise we might fail to allocate proper fake
3234e1069839SBorislav Petkov * state for this extra reg constraint. Also see the comment below.
3235e1069839SBorislav Petkov */
3236e1069839SBorislav Petkov if (reg->alloc && !cpuc->is_fake)
3237e1069839SBorislav Petkov return NULL; /* call x86_get_event_constraint() */
3238e1069839SBorislav Petkov
3239e1069839SBorislav Petkov again:
3240e1069839SBorislav Petkov era = &cpuc->shared_regs->regs[idx];
3241e1069839SBorislav Petkov /*
3242e1069839SBorislav Petkov * we use spin_lock_irqsave() to avoid lockdep issues when
3243e1069839SBorislav Petkov * passing a fake cpuc
3244e1069839SBorislav Petkov */
3245e1069839SBorislav Petkov raw_spin_lock_irqsave(&era->lock, flags);
3246e1069839SBorislav Petkov
3247e1069839SBorislav Petkov if (!atomic_read(&era->ref) || era->config == reg->config) {
3248e1069839SBorislav Petkov
3249e1069839SBorislav Petkov /*
3250e1069839SBorislav Petkov * If its a fake cpuc -- as per validate_{group,event}() we
3251e1069839SBorislav Petkov * shouldn't touch event state and we can avoid doing so
3252e1069839SBorislav Petkov * since both will only call get_event_constraints() once
3253e1069839SBorislav Petkov * on each event, this avoids the need for reg->alloc.
3254e1069839SBorislav Petkov *
3255e1069839SBorislav Petkov * Not doing the ER fixup will only result in era->reg being
3256e1069839SBorislav Petkov * wrong, but since we won't actually try and program hardware
3257e1069839SBorislav Petkov * this isn't a problem either.
3258e1069839SBorislav Petkov */
3259e1069839SBorislav Petkov if (!cpuc->is_fake) {
3260e1069839SBorislav Petkov if (idx != reg->idx)
3261e1069839SBorislav Petkov intel_fixup_er(event, idx);
3262e1069839SBorislav Petkov
3263e1069839SBorislav Petkov /*
3264e1069839SBorislav Petkov * x86_schedule_events() can call get_event_constraints()
3265e1069839SBorislav Petkov * multiple times on events in the case of incremental
3266e1069839SBorislav Petkov * scheduling(). reg->alloc ensures we only do the ER
3267e1069839SBorislav Petkov * allocation once.
3268e1069839SBorislav Petkov */
3269e1069839SBorislav Petkov reg->alloc = 1;
3270e1069839SBorislav Petkov }
3271e1069839SBorislav Petkov
3272e1069839SBorislav Petkov /* lock in msr value */
3273e1069839SBorislav Petkov era->config = reg->config;
3274e1069839SBorislav Petkov era->reg = reg->reg;
3275e1069839SBorislav Petkov
3276e1069839SBorislav Petkov /* one more user */
3277e1069839SBorislav Petkov atomic_inc(&era->ref);
3278e1069839SBorislav Petkov
3279e1069839SBorislav Petkov /*
3280e1069839SBorislav Petkov * need to call x86_get_event_constraint()
3281e1069839SBorislav Petkov * to check if associated event has constraints
3282e1069839SBorislav Petkov */
3283e1069839SBorislav Petkov c = NULL;
3284e1069839SBorislav Petkov } else {
3285183af736SKan Liang idx = intel_alt_er(cpuc, idx, reg->config);
3286e1069839SBorislav Petkov if (idx != reg->idx) {
3287e1069839SBorislav Petkov raw_spin_unlock_irqrestore(&era->lock, flags);
3288e1069839SBorislav Petkov goto again;
3289e1069839SBorislav Petkov }
3290e1069839SBorislav Petkov }
3291e1069839SBorislav Petkov raw_spin_unlock_irqrestore(&era->lock, flags);
3292e1069839SBorislav Petkov
3293e1069839SBorislav Petkov return c;
3294e1069839SBorislav Petkov }
3295e1069839SBorislav Petkov
3296e1069839SBorislav Petkov static void
__intel_shared_reg_put_constraints(struct cpu_hw_events * cpuc,struct hw_perf_event_extra * reg)3297e1069839SBorislav Petkov __intel_shared_reg_put_constraints(struct cpu_hw_events *cpuc,
3298e1069839SBorislav Petkov struct hw_perf_event_extra *reg)
3299e1069839SBorislav Petkov {
3300e1069839SBorislav Petkov struct er_account *era;
3301e1069839SBorislav Petkov
3302e1069839SBorislav Petkov /*
3303e1069839SBorislav Petkov * Only put constraint if extra reg was actually allocated. Also takes
3304e1069839SBorislav Petkov * care of event which do not use an extra shared reg.
3305e1069839SBorislav Petkov *
3306e1069839SBorislav Petkov * Also, if this is a fake cpuc we shouldn't touch any event state
3307e1069839SBorislav Petkov * (reg->alloc) and we don't care about leaving inconsistent cpuc state
3308e1069839SBorislav Petkov * either since it'll be thrown out.
3309e1069839SBorislav Petkov */
3310e1069839SBorislav Petkov if (!reg->alloc || cpuc->is_fake)
3311e1069839SBorislav Petkov return;
3312e1069839SBorislav Petkov
3313e1069839SBorislav Petkov era = &cpuc->shared_regs->regs[reg->idx];
3314e1069839SBorislav Petkov
3315e1069839SBorislav Petkov /* one fewer user */
3316e1069839SBorislav Petkov atomic_dec(&era->ref);
3317e1069839SBorislav Petkov
3318e1069839SBorislav Petkov /* allocate again next time */
3319e1069839SBorislav Petkov reg->alloc = 0;
3320e1069839SBorislav Petkov }
3321e1069839SBorislav Petkov
3322e1069839SBorislav Petkov static struct event_constraint *
intel_shared_regs_constraints(struct cpu_hw_events * cpuc,struct perf_event * event)3323e1069839SBorislav Petkov intel_shared_regs_constraints(struct cpu_hw_events *cpuc,
3324e1069839SBorislav Petkov struct perf_event *event)
3325e1069839SBorislav Petkov {
3326e1069839SBorislav Petkov struct event_constraint *c = NULL, *d;
3327e1069839SBorislav Petkov struct hw_perf_event_extra *xreg, *breg;
3328e1069839SBorislav Petkov
3329e1069839SBorislav Petkov xreg = &event->hw.extra_reg;
3330e1069839SBorislav Petkov if (xreg->idx != EXTRA_REG_NONE) {
3331e1069839SBorislav Petkov c = __intel_shared_reg_get_constraints(cpuc, event, xreg);
3332e1069839SBorislav Petkov if (c == &emptyconstraint)
3333e1069839SBorislav Petkov return c;
3334e1069839SBorislav Petkov }
3335e1069839SBorislav Petkov breg = &event->hw.branch_reg;
3336e1069839SBorislav Petkov if (breg->idx != EXTRA_REG_NONE) {
3337e1069839SBorislav Petkov d = __intel_shared_reg_get_constraints(cpuc, event, breg);
3338e1069839SBorislav Petkov if (d == &emptyconstraint) {
3339e1069839SBorislav Petkov __intel_shared_reg_put_constraints(cpuc, xreg);
3340e1069839SBorislav Petkov c = d;
3341e1069839SBorislav Petkov }
3342e1069839SBorislav Petkov }
3343e1069839SBorislav Petkov return c;
3344e1069839SBorislav Petkov }
3345e1069839SBorislav Petkov
3346e1069839SBorislav Petkov struct event_constraint *
x86_get_event_constraints(struct cpu_hw_events * cpuc,int idx,struct perf_event * event)3347e1069839SBorislav Petkov x86_get_event_constraints(struct cpu_hw_events *cpuc, int idx,
3348e1069839SBorislav Petkov struct perf_event *event)
3349e1069839SBorislav Petkov {
335024ee38ffSKan Liang struct event_constraint *event_constraints = hybrid(cpuc->pmu, event_constraints);
3351e1069839SBorislav Petkov struct event_constraint *c;
3352e1069839SBorislav Petkov
335324ee38ffSKan Liang if (event_constraints) {
335424ee38ffSKan Liang for_each_event_constraint(c, event_constraints) {
335563b79f6eSPeter Zijlstra if (constraint_match(c, event->hw.config)) {
3356e1069839SBorislav Petkov event->hw.flags |= c->flags;
3357e1069839SBorislav Petkov return c;
3358e1069839SBorislav Petkov }
3359e1069839SBorislav Petkov }
3360e1069839SBorislav Petkov }
3361e1069839SBorislav Petkov
3362eaacf07dSKan Liang return &hybrid_var(cpuc->pmu, unconstrained);
3363e1069839SBorislav Petkov }
3364e1069839SBorislav Petkov
3365e1069839SBorislav Petkov static struct event_constraint *
__intel_get_event_constraints(struct cpu_hw_events * cpuc,int idx,struct perf_event * event)3366e1069839SBorislav Petkov __intel_get_event_constraints(struct cpu_hw_events *cpuc, int idx,
3367e1069839SBorislav Petkov struct perf_event *event)
3368e1069839SBorislav Petkov {
3369e1069839SBorislav Petkov struct event_constraint *c;
3370e1069839SBorislav Petkov
3371097e4311SLike Xu c = intel_vlbr_constraints(event);
3372097e4311SLike Xu if (c)
3373097e4311SLike Xu return c;
3374097e4311SLike Xu
3375e1069839SBorislav Petkov c = intel_bts_constraints(event);
3376e1069839SBorislav Petkov if (c)
3377e1069839SBorislav Petkov return c;
3378e1069839SBorislav Petkov
3379e1069839SBorislav Petkov c = intel_shared_regs_constraints(cpuc, event);
3380e1069839SBorislav Petkov if (c)
3381e1069839SBorislav Petkov return c;
3382e1069839SBorislav Petkov
3383e1069839SBorislav Petkov c = intel_pebs_constraints(event);
3384e1069839SBorislav Petkov if (c)
3385e1069839SBorislav Petkov return c;
3386e1069839SBorislav Petkov
3387e1069839SBorislav Petkov return x86_get_event_constraints(cpuc, idx, event);
3388e1069839SBorislav Petkov }
3389e1069839SBorislav Petkov
3390e1069839SBorislav Petkov static void
intel_start_scheduling(struct cpu_hw_events * cpuc)3391e1069839SBorislav Petkov intel_start_scheduling(struct cpu_hw_events *cpuc)
3392e1069839SBorislav Petkov {
3393e1069839SBorislav Petkov struct intel_excl_cntrs *excl_cntrs = cpuc->excl_cntrs;
3394e1069839SBorislav Petkov struct intel_excl_states *xl;
3395e1069839SBorislav Petkov int tid = cpuc->excl_thread_id;
3396e1069839SBorislav Petkov
3397e1069839SBorislav Petkov /*
3398e1069839SBorislav Petkov * nothing needed if in group validation mode
3399e1069839SBorislav Petkov */
3400e1069839SBorislav Petkov if (cpuc->is_fake || !is_ht_workaround_enabled())
3401e1069839SBorislav Petkov return;
3402e1069839SBorislav Petkov
3403e1069839SBorislav Petkov /*
3404e1069839SBorislav Petkov * no exclusion needed
3405e1069839SBorislav Petkov */
3406e1069839SBorislav Petkov if (WARN_ON_ONCE(!excl_cntrs))
3407e1069839SBorislav Petkov return;
3408e1069839SBorislav Petkov
3409e1069839SBorislav Petkov xl = &excl_cntrs->states[tid];
3410e1069839SBorislav Petkov
3411e1069839SBorislav Petkov xl->sched_started = true;
3412e1069839SBorislav Petkov /*
3413e1069839SBorislav Petkov * lock shared state until we are done scheduling
3414e1069839SBorislav Petkov * in stop_event_scheduling()
3415e1069839SBorislav Petkov * makes scheduling appear as a transaction
3416e1069839SBorislav Petkov */
3417e1069839SBorislav Petkov raw_spin_lock(&excl_cntrs->lock);
3418e1069839SBorislav Petkov }
3419e1069839SBorislav Petkov
intel_commit_scheduling(struct cpu_hw_events * cpuc,int idx,int cntr)3420e1069839SBorislav Petkov static void intel_commit_scheduling(struct cpu_hw_events *cpuc, int idx, int cntr)
3421e1069839SBorislav Petkov {
3422e1069839SBorislav Petkov struct intel_excl_cntrs *excl_cntrs = cpuc->excl_cntrs;
3423e1069839SBorislav Petkov struct event_constraint *c = cpuc->event_constraint[idx];
3424e1069839SBorislav Petkov struct intel_excl_states *xl;
3425e1069839SBorislav Petkov int tid = cpuc->excl_thread_id;
3426e1069839SBorislav Petkov
3427e1069839SBorislav Petkov if (cpuc->is_fake || !is_ht_workaround_enabled())
3428e1069839SBorislav Petkov return;
3429e1069839SBorislav Petkov
3430e1069839SBorislav Petkov if (WARN_ON_ONCE(!excl_cntrs))
3431e1069839SBorislav Petkov return;
3432e1069839SBorislav Petkov
3433e1069839SBorislav Petkov if (!(c->flags & PERF_X86_EVENT_DYNAMIC))
3434e1069839SBorislav Petkov return;
3435e1069839SBorislav Petkov
3436e1069839SBorislav Petkov xl = &excl_cntrs->states[tid];
3437e1069839SBorislav Petkov
3438e1069839SBorislav Petkov lockdep_assert_held(&excl_cntrs->lock);
3439e1069839SBorislav Petkov
3440e1069839SBorislav Petkov if (c->flags & PERF_X86_EVENT_EXCL)
3441e1069839SBorislav Petkov xl->state[cntr] = INTEL_EXCL_EXCLUSIVE;
3442e1069839SBorislav Petkov else
3443e1069839SBorislav Petkov xl->state[cntr] = INTEL_EXCL_SHARED;
3444e1069839SBorislav Petkov }
3445e1069839SBorislav Petkov
3446e1069839SBorislav Petkov static void
intel_stop_scheduling(struct cpu_hw_events * cpuc)3447e1069839SBorislav Petkov intel_stop_scheduling(struct cpu_hw_events *cpuc)
3448e1069839SBorislav Petkov {
3449e1069839SBorislav Petkov struct intel_excl_cntrs *excl_cntrs = cpuc->excl_cntrs;
3450e1069839SBorislav Petkov struct intel_excl_states *xl;
3451e1069839SBorislav Petkov int tid = cpuc->excl_thread_id;
3452e1069839SBorislav Petkov
3453e1069839SBorislav Petkov /*
3454e1069839SBorislav Petkov * nothing needed if in group validation mode
3455e1069839SBorislav Petkov */
3456e1069839SBorislav Petkov if (cpuc->is_fake || !is_ht_workaround_enabled())
3457e1069839SBorislav Petkov return;
3458e1069839SBorislav Petkov /*
3459e1069839SBorislav Petkov * no exclusion needed
3460e1069839SBorislav Petkov */
3461e1069839SBorislav Petkov if (WARN_ON_ONCE(!excl_cntrs))
3462e1069839SBorislav Petkov return;
3463e1069839SBorislav Petkov
3464e1069839SBorislav Petkov xl = &excl_cntrs->states[tid];
3465e1069839SBorislav Petkov
3466e1069839SBorislav Petkov xl->sched_started = false;
3467e1069839SBorislav Petkov /*
3468e1069839SBorislav Petkov * release shared state lock (acquired in intel_start_scheduling())
3469e1069839SBorislav Petkov */
3470e1069839SBorislav Petkov raw_spin_unlock(&excl_cntrs->lock);
3471e1069839SBorislav Petkov }
3472e1069839SBorislav Petkov
3473e1069839SBorislav Petkov static struct event_constraint *
dyn_constraint(struct cpu_hw_events * cpuc,struct event_constraint * c,int idx)347411f8b2d6SPeter Zijlstra (Intel) dyn_constraint(struct cpu_hw_events *cpuc, struct event_constraint *c, int idx)
347511f8b2d6SPeter Zijlstra (Intel) {
347611f8b2d6SPeter Zijlstra (Intel) WARN_ON_ONCE(!cpuc->constraint_list);
347711f8b2d6SPeter Zijlstra (Intel)
347811f8b2d6SPeter Zijlstra (Intel) if (!(c->flags & PERF_X86_EVENT_DYNAMIC)) {
347911f8b2d6SPeter Zijlstra (Intel) struct event_constraint *cx;
348011f8b2d6SPeter Zijlstra (Intel)
348111f8b2d6SPeter Zijlstra (Intel) /*
348211f8b2d6SPeter Zijlstra (Intel) * grab pre-allocated constraint entry
348311f8b2d6SPeter Zijlstra (Intel) */
348411f8b2d6SPeter Zijlstra (Intel) cx = &cpuc->constraint_list[idx];
348511f8b2d6SPeter Zijlstra (Intel)
348611f8b2d6SPeter Zijlstra (Intel) /*
348711f8b2d6SPeter Zijlstra (Intel) * initialize dynamic constraint
348811f8b2d6SPeter Zijlstra (Intel) * with static constraint
348911f8b2d6SPeter Zijlstra (Intel) */
349011f8b2d6SPeter Zijlstra (Intel) *cx = *c;
349111f8b2d6SPeter Zijlstra (Intel)
349211f8b2d6SPeter Zijlstra (Intel) /*
349311f8b2d6SPeter Zijlstra (Intel) * mark constraint as dynamic
349411f8b2d6SPeter Zijlstra (Intel) */
349511f8b2d6SPeter Zijlstra (Intel) cx->flags |= PERF_X86_EVENT_DYNAMIC;
349611f8b2d6SPeter Zijlstra (Intel) c = cx;
349711f8b2d6SPeter Zijlstra (Intel) }
349811f8b2d6SPeter Zijlstra (Intel)
349911f8b2d6SPeter Zijlstra (Intel) return c;
350011f8b2d6SPeter Zijlstra (Intel) }
350111f8b2d6SPeter Zijlstra (Intel)
350211f8b2d6SPeter Zijlstra (Intel) static struct event_constraint *
intel_get_excl_constraints(struct cpu_hw_events * cpuc,struct perf_event * event,int idx,struct event_constraint * c)3503e1069839SBorislav Petkov intel_get_excl_constraints(struct cpu_hw_events *cpuc, struct perf_event *event,
3504e1069839SBorislav Petkov int idx, struct event_constraint *c)
3505e1069839SBorislav Petkov {
3506e1069839SBorislav Petkov struct intel_excl_cntrs *excl_cntrs = cpuc->excl_cntrs;
3507e1069839SBorislav Petkov struct intel_excl_states *xlo;
3508e1069839SBorislav Petkov int tid = cpuc->excl_thread_id;
3509c090cb70SPeter Zijlstra int is_excl, i, w;
3510e1069839SBorislav Petkov
3511e1069839SBorislav Petkov /*
3512e1069839SBorislav Petkov * validating a group does not require
3513e1069839SBorislav Petkov * enforcing cross-thread exclusion
3514e1069839SBorislav Petkov */
3515e1069839SBorislav Petkov if (cpuc->is_fake || !is_ht_workaround_enabled())
3516e1069839SBorislav Petkov return c;
3517e1069839SBorislav Petkov
3518e1069839SBorislav Petkov /*
3519e1069839SBorislav Petkov * no exclusion needed
3520e1069839SBorislav Petkov */
3521e1069839SBorislav Petkov if (WARN_ON_ONCE(!excl_cntrs))
3522e1069839SBorislav Petkov return c;
3523e1069839SBorislav Petkov
3524e1069839SBorislav Petkov /*
3525e1069839SBorislav Petkov * because we modify the constraint, we need
3526e1069839SBorislav Petkov * to make a copy. Static constraints come
3527e1069839SBorislav Petkov * from static const tables.
3528e1069839SBorislav Petkov *
3529e1069839SBorislav Petkov * only needed when constraint has not yet
3530e1069839SBorislav Petkov * been cloned (marked dynamic)
3531e1069839SBorislav Petkov */
353211f8b2d6SPeter Zijlstra (Intel) c = dyn_constraint(cpuc, c, idx);
3533e1069839SBorislav Petkov
3534e1069839SBorislav Petkov /*
3535e1069839SBorislav Petkov * From here on, the constraint is dynamic.
3536e1069839SBorislav Petkov * Either it was just allocated above, or it
3537e1069839SBorislav Petkov * was allocated during a earlier invocation
3538e1069839SBorislav Petkov * of this function
3539e1069839SBorislav Petkov */
3540e1069839SBorislav Petkov
3541e1069839SBorislav Petkov /*
3542e1069839SBorislav Petkov * state of sibling HT
3543e1069839SBorislav Petkov */
3544e1069839SBorislav Petkov xlo = &excl_cntrs->states[tid ^ 1];
3545e1069839SBorislav Petkov
3546e1069839SBorislav Petkov /*
3547e1069839SBorislav Petkov * event requires exclusive counter access
3548e1069839SBorislav Petkov * across HT threads
3549e1069839SBorislav Petkov */
3550e1069839SBorislav Petkov is_excl = c->flags & PERF_X86_EVENT_EXCL;
3551e1069839SBorislav Petkov if (is_excl && !(event->hw.flags & PERF_X86_EVENT_EXCL_ACCT)) {
3552e1069839SBorislav Petkov event->hw.flags |= PERF_X86_EVENT_EXCL_ACCT;
3553e1069839SBorislav Petkov if (!cpuc->n_excl++)
3554e1069839SBorislav Petkov WRITE_ONCE(excl_cntrs->has_exclusive[tid], 1);
3555e1069839SBorislav Petkov }
3556e1069839SBorislav Petkov
3557e1069839SBorislav Petkov /*
3558e1069839SBorislav Petkov * Modify static constraint with current dynamic
3559e1069839SBorislav Petkov * state of thread
3560e1069839SBorislav Petkov *
3561e1069839SBorislav Petkov * EXCLUSIVE: sibling counter measuring exclusive event
3562e1069839SBorislav Petkov * SHARED : sibling counter measuring non-exclusive event
3563e1069839SBorislav Petkov * UNUSED : sibling counter unused
3564e1069839SBorislav Petkov */
3565c090cb70SPeter Zijlstra w = c->weight;
3566e1069839SBorislav Petkov for_each_set_bit(i, c->idxmsk, X86_PMC_IDX_MAX) {
3567e1069839SBorislav Petkov /*
3568e1069839SBorislav Petkov * exclusive event in sibling counter
3569e1069839SBorislav Petkov * our corresponding counter cannot be used
3570e1069839SBorislav Petkov * regardless of our event
3571e1069839SBorislav Petkov */
3572c090cb70SPeter Zijlstra if (xlo->state[i] == INTEL_EXCL_EXCLUSIVE) {
3573e1069839SBorislav Petkov __clear_bit(i, c->idxmsk);
3574c090cb70SPeter Zijlstra w--;
3575c090cb70SPeter Zijlstra continue;
3576c090cb70SPeter Zijlstra }
3577e1069839SBorislav Petkov /*
3578e1069839SBorislav Petkov * if measuring an exclusive event, sibling
3579e1069839SBorislav Petkov * measuring non-exclusive, then counter cannot
3580e1069839SBorislav Petkov * be used
3581e1069839SBorislav Petkov */
3582c090cb70SPeter Zijlstra if (is_excl && xlo->state[i] == INTEL_EXCL_SHARED) {
3583e1069839SBorislav Petkov __clear_bit(i, c->idxmsk);
3584c090cb70SPeter Zijlstra w--;
3585c090cb70SPeter Zijlstra continue;
3586e1069839SBorislav Petkov }
3587c090cb70SPeter Zijlstra }
3588e1069839SBorislav Petkov
3589e1069839SBorislav Petkov /*
3590e1069839SBorislav Petkov * if we return an empty mask, then switch
3591e1069839SBorislav Petkov * back to static empty constraint to avoid
3592e1069839SBorislav Petkov * the cost of freeing later on
3593e1069839SBorislav Petkov */
3594c090cb70SPeter Zijlstra if (!w)
3595e1069839SBorislav Petkov c = &emptyconstraint;
3596e1069839SBorislav Petkov
3597c090cb70SPeter Zijlstra c->weight = w;
3598c090cb70SPeter Zijlstra
3599e1069839SBorislav Petkov return c;
3600e1069839SBorislav Petkov }
3601e1069839SBorislav Petkov
3602e1069839SBorislav Petkov static struct event_constraint *
intel_get_event_constraints(struct cpu_hw_events * cpuc,int idx,struct perf_event * event)3603e1069839SBorislav Petkov intel_get_event_constraints(struct cpu_hw_events *cpuc, int idx,
3604e1069839SBorislav Petkov struct perf_event *event)
3605e1069839SBorislav Petkov {
360621d65555SPeter Zijlstra struct event_constraint *c1, *c2;
3607e1069839SBorislav Petkov
3608e1069839SBorislav Petkov c1 = cpuc->event_constraint[idx];
3609e1069839SBorislav Petkov
3610e1069839SBorislav Petkov /*
3611e1069839SBorislav Petkov * first time only
3612e1069839SBorislav Petkov * - static constraint: no change across incremental scheduling calls
3613e1069839SBorislav Petkov * - dynamic constraint: handled by intel_get_excl_constraints()
3614e1069839SBorislav Petkov */
3615e1069839SBorislav Petkov c2 = __intel_get_event_constraints(cpuc, idx, event);
3616109717deSPeter Zijlstra if (c1) {
3617109717deSPeter Zijlstra WARN_ON_ONCE(!(c1->flags & PERF_X86_EVENT_DYNAMIC));
3618e1069839SBorislav Petkov bitmap_copy(c1->idxmsk, c2->idxmsk, X86_PMC_IDX_MAX);
3619e1069839SBorislav Petkov c1->weight = c2->weight;
3620e1069839SBorislav Petkov c2 = c1;
3621e1069839SBorislav Petkov }
3622e1069839SBorislav Petkov
3623e1069839SBorislav Petkov if (cpuc->excl_cntrs)
3624e1069839SBorislav Petkov return intel_get_excl_constraints(cpuc, event, idx, c2);
3625e1069839SBorislav Petkov
3626e1069839SBorislav Petkov return c2;
3627e1069839SBorislav Petkov }
3628e1069839SBorislav Petkov
intel_put_excl_constraints(struct cpu_hw_events * cpuc,struct perf_event * event)3629e1069839SBorislav Petkov static void intel_put_excl_constraints(struct cpu_hw_events *cpuc,
3630e1069839SBorislav Petkov struct perf_event *event)
3631e1069839SBorislav Petkov {
3632e1069839SBorislav Petkov struct hw_perf_event *hwc = &event->hw;
3633e1069839SBorislav Petkov struct intel_excl_cntrs *excl_cntrs = cpuc->excl_cntrs;
3634e1069839SBorislav Petkov int tid = cpuc->excl_thread_id;
3635e1069839SBorislav Petkov struct intel_excl_states *xl;
3636e1069839SBorislav Petkov
3637e1069839SBorislav Petkov /*
3638e1069839SBorislav Petkov * nothing needed if in group validation mode
3639e1069839SBorislav Petkov */
3640e1069839SBorislav Petkov if (cpuc->is_fake)
3641e1069839SBorislav Petkov return;
3642e1069839SBorislav Petkov
3643e1069839SBorislav Petkov if (WARN_ON_ONCE(!excl_cntrs))
3644e1069839SBorislav Petkov return;
3645e1069839SBorislav Petkov
3646e1069839SBorislav Petkov if (hwc->flags & PERF_X86_EVENT_EXCL_ACCT) {
3647e1069839SBorislav Petkov hwc->flags &= ~PERF_X86_EVENT_EXCL_ACCT;
3648e1069839SBorislav Petkov if (!--cpuc->n_excl)
3649e1069839SBorislav Petkov WRITE_ONCE(excl_cntrs->has_exclusive[tid], 0);
3650e1069839SBorislav Petkov }
3651e1069839SBorislav Petkov
3652e1069839SBorislav Petkov /*
3653e1069839SBorislav Petkov * If event was actually assigned, then mark the counter state as
3654e1069839SBorislav Petkov * unused now.
3655e1069839SBorislav Petkov */
3656e1069839SBorislav Petkov if (hwc->idx >= 0) {
3657e1069839SBorislav Petkov xl = &excl_cntrs->states[tid];
3658e1069839SBorislav Petkov
3659e1069839SBorislav Petkov /*
3660e1069839SBorislav Petkov * put_constraint may be called from x86_schedule_events()
3661e1069839SBorislav Petkov * which already has the lock held so here make locking
3662e1069839SBorislav Petkov * conditional.
3663e1069839SBorislav Petkov */
3664e1069839SBorislav Petkov if (!xl->sched_started)
3665e1069839SBorislav Petkov raw_spin_lock(&excl_cntrs->lock);
3666e1069839SBorislav Petkov
3667e1069839SBorislav Petkov xl->state[hwc->idx] = INTEL_EXCL_UNUSED;
3668e1069839SBorislav Petkov
3669e1069839SBorislav Petkov if (!xl->sched_started)
3670e1069839SBorislav Petkov raw_spin_unlock(&excl_cntrs->lock);
3671e1069839SBorislav Petkov }
3672e1069839SBorislav Petkov }
3673e1069839SBorislav Petkov
3674e1069839SBorislav Petkov static void
intel_put_shared_regs_event_constraints(struct cpu_hw_events * cpuc,struct perf_event * event)3675e1069839SBorislav Petkov intel_put_shared_regs_event_constraints(struct cpu_hw_events *cpuc,
3676e1069839SBorislav Petkov struct perf_event *event)
3677e1069839SBorislav Petkov {
3678e1069839SBorislav Petkov struct hw_perf_event_extra *reg;
3679e1069839SBorislav Petkov
3680e1069839SBorislav Petkov reg = &event->hw.extra_reg;
3681e1069839SBorislav Petkov if (reg->idx != EXTRA_REG_NONE)
3682e1069839SBorislav Petkov __intel_shared_reg_put_constraints(cpuc, reg);
3683e1069839SBorislav Petkov
3684e1069839SBorislav Petkov reg = &event->hw.branch_reg;
3685e1069839SBorislav Petkov if (reg->idx != EXTRA_REG_NONE)
3686e1069839SBorislav Petkov __intel_shared_reg_put_constraints(cpuc, reg);
3687e1069839SBorislav Petkov }
3688e1069839SBorislav Petkov
intel_put_event_constraints(struct cpu_hw_events * cpuc,struct perf_event * event)3689e1069839SBorislav Petkov static void intel_put_event_constraints(struct cpu_hw_events *cpuc,
3690e1069839SBorislav Petkov struct perf_event *event)
3691e1069839SBorislav Petkov {
3692e1069839SBorislav Petkov intel_put_shared_regs_event_constraints(cpuc, event);
3693e1069839SBorislav Petkov
3694e1069839SBorislav Petkov /*
3695e1069839SBorislav Petkov * is PMU has exclusive counter restrictions, then
3696e1069839SBorislav Petkov * all events are subject to and must call the
3697e1069839SBorislav Petkov * put_excl_constraints() routine
3698e1069839SBorislav Petkov */
3699e1069839SBorislav Petkov if (cpuc->excl_cntrs)
3700e1069839SBorislav Petkov intel_put_excl_constraints(cpuc, event);
3701e1069839SBorislav Petkov }
3702e1069839SBorislav Petkov
intel_pebs_aliases_core2(struct perf_event * event)3703e1069839SBorislav Petkov static void intel_pebs_aliases_core2(struct perf_event *event)
3704e1069839SBorislav Petkov {
3705e1069839SBorislav Petkov if ((event->hw.config & X86_RAW_EVENT_MASK) == 0x003c) {
3706e1069839SBorislav Petkov /*
3707e1069839SBorislav Petkov * Use an alternative encoding for CPU_CLK_UNHALTED.THREAD_P
3708e1069839SBorislav Petkov * (0x003c) so that we can use it with PEBS.
3709e1069839SBorislav Petkov *
3710e1069839SBorislav Petkov * The regular CPU_CLK_UNHALTED.THREAD_P event (0x003c) isn't
3711e1069839SBorislav Petkov * PEBS capable. However we can use INST_RETIRED.ANY_P
3712e1069839SBorislav Petkov * (0x00c0), which is a PEBS capable event, to get the same
3713e1069839SBorislav Petkov * count.
3714e1069839SBorislav Petkov *
3715e1069839SBorislav Petkov * INST_RETIRED.ANY_P counts the number of cycles that retires
3716e1069839SBorislav Petkov * CNTMASK instructions. By setting CNTMASK to a value (16)
3717e1069839SBorislav Petkov * larger than the maximum number of instructions that can be
3718e1069839SBorislav Petkov * retired per cycle (4) and then inverting the condition, we
3719e1069839SBorislav Petkov * count all cycles that retire 16 or less instructions, which
3720e1069839SBorislav Petkov * is every cycle.
3721e1069839SBorislav Petkov *
3722e1069839SBorislav Petkov * Thereby we gain a PEBS capable cycle counter.
3723e1069839SBorislav Petkov */
3724e1069839SBorislav Petkov u64 alt_config = X86_CONFIG(.event=0xc0, .inv=1, .cmask=16);
3725e1069839SBorislav Petkov
3726e1069839SBorislav Petkov alt_config |= (event->hw.config & ~X86_RAW_EVENT_MASK);
3727e1069839SBorislav Petkov event->hw.config = alt_config;
3728e1069839SBorislav Petkov }
3729e1069839SBorislav Petkov }
3730e1069839SBorislav Petkov
intel_pebs_aliases_snb(struct perf_event * event)3731e1069839SBorislav Petkov static void intel_pebs_aliases_snb(struct perf_event *event)
3732e1069839SBorislav Petkov {
3733e1069839SBorislav Petkov if ((event->hw.config & X86_RAW_EVENT_MASK) == 0x003c) {
3734e1069839SBorislav Petkov /*
3735e1069839SBorislav Petkov * Use an alternative encoding for CPU_CLK_UNHALTED.THREAD_P
3736e1069839SBorislav Petkov * (0x003c) so that we can use it with PEBS.
3737e1069839SBorislav Petkov *
3738e1069839SBorislav Petkov * The regular CPU_CLK_UNHALTED.THREAD_P event (0x003c) isn't
3739e1069839SBorislav Petkov * PEBS capable. However we can use UOPS_RETIRED.ALL
3740e1069839SBorislav Petkov * (0x01c2), which is a PEBS capable event, to get the same
3741e1069839SBorislav Petkov * count.
3742e1069839SBorislav Petkov *
3743e1069839SBorislav Petkov * UOPS_RETIRED.ALL counts the number of cycles that retires
3744e1069839SBorislav Petkov * CNTMASK micro-ops. By setting CNTMASK to a value (16)
3745e1069839SBorislav Petkov * larger than the maximum number of micro-ops that can be
3746e1069839SBorislav Petkov * retired per cycle (4) and then inverting the condition, we
3747e1069839SBorislav Petkov * count all cycles that retire 16 or less micro-ops, which
3748e1069839SBorislav Petkov * is every cycle.
3749e1069839SBorislav Petkov *
3750e1069839SBorislav Petkov * Thereby we gain a PEBS capable cycle counter.
3751e1069839SBorislav Petkov */
3752e1069839SBorislav Petkov u64 alt_config = X86_CONFIG(.event=0xc2, .umask=0x01, .inv=1, .cmask=16);
3753e1069839SBorislav Petkov
3754e1069839SBorislav Petkov alt_config |= (event->hw.config & ~X86_RAW_EVENT_MASK);
3755e1069839SBorislav Petkov event->hw.config = alt_config;
3756e1069839SBorislav Petkov }
3757e1069839SBorislav Petkov }
3758e1069839SBorislav Petkov
intel_pebs_aliases_precdist(struct perf_event * event)3759e1069839SBorislav Petkov static void intel_pebs_aliases_precdist(struct perf_event *event)
3760e1069839SBorislav Petkov {
3761e1069839SBorislav Petkov if ((event->hw.config & X86_RAW_EVENT_MASK) == 0x003c) {
3762e1069839SBorislav Petkov /*
3763e1069839SBorislav Petkov * Use an alternative encoding for CPU_CLK_UNHALTED.THREAD_P
3764e1069839SBorislav Petkov * (0x003c) so that we can use it with PEBS.
3765e1069839SBorislav Petkov *
3766e1069839SBorislav Petkov * The regular CPU_CLK_UNHALTED.THREAD_P event (0x003c) isn't
3767e1069839SBorislav Petkov * PEBS capable. However we can use INST_RETIRED.PREC_DIST
3768e1069839SBorislav Petkov * (0x01c0), which is a PEBS capable event, to get the same
3769e1069839SBorislav Petkov * count.
3770e1069839SBorislav Petkov *
3771e1069839SBorislav Petkov * The PREC_DIST event has special support to minimize sample
3772e1069839SBorislav Petkov * shadowing effects. One drawback is that it can be
3773e1069839SBorislav Petkov * only programmed on counter 1, but that seems like an
3774e1069839SBorislav Petkov * acceptable trade off.
3775e1069839SBorislav Petkov */
3776e1069839SBorislav Petkov u64 alt_config = X86_CONFIG(.event=0xc0, .umask=0x01, .inv=1, .cmask=16);
3777e1069839SBorislav Petkov
3778e1069839SBorislav Petkov alt_config |= (event->hw.config & ~X86_RAW_EVENT_MASK);
3779e1069839SBorislav Petkov event->hw.config = alt_config;
3780e1069839SBorislav Petkov }
3781e1069839SBorislav Petkov }
3782e1069839SBorislav Petkov
intel_pebs_aliases_ivb(struct perf_event * event)3783e1069839SBorislav Petkov static void intel_pebs_aliases_ivb(struct perf_event *event)
3784e1069839SBorislav Petkov {
3785e1069839SBorislav Petkov if (event->attr.precise_ip < 3)
3786e1069839SBorislav Petkov return intel_pebs_aliases_snb(event);
3787e1069839SBorislav Petkov return intel_pebs_aliases_precdist(event);
3788e1069839SBorislav Petkov }
3789e1069839SBorislav Petkov
intel_pebs_aliases_skl(struct perf_event * event)3790e1069839SBorislav Petkov static void intel_pebs_aliases_skl(struct perf_event *event)
3791e1069839SBorislav Petkov {
3792e1069839SBorislav Petkov if (event->attr.precise_ip < 3)
3793e1069839SBorislav Petkov return intel_pebs_aliases_core2(event);
3794e1069839SBorislav Petkov return intel_pebs_aliases_precdist(event);
3795e1069839SBorislav Petkov }
3796e1069839SBorislav Petkov
intel_pmu_large_pebs_flags(struct perf_event * event)3797174afc3eSKan Liang static unsigned long intel_pmu_large_pebs_flags(struct perf_event *event)
3798e1069839SBorislav Petkov {
3799174afc3eSKan Liang unsigned long flags = x86_pmu.large_pebs_flags;
3800e1069839SBorislav Petkov
3801e1069839SBorislav Petkov if (event->attr.use_clockid)
3802e1069839SBorislav Petkov flags &= ~PERF_SAMPLE_TIME;
3803a47ba4d7SAndi Kleen if (!event->attr.exclude_kernel)
3804a47ba4d7SAndi Kleen flags &= ~PERF_SAMPLE_REGS_USER;
38059d5dcc93SKan Liang if (event->attr.sample_regs_user & ~PEBS_GP_REGS)
3806a47ba4d7SAndi Kleen flags &= ~(PERF_SAMPLE_REGS_USER | PERF_SAMPLE_REGS_INTR);
3807e1069839SBorislav Petkov return flags;
3808e1069839SBorislav Petkov }
3809e1069839SBorislav Petkov
intel_pmu_bts_config(struct perf_event * event)3810ed6101bbSJiri Olsa static int intel_pmu_bts_config(struct perf_event *event)
3811ed6101bbSJiri Olsa {
3812ed6101bbSJiri Olsa struct perf_event_attr *attr = &event->attr;
3813ed6101bbSJiri Olsa
381467266c10SJiri Olsa if (unlikely(intel_pmu_has_bts(event))) {
3815ed6101bbSJiri Olsa /* BTS is not supported by this architecture. */
3816ed6101bbSJiri Olsa if (!x86_pmu.bts_active)
3817ed6101bbSJiri Olsa return -EOPNOTSUPP;
3818ed6101bbSJiri Olsa
3819ed6101bbSJiri Olsa /* BTS is currently only allowed for user-mode. */
3820ed6101bbSJiri Olsa if (!attr->exclude_kernel)
3821ed6101bbSJiri Olsa return -EOPNOTSUPP;
3822ed6101bbSJiri Olsa
3823472de49fSJiri Olsa /* BTS is not allowed for precise events. */
3824472de49fSJiri Olsa if (attr->precise_ip)
3825472de49fSJiri Olsa return -EOPNOTSUPP;
3826472de49fSJiri Olsa
3827ed6101bbSJiri Olsa /* disallow bts if conflicting events are present */
3828ed6101bbSJiri Olsa if (x86_add_exclusive(x86_lbr_exclusive_lbr))
3829ed6101bbSJiri Olsa return -EBUSY;
3830ed6101bbSJiri Olsa
3831ed6101bbSJiri Olsa event->destroy = hw_perf_lbr_event_destroy;
3832ed6101bbSJiri Olsa }
3833ed6101bbSJiri Olsa
3834ed6101bbSJiri Olsa return 0;
3835ed6101bbSJiri Olsa }
3836ed6101bbSJiri Olsa
core_pmu_hw_config(struct perf_event * event)3837ed6101bbSJiri Olsa static int core_pmu_hw_config(struct perf_event *event)
3838ed6101bbSJiri Olsa {
3839ed6101bbSJiri Olsa int ret = x86_pmu_hw_config(event);
3840ed6101bbSJiri Olsa
3841ed6101bbSJiri Olsa if (ret)
3842ed6101bbSJiri Olsa return ret;
3843ed6101bbSJiri Olsa
3844ed6101bbSJiri Olsa return intel_pmu_bts_config(event);
3845ed6101bbSJiri Olsa }
3846ed6101bbSJiri Olsa
38471ab5f235SKan Liang #define INTEL_TD_METRIC_AVAILABLE_MAX (INTEL_TD_METRIC_RETIRING + \
38481ab5f235SKan Liang ((x86_pmu.num_topdown_events - 1) << 8))
38491ab5f235SKan Liang
is_available_metric_event(struct perf_event * event)38501ab5f235SKan Liang static bool is_available_metric_event(struct perf_event *event)
38511ab5f235SKan Liang {
38521ab5f235SKan Liang return is_metric_event(event) &&
38531ab5f235SKan Liang event->attr.config <= INTEL_TD_METRIC_AVAILABLE_MAX;
38541ab5f235SKan Liang }
38551ab5f235SKan Liang
is_mem_loads_event(struct perf_event * event)385661b985e3SKan Liang static inline bool is_mem_loads_event(struct perf_event *event)
385761b985e3SKan Liang {
385861b985e3SKan Liang return (event->attr.config & INTEL_ARCH_EVENT_MASK) == X86_CONFIG(.event=0xcd, .umask=0x01);
385961b985e3SKan Liang }
386061b985e3SKan Liang
is_mem_loads_aux_event(struct perf_event * event)386161b985e3SKan Liang static inline bool is_mem_loads_aux_event(struct perf_event *event)
386261b985e3SKan Liang {
386361b985e3SKan Liang return (event->attr.config & INTEL_ARCH_EVENT_MASK) == X86_CONFIG(.event=0x03, .umask=0x82);
386461b985e3SKan Liang }
386561b985e3SKan Liang
require_mem_loads_aux_event(struct perf_event * event)3866f83d2f91SKan Liang static inline bool require_mem_loads_aux_event(struct perf_event *event)
3867f83d2f91SKan Liang {
3868f83d2f91SKan Liang if (!(x86_pmu.flags & PMU_FL_MEM_LOADS_AUX))
3869f83d2f91SKan Liang return false;
3870f83d2f91SKan Liang
3871f83d2f91SKan Liang if (is_hybrid())
3872f83d2f91SKan Liang return hybrid_pmu(event->pmu)->cpu_type == hybrid_big;
3873f83d2f91SKan Liang
3874f83d2f91SKan Liang return true;
3875f83d2f91SKan Liang }
3876f83d2f91SKan Liang
intel_pmu_has_cap(struct perf_event * event,int idx)3877d0946a88SKan Liang static inline bool intel_pmu_has_cap(struct perf_event *event, int idx)
3878d0946a88SKan Liang {
3879d0946a88SKan Liang union perf_capabilities *intel_cap = &hybrid(event->pmu, intel_cap);
3880d0946a88SKan Liang
3881d0946a88SKan Liang return test_bit(idx, (unsigned long *)&intel_cap->capabilities);
3882d0946a88SKan Liang }
388361b985e3SKan Liang
intel_pmu_hw_config(struct perf_event * event)3884e1069839SBorislav Petkov static int intel_pmu_hw_config(struct perf_event *event)
3885e1069839SBorislav Petkov {
3886e1069839SBorislav Petkov int ret = x86_pmu_hw_config(event);
3887e1069839SBorislav Petkov
3888e1069839SBorislav Petkov if (ret)
3889e1069839SBorislav Petkov return ret;
3890e1069839SBorislav Petkov
3891ed6101bbSJiri Olsa ret = intel_pmu_bts_config(event);
3892ed6101bbSJiri Olsa if (ret)
3893ed6101bbSJiri Olsa return ret;
3894ed6101bbSJiri Olsa
3895e1069839SBorislav Petkov if (event->attr.precise_ip) {
38962dc0572fSKan Liang if ((event->attr.config & INTEL_ARCH_EVENT_MASK) == INTEL_FIXED_VLBR_EVENT)
38972dc0572fSKan Liang return -EINVAL;
38982dc0572fSKan Liang
3899c7a28657SStephane Eranian if (!(event->attr.freq || (event->attr.wakeup_events && !event->attr.watermark))) {
3900e1069839SBorislav Petkov event->hw.flags |= PERF_X86_EVENT_AUTO_RELOAD;
3901e1069839SBorislav Petkov if (!(event->attr.sample_type &
3902afbef301SKan Liang ~intel_pmu_large_pebs_flags(event))) {
3903174afc3eSKan Liang event->hw.flags |= PERF_X86_EVENT_LARGE_PEBS;
3904afbef301SKan Liang event->attach_state |= PERF_ATTACH_SCHED_CB;
3905afbef301SKan Liang }
3906e1069839SBorislav Petkov }
3907e1069839SBorislav Petkov if (x86_pmu.pebs_aliases)
3908e1069839SBorislav Petkov x86_pmu.pebs_aliases(event);
3909e1069839SBorislav Petkov }
3910e1069839SBorislav Petkov
3911e1069839SBorislav Petkov if (needs_branch_stack(event)) {
3912e1069839SBorislav Petkov ret = intel_pmu_setup_lbr_filter(event);
3913e1069839SBorislav Petkov if (ret)
3914e1069839SBorislav Petkov return ret;
3915afbef301SKan Liang event->attach_state |= PERF_ATTACH_SCHED_CB;
3916e1069839SBorislav Petkov
3917e1069839SBorislav Petkov /*
3918e1069839SBorislav Petkov * BTS is set up earlier in this path, so don't account twice
3919e1069839SBorislav Petkov */
392067266c10SJiri Olsa if (!unlikely(intel_pmu_has_bts(event))) {
3921e1069839SBorislav Petkov /* disallow lbr if conflicting events are present */
3922e1069839SBorislav Petkov if (x86_add_exclusive(x86_lbr_exclusive_lbr))
3923e1069839SBorislav Petkov return -EBUSY;
3924e1069839SBorislav Petkov
3925e1069839SBorislav Petkov event->destroy = hw_perf_lbr_event_destroy;
3926e1069839SBorislav Petkov }
3927e1069839SBorislav Petkov }
3928e1069839SBorislav Petkov
392942880f72SAlexander Shishkin if (event->attr.aux_output) {
393042880f72SAlexander Shishkin if (!event->attr.precise_ip)
393142880f72SAlexander Shishkin return -EINVAL;
393242880f72SAlexander Shishkin
393342880f72SAlexander Shishkin event->hw.flags |= PERF_X86_EVENT_PEBS_VIA_PT;
393442880f72SAlexander Shishkin }
393542880f72SAlexander Shishkin
3936d9977c43SKan Liang if ((event->attr.type == PERF_TYPE_HARDWARE) ||
3937d9977c43SKan Liang (event->attr.type == PERF_TYPE_HW_CACHE))
3938e1069839SBorislav Petkov return 0;
3939e1069839SBorislav Petkov
39407b2c05a1SKan Liang /*
39417b2c05a1SKan Liang * Config Topdown slots and metric events
39427b2c05a1SKan Liang *
39437b2c05a1SKan Liang * The slots event on Fixed Counter 3 can support sampling,
39447b2c05a1SKan Liang * which will be handled normally in x86_perf_event_update().
39457b2c05a1SKan Liang *
39467b2c05a1SKan Liang * Metric events don't support sampling and require being paired
39477b2c05a1SKan Liang * with a slots event as group leader. When the slots event
39487b2c05a1SKan Liang * is used in a metrics group, it too cannot support sampling.
39497b2c05a1SKan Liang */
3950d0946a88SKan Liang if (intel_pmu_has_cap(event, PERF_CAP_METRICS_IDX) && is_topdown_event(event)) {
39517b2c05a1SKan Liang if (event->attr.config1 || event->attr.config2)
39527b2c05a1SKan Liang return -EINVAL;
39537b2c05a1SKan Liang
39547b2c05a1SKan Liang /*
39557b2c05a1SKan Liang * The TopDown metrics events and slots event don't
39567b2c05a1SKan Liang * support any filters.
39577b2c05a1SKan Liang */
39587b2c05a1SKan Liang if (event->attr.config & X86_ALL_EVENT_FLAGS)
39597b2c05a1SKan Liang return -EINVAL;
39607b2c05a1SKan Liang
39611ab5f235SKan Liang if (is_available_metric_event(event)) {
39627b2c05a1SKan Liang struct perf_event *leader = event->group_leader;
39637b2c05a1SKan Liang
39647b2c05a1SKan Liang /* The metric events don't support sampling. */
39657b2c05a1SKan Liang if (is_sampling_event(event))
39667b2c05a1SKan Liang return -EINVAL;
39677b2c05a1SKan Liang
39687b2c05a1SKan Liang /* The metric events require a slots group leader. */
39697b2c05a1SKan Liang if (!is_slots_event(leader))
39707b2c05a1SKan Liang return -EINVAL;
39717b2c05a1SKan Liang
39727b2c05a1SKan Liang /*
39737b2c05a1SKan Liang * The leader/SLOTS must not be a sampling event for
39747b2c05a1SKan Liang * metric use; hardware requires it starts at 0 when used
39757b2c05a1SKan Liang * in conjunction with MSR_PERF_METRICS.
39767b2c05a1SKan Liang */
39777b2c05a1SKan Liang if (is_sampling_event(leader))
39787b2c05a1SKan Liang return -EINVAL;
39797b2c05a1SKan Liang
39807b2c05a1SKan Liang event->event_caps |= PERF_EV_CAP_SIBLING;
39817b2c05a1SKan Liang /*
39827b2c05a1SKan Liang * Only once we have a METRICs sibling do we
39837b2c05a1SKan Liang * need TopDown magic.
39847b2c05a1SKan Liang */
39857b2c05a1SKan Liang leader->hw.flags |= PERF_X86_EVENT_TOPDOWN;
39867b2c05a1SKan Liang event->hw.flags |= PERF_X86_EVENT_TOPDOWN;
39877b2c05a1SKan Liang }
39887b2c05a1SKan Liang }
39897b2c05a1SKan Liang
399061b985e3SKan Liang /*
399161b985e3SKan Liang * The load latency event X86_CONFIG(.event=0xcd, .umask=0x01) on SPR
399261b985e3SKan Liang * doesn't function quite right. As a work-around it needs to always be
399361b985e3SKan Liang * co-scheduled with a auxiliary event X86_CONFIG(.event=0x03, .umask=0x82).
399461b985e3SKan Liang * The actual count of this second event is irrelevant it just needs
399561b985e3SKan Liang * to be active to make the first event function correctly.
399661b985e3SKan Liang *
399761b985e3SKan Liang * In a group, the auxiliary event must be in front of the load latency
399861b985e3SKan Liang * event. The rule is to simplify the implementation of the check.
399961b985e3SKan Liang * That's because perf cannot have a complete group at the moment.
400061b985e3SKan Liang */
4001f83d2f91SKan Liang if (require_mem_loads_aux_event(event) &&
400261b985e3SKan Liang (event->attr.sample_type & PERF_SAMPLE_DATA_SRC) &&
400361b985e3SKan Liang is_mem_loads_event(event)) {
400461b985e3SKan Liang struct perf_event *leader = event->group_leader;
400561b985e3SKan Liang struct perf_event *sibling = NULL;
400661b985e3SKan Liang
400727c68c21SNamhyung Kim /*
400827c68c21SNamhyung Kim * When this memload event is also the first event (no group
400927c68c21SNamhyung Kim * exists yet), then there is no aux event before it.
401027c68c21SNamhyung Kim */
401127c68c21SNamhyung Kim if (leader == event)
401227c68c21SNamhyung Kim return -ENODATA;
401327c68c21SNamhyung Kim
401461b985e3SKan Liang if (!is_mem_loads_aux_event(leader)) {
401561b985e3SKan Liang for_each_sibling_event(sibling, leader) {
401661b985e3SKan Liang if (is_mem_loads_aux_event(sibling))
401761b985e3SKan Liang break;
401861b985e3SKan Liang }
401961b985e3SKan Liang if (list_entry_is_head(sibling, &leader->sibling_list, sibling_list))
402061b985e3SKan Liang return -ENODATA;
402161b985e3SKan Liang }
402261b985e3SKan Liang }
402361b985e3SKan Liang
4024e1069839SBorislav Petkov if (!(event->attr.config & ARCH_PERFMON_EVENTSEL_ANY))
4025e1069839SBorislav Petkov return 0;
4026e1069839SBorislav Petkov
4027e1069839SBorislav Petkov if (x86_pmu.version < 3)
4028e1069839SBorislav Petkov return -EINVAL;
4029e1069839SBorislav Petkov
4030da97e184SJoel Fernandes (Google) ret = perf_allow_cpu(&event->attr);
4031da97e184SJoel Fernandes (Google) if (ret)
4032da97e184SJoel Fernandes (Google) return ret;
4033e1069839SBorislav Petkov
4034e1069839SBorislav Petkov event->hw.config |= ARCH_PERFMON_EVENTSEL_ANY;
4035e1069839SBorislav Petkov
4036e1069839SBorislav Petkov return 0;
4037e1069839SBorislav Petkov }
4038e1069839SBorislav Petkov
4039c59a1f10SLike Xu /*
4040c59a1f10SLike Xu * Currently, the only caller of this function is the atomic_switch_perf_msrs().
4041c59a1f10SLike Xu * The host perf conext helps to prepare the values of the real hardware for
4042c59a1f10SLike Xu * a set of msrs that need to be switched atomically in a vmx transaction.
4043c59a1f10SLike Xu *
4044c59a1f10SLike Xu * For example, the pseudocode needed to add a new msr should look like:
4045c59a1f10SLike Xu *
4046c59a1f10SLike Xu * arr[(*nr)++] = (struct perf_guest_switch_msr){
4047c59a1f10SLike Xu * .msr = the hardware msr address,
4048c59a1f10SLike Xu * .host = the value the hardware has when it doesn't run a guest,
4049c59a1f10SLike Xu * .guest = the value the hardware has when it runs a guest,
4050c59a1f10SLike Xu * };
4051c59a1f10SLike Xu *
4052c59a1f10SLike Xu * These values have nothing to do with the emulated values the guest sees
4053c59a1f10SLike Xu * when it uses {RD,WR}MSR, which should be handled by the KVM context,
4054c59a1f10SLike Xu * specifically in the intel_pmu_{get,set}_msr().
4055c59a1f10SLike Xu */
intel_guest_get_msrs(int * nr,void * data)405639a4d779SLike Xu static struct perf_guest_switch_msr *intel_guest_get_msrs(int *nr, void *data)
4057e1069839SBorislav Petkov {
4058e1069839SBorislav Petkov struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
4059e1069839SBorislav Petkov struct perf_guest_switch_msr *arr = cpuc->guest_switch_msrs;
40608183a538SLike Xu struct kvm_pmu *kvm_pmu = (struct kvm_pmu *)data;
4061fc4b8fcaSKan Liang u64 intel_ctrl = hybrid(cpuc->pmu, intel_ctrl);
4062c59a1f10SLike Xu u64 pebs_mask = cpuc->pebs_enabled & x86_pmu.pebs_capable;
4063c59a1f10SLike Xu int global_ctrl, pebs_enable;
4064e1069839SBorislav Petkov
406530912a7fSPaolo Bonzini /*
406630912a7fSPaolo Bonzini * In addition to obeying exclude_guest/exclude_host, remove bits being
406730912a7fSPaolo Bonzini * used for PEBS when running a guest, because PEBS writes to virtual
406830912a7fSPaolo Bonzini * addresses (not physical addresses).
406930912a7fSPaolo Bonzini */
4070c59a1f10SLike Xu *nr = 0;
4071c59a1f10SLike Xu global_ctrl = (*nr)++;
4072c59a1f10SLike Xu arr[global_ctrl] = (struct perf_guest_switch_msr){
4073c59a1f10SLike Xu .msr = MSR_CORE_PERF_GLOBAL_CTRL,
4074c59a1f10SLike Xu .host = intel_ctrl & ~cpuc->intel_ctrl_guest_mask,
407530912a7fSPaolo Bonzini .guest = intel_ctrl & ~cpuc->intel_ctrl_host_mask & ~pebs_mask,
4076c59a1f10SLike Xu };
40779b545c04SAndi Kleen
4078c59a1f10SLike Xu if (!x86_pmu.pebs)
4079c59a1f10SLike Xu return arr;
4080c59a1f10SLike Xu
4081e1069839SBorislav Petkov /*
40829b545c04SAndi Kleen * If PMU counter has PEBS enabled it is not enough to
40839b545c04SAndi Kleen * disable counter on a guest entry since PEBS memory
40849b545c04SAndi Kleen * write can overshoot guest entry and corrupt guest
40859b545c04SAndi Kleen * memory. Disabling PEBS solves the problem.
40869b545c04SAndi Kleen *
40879b545c04SAndi Kleen * Don't do this if the CPU already enforces it.
4088e1069839SBorislav Petkov */
4089c59a1f10SLike Xu if (x86_pmu.pebs_no_isolation) {
4090c59a1f10SLike Xu arr[(*nr)++] = (struct perf_guest_switch_msr){
4091c59a1f10SLike Xu .msr = MSR_IA32_PEBS_ENABLE,
4092c59a1f10SLike Xu .host = cpuc->pebs_enabled,
4093c59a1f10SLike Xu .guest = 0,
4094c59a1f10SLike Xu };
4095c59a1f10SLike Xu return arr;
40969b545c04SAndi Kleen }
40979b545c04SAndi Kleen
40988183a538SLike Xu if (!kvm_pmu || !x86_pmu.pebs_ept)
4099c59a1f10SLike Xu return arr;
4100c59a1f10SLike Xu
41018183a538SLike Xu arr[(*nr)++] = (struct perf_guest_switch_msr){
41028183a538SLike Xu .msr = MSR_IA32_DS_AREA,
41038183a538SLike Xu .host = (unsigned long)cpuc->ds,
41048183a538SLike Xu .guest = kvm_pmu->ds_area,
41058183a538SLike Xu };
41068183a538SLike Xu
4107902caeb6SLike Xu if (x86_pmu.intel_cap.pebs_baseline) {
4108902caeb6SLike Xu arr[(*nr)++] = (struct perf_guest_switch_msr){
4109902caeb6SLike Xu .msr = MSR_PEBS_DATA_CFG,
41103c845304SLike Xu .host = cpuc->active_pebs_data_cfg,
4111902caeb6SLike Xu .guest = kvm_pmu->pebs_data_cfg,
4112902caeb6SLike Xu };
4113902caeb6SLike Xu }
4114902caeb6SLike Xu
41156ebe4436SLike Xu pebs_enable = (*nr)++;
4116c59a1f10SLike Xu arr[pebs_enable] = (struct perf_guest_switch_msr){
4117c59a1f10SLike Xu .msr = MSR_IA32_PEBS_ENABLE,
4118c59a1f10SLike Xu .host = cpuc->pebs_enabled & ~cpuc->intel_ctrl_guest_mask,
4119c59a1f10SLike Xu .guest = pebs_mask & ~cpuc->intel_ctrl_host_mask,
4120c59a1f10SLike Xu };
4121c59a1f10SLike Xu
412285425032SLike Xu if (arr[pebs_enable].host) {
412385425032SLike Xu /* Disable guest PEBS if host PEBS is enabled. */
412485425032SLike Xu arr[pebs_enable].guest = 0;
412585425032SLike Xu } else {
4126f2aeea57SLike Xu /* Disable guest PEBS thoroughly for cross-mapped PEBS counters. */
412785425032SLike Xu arr[pebs_enable].guest &= ~kvm_pmu->host_cross_mapped_mask;
4128f2aeea57SLike Xu arr[global_ctrl].guest &= ~kvm_pmu->host_cross_mapped_mask;
4129c59a1f10SLike Xu /* Set hw GLOBAL_CTRL bits for PEBS counter when it runs for guest */
413085425032SLike Xu arr[global_ctrl].guest |= arr[pebs_enable].guest;
4131e1069839SBorislav Petkov }
4132e1069839SBorislav Petkov
4133e1069839SBorislav Petkov return arr;
4134e1069839SBorislav Petkov }
4135e1069839SBorislav Petkov
core_guest_get_msrs(int * nr,void * data)413639a4d779SLike Xu static struct perf_guest_switch_msr *core_guest_get_msrs(int *nr, void *data)
4137e1069839SBorislav Petkov {
4138e1069839SBorislav Petkov struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
4139e1069839SBorislav Petkov struct perf_guest_switch_msr *arr = cpuc->guest_switch_msrs;
4140e1069839SBorislav Petkov int idx;
4141e1069839SBorislav Petkov
4142e1069839SBorislav Petkov for (idx = 0; idx < x86_pmu.num_counters; idx++) {
4143e1069839SBorislav Petkov struct perf_event *event = cpuc->events[idx];
4144e1069839SBorislav Petkov
4145e1069839SBorislav Petkov arr[idx].msr = x86_pmu_config_addr(idx);
4146e1069839SBorislav Petkov arr[idx].host = arr[idx].guest = 0;
4147e1069839SBorislav Petkov
4148e1069839SBorislav Petkov if (!test_bit(idx, cpuc->active_mask))
4149e1069839SBorislav Petkov continue;
4150e1069839SBorislav Petkov
4151e1069839SBorislav Petkov arr[idx].host = arr[idx].guest =
4152e1069839SBorislav Petkov event->hw.config | ARCH_PERFMON_EVENTSEL_ENABLE;
4153e1069839SBorislav Petkov
4154e1069839SBorislav Petkov if (event->attr.exclude_host)
4155e1069839SBorislav Petkov arr[idx].host &= ~ARCH_PERFMON_EVENTSEL_ENABLE;
4156e1069839SBorislav Petkov else if (event->attr.exclude_guest)
4157e1069839SBorislav Petkov arr[idx].guest &= ~ARCH_PERFMON_EVENTSEL_ENABLE;
4158e1069839SBorislav Petkov }
4159e1069839SBorislav Petkov
4160e1069839SBorislav Petkov *nr = x86_pmu.num_counters;
4161e1069839SBorislav Petkov return arr;
4162e1069839SBorislav Petkov }
4163e1069839SBorislav Petkov
core_pmu_enable_event(struct perf_event * event)4164e1069839SBorislav Petkov static void core_pmu_enable_event(struct perf_event *event)
4165e1069839SBorislav Petkov {
4166e1069839SBorislav Petkov if (!event->attr.exclude_host)
4167e1069839SBorislav Petkov x86_pmu_enable_event(event);
4168e1069839SBorislav Petkov }
4169e1069839SBorislav Petkov
core_pmu_enable_all(int added)4170e1069839SBorislav Petkov static void core_pmu_enable_all(int added)
4171e1069839SBorislav Petkov {
4172e1069839SBorislav Petkov struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
4173e1069839SBorislav Petkov int idx;
4174e1069839SBorislav Petkov
4175e1069839SBorislav Petkov for (idx = 0; idx < x86_pmu.num_counters; idx++) {
4176e1069839SBorislav Petkov struct hw_perf_event *hwc = &cpuc->events[idx]->hw;
4177e1069839SBorislav Petkov
4178e1069839SBorislav Petkov if (!test_bit(idx, cpuc->active_mask) ||
4179e1069839SBorislav Petkov cpuc->events[idx]->attr.exclude_host)
4180e1069839SBorislav Petkov continue;
4181e1069839SBorislav Petkov
4182e1069839SBorislav Petkov __x86_pmu_enable_event(hwc, ARCH_PERFMON_EVENTSEL_ENABLE);
4183e1069839SBorislav Petkov }
4184e1069839SBorislav Petkov }
4185e1069839SBorislav Petkov
hsw_hw_config(struct perf_event * event)4186e1069839SBorislav Petkov static int hsw_hw_config(struct perf_event *event)
4187e1069839SBorislav Petkov {
4188e1069839SBorislav Petkov int ret = intel_pmu_hw_config(event);
4189e1069839SBorislav Petkov
4190e1069839SBorislav Petkov if (ret)
4191e1069839SBorislav Petkov return ret;
4192e1069839SBorislav Petkov if (!boot_cpu_has(X86_FEATURE_RTM) && !boot_cpu_has(X86_FEATURE_HLE))
4193e1069839SBorislav Petkov return 0;
4194e1069839SBorislav Petkov event->hw.config |= event->attr.config & (HSW_IN_TX|HSW_IN_TX_CHECKPOINTED);
4195e1069839SBorislav Petkov
4196e1069839SBorislav Petkov /*
4197e1069839SBorislav Petkov * IN_TX/IN_TX-CP filters are not supported by the Haswell PMU with
4198e1069839SBorislav Petkov * PEBS or in ANY thread mode. Since the results are non-sensical forbid
4199e1069839SBorislav Petkov * this combination.
4200e1069839SBorislav Petkov */
4201e1069839SBorislav Petkov if ((event->hw.config & (HSW_IN_TX|HSW_IN_TX_CHECKPOINTED)) &&
4202e1069839SBorislav Petkov ((event->hw.config & ARCH_PERFMON_EVENTSEL_ANY) ||
4203e1069839SBorislav Petkov event->attr.precise_ip > 0))
4204e1069839SBorislav Petkov return -EOPNOTSUPP;
4205e1069839SBorislav Petkov
4206e1069839SBorislav Petkov if (event_is_checkpointed(event)) {
4207e1069839SBorislav Petkov /*
4208e1069839SBorislav Petkov * Sampling of checkpointed events can cause situations where
4209e1069839SBorislav Petkov * the CPU constantly aborts because of a overflow, which is
4210e1069839SBorislav Petkov * then checkpointed back and ignored. Forbid checkpointing
4211e1069839SBorislav Petkov * for sampling.
4212e1069839SBorislav Petkov *
4213e1069839SBorislav Petkov * But still allow a long sampling period, so that perf stat
4214e1069839SBorislav Petkov * from KVM works.
4215e1069839SBorislav Petkov */
4216e1069839SBorislav Petkov if (event->attr.sample_period > 0 &&
4217e1069839SBorislav Petkov event->attr.sample_period < 0x7fffffff)
4218e1069839SBorislav Petkov return -EOPNOTSUPP;
4219e1069839SBorislav Petkov }
4220e1069839SBorislav Petkov return 0;
4221e1069839SBorislav Petkov }
4222e1069839SBorislav Petkov
4223dd0b06b5SKan Liang static struct event_constraint counter0_constraint =
4224dd0b06b5SKan Liang INTEL_ALL_EVENT_CONSTRAINT(0, 0x1);
4225dd0b06b5SKan Liang
422638aaf921SKan Liang static struct event_constraint counter1_constraint =
422738aaf921SKan Liang INTEL_ALL_EVENT_CONSTRAINT(0, 0x2);
422838aaf921SKan Liang
422938aaf921SKan Liang static struct event_constraint counter0_1_constraint =
423038aaf921SKan Liang INTEL_ALL_EVENT_CONSTRAINT(0, 0x3);
423138aaf921SKan Liang
4232e1069839SBorislav Petkov static struct event_constraint counter2_constraint =
4233e1069839SBorislav Petkov EVENT_CONSTRAINT(0, 0x4, 0);
4234e1069839SBorislav Petkov
423560176089SKan Liang static struct event_constraint fixed0_constraint =
423660176089SKan Liang FIXED_EVENT_CONSTRAINT(0x00c0, 0);
423760176089SKan Liang
42386daeb873SKan Liang static struct event_constraint fixed0_counter0_constraint =
42396daeb873SKan Liang INTEL_ALL_EVENT_CONSTRAINT(0, 0x100000001ULL);
42406daeb873SKan Liang
424138aaf921SKan Liang static struct event_constraint fixed0_counter0_1_constraint =
424238aaf921SKan Liang INTEL_ALL_EVENT_CONSTRAINT(0, 0x100000003ULL);
424338aaf921SKan Liang
4244c87a3109SKan Liang static struct event_constraint counters_1_7_constraint =
4245c87a3109SKan Liang INTEL_ALL_EVENT_CONSTRAINT(0, 0xfeULL);
4246c87a3109SKan Liang
4247e1069839SBorislav Petkov static struct event_constraint *
hsw_get_event_constraints(struct cpu_hw_events * cpuc,int idx,struct perf_event * event)4248e1069839SBorislav Petkov hsw_get_event_constraints(struct cpu_hw_events *cpuc, int idx,
4249e1069839SBorislav Petkov struct perf_event *event)
4250e1069839SBorislav Petkov {
4251e1069839SBorislav Petkov struct event_constraint *c;
4252e1069839SBorislav Petkov
4253e1069839SBorislav Petkov c = intel_get_event_constraints(cpuc, idx, event);
4254e1069839SBorislav Petkov
4255e1069839SBorislav Petkov /* Handle special quirk on in_tx_checkpointed only in counter 2 */
4256e1069839SBorislav Petkov if (event->hw.config & HSW_IN_TX_CHECKPOINTED) {
4257e1069839SBorislav Petkov if (c->idxmsk64 & (1U << 2))
4258e1069839SBorislav Petkov return &counter2_constraint;
4259e1069839SBorislav Petkov return &emptyconstraint;
4260e1069839SBorislav Petkov }
4261e1069839SBorislav Petkov
4262e1069839SBorislav Petkov return c;
4263e1069839SBorislav Petkov }
4264e1069839SBorislav Petkov
4265dd0b06b5SKan Liang static struct event_constraint *
icl_get_event_constraints(struct cpu_hw_events * cpuc,int idx,struct perf_event * event)426660176089SKan Liang icl_get_event_constraints(struct cpu_hw_events *cpuc, int idx,
426760176089SKan Liang struct perf_event *event)
426860176089SKan Liang {
426960176089SKan Liang /*
427060176089SKan Liang * Fixed counter 0 has less skid.
427160176089SKan Liang * Force instruction:ppp in Fixed counter 0
427260176089SKan Liang */
427360176089SKan Liang if ((event->attr.precise_ip == 3) &&
427460176089SKan Liang constraint_match(&fixed0_constraint, event->hw.config))
427560176089SKan Liang return &fixed0_constraint;
427660176089SKan Liang
427760176089SKan Liang return hsw_get_event_constraints(cpuc, idx, event);
427860176089SKan Liang }
427960176089SKan Liang
428060176089SKan Liang static struct event_constraint *
spr_get_event_constraints(struct cpu_hw_events * cpuc,int idx,struct perf_event * event)428161b985e3SKan Liang spr_get_event_constraints(struct cpu_hw_events *cpuc, int idx,
428261b985e3SKan Liang struct perf_event *event)
428361b985e3SKan Liang {
428461b985e3SKan Liang struct event_constraint *c;
428561b985e3SKan Liang
428661b985e3SKan Liang c = icl_get_event_constraints(cpuc, idx, event);
428761b985e3SKan Liang
428861b985e3SKan Liang /*
428961b985e3SKan Liang * The :ppp indicates the Precise Distribution (PDist) facility, which
429061b985e3SKan Liang * is only supported on the GP counter 0. If a :ppp event which is not
429161b985e3SKan Liang * available on the GP counter 0, error out.
42921d5c7880SKan Liang * Exception: Instruction PDIR is only available on the fixed counter 0.
429361b985e3SKan Liang */
42941d5c7880SKan Liang if ((event->attr.precise_ip == 3) &&
42951d5c7880SKan Liang !constraint_match(&fixed0_constraint, event->hw.config)) {
429661b985e3SKan Liang if (c->idxmsk64 & BIT_ULL(0))
429761b985e3SKan Liang return &counter0_constraint;
429861b985e3SKan Liang
429961b985e3SKan Liang return &emptyconstraint;
430061b985e3SKan Liang }
430161b985e3SKan Liang
430261b985e3SKan Liang return c;
430361b985e3SKan Liang }
430461b985e3SKan Liang
430561b985e3SKan Liang static struct event_constraint *
glp_get_event_constraints(struct cpu_hw_events * cpuc,int idx,struct perf_event * event)4306dd0b06b5SKan Liang glp_get_event_constraints(struct cpu_hw_events *cpuc, int idx,
4307dd0b06b5SKan Liang struct perf_event *event)
4308dd0b06b5SKan Liang {
4309dd0b06b5SKan Liang struct event_constraint *c;
4310dd0b06b5SKan Liang
4311dd0b06b5SKan Liang /* :ppp means to do reduced skid PEBS which is PMC0 only. */
4312dd0b06b5SKan Liang if (event->attr.precise_ip == 3)
4313dd0b06b5SKan Liang return &counter0_constraint;
4314dd0b06b5SKan Liang
4315dd0b06b5SKan Liang c = intel_get_event_constraints(cpuc, idx, event);
4316dd0b06b5SKan Liang
4317dd0b06b5SKan Liang return c;
4318dd0b06b5SKan Liang }
4319dd0b06b5SKan Liang
43206daeb873SKan Liang static struct event_constraint *
tnt_get_event_constraints(struct cpu_hw_events * cpuc,int idx,struct perf_event * event)43216daeb873SKan Liang tnt_get_event_constraints(struct cpu_hw_events *cpuc, int idx,
43226daeb873SKan Liang struct perf_event *event)
43236daeb873SKan Liang {
43246daeb873SKan Liang struct event_constraint *c;
43256daeb873SKan Liang
432639a41278SKan Liang c = intel_get_event_constraints(cpuc, idx, event);
432739a41278SKan Liang
43286daeb873SKan Liang /*
43296daeb873SKan Liang * :ppp means to do reduced skid PEBS,
43306daeb873SKan Liang * which is available on PMC0 and fixed counter 0.
43316daeb873SKan Liang */
43326daeb873SKan Liang if (event->attr.precise_ip == 3) {
43336daeb873SKan Liang /* Force instruction:ppp on PMC0 and Fixed counter 0 */
43346daeb873SKan Liang if (constraint_match(&fixed0_constraint, event->hw.config))
43356daeb873SKan Liang return &fixed0_counter0_constraint;
43366daeb873SKan Liang
43376daeb873SKan Liang return &counter0_constraint;
43386daeb873SKan Liang }
43396daeb873SKan Liang
43406daeb873SKan Liang return c;
43416daeb873SKan Liang }
43426daeb873SKan Liang
4343400816f6SPeter Zijlstra (Intel) static bool allow_tsx_force_abort = true;
4344400816f6SPeter Zijlstra (Intel)
4345400816f6SPeter Zijlstra (Intel) static struct event_constraint *
tfa_get_event_constraints(struct cpu_hw_events * cpuc,int idx,struct perf_event * event)4346400816f6SPeter Zijlstra (Intel) tfa_get_event_constraints(struct cpu_hw_events *cpuc, int idx,
4347400816f6SPeter Zijlstra (Intel) struct perf_event *event)
4348400816f6SPeter Zijlstra (Intel) {
4349400816f6SPeter Zijlstra (Intel) struct event_constraint *c = hsw_get_event_constraints(cpuc, idx, event);
4350400816f6SPeter Zijlstra (Intel)
4351400816f6SPeter Zijlstra (Intel) /*
4352400816f6SPeter Zijlstra (Intel) * Without TFA we must not use PMC3.
4353400816f6SPeter Zijlstra (Intel) */
435421d65555SPeter Zijlstra if (!allow_tsx_force_abort && test_bit(3, c->idxmsk)) {
4355400816f6SPeter Zijlstra (Intel) c = dyn_constraint(cpuc, c, idx);
4356400816f6SPeter Zijlstra (Intel) c->idxmsk64 &= ~(1ULL << 3);
4357400816f6SPeter Zijlstra (Intel) c->weight--;
4358400816f6SPeter Zijlstra (Intel) }
4359400816f6SPeter Zijlstra (Intel)
4360400816f6SPeter Zijlstra (Intel) return c;
4361400816f6SPeter Zijlstra (Intel) }
4362400816f6SPeter Zijlstra (Intel)
4363f83d2f91SKan Liang static struct event_constraint *
adl_get_event_constraints(struct cpu_hw_events * cpuc,int idx,struct perf_event * event)4364f83d2f91SKan Liang adl_get_event_constraints(struct cpu_hw_events *cpuc, int idx,
4365f83d2f91SKan Liang struct perf_event *event)
4366f83d2f91SKan Liang {
4367f83d2f91SKan Liang struct x86_hybrid_pmu *pmu = hybrid_pmu(event->pmu);
4368f83d2f91SKan Liang
4369f83d2f91SKan Liang if (pmu->cpu_type == hybrid_big)
4370f83d2f91SKan Liang return spr_get_event_constraints(cpuc, idx, event);
4371f83d2f91SKan Liang else if (pmu->cpu_type == hybrid_small)
4372f83d2f91SKan Liang return tnt_get_event_constraints(cpuc, idx, event);
4373f83d2f91SKan Liang
4374f83d2f91SKan Liang WARN_ON(1);
4375f83d2f91SKan Liang return &emptyconstraint;
4376f83d2f91SKan Liang }
4377f83d2f91SKan Liang
437838aaf921SKan Liang static struct event_constraint *
cmt_get_event_constraints(struct cpu_hw_events * cpuc,int idx,struct perf_event * event)437938aaf921SKan Liang cmt_get_event_constraints(struct cpu_hw_events *cpuc, int idx,
438038aaf921SKan Liang struct perf_event *event)
438138aaf921SKan Liang {
438238aaf921SKan Liang struct event_constraint *c;
438338aaf921SKan Liang
438438aaf921SKan Liang c = intel_get_event_constraints(cpuc, idx, event);
438538aaf921SKan Liang
438638aaf921SKan Liang /*
438738aaf921SKan Liang * The :ppp indicates the Precise Distribution (PDist) facility, which
438838aaf921SKan Liang * is only supported on the GP counter 0 & 1 and Fixed counter 0.
438938aaf921SKan Liang * If a :ppp event which is not available on the above eligible counters,
439038aaf921SKan Liang * error out.
439138aaf921SKan Liang */
439238aaf921SKan Liang if (event->attr.precise_ip == 3) {
439338aaf921SKan Liang /* Force instruction:ppp on PMC0, 1 and Fixed counter 0 */
439438aaf921SKan Liang if (constraint_match(&fixed0_constraint, event->hw.config))
439538aaf921SKan Liang return &fixed0_counter0_1_constraint;
439638aaf921SKan Liang
439738aaf921SKan Liang switch (c->idxmsk64 & 0x3ull) {
439838aaf921SKan Liang case 0x1:
439938aaf921SKan Liang return &counter0_constraint;
440038aaf921SKan Liang case 0x2:
440138aaf921SKan Liang return &counter1_constraint;
440238aaf921SKan Liang case 0x3:
440338aaf921SKan Liang return &counter0_1_constraint;
440438aaf921SKan Liang }
440538aaf921SKan Liang return &emptyconstraint;
440638aaf921SKan Liang }
440738aaf921SKan Liang
440838aaf921SKan Liang return c;
440938aaf921SKan Liang }
441038aaf921SKan Liang
441138aaf921SKan Liang static struct event_constraint *
rwc_get_event_constraints(struct cpu_hw_events * cpuc,int idx,struct perf_event * event)4412c87a3109SKan Liang rwc_get_event_constraints(struct cpu_hw_events *cpuc, int idx,
4413c87a3109SKan Liang struct perf_event *event)
4414c87a3109SKan Liang {
4415c87a3109SKan Liang struct event_constraint *c;
4416c87a3109SKan Liang
4417c87a3109SKan Liang c = spr_get_event_constraints(cpuc, idx, event);
4418c87a3109SKan Liang
4419c87a3109SKan Liang /* The Retire Latency is not supported by the fixed counter 0. */
4420c87a3109SKan Liang if (event->attr.precise_ip &&
4421c87a3109SKan Liang (event->attr.sample_type & PERF_SAMPLE_WEIGHT_TYPE) &&
4422c87a3109SKan Liang constraint_match(&fixed0_constraint, event->hw.config)) {
4423c87a3109SKan Liang /*
4424c87a3109SKan Liang * The Instruction PDIR is only available
4425c87a3109SKan Liang * on the fixed counter 0. Error out for this case.
4426c87a3109SKan Liang */
4427c87a3109SKan Liang if (event->attr.precise_ip == 3)
4428c87a3109SKan Liang return &emptyconstraint;
4429c87a3109SKan Liang return &counters_1_7_constraint;
4430c87a3109SKan Liang }
4431c87a3109SKan Liang
4432c87a3109SKan Liang return c;
4433c87a3109SKan Liang }
4434c87a3109SKan Liang
4435c87a3109SKan Liang static struct event_constraint *
mtl_get_event_constraints(struct cpu_hw_events * cpuc,int idx,struct perf_event * event)443638aaf921SKan Liang mtl_get_event_constraints(struct cpu_hw_events *cpuc, int idx,
443738aaf921SKan Liang struct perf_event *event)
443838aaf921SKan Liang {
443938aaf921SKan Liang struct x86_hybrid_pmu *pmu = hybrid_pmu(event->pmu);
444038aaf921SKan Liang
444138aaf921SKan Liang if (pmu->cpu_type == hybrid_big)
4442c87a3109SKan Liang return rwc_get_event_constraints(cpuc, idx, event);
444338aaf921SKan Liang if (pmu->cpu_type == hybrid_small)
444438aaf921SKan Liang return cmt_get_event_constraints(cpuc, idx, event);
444538aaf921SKan Liang
444638aaf921SKan Liang WARN_ON(1);
444738aaf921SKan Liang return &emptyconstraint;
444838aaf921SKan Liang }
444938aaf921SKan Liang
adl_hw_config(struct perf_event * event)4450f83d2f91SKan Liang static int adl_hw_config(struct perf_event *event)
4451f83d2f91SKan Liang {
4452f83d2f91SKan Liang struct x86_hybrid_pmu *pmu = hybrid_pmu(event->pmu);
4453f83d2f91SKan Liang
4454f83d2f91SKan Liang if (pmu->cpu_type == hybrid_big)
4455f83d2f91SKan Liang return hsw_hw_config(event);
4456f83d2f91SKan Liang else if (pmu->cpu_type == hybrid_small)
4457f83d2f91SKan Liang return intel_pmu_hw_config(event);
4458f83d2f91SKan Liang
4459f83d2f91SKan Liang WARN_ON(1);
4460f83d2f91SKan Liang return -EOPNOTSUPP;
4461f83d2f91SKan Liang }
4462f83d2f91SKan Liang
adl_get_hybrid_cpu_type(void)4463f83d2f91SKan Liang static u8 adl_get_hybrid_cpu_type(void)
4464f83d2f91SKan Liang {
4465f83d2f91SKan Liang return hybrid_big;
4466f83d2f91SKan Liang }
4467f83d2f91SKan Liang
erratum_hsw11(struct perf_event * event)44680eaf812aSKan Liang static inline bool erratum_hsw11(struct perf_event *event)
44690eaf812aSKan Liang {
44700eaf812aSKan Liang return (event->hw.config & INTEL_ARCH_EVENT_MASK) ==
44710eaf812aSKan Liang X86_CONFIG(.event=0xc0, .umask=0x01);
44720eaf812aSKan Liang }
44730eaf812aSKan Liang
44740eaf812aSKan Liang /*
44750eaf812aSKan Liang * The HSW11 requires a period larger than 100 which is the same as the BDM11.
44760eaf812aSKan Liang * A minimum period of 128 is enforced as well for the INST_RETIRED.ALL.
44770eaf812aSKan Liang *
44780eaf812aSKan Liang * The message 'interrupt took too long' can be observed on any counter which
44790eaf812aSKan Liang * was armed with a period < 32 and two events expired in the same NMI.
44800eaf812aSKan Liang * A minimum period of 32 is enforced for the rest of the events.
44810eaf812aSKan Liang */
hsw_limit_period(struct perf_event * event,s64 * left)44820eaf812aSKan Liang static void hsw_limit_period(struct perf_event *event, s64 *left)
44830eaf812aSKan Liang {
44840eaf812aSKan Liang *left = max(*left, erratum_hsw11(event) ? 128 : 32);
44850eaf812aSKan Liang }
44860eaf812aSKan Liang
4487e1069839SBorislav Petkov /*
4488e1069839SBorislav Petkov * Broadwell:
4489e1069839SBorislav Petkov *
4490e1069839SBorislav Petkov * The INST_RETIRED.ALL period always needs to have lowest 6 bits cleared
4491e1069839SBorislav Petkov * (BDM55) and it must not use a period smaller than 100 (BDM11). We combine
4492e1069839SBorislav Petkov * the two to enforce a minimum period of 128 (the smallest value that has bits
4493e1069839SBorislav Petkov * 0-5 cleared and >= 100).
4494e1069839SBorislav Petkov *
4495e1069839SBorislav Petkov * Because of how the code in x86_perf_event_set_period() works, the truncation
4496e1069839SBorislav Petkov * of the lower 6 bits is 'harmless' as we'll occasionally add a longer period
4497e1069839SBorislav Petkov * to make up for the 'lost' events due to carrying the 'error' in period_left.
4498e1069839SBorislav Petkov *
4499e1069839SBorislav Petkov * Therefore the effective (average) period matches the requested period,
4500e1069839SBorislav Petkov * despite coarser hardware granularity.
4501e1069839SBorislav Petkov */
bdw_limit_period(struct perf_event * event,s64 * left)450228f0f3c4SPeter Zijlstra static void bdw_limit_period(struct perf_event *event, s64 *left)
4503e1069839SBorislav Petkov {
45040eaf812aSKan Liang if (erratum_hsw11(event)) {
450528f0f3c4SPeter Zijlstra if (*left < 128)
450628f0f3c4SPeter Zijlstra *left = 128;
450728f0f3c4SPeter Zijlstra *left &= ~0x3fULL;
4508e1069839SBorislav Petkov }
4509e1069839SBorislav Petkov }
4510e1069839SBorislav Petkov
nhm_limit_period(struct perf_event * event,s64 * left)451128f0f3c4SPeter Zijlstra static void nhm_limit_period(struct perf_event *event, s64 *left)
451244d3bbb6SJosh Hunt {
451328f0f3c4SPeter Zijlstra *left = max(*left, 32LL);
451444d3bbb6SJosh Hunt }
451544d3bbb6SJosh Hunt
spr_limit_period(struct perf_event * event,s64 * left)451628f0f3c4SPeter Zijlstra static void spr_limit_period(struct perf_event *event, s64 *left)
451761b985e3SKan Liang {
451861b985e3SKan Liang if (event->attr.precise_ip == 3)
451928f0f3c4SPeter Zijlstra *left = max(*left, 128LL);
452061b985e3SKan Liang }
452161b985e3SKan Liang
4522e1069839SBorislav Petkov PMU_FORMAT_ATTR(event, "config:0-7" );
4523e1069839SBorislav Petkov PMU_FORMAT_ATTR(umask, "config:8-15" );
4524e1069839SBorislav Petkov PMU_FORMAT_ATTR(edge, "config:18" );
4525e1069839SBorislav Petkov PMU_FORMAT_ATTR(pc, "config:19" );
4526e1069839SBorislav Petkov PMU_FORMAT_ATTR(any, "config:21" ); /* v3 + */
4527e1069839SBorislav Petkov PMU_FORMAT_ATTR(inv, "config:23" );
4528e1069839SBorislav Petkov PMU_FORMAT_ATTR(cmask, "config:24-31" );
4529e1069839SBorislav Petkov PMU_FORMAT_ATTR(in_tx, "config:32");
4530e1069839SBorislav Petkov PMU_FORMAT_ATTR(in_tx_cp, "config:33");
4531e1069839SBorislav Petkov
4532e1069839SBorislav Petkov static struct attribute *intel_arch_formats_attr[] = {
4533e1069839SBorislav Petkov &format_attr_event.attr,
4534e1069839SBorislav Petkov &format_attr_umask.attr,
4535e1069839SBorislav Petkov &format_attr_edge.attr,
4536e1069839SBorislav Petkov &format_attr_pc.attr,
4537e1069839SBorislav Petkov &format_attr_inv.attr,
4538e1069839SBorislav Petkov &format_attr_cmask.attr,
4539e1069839SBorislav Petkov NULL,
4540e1069839SBorislav Petkov };
4541e1069839SBorislav Petkov
intel_event_sysfs_show(char * page,u64 config)4542e1069839SBorislav Petkov ssize_t intel_event_sysfs_show(char *page, u64 config)
4543e1069839SBorislav Petkov {
4544e1069839SBorislav Petkov u64 event = (config & ARCH_PERFMON_EVENTSEL_EVENT);
4545e1069839SBorislav Petkov
4546e1069839SBorislav Petkov return x86_event_sysfs_show(page, config, event);
4547e1069839SBorislav Petkov }
4548e1069839SBorislav Petkov
allocate_shared_regs(int cpu)4549d01b1f96SPeter Zijlstra (Intel) static struct intel_shared_regs *allocate_shared_regs(int cpu)
4550e1069839SBorislav Petkov {
4551e1069839SBorislav Petkov struct intel_shared_regs *regs;
4552e1069839SBorislav Petkov int i;
4553e1069839SBorislav Petkov
4554e1069839SBorislav Petkov regs = kzalloc_node(sizeof(struct intel_shared_regs),
4555e1069839SBorislav Petkov GFP_KERNEL, cpu_to_node(cpu));
4556e1069839SBorislav Petkov if (regs) {
4557e1069839SBorislav Petkov /*
4558e1069839SBorislav Petkov * initialize the locks to keep lockdep happy
4559e1069839SBorislav Petkov */
4560e1069839SBorislav Petkov for (i = 0; i < EXTRA_REG_MAX; i++)
4561e1069839SBorislav Petkov raw_spin_lock_init(®s->regs[i].lock);
4562e1069839SBorislav Petkov
4563e1069839SBorislav Petkov regs->core_id = -1;
4564e1069839SBorislav Petkov }
4565e1069839SBorislav Petkov return regs;
4566e1069839SBorislav Petkov }
4567e1069839SBorislav Petkov
allocate_excl_cntrs(int cpu)4568e1069839SBorislav Petkov static struct intel_excl_cntrs *allocate_excl_cntrs(int cpu)
4569e1069839SBorislav Petkov {
4570e1069839SBorislav Petkov struct intel_excl_cntrs *c;
4571e1069839SBorislav Petkov
4572e1069839SBorislav Petkov c = kzalloc_node(sizeof(struct intel_excl_cntrs),
4573e1069839SBorislav Petkov GFP_KERNEL, cpu_to_node(cpu));
4574e1069839SBorislav Petkov if (c) {
4575e1069839SBorislav Petkov raw_spin_lock_init(&c->lock);
4576e1069839SBorislav Petkov c->core_id = -1;
4577e1069839SBorislav Petkov }
4578e1069839SBorislav Petkov return c;
4579e1069839SBorislav Petkov }
4580e1069839SBorislav Petkov
4581e1069839SBorislav Petkov
intel_cpuc_prepare(struct cpu_hw_events * cpuc,int cpu)4582d01b1f96SPeter Zijlstra (Intel) int intel_cpuc_prepare(struct cpu_hw_events *cpuc, int cpu)
4583d01b1f96SPeter Zijlstra (Intel) {
4584c22497f5SKan Liang cpuc->pebs_record_size = x86_pmu.pebs_record_size;
4585c22497f5SKan Liang
4586183af736SKan Liang if (is_hybrid() || x86_pmu.extra_regs || x86_pmu.lbr_sel_map) {
4587e1069839SBorislav Petkov cpuc->shared_regs = allocate_shared_regs(cpu);
4588e1069839SBorislav Petkov if (!cpuc->shared_regs)
4589e1069839SBorislav Petkov goto err;
4590e1069839SBorislav Petkov }
4591e1069839SBorislav Petkov
4592400816f6SPeter Zijlstra (Intel) if (x86_pmu.flags & (PMU_FL_EXCL_CNTRS | PMU_FL_TFA)) {
4593e1069839SBorislav Petkov size_t sz = X86_PMC_IDX_MAX * sizeof(struct event_constraint);
4594e1069839SBorislav Petkov
4595d01b1f96SPeter Zijlstra (Intel) cpuc->constraint_list = kzalloc_node(sz, GFP_KERNEL, cpu_to_node(cpu));
4596e1069839SBorislav Petkov if (!cpuc->constraint_list)
4597e1069839SBorislav Petkov goto err_shared_regs;
4598400816f6SPeter Zijlstra (Intel) }
4599e1069839SBorislav Petkov
4600400816f6SPeter Zijlstra (Intel) if (x86_pmu.flags & PMU_FL_EXCL_CNTRS) {
4601e1069839SBorislav Petkov cpuc->excl_cntrs = allocate_excl_cntrs(cpu);
4602e1069839SBorislav Petkov if (!cpuc->excl_cntrs)
4603e1069839SBorislav Petkov goto err_constraint_list;
4604e1069839SBorislav Petkov
4605e1069839SBorislav Petkov cpuc->excl_thread_id = 0;
4606e1069839SBorislav Petkov }
4607e1069839SBorislav Petkov
460895ca792cSThomas Gleixner return 0;
4609e1069839SBorislav Petkov
4610e1069839SBorislav Petkov err_constraint_list:
4611e1069839SBorislav Petkov kfree(cpuc->constraint_list);
4612e1069839SBorislav Petkov cpuc->constraint_list = NULL;
4613e1069839SBorislav Petkov
4614e1069839SBorislav Petkov err_shared_regs:
4615e1069839SBorislav Petkov kfree(cpuc->shared_regs);
4616e1069839SBorislav Petkov cpuc->shared_regs = NULL;
4617e1069839SBorislav Petkov
4618e1069839SBorislav Petkov err:
461995ca792cSThomas Gleixner return -ENOMEM;
4620e1069839SBorislav Petkov }
4621e1069839SBorislav Petkov
intel_pmu_cpu_prepare(int cpu)4622d01b1f96SPeter Zijlstra (Intel) static int intel_pmu_cpu_prepare(int cpu)
4623d01b1f96SPeter Zijlstra (Intel) {
4624d01b1f96SPeter Zijlstra (Intel) return intel_cpuc_prepare(&per_cpu(cpu_hw_events, cpu), cpu);
4625d01b1f96SPeter Zijlstra (Intel) }
4626d01b1f96SPeter Zijlstra (Intel)
flip_smm_bit(void * data)46276089327fSKan Liang static void flip_smm_bit(void *data)
46286089327fSKan Liang {
46296089327fSKan Liang unsigned long set = *(unsigned long *)data;
46306089327fSKan Liang
46316089327fSKan Liang if (set > 0) {
46326089327fSKan Liang msr_set_bit(MSR_IA32_DEBUGCTLMSR,
46336089327fSKan Liang DEBUGCTLMSR_FREEZE_IN_SMM_BIT);
46346089327fSKan Liang } else {
46356089327fSKan Liang msr_clear_bit(MSR_IA32_DEBUGCTLMSR,
46366089327fSKan Liang DEBUGCTLMSR_FREEZE_IN_SMM_BIT);
46376089327fSKan Liang }
46386089327fSKan Liang }
46396089327fSKan Liang
4640eb467aaaSKan Liang static void intel_pmu_check_num_counters(int *num_counters,
4641eb467aaaSKan Liang int *num_counters_fixed,
4642eb467aaaSKan Liang u64 *intel_ctrl, u64 fixed_mask);
4643eb467aaaSKan Liang
update_pmu_cap(struct x86_hybrid_pmu * pmu)4644eb467aaaSKan Liang static void update_pmu_cap(struct x86_hybrid_pmu *pmu)
4645eb467aaaSKan Liang {
4646eb467aaaSKan Liang unsigned int sub_bitmaps = cpuid_eax(ARCH_PERFMON_EXT_LEAF);
4647eb467aaaSKan Liang unsigned int eax, ebx, ecx, edx;
4648eb467aaaSKan Liang
4649eb467aaaSKan Liang if (sub_bitmaps & ARCH_PERFMON_NUM_COUNTER_LEAF_BIT) {
4650eb467aaaSKan Liang cpuid_count(ARCH_PERFMON_EXT_LEAF, ARCH_PERFMON_NUM_COUNTER_LEAF,
4651eb467aaaSKan Liang &eax, &ebx, &ecx, &edx);
4652eb467aaaSKan Liang pmu->num_counters = fls(eax);
4653eb467aaaSKan Liang pmu->num_counters_fixed = fls(ebx);
4654eb467aaaSKan Liang intel_pmu_check_num_counters(&pmu->num_counters, &pmu->num_counters_fixed,
4655eb467aaaSKan Liang &pmu->intel_ctrl, ebx);
4656eb467aaaSKan Liang }
4657eb467aaaSKan Liang }
4658eb467aaaSKan Liang
init_hybrid_pmu(int cpu)4659d9977c43SKan Liang static bool init_hybrid_pmu(int cpu)
4660d9977c43SKan Liang {
4661d9977c43SKan Liang struct cpu_hw_events *cpuc = &per_cpu(cpu_hw_events, cpu);
4662d9977c43SKan Liang u8 cpu_type = get_this_hybrid_cpu_type();
4663d9977c43SKan Liang struct x86_hybrid_pmu *pmu = NULL;
4664d9977c43SKan Liang int i;
4665d9977c43SKan Liang
4666d9977c43SKan Liang if (!cpu_type && x86_pmu.get_hybrid_cpu_type)
4667d9977c43SKan Liang cpu_type = x86_pmu.get_hybrid_cpu_type();
4668d9977c43SKan Liang
4669d9977c43SKan Liang for (i = 0; i < x86_pmu.num_hybrid_pmus; i++) {
4670d9977c43SKan Liang if (x86_pmu.hybrid_pmu[i].cpu_type == cpu_type) {
4671d9977c43SKan Liang pmu = &x86_pmu.hybrid_pmu[i];
4672d9977c43SKan Liang break;
4673d9977c43SKan Liang }
4674d9977c43SKan Liang }
4675d9977c43SKan Liang if (WARN_ON_ONCE(!pmu || (pmu->pmu.type == -1))) {
4676d9977c43SKan Liang cpuc->pmu = NULL;
4677d9977c43SKan Liang return false;
4678d9977c43SKan Liang }
4679d9977c43SKan Liang
4680d9977c43SKan Liang /* Only check and dump the PMU information for the first CPU */
4681d9977c43SKan Liang if (!cpumask_empty(&pmu->supported_cpus))
4682d9977c43SKan Liang goto end;
4683d9977c43SKan Liang
4684eb467aaaSKan Liang if (this_cpu_has(X86_FEATURE_ARCH_PERFMON_EXT))
4685eb467aaaSKan Liang update_pmu_cap(pmu);
4686eb467aaaSKan Liang
4687d9977c43SKan Liang if (!check_hw_exists(&pmu->pmu, pmu->num_counters, pmu->num_counters_fixed))
4688d9977c43SKan Liang return false;
4689d9977c43SKan Liang
4690d9977c43SKan Liang pr_info("%s PMU driver: ", pmu->name);
4691d9977c43SKan Liang
4692d9977c43SKan Liang if (pmu->intel_cap.pebs_output_pt_available)
4693d9977c43SKan Liang pr_cont("PEBS-via-PT ");
4694d9977c43SKan Liang
4695d9977c43SKan Liang pr_cont("\n");
4696d9977c43SKan Liang
4697d9977c43SKan Liang x86_pmu_show_pmu_cap(pmu->num_counters, pmu->num_counters_fixed,
4698d9977c43SKan Liang pmu->intel_ctrl);
4699d9977c43SKan Liang
4700d9977c43SKan Liang end:
4701d9977c43SKan Liang cpumask_set_cpu(cpu, &pmu->supported_cpus);
4702d9977c43SKan Liang cpuc->pmu = &pmu->pmu;
4703d9977c43SKan Liang
4704d9977c43SKan Liang return true;
4705d9977c43SKan Liang }
4706d9977c43SKan Liang
intel_pmu_cpu_starting(int cpu)4707e1069839SBorislav Petkov static void intel_pmu_cpu_starting(int cpu)
4708e1069839SBorislav Petkov {
4709e1069839SBorislav Petkov struct cpu_hw_events *cpuc = &per_cpu(cpu_hw_events, cpu);
4710e1069839SBorislav Petkov int core_id = topology_core_id(cpu);
4711e1069839SBorislav Petkov int i;
4712e1069839SBorislav Petkov
4713d9977c43SKan Liang if (is_hybrid() && !init_hybrid_pmu(cpu))
4714d9977c43SKan Liang return;
4715d9977c43SKan Liang
4716e1069839SBorislav Petkov init_debug_store_on_cpu(cpu);
4717e1069839SBorislav Petkov /*
4718e1069839SBorislav Petkov * Deal with CPUs that don't clear their LBRs on power-up.
4719e1069839SBorislav Petkov */
4720e1069839SBorislav Petkov intel_pmu_lbr_reset();
4721e1069839SBorislav Petkov
4722e1069839SBorislav Petkov cpuc->lbr_sel = NULL;
4723e1069839SBorislav Petkov
4724d7262457SPeter Zijlstra if (x86_pmu.flags & PMU_FL_TFA) {
4725d7262457SPeter Zijlstra WARN_ON_ONCE(cpuc->tfa_shadow);
4726d7262457SPeter Zijlstra cpuc->tfa_shadow = ~0ULL;
4727d7262457SPeter Zijlstra intel_set_tfa(cpuc, false);
4728d7262457SPeter Zijlstra }
4729d7262457SPeter Zijlstra
47304e949e9bSKan Liang if (x86_pmu.version > 1)
47316089327fSKan Liang flip_smm_bit(&x86_pmu.attr_freeze_on_smi);
47326089327fSKan Liang
4733d0946a88SKan Liang /*
4734d0946a88SKan Liang * Disable perf metrics if any added CPU doesn't support it.
4735d0946a88SKan Liang *
4736d0946a88SKan Liang * Turn off the check for a hybrid architecture, because the
4737d0946a88SKan Liang * architecture MSR, MSR_IA32_PERF_CAPABILITIES, only indicate
4738d0946a88SKan Liang * the architecture features. The perf metrics is a model-specific
4739d0946a88SKan Liang * feature for now. The corresponding bit should always be 0 on
4740d0946a88SKan Liang * a hybrid platform, e.g., Alder Lake.
4741d0946a88SKan Liang */
4742d0946a88SKan Liang if (!is_hybrid() && x86_pmu.intel_cap.perf_metrics) {
474380a5ce11SKan Liang union perf_capabilities perf_cap;
474480a5ce11SKan Liang
474580a5ce11SKan Liang rdmsrl(MSR_IA32_PERF_CAPABILITIES, perf_cap.capabilities);
474680a5ce11SKan Liang if (!perf_cap.perf_metrics) {
474780a5ce11SKan Liang x86_pmu.intel_cap.perf_metrics = 0;
474880a5ce11SKan Liang x86_pmu.intel_ctrl &= ~(1ULL << GLOBAL_CTRL_EN_PERF_METRICS);
474980a5ce11SKan Liang }
475080a5ce11SKan Liang }
475180a5ce11SKan Liang
4752e1069839SBorislav Petkov if (!cpuc->shared_regs)
4753e1069839SBorislav Petkov return;
4754e1069839SBorislav Petkov
4755e1069839SBorislav Petkov if (!(x86_pmu.flags & PMU_FL_NO_HT_SHARING)) {
4756e1069839SBorislav Petkov for_each_cpu(i, topology_sibling_cpumask(cpu)) {
4757e1069839SBorislav Petkov struct intel_shared_regs *pc;
4758e1069839SBorislav Petkov
4759e1069839SBorislav Petkov pc = per_cpu(cpu_hw_events, i).shared_regs;
4760e1069839SBorislav Petkov if (pc && pc->core_id == core_id) {
4761e1069839SBorislav Petkov cpuc->kfree_on_online[0] = cpuc->shared_regs;
4762e1069839SBorislav Petkov cpuc->shared_regs = pc;
4763e1069839SBorislav Petkov break;
4764e1069839SBorislav Petkov }
4765e1069839SBorislav Petkov }
4766e1069839SBorislav Petkov cpuc->shared_regs->core_id = core_id;
4767e1069839SBorislav Petkov cpuc->shared_regs->refcnt++;
4768e1069839SBorislav Petkov }
4769e1069839SBorislav Petkov
4770e1069839SBorislav Petkov if (x86_pmu.lbr_sel_map)
4771e1069839SBorislav Petkov cpuc->lbr_sel = &cpuc->shared_regs->regs[EXTRA_REG_LBR];
4772e1069839SBorislav Petkov
4773e1069839SBorislav Petkov if (x86_pmu.flags & PMU_FL_EXCL_CNTRS) {
4774e1069839SBorislav Petkov for_each_cpu(i, topology_sibling_cpumask(cpu)) {
47754e71de79SZhou Chengming struct cpu_hw_events *sibling;
4776e1069839SBorislav Petkov struct intel_excl_cntrs *c;
4777e1069839SBorislav Petkov
47784e71de79SZhou Chengming sibling = &per_cpu(cpu_hw_events, i);
47794e71de79SZhou Chengming c = sibling->excl_cntrs;
4780e1069839SBorislav Petkov if (c && c->core_id == core_id) {
4781e1069839SBorislav Petkov cpuc->kfree_on_online[1] = cpuc->excl_cntrs;
4782e1069839SBorislav Petkov cpuc->excl_cntrs = c;
47834e71de79SZhou Chengming if (!sibling->excl_thread_id)
4784e1069839SBorislav Petkov cpuc->excl_thread_id = 1;
4785e1069839SBorislav Petkov break;
4786e1069839SBorislav Petkov }
4787e1069839SBorislav Petkov }
4788e1069839SBorislav Petkov cpuc->excl_cntrs->core_id = core_id;
4789e1069839SBorislav Petkov cpuc->excl_cntrs->refcnt++;
4790e1069839SBorislav Petkov }
4791e1069839SBorislav Petkov }
4792e1069839SBorislav Petkov
free_excl_cntrs(struct cpu_hw_events * cpuc)4793d01b1f96SPeter Zijlstra (Intel) static void free_excl_cntrs(struct cpu_hw_events *cpuc)
4794e1069839SBorislav Petkov {
4795e1069839SBorislav Petkov struct intel_excl_cntrs *c;
4796e1069839SBorislav Petkov
4797e1069839SBorislav Petkov c = cpuc->excl_cntrs;
4798e1069839SBorislav Petkov if (c) {
4799e1069839SBorislav Petkov if (c->core_id == -1 || --c->refcnt == 0)
4800e1069839SBorislav Petkov kfree(c);
4801e1069839SBorislav Petkov cpuc->excl_cntrs = NULL;
4802400816f6SPeter Zijlstra (Intel) }
4803400816f6SPeter Zijlstra (Intel)
4804e1069839SBorislav Petkov kfree(cpuc->constraint_list);
4805e1069839SBorislav Petkov cpuc->constraint_list = NULL;
4806e1069839SBorislav Petkov }
4807e1069839SBorislav Petkov
intel_pmu_cpu_dying(int cpu)4808e1069839SBorislav Petkov static void intel_pmu_cpu_dying(int cpu)
4809e1069839SBorislav Petkov {
4810602cae04SPeter Zijlstra fini_debug_store_on_cpu(cpu);
4811602cae04SPeter Zijlstra }
4812602cae04SPeter Zijlstra
intel_cpuc_finish(struct cpu_hw_events * cpuc)4813d01b1f96SPeter Zijlstra (Intel) void intel_cpuc_finish(struct cpu_hw_events *cpuc)
4814602cae04SPeter Zijlstra {
4815e1069839SBorislav Petkov struct intel_shared_regs *pc;
4816e1069839SBorislav Petkov
4817e1069839SBorislav Petkov pc = cpuc->shared_regs;
4818e1069839SBorislav Petkov if (pc) {
4819e1069839SBorislav Petkov if (pc->core_id == -1 || --pc->refcnt == 0)
4820e1069839SBorislav Petkov kfree(pc);
4821e1069839SBorislav Petkov cpuc->shared_regs = NULL;
4822e1069839SBorislav Petkov }
4823e1069839SBorislav Petkov
4824d01b1f96SPeter Zijlstra (Intel) free_excl_cntrs(cpuc);
4825d01b1f96SPeter Zijlstra (Intel) }
4826d01b1f96SPeter Zijlstra (Intel)
intel_pmu_cpu_dead(int cpu)4827d01b1f96SPeter Zijlstra (Intel) static void intel_pmu_cpu_dead(int cpu)
4828d01b1f96SPeter Zijlstra (Intel) {
4829d9977c43SKan Liang struct cpu_hw_events *cpuc = &per_cpu(cpu_hw_events, cpu);
4830d9977c43SKan Liang
4831d9977c43SKan Liang intel_cpuc_finish(cpuc);
4832d9977c43SKan Liang
4833d9977c43SKan Liang if (is_hybrid() && cpuc->pmu)
4834d9977c43SKan Liang cpumask_clear_cpu(cpu, &hybrid_pmu(cpuc->pmu)->supported_cpus);
4835e1069839SBorislav Petkov }
4836e1069839SBorislav Petkov
intel_pmu_sched_task(struct perf_event_pmu_context * pmu_ctx,bool sched_in)4837bd275681SPeter Zijlstra static void intel_pmu_sched_task(struct perf_event_pmu_context *pmu_ctx,
4838e1069839SBorislav Petkov bool sched_in)
4839e1069839SBorislav Petkov {
4840bd275681SPeter Zijlstra intel_pmu_pebs_sched_task(pmu_ctx, sched_in);
4841bd275681SPeter Zijlstra intel_pmu_lbr_sched_task(pmu_ctx, sched_in);
4842e1069839SBorislav Petkov }
4843e1069839SBorislav Petkov
intel_pmu_swap_task_ctx(struct perf_event_pmu_context * prev_epc,struct perf_event_pmu_context * next_epc)4844bd275681SPeter Zijlstra static void intel_pmu_swap_task_ctx(struct perf_event_pmu_context *prev_epc,
4845bd275681SPeter Zijlstra struct perf_event_pmu_context *next_epc)
4846c2b98a86SAlexey Budankov {
4847bd275681SPeter Zijlstra intel_pmu_lbr_swap_task_ctx(prev_epc, next_epc);
4848c2b98a86SAlexey Budankov }
4849c2b98a86SAlexey Budankov
intel_pmu_check_period(struct perf_event * event,u64 value)485081ec3f3cSJiri Olsa static int intel_pmu_check_period(struct perf_event *event, u64 value)
485181ec3f3cSJiri Olsa {
485281ec3f3cSJiri Olsa return intel_pmu_has_bts_period(event, value) ? -EINVAL : 0;
485381ec3f3cSJiri Olsa }
485481ec3f3cSJiri Olsa
intel_aux_output_init(void)48558b8ff8ccSAdrian Hunter static void intel_aux_output_init(void)
48568b8ff8ccSAdrian Hunter {
48578b8ff8ccSAdrian Hunter /* Refer also intel_pmu_aux_output_match() */
48588b8ff8ccSAdrian Hunter if (x86_pmu.intel_cap.pebs_output_pt_available)
48598b8ff8ccSAdrian Hunter x86_pmu.assign = intel_pmu_assign_event;
48608b8ff8ccSAdrian Hunter }
48618b8ff8ccSAdrian Hunter
intel_pmu_aux_output_match(struct perf_event * event)486242880f72SAlexander Shishkin static int intel_pmu_aux_output_match(struct perf_event *event)
486342880f72SAlexander Shishkin {
48648b8ff8ccSAdrian Hunter /* intel_pmu_assign_event() is needed, refer intel_aux_output_init() */
486542880f72SAlexander Shishkin if (!x86_pmu.intel_cap.pebs_output_pt_available)
486642880f72SAlexander Shishkin return 0;
486742880f72SAlexander Shishkin
486842880f72SAlexander Shishkin return is_intel_pt_event(event);
486942880f72SAlexander Shishkin }
487042880f72SAlexander Shishkin
intel_pmu_filter(struct pmu * pmu,int cpu,bool * ret)4871bd275681SPeter Zijlstra static void intel_pmu_filter(struct pmu *pmu, int cpu, bool *ret)
4872f83d2f91SKan Liang {
4873bd275681SPeter Zijlstra struct x86_hybrid_pmu *hpmu = hybrid_pmu(pmu);
4874f83d2f91SKan Liang
4875bd275681SPeter Zijlstra *ret = !cpumask_test_cpu(cpu, &hpmu->supported_cpus);
4876f83d2f91SKan Liang }
4877f83d2f91SKan Liang
4878e1069839SBorislav Petkov PMU_FORMAT_ATTR(offcore_rsp, "config1:0-63");
4879e1069839SBorislav Petkov
4880e1069839SBorislav Petkov PMU_FORMAT_ATTR(ldlat, "config1:0-15");
4881e1069839SBorislav Petkov
4882e1069839SBorislav Petkov PMU_FORMAT_ATTR(frontend, "config1:0-23");
4883e1069839SBorislav Petkov
4884a430021fSKan Liang PMU_FORMAT_ATTR(snoop_rsp, "config1:0-63");
4885a430021fSKan Liang
4886e1069839SBorislav Petkov static struct attribute *intel_arch3_formats_attr[] = {
4887e1069839SBorislav Petkov &format_attr_event.attr,
4888e1069839SBorislav Petkov &format_attr_umask.attr,
4889e1069839SBorislav Petkov &format_attr_edge.attr,
4890e1069839SBorislav Petkov &format_attr_pc.attr,
4891e1069839SBorislav Petkov &format_attr_any.attr,
4892e1069839SBorislav Petkov &format_attr_inv.attr,
4893e1069839SBorislav Petkov &format_attr_cmask.attr,
4894a5df70c3SAndi Kleen NULL,
4895a5df70c3SAndi Kleen };
4896a5df70c3SAndi Kleen
4897a5df70c3SAndi Kleen static struct attribute *hsw_format_attr[] = {
4898e1069839SBorislav Petkov &format_attr_in_tx.attr,
4899e1069839SBorislav Petkov &format_attr_in_tx_cp.attr,
4900a5df70c3SAndi Kleen &format_attr_offcore_rsp.attr,
4901a5df70c3SAndi Kleen &format_attr_ldlat.attr,
4902a5df70c3SAndi Kleen NULL
4903a5df70c3SAndi Kleen };
4904e1069839SBorislav Petkov
4905a5df70c3SAndi Kleen static struct attribute *nhm_format_attr[] = {
4906a5df70c3SAndi Kleen &format_attr_offcore_rsp.attr,
4907a5df70c3SAndi Kleen &format_attr_ldlat.attr,
4908a5df70c3SAndi Kleen NULL
4909a5df70c3SAndi Kleen };
4910a5df70c3SAndi Kleen
4911a5df70c3SAndi Kleen static struct attribute *slm_format_attr[] = {
4912a5df70c3SAndi Kleen &format_attr_offcore_rsp.attr,
4913a5df70c3SAndi Kleen NULL
4914e1069839SBorislav Petkov };
4915e1069839SBorislav Petkov
4916a430021fSKan Liang static struct attribute *cmt_format_attr[] = {
4917a430021fSKan Liang &format_attr_offcore_rsp.attr,
4918a430021fSKan Liang &format_attr_ldlat.attr,
4919a430021fSKan Liang &format_attr_snoop_rsp.attr,
4920a430021fSKan Liang NULL
4921a430021fSKan Liang };
4922a430021fSKan Liang
4923e1069839SBorislav Petkov static struct attribute *skl_format_attr[] = {
4924e1069839SBorislav Petkov &format_attr_frontend.attr,
4925e1069839SBorislav Petkov NULL,
4926e1069839SBorislav Petkov };
4927e1069839SBorislav Petkov
4928e1069839SBorislav Petkov static __initconst const struct x86_pmu core_pmu = {
4929e1069839SBorislav Petkov .name = "core",
4930e1069839SBorislav Petkov .handle_irq = x86_pmu_handle_irq,
4931e1069839SBorislav Petkov .disable_all = x86_pmu_disable_all,
4932e1069839SBorislav Petkov .enable_all = core_pmu_enable_all,
4933e1069839SBorislav Petkov .enable = core_pmu_enable_event,
4934e1069839SBorislav Petkov .disable = x86_pmu_disable_event,
4935ed6101bbSJiri Olsa .hw_config = core_pmu_hw_config,
4936e1069839SBorislav Petkov .schedule_events = x86_schedule_events,
4937e1069839SBorislav Petkov .eventsel = MSR_ARCH_PERFMON_EVENTSEL0,
4938e1069839SBorislav Petkov .perfctr = MSR_ARCH_PERFMON_PERFCTR0,
4939e1069839SBorislav Petkov .event_map = intel_pmu_event_map,
4940e1069839SBorislav Petkov .max_events = ARRAY_SIZE(intel_perfmon_event_map),
4941e1069839SBorislav Petkov .apic = 1,
4942174afc3eSKan Liang .large_pebs_flags = LARGE_PEBS_FLAGS,
4943e1069839SBorislav Petkov
4944e1069839SBorislav Petkov /*
4945e1069839SBorislav Petkov * Intel PMCs cannot be accessed sanely above 32-bit width,
4946e1069839SBorislav Petkov * so we install an artificial 1<<31 period regardless of
4947e1069839SBorislav Petkov * the generic event period:
4948e1069839SBorislav Petkov */
4949e1069839SBorislav Petkov .max_period = (1ULL<<31) - 1,
4950e1069839SBorislav Petkov .get_event_constraints = intel_get_event_constraints,
4951e1069839SBorislav Petkov .put_event_constraints = intel_put_event_constraints,
4952e1069839SBorislav Petkov .event_constraints = intel_core_event_constraints,
4953e1069839SBorislav Petkov .guest_get_msrs = core_guest_get_msrs,
4954e1069839SBorislav Petkov .format_attrs = intel_arch_formats_attr,
4955e1069839SBorislav Petkov .events_sysfs_show = intel_event_sysfs_show,
4956e1069839SBorislav Petkov
4957e1069839SBorislav Petkov /*
4958e1069839SBorislav Petkov * Virtual (or funny metal) CPU can define x86_pmu.extra_regs
4959e1069839SBorislav Petkov * together with PMU version 1 and thus be using core_pmu with
4960e1069839SBorislav Petkov * shared_regs. We need following callbacks here to allocate
4961e1069839SBorislav Petkov * it properly.
4962e1069839SBorislav Petkov */
4963e1069839SBorislav Petkov .cpu_prepare = intel_pmu_cpu_prepare,
4964e1069839SBorislav Petkov .cpu_starting = intel_pmu_cpu_starting,
4965e1069839SBorislav Petkov .cpu_dying = intel_pmu_cpu_dying,
4966602cae04SPeter Zijlstra .cpu_dead = intel_pmu_cpu_dead,
496781ec3f3cSJiri Olsa
496881ec3f3cSJiri Olsa .check_period = intel_pmu_check_period,
49699f354a72SKan Liang
49709f354a72SKan Liang .lbr_reset = intel_pmu_lbr_reset_64,
4971c301b1d8SKan Liang .lbr_read = intel_pmu_lbr_read_64,
4972799571bfSKan Liang .lbr_save = intel_pmu_lbr_save,
4973799571bfSKan Liang .lbr_restore = intel_pmu_lbr_restore,
4974e1069839SBorislav Petkov };
4975e1069839SBorislav Petkov
4976e1069839SBorislav Petkov static __initconst const struct x86_pmu intel_pmu = {
4977e1069839SBorislav Petkov .name = "Intel",
4978e1069839SBorislav Petkov .handle_irq = intel_pmu_handle_irq,
4979e1069839SBorislav Petkov .disable_all = intel_pmu_disable_all,
4980e1069839SBorislav Petkov .enable_all = intel_pmu_enable_all,
4981e1069839SBorislav Petkov .enable = intel_pmu_enable_event,
4982e1069839SBorislav Petkov .disable = intel_pmu_disable_event,
498368f7082fSPeter Zijlstra .add = intel_pmu_add_event,
498468f7082fSPeter Zijlstra .del = intel_pmu_del_event,
4985ceb90d9eSKan Liang .read = intel_pmu_read_event,
4986e577bb17SPeter Zijlstra .set_period = intel_pmu_set_period,
4987e577bb17SPeter Zijlstra .update = intel_pmu_update,
4988e1069839SBorislav Petkov .hw_config = intel_pmu_hw_config,
4989e1069839SBorislav Petkov .schedule_events = x86_schedule_events,
4990e1069839SBorislav Petkov .eventsel = MSR_ARCH_PERFMON_EVENTSEL0,
4991e1069839SBorislav Petkov .perfctr = MSR_ARCH_PERFMON_PERFCTR0,
4992e1069839SBorislav Petkov .event_map = intel_pmu_event_map,
4993e1069839SBorislav Petkov .max_events = ARRAY_SIZE(intel_perfmon_event_map),
4994e1069839SBorislav Petkov .apic = 1,
4995174afc3eSKan Liang .large_pebs_flags = LARGE_PEBS_FLAGS,
4996e1069839SBorislav Petkov /*
4997e1069839SBorislav Petkov * Intel PMCs cannot be accessed sanely above 32 bit width,
4998e1069839SBorislav Petkov * so we install an artificial 1<<31 period regardless of
4999e1069839SBorislav Petkov * the generic event period:
5000e1069839SBorislav Petkov */
5001e1069839SBorislav Petkov .max_period = (1ULL << 31) - 1,
5002e1069839SBorislav Petkov .get_event_constraints = intel_get_event_constraints,
5003e1069839SBorislav Petkov .put_event_constraints = intel_put_event_constraints,
5004e1069839SBorislav Petkov .pebs_aliases = intel_pebs_aliases_core2,
5005e1069839SBorislav Petkov
5006e1069839SBorislav Petkov .format_attrs = intel_arch3_formats_attr,
5007e1069839SBorislav Petkov .events_sysfs_show = intel_event_sysfs_show,
5008e1069839SBorislav Petkov
5009e1069839SBorislav Petkov .cpu_prepare = intel_pmu_cpu_prepare,
5010e1069839SBorislav Petkov .cpu_starting = intel_pmu_cpu_starting,
5011e1069839SBorislav Petkov .cpu_dying = intel_pmu_cpu_dying,
5012602cae04SPeter Zijlstra .cpu_dead = intel_pmu_cpu_dead,
5013602cae04SPeter Zijlstra
5014e1069839SBorislav Petkov .guest_get_msrs = intel_guest_get_msrs,
5015e1069839SBorislav Petkov .sched_task = intel_pmu_sched_task,
5016c2b98a86SAlexey Budankov .swap_task_ctx = intel_pmu_swap_task_ctx,
501781ec3f3cSJiri Olsa
501881ec3f3cSJiri Olsa .check_period = intel_pmu_check_period,
501942880f72SAlexander Shishkin
502042880f72SAlexander Shishkin .aux_output_match = intel_pmu_aux_output_match,
50219f354a72SKan Liang
50229f354a72SKan Liang .lbr_reset = intel_pmu_lbr_reset_64,
5023c301b1d8SKan Liang .lbr_read = intel_pmu_lbr_read_64,
5024799571bfSKan Liang .lbr_save = intel_pmu_lbr_save,
5025799571bfSKan Liang .lbr_restore = intel_pmu_lbr_restore,
5026a01994f5SPeter Zijlstra
5027a01994f5SPeter Zijlstra /*
5028a01994f5SPeter Zijlstra * SMM has access to all 4 rings and while traditionally SMM code only
5029a01994f5SPeter Zijlstra * ran in CPL0, 2021-era firmware is starting to make use of CPL3 in SMM.
5030a01994f5SPeter Zijlstra *
5031a01994f5SPeter Zijlstra * Since the EVENTSEL.{USR,OS} CPL filtering makes no distinction
5032a01994f5SPeter Zijlstra * between SMM or not, this results in what should be pure userspace
5033a01994f5SPeter Zijlstra * counters including SMM data.
5034a01994f5SPeter Zijlstra *
5035a01994f5SPeter Zijlstra * This is a clear privilege issue, therefore globally disable
5036a01994f5SPeter Zijlstra * counting SMM by default.
5037a01994f5SPeter Zijlstra */
5038a01994f5SPeter Zijlstra .attr_freeze_on_smi = 1,
5039e1069839SBorislav Petkov };
5040e1069839SBorislav Petkov
intel_clovertown_quirk(void)5041e1069839SBorislav Petkov static __init void intel_clovertown_quirk(void)
5042e1069839SBorislav Petkov {
5043e1069839SBorislav Petkov /*
5044e1069839SBorislav Petkov * PEBS is unreliable due to:
5045e1069839SBorislav Petkov *
5046e1069839SBorislav Petkov * AJ67 - PEBS may experience CPL leaks
5047e1069839SBorislav Petkov * AJ68 - PEBS PMI may be delayed by one event
5048e1069839SBorislav Petkov * AJ69 - GLOBAL_STATUS[62] will only be set when DEBUGCTL[12]
5049e1069839SBorislav Petkov * AJ106 - FREEZE_LBRS_ON_PMI doesn't work in combination with PEBS
5050e1069839SBorislav Petkov *
5051e1069839SBorislav Petkov * AJ67 could be worked around by restricting the OS/USR flags.
5052e1069839SBorislav Petkov * AJ69 could be worked around by setting PMU_FREEZE_ON_PMI.
5053e1069839SBorislav Petkov *
5054e1069839SBorislav Petkov * AJ106 could possibly be worked around by not allowing LBR
5055e1069839SBorislav Petkov * usage from PEBS, including the fixup.
5056e1069839SBorislav Petkov * AJ68 could possibly be worked around by always programming
5057e1069839SBorislav Petkov * a pebs_event_reset[0] value and coping with the lost events.
5058e1069839SBorislav Petkov *
5059e1069839SBorislav Petkov * But taken together it might just make sense to not enable PEBS on
5060e1069839SBorislav Petkov * these chips.
5061e1069839SBorislav Petkov */
5062e1069839SBorislav Petkov pr_warn("PEBS disabled due to CPU errata\n");
5063e1069839SBorislav Petkov x86_pmu.pebs = 0;
5064e1069839SBorislav Petkov x86_pmu.pebs_constraints = NULL;
5065e1069839SBorislav Petkov }
5066e1069839SBorislav Petkov
50679b545c04SAndi Kleen static const struct x86_cpu_desc isolation_ucodes[] = {
5068c66f78a6SPeter Zijlstra INTEL_CPU_DESC(INTEL_FAM6_HASWELL, 3, 0x0000001f),
5069af239c44SPeter Zijlstra INTEL_CPU_DESC(INTEL_FAM6_HASWELL_L, 1, 0x0000001e),
50705e741407SPeter Zijlstra INTEL_CPU_DESC(INTEL_FAM6_HASWELL_G, 1, 0x00000015),
50719b545c04SAndi Kleen INTEL_CPU_DESC(INTEL_FAM6_HASWELL_X, 2, 0x00000037),
50729b545c04SAndi Kleen INTEL_CPU_DESC(INTEL_FAM6_HASWELL_X, 4, 0x0000000a),
5073c66f78a6SPeter Zijlstra INTEL_CPU_DESC(INTEL_FAM6_BROADWELL, 4, 0x00000023),
50745e741407SPeter Zijlstra INTEL_CPU_DESC(INTEL_FAM6_BROADWELL_G, 1, 0x00000014),
50755ebb34edSPeter Zijlstra INTEL_CPU_DESC(INTEL_FAM6_BROADWELL_D, 2, 0x00000010),
50765ebb34edSPeter Zijlstra INTEL_CPU_DESC(INTEL_FAM6_BROADWELL_D, 3, 0x07000009),
50775ebb34edSPeter Zijlstra INTEL_CPU_DESC(INTEL_FAM6_BROADWELL_D, 4, 0x0f000009),
50785ebb34edSPeter Zijlstra INTEL_CPU_DESC(INTEL_FAM6_BROADWELL_D, 5, 0x0e000002),
50794b2f1e59SJim Mattson INTEL_CPU_DESC(INTEL_FAM6_BROADWELL_X, 1, 0x0b000014),
50809b545c04SAndi Kleen INTEL_CPU_DESC(INTEL_FAM6_SKYLAKE_X, 3, 0x00000021),
50819b545c04SAndi Kleen INTEL_CPU_DESC(INTEL_FAM6_SKYLAKE_X, 4, 0x00000000),
5082b3c3361fSJim Mattson INTEL_CPU_DESC(INTEL_FAM6_SKYLAKE_X, 5, 0x00000000),
5083b3c3361fSJim Mattson INTEL_CPU_DESC(INTEL_FAM6_SKYLAKE_X, 6, 0x00000000),
5084b3c3361fSJim Mattson INTEL_CPU_DESC(INTEL_FAM6_SKYLAKE_X, 7, 0x00000000),
50856f8faf47SKan Liang INTEL_CPU_DESC(INTEL_FAM6_SKYLAKE_X, 11, 0x00000000),
5086af239c44SPeter Zijlstra INTEL_CPU_DESC(INTEL_FAM6_SKYLAKE_L, 3, 0x0000007c),
5087c66f78a6SPeter Zijlstra INTEL_CPU_DESC(INTEL_FAM6_SKYLAKE, 3, 0x0000007c),
5088c66f78a6SPeter Zijlstra INTEL_CPU_DESC(INTEL_FAM6_KABYLAKE, 9, 0x0000004e),
5089af239c44SPeter Zijlstra INTEL_CPU_DESC(INTEL_FAM6_KABYLAKE_L, 9, 0x0000004e),
5090af239c44SPeter Zijlstra INTEL_CPU_DESC(INTEL_FAM6_KABYLAKE_L, 10, 0x0000004e),
5091af239c44SPeter Zijlstra INTEL_CPU_DESC(INTEL_FAM6_KABYLAKE_L, 11, 0x0000004e),
5092af239c44SPeter Zijlstra INTEL_CPU_DESC(INTEL_FAM6_KABYLAKE_L, 12, 0x0000004e),
5093c66f78a6SPeter Zijlstra INTEL_CPU_DESC(INTEL_FAM6_KABYLAKE, 10, 0x0000004e),
5094c66f78a6SPeter Zijlstra INTEL_CPU_DESC(INTEL_FAM6_KABYLAKE, 11, 0x0000004e),
5095c66f78a6SPeter Zijlstra INTEL_CPU_DESC(INTEL_FAM6_KABYLAKE, 12, 0x0000004e),
5096c66f78a6SPeter Zijlstra INTEL_CPU_DESC(INTEL_FAM6_KABYLAKE, 13, 0x0000004e),
50979b545c04SAndi Kleen {}
50989b545c04SAndi Kleen };
50999b545c04SAndi Kleen
intel_check_pebs_isolation(void)51009b545c04SAndi Kleen static void intel_check_pebs_isolation(void)
51019b545c04SAndi Kleen {
51029b545c04SAndi Kleen x86_pmu.pebs_no_isolation = !x86_cpu_has_min_microcode_rev(isolation_ucodes);
51039b545c04SAndi Kleen }
51049b545c04SAndi Kleen
intel_pebs_isolation_quirk(void)51059b545c04SAndi Kleen static __init void intel_pebs_isolation_quirk(void)
51069b545c04SAndi Kleen {
51079b545c04SAndi Kleen WARN_ON_ONCE(x86_pmu.check_microcode);
51089b545c04SAndi Kleen x86_pmu.check_microcode = intel_check_pebs_isolation;
51099b545c04SAndi Kleen intel_check_pebs_isolation();
51109b545c04SAndi Kleen }
51119b545c04SAndi Kleen
5112a96fff8dSKan Liang static const struct x86_cpu_desc pebs_ucodes[] = {
5113a96fff8dSKan Liang INTEL_CPU_DESC(INTEL_FAM6_SANDYBRIDGE, 7, 0x00000028),
5114a96fff8dSKan Liang INTEL_CPU_DESC(INTEL_FAM6_SANDYBRIDGE_X, 6, 0x00000618),
5115a96fff8dSKan Liang INTEL_CPU_DESC(INTEL_FAM6_SANDYBRIDGE_X, 7, 0x0000070c),
5116a96fff8dSKan Liang {}
5117a96fff8dSKan Liang };
5118a96fff8dSKan Liang
intel_snb_pebs_broken(void)5119a96fff8dSKan Liang static bool intel_snb_pebs_broken(void)
5120e1069839SBorislav Petkov {
5121a96fff8dSKan Liang return !x86_cpu_has_min_microcode_rev(pebs_ucodes);
5122e1069839SBorislav Petkov }
5123e1069839SBorislav Petkov
intel_snb_check_microcode(void)5124e1069839SBorislav Petkov static void intel_snb_check_microcode(void)
5125e1069839SBorislav Petkov {
5126a96fff8dSKan Liang if (intel_snb_pebs_broken() == x86_pmu.pebs_broken)
5127e1069839SBorislav Petkov return;
5128e1069839SBorislav Petkov
5129e1069839SBorislav Petkov /*
5130e1069839SBorislav Petkov * Serialized by the microcode lock..
5131e1069839SBorislav Petkov */
5132e1069839SBorislav Petkov if (x86_pmu.pebs_broken) {
5133e1069839SBorislav Petkov pr_info("PEBS enabled due to microcode update\n");
5134e1069839SBorislav Petkov x86_pmu.pebs_broken = 0;
5135e1069839SBorislav Petkov } else {
5136e1069839SBorislav Petkov pr_info("PEBS disabled due to CPU errata, please upgrade microcode\n");
5137e1069839SBorislav Petkov x86_pmu.pebs_broken = 1;
5138e1069839SBorislav Petkov }
5139e1069839SBorislav Petkov }
5140e1069839SBorislav Petkov
is_lbr_from(unsigned long msr)514119fc9dddSDavid Carrillo-Cisneros static bool is_lbr_from(unsigned long msr)
514219fc9dddSDavid Carrillo-Cisneros {
514319fc9dddSDavid Carrillo-Cisneros unsigned long lbr_from_nr = x86_pmu.lbr_from + x86_pmu.lbr_nr;
514419fc9dddSDavid Carrillo-Cisneros
514519fc9dddSDavid Carrillo-Cisneros return x86_pmu.lbr_from <= msr && msr < lbr_from_nr;
514619fc9dddSDavid Carrillo-Cisneros }
514719fc9dddSDavid Carrillo-Cisneros
5148e1069839SBorislav Petkov /*
5149e1069839SBorislav Petkov * Under certain circumstances, access certain MSR may cause #GP.
5150e1069839SBorislav Petkov * The function tests if the input MSR can be safely accessed.
5151e1069839SBorislav Petkov */
check_msr(unsigned long msr,u64 mask)5152e1069839SBorislav Petkov static bool check_msr(unsigned long msr, u64 mask)
5153e1069839SBorislav Petkov {
5154e1069839SBorislav Petkov u64 val_old, val_new, val_tmp;
5155e1069839SBorislav Petkov
5156e1069839SBorislav Petkov /*
5157d0e1a507SJiri Olsa * Disable the check for real HW, so we don't
5158d9f6e12fSIngo Molnar * mess with potentially enabled registers:
5159d0e1a507SJiri Olsa */
51605ea3f6fbSZhenzhong Duan if (!boot_cpu_has(X86_FEATURE_HYPERVISOR))
5161d0e1a507SJiri Olsa return true;
5162d0e1a507SJiri Olsa
5163d0e1a507SJiri Olsa /*
5164e1069839SBorislav Petkov * Read the current value, change it and read it back to see if it
5165e1069839SBorislav Petkov * matches, this is needed to detect certain hardware emulators
5166e1069839SBorislav Petkov * (qemu/kvm) that don't trap on the MSR access and always return 0s.
5167e1069839SBorislav Petkov */
5168e1069839SBorislav Petkov if (rdmsrl_safe(msr, &val_old))
5169e1069839SBorislav Petkov return false;
5170e1069839SBorislav Petkov
5171e1069839SBorislav Petkov /*
5172e1069839SBorislav Petkov * Only change the bits which can be updated by wrmsrl.
5173e1069839SBorislav Petkov */
5174e1069839SBorislav Petkov val_tmp = val_old ^ mask;
517519fc9dddSDavid Carrillo-Cisneros
517619fc9dddSDavid Carrillo-Cisneros if (is_lbr_from(msr))
517719fc9dddSDavid Carrillo-Cisneros val_tmp = lbr_from_signext_quirk_wr(val_tmp);
517819fc9dddSDavid Carrillo-Cisneros
5179e1069839SBorislav Petkov if (wrmsrl_safe(msr, val_tmp) ||
5180e1069839SBorislav Petkov rdmsrl_safe(msr, &val_new))
5181e1069839SBorislav Petkov return false;
5182e1069839SBorislav Petkov
518319fc9dddSDavid Carrillo-Cisneros /*
518419fc9dddSDavid Carrillo-Cisneros * Quirk only affects validation in wrmsr(), so wrmsrl()'s value
518519fc9dddSDavid Carrillo-Cisneros * should equal rdmsrl()'s even with the quirk.
518619fc9dddSDavid Carrillo-Cisneros */
5187e1069839SBorislav Petkov if (val_new != val_tmp)
5188e1069839SBorislav Petkov return false;
5189e1069839SBorislav Petkov
519019fc9dddSDavid Carrillo-Cisneros if (is_lbr_from(msr))
519119fc9dddSDavid Carrillo-Cisneros val_old = lbr_from_signext_quirk_wr(val_old);
519219fc9dddSDavid Carrillo-Cisneros
5193e1069839SBorislav Petkov /* Here it's sure that the MSR can be safely accessed.
5194e1069839SBorislav Petkov * Restore the old value and return.
5195e1069839SBorislav Petkov */
5196e1069839SBorislav Petkov wrmsrl(msr, val_old);
5197e1069839SBorislav Petkov
5198e1069839SBorislav Petkov return true;
5199e1069839SBorislav Petkov }
5200e1069839SBorislav Petkov
intel_sandybridge_quirk(void)5201e1069839SBorislav Petkov static __init void intel_sandybridge_quirk(void)
5202e1069839SBorislav Petkov {
5203e1069839SBorislav Petkov x86_pmu.check_microcode = intel_snb_check_microcode;
52041ba143a5SSebastian Andrzej Siewior cpus_read_lock();
5205e1069839SBorislav Petkov intel_snb_check_microcode();
52061ba143a5SSebastian Andrzej Siewior cpus_read_unlock();
5207e1069839SBorislav Petkov }
5208e1069839SBorislav Petkov
5209e1069839SBorislav Petkov static const struct { int id; char *name; } intel_arch_events_map[] __initconst = {
5210e1069839SBorislav Petkov { PERF_COUNT_HW_CPU_CYCLES, "cpu cycles" },
5211e1069839SBorislav Petkov { PERF_COUNT_HW_INSTRUCTIONS, "instructions" },
5212e1069839SBorislav Petkov { PERF_COUNT_HW_BUS_CYCLES, "bus cycles" },
5213e1069839SBorislav Petkov { PERF_COUNT_HW_CACHE_REFERENCES, "cache references" },
5214e1069839SBorislav Petkov { PERF_COUNT_HW_CACHE_MISSES, "cache misses" },
5215e1069839SBorislav Petkov { PERF_COUNT_HW_BRANCH_INSTRUCTIONS, "branch instructions" },
5216e1069839SBorislav Petkov { PERF_COUNT_HW_BRANCH_MISSES, "branch misses" },
5217e1069839SBorislav Petkov };
5218e1069839SBorislav Petkov
intel_arch_events_quirk(void)5219e1069839SBorislav Petkov static __init void intel_arch_events_quirk(void)
5220e1069839SBorislav Petkov {
5221e1069839SBorislav Petkov int bit;
5222e1069839SBorislav Petkov
5223d9f6e12fSIngo Molnar /* disable event that reported as not present by cpuid */
5224e1069839SBorislav Petkov for_each_set_bit(bit, x86_pmu.events_mask, ARRAY_SIZE(intel_arch_events_map)) {
5225e1069839SBorislav Petkov intel_perfmon_event_map[intel_arch_events_map[bit].id] = 0;
5226e1069839SBorislav Petkov pr_warn("CPUID marked event: \'%s\' unavailable\n",
5227e1069839SBorislav Petkov intel_arch_events_map[bit].name);
5228e1069839SBorislav Petkov }
5229e1069839SBorislav Petkov }
5230e1069839SBorislav Petkov
intel_nehalem_quirk(void)5231e1069839SBorislav Petkov static __init void intel_nehalem_quirk(void)
5232e1069839SBorislav Petkov {
5233e1069839SBorislav Petkov union cpuid10_ebx ebx;
5234e1069839SBorislav Petkov
5235e1069839SBorislav Petkov ebx.full = x86_pmu.events_maskl;
5236e1069839SBorislav Petkov if (ebx.split.no_branch_misses_retired) {
5237e1069839SBorislav Petkov /*
5238e1069839SBorislav Petkov * Erratum AAJ80 detected, we work it around by using
5239e1069839SBorislav Petkov * the BR_MISP_EXEC.ANY event. This will over-count
5240e1069839SBorislav Petkov * branch-misses, but it's still much better than the
5241e1069839SBorislav Petkov * architectural event which is often completely bogus:
5242e1069839SBorislav Petkov */
5243e1069839SBorislav Petkov intel_perfmon_event_map[PERF_COUNT_HW_BRANCH_MISSES] = 0x7f89;
5244e1069839SBorislav Petkov ebx.split.no_branch_misses_retired = 0;
5245e1069839SBorislav Petkov x86_pmu.events_maskl = ebx.full;
5246e1069839SBorislav Petkov pr_info("CPU erratum AAJ80 worked around\n");
5247e1069839SBorislav Petkov }
5248e1069839SBorislav Petkov }
5249e1069839SBorislav Petkov
5250e1069839SBorislav Petkov /*
5251e1069839SBorislav Petkov * enable software workaround for errata:
5252e1069839SBorislav Petkov * SNB: BJ122
5253e1069839SBorislav Petkov * IVB: BV98
5254e1069839SBorislav Petkov * HSW: HSD29
5255e1069839SBorislav Petkov *
5256e1069839SBorislav Petkov * Only needed when HT is enabled. However detecting
5257e1069839SBorislav Petkov * if HT is enabled is difficult (model specific). So instead,
5258e1069839SBorislav Petkov * we enable the workaround in the early boot, and verify if
5259e1069839SBorislav Petkov * it is needed in a later initcall phase once we have valid
5260e1069839SBorislav Petkov * topology information to check if HT is actually enabled
5261e1069839SBorislav Petkov */
intel_ht_bug(void)5262e1069839SBorislav Petkov static __init void intel_ht_bug(void)
5263e1069839SBorislav Petkov {
5264e1069839SBorislav Petkov x86_pmu.flags |= PMU_FL_EXCL_CNTRS | PMU_FL_EXCL_ENABLED;
5265e1069839SBorislav Petkov
5266e1069839SBorislav Petkov x86_pmu.start_scheduling = intel_start_scheduling;
5267e1069839SBorislav Petkov x86_pmu.commit_scheduling = intel_commit_scheduling;
5268e1069839SBorislav Petkov x86_pmu.stop_scheduling = intel_stop_scheduling;
5269e1069839SBorislav Petkov }
5270e1069839SBorislav Petkov
5271e1069839SBorislav Petkov EVENT_ATTR_STR(mem-loads, mem_ld_hsw, "event=0xcd,umask=0x1,ldlat=3");
5272e1069839SBorislav Petkov EVENT_ATTR_STR(mem-stores, mem_st_hsw, "event=0xd0,umask=0x82")
5273e1069839SBorislav Petkov
5274e1069839SBorislav Petkov /* Haswell special events */
5275e1069839SBorislav Petkov EVENT_ATTR_STR(tx-start, tx_start, "event=0xc9,umask=0x1");
5276e1069839SBorislav Petkov EVENT_ATTR_STR(tx-commit, tx_commit, "event=0xc9,umask=0x2");
5277e1069839SBorislav Petkov EVENT_ATTR_STR(tx-abort, tx_abort, "event=0xc9,umask=0x4");
5278e1069839SBorislav Petkov EVENT_ATTR_STR(tx-capacity, tx_capacity, "event=0x54,umask=0x2");
5279e1069839SBorislav Petkov EVENT_ATTR_STR(tx-conflict, tx_conflict, "event=0x54,umask=0x1");
5280e1069839SBorislav Petkov EVENT_ATTR_STR(el-start, el_start, "event=0xc8,umask=0x1");
5281e1069839SBorislav Petkov EVENT_ATTR_STR(el-commit, el_commit, "event=0xc8,umask=0x2");
5282e1069839SBorislav Petkov EVENT_ATTR_STR(el-abort, el_abort, "event=0xc8,umask=0x4");
5283e1069839SBorislav Petkov EVENT_ATTR_STR(el-capacity, el_capacity, "event=0x54,umask=0x2");
5284e1069839SBorislav Petkov EVENT_ATTR_STR(el-conflict, el_conflict, "event=0x54,umask=0x1");
5285e1069839SBorislav Petkov EVENT_ATTR_STR(cycles-t, cycles_t, "event=0x3c,in_tx=1");
5286e1069839SBorislav Petkov EVENT_ATTR_STR(cycles-ct, cycles_ct, "event=0x3c,in_tx=1,in_tx_cp=1");
5287e1069839SBorislav Petkov
5288e1069839SBorislav Petkov static struct attribute *hsw_events_attrs[] = {
528958ba4d5aSAndi Kleen EVENT_PTR(td_slots_issued),
529058ba4d5aSAndi Kleen EVENT_PTR(td_slots_retired),
529158ba4d5aSAndi Kleen EVENT_PTR(td_fetch_bubbles),
529258ba4d5aSAndi Kleen EVENT_PTR(td_total_slots),
529358ba4d5aSAndi Kleen EVENT_PTR(td_total_slots_scale),
529458ba4d5aSAndi Kleen EVENT_PTR(td_recovery_bubbles),
529558ba4d5aSAndi Kleen EVENT_PTR(td_recovery_bubbles_scale),
529658ba4d5aSAndi Kleen NULL
529758ba4d5aSAndi Kleen };
529858ba4d5aSAndi Kleen
5299d4ae5529SJiri Olsa static struct attribute *hsw_mem_events_attrs[] = {
5300d4ae5529SJiri Olsa EVENT_PTR(mem_ld_hsw),
5301d4ae5529SJiri Olsa EVENT_PTR(mem_st_hsw),
5302d4ae5529SJiri Olsa NULL,
5303d4ae5529SJiri Olsa };
5304d4ae5529SJiri Olsa
530558ba4d5aSAndi Kleen static struct attribute *hsw_tsx_events_attrs[] = {
5306e1069839SBorislav Petkov EVENT_PTR(tx_start),
5307e1069839SBorislav Petkov EVENT_PTR(tx_commit),
5308e1069839SBorislav Petkov EVENT_PTR(tx_abort),
5309e1069839SBorislav Petkov EVENT_PTR(tx_capacity),
5310e1069839SBorislav Petkov EVENT_PTR(tx_conflict),
5311e1069839SBorislav Petkov EVENT_PTR(el_start),
5312e1069839SBorislav Petkov EVENT_PTR(el_commit),
5313e1069839SBorislav Petkov EVENT_PTR(el_abort),
5314e1069839SBorislav Petkov EVENT_PTR(el_capacity),
5315e1069839SBorislav Petkov EVENT_PTR(el_conflict),
5316e1069839SBorislav Petkov EVENT_PTR(cycles_t),
5317e1069839SBorislav Petkov EVENT_PTR(cycles_ct),
5318e1069839SBorislav Petkov NULL
5319e1069839SBorislav Petkov };
5320e1069839SBorislav Petkov
532160176089SKan Liang EVENT_ATTR_STR(tx-capacity-read, tx_capacity_read, "event=0x54,umask=0x80");
532260176089SKan Liang EVENT_ATTR_STR(tx-capacity-write, tx_capacity_write, "event=0x54,umask=0x2");
532360176089SKan Liang EVENT_ATTR_STR(el-capacity-read, el_capacity_read, "event=0x54,umask=0x80");
532460176089SKan Liang EVENT_ATTR_STR(el-capacity-write, el_capacity_write, "event=0x54,umask=0x2");
532560176089SKan Liang
532660176089SKan Liang static struct attribute *icl_events_attrs[] = {
532760176089SKan Liang EVENT_PTR(mem_ld_hsw),
532860176089SKan Liang EVENT_PTR(mem_st_hsw),
532960176089SKan Liang NULL,
533060176089SKan Liang };
533160176089SKan Liang
533259a854e2SKan Liang static struct attribute *icl_td_events_attrs[] = {
533359a854e2SKan Liang EVENT_PTR(slots),
533459a854e2SKan Liang EVENT_PTR(td_retiring),
533559a854e2SKan Liang EVENT_PTR(td_bad_spec),
533659a854e2SKan Liang EVENT_PTR(td_fe_bound),
533759a854e2SKan Liang EVENT_PTR(td_be_bound),
533859a854e2SKan Liang NULL,
533959a854e2SKan Liang };
534059a854e2SKan Liang
534160176089SKan Liang static struct attribute *icl_tsx_events_attrs[] = {
534260176089SKan Liang EVENT_PTR(tx_start),
534360176089SKan Liang EVENT_PTR(tx_abort),
534460176089SKan Liang EVENT_PTR(tx_commit),
534560176089SKan Liang EVENT_PTR(tx_capacity_read),
534660176089SKan Liang EVENT_PTR(tx_capacity_write),
534760176089SKan Liang EVENT_PTR(tx_conflict),
534860176089SKan Liang EVENT_PTR(el_start),
534960176089SKan Liang EVENT_PTR(el_abort),
535060176089SKan Liang EVENT_PTR(el_commit),
535160176089SKan Liang EVENT_PTR(el_capacity_read),
535260176089SKan Liang EVENT_PTR(el_capacity_write),
535360176089SKan Liang EVENT_PTR(el_conflict),
535460176089SKan Liang EVENT_PTR(cycles_t),
535560176089SKan Liang EVENT_PTR(cycles_ct),
535660176089SKan Liang NULL,
535760176089SKan Liang };
535860176089SKan Liang
535961b985e3SKan Liang
536061b985e3SKan Liang EVENT_ATTR_STR(mem-stores, mem_st_spr, "event=0xcd,umask=0x2");
536161b985e3SKan Liang EVENT_ATTR_STR(mem-loads-aux, mem_ld_aux, "event=0x03,umask=0x82");
536261b985e3SKan Liang
536361b985e3SKan Liang static struct attribute *spr_events_attrs[] = {
536461b985e3SKan Liang EVENT_PTR(mem_ld_hsw),
536561b985e3SKan Liang EVENT_PTR(mem_st_spr),
536661b985e3SKan Liang EVENT_PTR(mem_ld_aux),
536761b985e3SKan Liang NULL,
536861b985e3SKan Liang };
536961b985e3SKan Liang
537061b985e3SKan Liang static struct attribute *spr_td_events_attrs[] = {
537161b985e3SKan Liang EVENT_PTR(slots),
537261b985e3SKan Liang EVENT_PTR(td_retiring),
537361b985e3SKan Liang EVENT_PTR(td_bad_spec),
537461b985e3SKan Liang EVENT_PTR(td_fe_bound),
537561b985e3SKan Liang EVENT_PTR(td_be_bound),
537661b985e3SKan Liang EVENT_PTR(td_heavy_ops),
537761b985e3SKan Liang EVENT_PTR(td_br_mispredict),
537861b985e3SKan Liang EVENT_PTR(td_fetch_lat),
537961b985e3SKan Liang EVENT_PTR(td_mem_bound),
538061b985e3SKan Liang NULL,
538161b985e3SKan Liang };
538261b985e3SKan Liang
538361b985e3SKan Liang static struct attribute *spr_tsx_events_attrs[] = {
538461b985e3SKan Liang EVENT_PTR(tx_start),
538561b985e3SKan Liang EVENT_PTR(tx_abort),
538661b985e3SKan Liang EVENT_PTR(tx_commit),
538761b985e3SKan Liang EVENT_PTR(tx_capacity_read),
538861b985e3SKan Liang EVENT_PTR(tx_capacity_write),
538961b985e3SKan Liang EVENT_PTR(tx_conflict),
539061b985e3SKan Liang EVENT_PTR(cycles_t),
539161b985e3SKan Liang EVENT_PTR(cycles_ct),
539261b985e3SKan Liang NULL,
539361b985e3SKan Liang };
539461b985e3SKan Liang
freeze_on_smi_show(struct device * cdev,struct device_attribute * attr,char * buf)53956089327fSKan Liang static ssize_t freeze_on_smi_show(struct device *cdev,
53966089327fSKan Liang struct device_attribute *attr,
53976089327fSKan Liang char *buf)
53986089327fSKan Liang {
53996089327fSKan Liang return sprintf(buf, "%lu\n", x86_pmu.attr_freeze_on_smi);
54006089327fSKan Liang }
54016089327fSKan Liang
54026089327fSKan Liang static DEFINE_MUTEX(freeze_on_smi_mutex);
54036089327fSKan Liang
freeze_on_smi_store(struct device * cdev,struct device_attribute * attr,const char * buf,size_t count)54046089327fSKan Liang static ssize_t freeze_on_smi_store(struct device *cdev,
54056089327fSKan Liang struct device_attribute *attr,
54066089327fSKan Liang const char *buf, size_t count)
54076089327fSKan Liang {
54086089327fSKan Liang unsigned long val;
54096089327fSKan Liang ssize_t ret;
54106089327fSKan Liang
54116089327fSKan Liang ret = kstrtoul(buf, 0, &val);
54126089327fSKan Liang if (ret)
54136089327fSKan Liang return ret;
54146089327fSKan Liang
54156089327fSKan Liang if (val > 1)
54166089327fSKan Liang return -EINVAL;
54176089327fSKan Liang
54186089327fSKan Liang mutex_lock(&freeze_on_smi_mutex);
54196089327fSKan Liang
54206089327fSKan Liang if (x86_pmu.attr_freeze_on_smi == val)
54216089327fSKan Liang goto done;
54226089327fSKan Liang
54236089327fSKan Liang x86_pmu.attr_freeze_on_smi = val;
54246089327fSKan Liang
5425eda8a2c5SSebastian Andrzej Siewior cpus_read_lock();
54266089327fSKan Liang on_each_cpu(flip_smm_bit, &val, 1);
5427eda8a2c5SSebastian Andrzej Siewior cpus_read_unlock();
54286089327fSKan Liang done:
54296089327fSKan Liang mutex_unlock(&freeze_on_smi_mutex);
54306089327fSKan Liang
54316089327fSKan Liang return count;
54326089327fSKan Liang }
54336089327fSKan Liang
update_tfa_sched(void * ignored)5434f447e4ebSStephane Eranian static void update_tfa_sched(void *ignored)
5435f447e4ebSStephane Eranian {
5436f447e4ebSStephane Eranian struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
5437f447e4ebSStephane Eranian
5438f447e4ebSStephane Eranian /*
5439f447e4ebSStephane Eranian * check if PMC3 is used
5440f447e4ebSStephane Eranian * and if so force schedule out for all event types all contexts
5441f447e4ebSStephane Eranian */
5442f447e4ebSStephane Eranian if (test_bit(3, cpuc->active_mask))
544361e76d53SKan Liang perf_pmu_resched(x86_get_pmu(smp_processor_id()));
5444f447e4ebSStephane Eranian }
5445f447e4ebSStephane Eranian
show_sysctl_tfa(struct device * cdev,struct device_attribute * attr,char * buf)5446f447e4ebSStephane Eranian static ssize_t show_sysctl_tfa(struct device *cdev,
5447f447e4ebSStephane Eranian struct device_attribute *attr,
5448f447e4ebSStephane Eranian char *buf)
5449f447e4ebSStephane Eranian {
5450f447e4ebSStephane Eranian return snprintf(buf, 40, "%d\n", allow_tsx_force_abort);
5451f447e4ebSStephane Eranian }
5452f447e4ebSStephane Eranian
set_sysctl_tfa(struct device * cdev,struct device_attribute * attr,const char * buf,size_t count)5453f447e4ebSStephane Eranian static ssize_t set_sysctl_tfa(struct device *cdev,
5454f447e4ebSStephane Eranian struct device_attribute *attr,
5455f447e4ebSStephane Eranian const char *buf, size_t count)
5456f447e4ebSStephane Eranian {
5457f447e4ebSStephane Eranian bool val;
5458f447e4ebSStephane Eranian ssize_t ret;
5459f447e4ebSStephane Eranian
5460f447e4ebSStephane Eranian ret = kstrtobool(buf, &val);
5461f447e4ebSStephane Eranian if (ret)
5462f447e4ebSStephane Eranian return ret;
5463f447e4ebSStephane Eranian
5464f447e4ebSStephane Eranian /* no change */
5465f447e4ebSStephane Eranian if (val == allow_tsx_force_abort)
5466f447e4ebSStephane Eranian return count;
5467f447e4ebSStephane Eranian
5468f447e4ebSStephane Eranian allow_tsx_force_abort = val;
5469f447e4ebSStephane Eranian
5470eda8a2c5SSebastian Andrzej Siewior cpus_read_lock();
5471f447e4ebSStephane Eranian on_each_cpu(update_tfa_sched, NULL, 1);
5472eda8a2c5SSebastian Andrzej Siewior cpus_read_unlock();
5473f447e4ebSStephane Eranian
5474f447e4ebSStephane Eranian return count;
5475f447e4ebSStephane Eranian }
5476f447e4ebSStephane Eranian
5477f447e4ebSStephane Eranian
54786089327fSKan Liang static DEVICE_ATTR_RW(freeze_on_smi);
54796089327fSKan Liang
branches_show(struct device * cdev,struct device_attribute * attr,char * buf)5480b00233b5SAndi Kleen static ssize_t branches_show(struct device *cdev,
5481b00233b5SAndi Kleen struct device_attribute *attr,
5482b00233b5SAndi Kleen char *buf)
5483b00233b5SAndi Kleen {
5484b00233b5SAndi Kleen return snprintf(buf, PAGE_SIZE, "%d\n", x86_pmu.lbr_nr);
5485b00233b5SAndi Kleen }
5486b00233b5SAndi Kleen
5487b00233b5SAndi Kleen static DEVICE_ATTR_RO(branches);
5488b00233b5SAndi Kleen
5489b00233b5SAndi Kleen static struct attribute *lbr_attrs[] = {
5490b00233b5SAndi Kleen &dev_attr_branches.attr,
5491b00233b5SAndi Kleen NULL
5492b00233b5SAndi Kleen };
5493b00233b5SAndi Kleen
5494b00233b5SAndi Kleen static char pmu_name_str[30];
5495b00233b5SAndi Kleen
pmu_name_show(struct device * cdev,struct device_attribute * attr,char * buf)5496b00233b5SAndi Kleen static ssize_t pmu_name_show(struct device *cdev,
5497b00233b5SAndi Kleen struct device_attribute *attr,
5498b00233b5SAndi Kleen char *buf)
5499b00233b5SAndi Kleen {
5500b00233b5SAndi Kleen return snprintf(buf, PAGE_SIZE, "%s\n", pmu_name_str);
5501b00233b5SAndi Kleen }
5502b00233b5SAndi Kleen
5503b00233b5SAndi Kleen static DEVICE_ATTR_RO(pmu_name);
5504b00233b5SAndi Kleen
5505b00233b5SAndi Kleen static struct attribute *intel_pmu_caps_attrs[] = {
5506b00233b5SAndi Kleen &dev_attr_pmu_name.attr,
5507b00233b5SAndi Kleen NULL
5508b00233b5SAndi Kleen };
5509b00233b5SAndi Kleen
5510f447e4ebSStephane Eranian static DEVICE_ATTR(allow_tsx_force_abort, 0644,
5511f447e4ebSStephane Eranian show_sysctl_tfa,
5512f447e4ebSStephane Eranian set_sysctl_tfa);
5513400816f6SPeter Zijlstra (Intel)
55146089327fSKan Liang static struct attribute *intel_pmu_attrs[] = {
55156089327fSKan Liang &dev_attr_freeze_on_smi.attr,
5516b7c9b392SJiri Olsa &dev_attr_allow_tsx_force_abort.attr,
55176089327fSKan Liang NULL,
55186089327fSKan Liang };
55196089327fSKan Liang
5520baa0c833SJiri Olsa static umode_t
tsx_is_visible(struct kobject * kobj,struct attribute * attr,int i)5521baa0c833SJiri Olsa tsx_is_visible(struct kobject *kobj, struct attribute *attr, int i)
5522d4ae5529SJiri Olsa {
5523baa0c833SJiri Olsa return boot_cpu_has(X86_FEATURE_RTM) ? attr->mode : 0;
5524d4ae5529SJiri Olsa }
5525d4ae5529SJiri Olsa
5526baa0c833SJiri Olsa static umode_t
pebs_is_visible(struct kobject * kobj,struct attribute * attr,int i)5527baa0c833SJiri Olsa pebs_is_visible(struct kobject *kobj, struct attribute *attr, int i)
5528baa0c833SJiri Olsa {
5529baa0c833SJiri Olsa return x86_pmu.pebs ? attr->mode : 0;
5530d4ae5529SJiri Olsa }
5531d4ae5529SJiri Olsa
55321f157286SJiri Olsa static umode_t
mem_is_visible(struct kobject * kobj,struct attribute * attr,int i)5533bc4000fdSKan Liang mem_is_visible(struct kobject *kobj, struct attribute *attr, int i)
5534bc4000fdSKan Liang {
5535bc4000fdSKan Liang if (attr == &event_attr_mem_ld_aux.attr.attr)
5536bc4000fdSKan Liang return x86_pmu.flags & PMU_FL_MEM_LOADS_AUX ? attr->mode : 0;
5537bc4000fdSKan Liang
5538bc4000fdSKan Liang return pebs_is_visible(kobj, attr, i);
5539bc4000fdSKan Liang }
5540bc4000fdSKan Liang
5541bc4000fdSKan Liang static umode_t
lbr_is_visible(struct kobject * kobj,struct attribute * attr,int i)55421f157286SJiri Olsa lbr_is_visible(struct kobject *kobj, struct attribute *attr, int i)
55431f157286SJiri Olsa {
55441f157286SJiri Olsa return x86_pmu.lbr_nr ? attr->mode : 0;
55451f157286SJiri Olsa }
55461f157286SJiri Olsa
55473ea40ac7SJiri Olsa static umode_t
exra_is_visible(struct kobject * kobj,struct attribute * attr,int i)55483ea40ac7SJiri Olsa exra_is_visible(struct kobject *kobj, struct attribute *attr, int i)
55493ea40ac7SJiri Olsa {
55503ea40ac7SJiri Olsa return x86_pmu.version >= 2 ? attr->mode : 0;
55513ea40ac7SJiri Olsa }
55523ea40ac7SJiri Olsa
5553b7c9b392SJiri Olsa static umode_t
default_is_visible(struct kobject * kobj,struct attribute * attr,int i)5554b7c9b392SJiri Olsa default_is_visible(struct kobject *kobj, struct attribute *attr, int i)
5555b7c9b392SJiri Olsa {
5556b7c9b392SJiri Olsa if (attr == &dev_attr_allow_tsx_force_abort.attr)
5557b7c9b392SJiri Olsa return x86_pmu.flags & PMU_FL_TFA ? attr->mode : 0;
5558b7c9b392SJiri Olsa
5559b7c9b392SJiri Olsa return attr->mode;
5560b7c9b392SJiri Olsa }
5561b7c9b392SJiri Olsa
5562*475d99e6SKan Liang static umode_t
td_is_visible(struct kobject * kobj,struct attribute * attr,int i)5563*475d99e6SKan Liang td_is_visible(struct kobject *kobj, struct attribute *attr, int i)
5564*475d99e6SKan Liang {
5565*475d99e6SKan Liang /*
5566*475d99e6SKan Liang * Hide the perf metrics topdown events
5567*475d99e6SKan Liang * if the feature is not enumerated.
5568*475d99e6SKan Liang */
5569*475d99e6SKan Liang if (x86_pmu.num_topdown_events)
5570*475d99e6SKan Liang return x86_pmu.intel_cap.perf_metrics ? attr->mode : 0;
5571*475d99e6SKan Liang
5572*475d99e6SKan Liang return attr->mode;
5573*475d99e6SKan Liang }
5574*475d99e6SKan Liang
5575baa0c833SJiri Olsa static struct attribute_group group_events_td = {
5576baa0c833SJiri Olsa .name = "events",
5577*475d99e6SKan Liang .is_visible = td_is_visible,
5578baa0c833SJiri Olsa };
5579baa0c833SJiri Olsa
5580baa0c833SJiri Olsa static struct attribute_group group_events_mem = {
5581baa0c833SJiri Olsa .name = "events",
5582bc4000fdSKan Liang .is_visible = mem_is_visible,
5583baa0c833SJiri Olsa };
5584baa0c833SJiri Olsa
5585baa0c833SJiri Olsa static struct attribute_group group_events_tsx = {
5586baa0c833SJiri Olsa .name = "events",
5587baa0c833SJiri Olsa .is_visible = tsx_is_visible,
5588baa0c833SJiri Olsa };
5589baa0c833SJiri Olsa
55901f157286SJiri Olsa static struct attribute_group group_caps_gen = {
55911f157286SJiri Olsa .name = "caps",
55921f157286SJiri Olsa .attrs = intel_pmu_caps_attrs,
55931f157286SJiri Olsa };
55941f157286SJiri Olsa
55951f157286SJiri Olsa static struct attribute_group group_caps_lbr = {
55961f157286SJiri Olsa .name = "caps",
55971f157286SJiri Olsa .attrs = lbr_attrs,
55981f157286SJiri Olsa .is_visible = lbr_is_visible,
55991f157286SJiri Olsa };
56001f157286SJiri Olsa
56013ea40ac7SJiri Olsa static struct attribute_group group_format_extra = {
56023ea40ac7SJiri Olsa .name = "format",
56033ea40ac7SJiri Olsa .is_visible = exra_is_visible,
56043ea40ac7SJiri Olsa };
56053ea40ac7SJiri Olsa
5606b6576880SJiri Olsa static struct attribute_group group_format_extra_skl = {
5607b6576880SJiri Olsa .name = "format",
5608b6576880SJiri Olsa .is_visible = exra_is_visible,
5609b6576880SJiri Olsa };
5610b6576880SJiri Olsa
56116a9f4efeSJiri Olsa static struct attribute_group group_default = {
56126a9f4efeSJiri Olsa .attrs = intel_pmu_attrs,
5613b7c9b392SJiri Olsa .is_visible = default_is_visible,
56146a9f4efeSJiri Olsa };
56156a9f4efeSJiri Olsa
5616baa0c833SJiri Olsa static const struct attribute_group *attr_update[] = {
5617baa0c833SJiri Olsa &group_events_td,
5618baa0c833SJiri Olsa &group_events_mem,
5619baa0c833SJiri Olsa &group_events_tsx,
56201f157286SJiri Olsa &group_caps_gen,
56211f157286SJiri Olsa &group_caps_lbr,
56223ea40ac7SJiri Olsa &group_format_extra,
5623b6576880SJiri Olsa &group_format_extra_skl,
56246a9f4efeSJiri Olsa &group_default,
5625baa0c833SJiri Olsa NULL,
5626baa0c833SJiri Olsa };
5627baa0c833SJiri Olsa
5628f83d2f91SKan Liang EVENT_ATTR_STR_HYBRID(slots, slots_adl, "event=0x00,umask=0x4", hybrid_big);
5629f83d2f91SKan Liang EVENT_ATTR_STR_HYBRID(topdown-retiring, td_retiring_adl, "event=0xc2,umask=0x0;event=0x00,umask=0x80", hybrid_big_small);
5630f83d2f91SKan Liang EVENT_ATTR_STR_HYBRID(topdown-bad-spec, td_bad_spec_adl, "event=0x73,umask=0x0;event=0x00,umask=0x81", hybrid_big_small);
5631f83d2f91SKan Liang EVENT_ATTR_STR_HYBRID(topdown-fe-bound, td_fe_bound_adl, "event=0x71,umask=0x0;event=0x00,umask=0x82", hybrid_big_small);
5632f83d2f91SKan Liang EVENT_ATTR_STR_HYBRID(topdown-be-bound, td_be_bound_adl, "event=0x74,umask=0x0;event=0x00,umask=0x83", hybrid_big_small);
5633f83d2f91SKan Liang EVENT_ATTR_STR_HYBRID(topdown-heavy-ops, td_heavy_ops_adl, "event=0x00,umask=0x84", hybrid_big);
5634f83d2f91SKan Liang EVENT_ATTR_STR_HYBRID(topdown-br-mispredict, td_br_mis_adl, "event=0x00,umask=0x85", hybrid_big);
5635f83d2f91SKan Liang EVENT_ATTR_STR_HYBRID(topdown-fetch-lat, td_fetch_lat_adl, "event=0x00,umask=0x86", hybrid_big);
5636f83d2f91SKan Liang EVENT_ATTR_STR_HYBRID(topdown-mem-bound, td_mem_bound_adl, "event=0x00,umask=0x87", hybrid_big);
5637f83d2f91SKan Liang
5638f83d2f91SKan Liang static struct attribute *adl_hybrid_events_attrs[] = {
5639f83d2f91SKan Liang EVENT_PTR(slots_adl),
5640f83d2f91SKan Liang EVENT_PTR(td_retiring_adl),
5641f83d2f91SKan Liang EVENT_PTR(td_bad_spec_adl),
5642f83d2f91SKan Liang EVENT_PTR(td_fe_bound_adl),
5643f83d2f91SKan Liang EVENT_PTR(td_be_bound_adl),
5644f83d2f91SKan Liang EVENT_PTR(td_heavy_ops_adl),
5645f83d2f91SKan Liang EVENT_PTR(td_br_mis_adl),
5646f83d2f91SKan Liang EVENT_PTR(td_fetch_lat_adl),
5647f83d2f91SKan Liang EVENT_PTR(td_mem_bound_adl),
5648f83d2f91SKan Liang NULL,
5649f83d2f91SKan Liang };
5650f83d2f91SKan Liang
5651f83d2f91SKan Liang /* Must be in IDX order */
5652f83d2f91SKan Liang EVENT_ATTR_STR_HYBRID(mem-loads, mem_ld_adl, "event=0xd0,umask=0x5,ldlat=3;event=0xcd,umask=0x1,ldlat=3", hybrid_big_small);
5653f83d2f91SKan Liang EVENT_ATTR_STR_HYBRID(mem-stores, mem_st_adl, "event=0xd0,umask=0x6;event=0xcd,umask=0x2", hybrid_big_small);
5654f83d2f91SKan Liang EVENT_ATTR_STR_HYBRID(mem-loads-aux, mem_ld_aux_adl, "event=0x03,umask=0x82", hybrid_big);
5655f83d2f91SKan Liang
5656f83d2f91SKan Liang static struct attribute *adl_hybrid_mem_attrs[] = {
5657f83d2f91SKan Liang EVENT_PTR(mem_ld_adl),
5658f83d2f91SKan Liang EVENT_PTR(mem_st_adl),
5659f83d2f91SKan Liang EVENT_PTR(mem_ld_aux_adl),
5660f83d2f91SKan Liang NULL,
5661f83d2f91SKan Liang };
5662f83d2f91SKan Liang
566338aaf921SKan Liang static struct attribute *mtl_hybrid_mem_attrs[] = {
566438aaf921SKan Liang EVENT_PTR(mem_ld_adl),
566538aaf921SKan Liang EVENT_PTR(mem_st_adl),
566638aaf921SKan Liang NULL
566738aaf921SKan Liang };
566838aaf921SKan Liang
5669f83d2f91SKan Liang EVENT_ATTR_STR_HYBRID(tx-start, tx_start_adl, "event=0xc9,umask=0x1", hybrid_big);
5670f83d2f91SKan Liang EVENT_ATTR_STR_HYBRID(tx-commit, tx_commit_adl, "event=0xc9,umask=0x2", hybrid_big);
5671f83d2f91SKan Liang EVENT_ATTR_STR_HYBRID(tx-abort, tx_abort_adl, "event=0xc9,umask=0x4", hybrid_big);
5672f83d2f91SKan Liang EVENT_ATTR_STR_HYBRID(tx-conflict, tx_conflict_adl, "event=0x54,umask=0x1", hybrid_big);
5673f83d2f91SKan Liang EVENT_ATTR_STR_HYBRID(cycles-t, cycles_t_adl, "event=0x3c,in_tx=1", hybrid_big);
5674f83d2f91SKan Liang EVENT_ATTR_STR_HYBRID(cycles-ct, cycles_ct_adl, "event=0x3c,in_tx=1,in_tx_cp=1", hybrid_big);
5675f83d2f91SKan Liang EVENT_ATTR_STR_HYBRID(tx-capacity-read, tx_capacity_read_adl, "event=0x54,umask=0x80", hybrid_big);
5676f83d2f91SKan Liang EVENT_ATTR_STR_HYBRID(tx-capacity-write, tx_capacity_write_adl, "event=0x54,umask=0x2", hybrid_big);
5677f83d2f91SKan Liang
5678f83d2f91SKan Liang static struct attribute *adl_hybrid_tsx_attrs[] = {
5679f83d2f91SKan Liang EVENT_PTR(tx_start_adl),
5680f83d2f91SKan Liang EVENT_PTR(tx_abort_adl),
5681f83d2f91SKan Liang EVENT_PTR(tx_commit_adl),
5682f83d2f91SKan Liang EVENT_PTR(tx_capacity_read_adl),
5683f83d2f91SKan Liang EVENT_PTR(tx_capacity_write_adl),
5684f83d2f91SKan Liang EVENT_PTR(tx_conflict_adl),
5685f83d2f91SKan Liang EVENT_PTR(cycles_t_adl),
5686f83d2f91SKan Liang EVENT_PTR(cycles_ct_adl),
5687f83d2f91SKan Liang NULL,
5688f83d2f91SKan Liang };
5689f83d2f91SKan Liang
5690f83d2f91SKan Liang FORMAT_ATTR_HYBRID(in_tx, hybrid_big);
5691f83d2f91SKan Liang FORMAT_ATTR_HYBRID(in_tx_cp, hybrid_big);
5692f83d2f91SKan Liang FORMAT_ATTR_HYBRID(offcore_rsp, hybrid_big_small);
5693f83d2f91SKan Liang FORMAT_ATTR_HYBRID(ldlat, hybrid_big_small);
5694f83d2f91SKan Liang FORMAT_ATTR_HYBRID(frontend, hybrid_big);
5695f83d2f91SKan Liang
569638aaf921SKan Liang #define ADL_HYBRID_RTM_FORMAT_ATTR \
569738aaf921SKan Liang FORMAT_HYBRID_PTR(in_tx), \
569838aaf921SKan Liang FORMAT_HYBRID_PTR(in_tx_cp)
569938aaf921SKan Liang
570038aaf921SKan Liang #define ADL_HYBRID_FORMAT_ATTR \
570138aaf921SKan Liang FORMAT_HYBRID_PTR(offcore_rsp), \
570238aaf921SKan Liang FORMAT_HYBRID_PTR(ldlat), \
570338aaf921SKan Liang FORMAT_HYBRID_PTR(frontend)
570438aaf921SKan Liang
5705f83d2f91SKan Liang static struct attribute *adl_hybrid_extra_attr_rtm[] = {
570638aaf921SKan Liang ADL_HYBRID_RTM_FORMAT_ATTR,
570738aaf921SKan Liang ADL_HYBRID_FORMAT_ATTR,
570838aaf921SKan Liang NULL
5709f83d2f91SKan Liang };
5710f83d2f91SKan Liang
5711f83d2f91SKan Liang static struct attribute *adl_hybrid_extra_attr[] = {
571238aaf921SKan Liang ADL_HYBRID_FORMAT_ATTR,
571338aaf921SKan Liang NULL
571438aaf921SKan Liang };
571538aaf921SKan Liang
571638aaf921SKan Liang FORMAT_ATTR_HYBRID(snoop_rsp, hybrid_small);
571738aaf921SKan Liang
571838aaf921SKan Liang static struct attribute *mtl_hybrid_extra_attr_rtm[] = {
571938aaf921SKan Liang ADL_HYBRID_RTM_FORMAT_ATTR,
572038aaf921SKan Liang ADL_HYBRID_FORMAT_ATTR,
572138aaf921SKan Liang FORMAT_HYBRID_PTR(snoop_rsp),
572238aaf921SKan Liang NULL
572338aaf921SKan Liang };
572438aaf921SKan Liang
572538aaf921SKan Liang static struct attribute *mtl_hybrid_extra_attr[] = {
572638aaf921SKan Liang ADL_HYBRID_FORMAT_ATTR,
572738aaf921SKan Liang FORMAT_HYBRID_PTR(snoop_rsp),
572838aaf921SKan Liang NULL
5729f83d2f91SKan Liang };
5730f83d2f91SKan Liang
is_attr_for_this_pmu(struct kobject * kobj,struct attribute * attr)573158ae30c2SKan Liang static bool is_attr_for_this_pmu(struct kobject *kobj, struct attribute *attr)
573258ae30c2SKan Liang {
573358ae30c2SKan Liang struct device *dev = kobj_to_dev(kobj);
573458ae30c2SKan Liang struct x86_hybrid_pmu *pmu =
573558ae30c2SKan Liang container_of(dev_get_drvdata(dev), struct x86_hybrid_pmu, pmu);
573658ae30c2SKan Liang struct perf_pmu_events_hybrid_attr *pmu_attr =
573758ae30c2SKan Liang container_of(attr, struct perf_pmu_events_hybrid_attr, attr.attr);
573858ae30c2SKan Liang
573958ae30c2SKan Liang return pmu->cpu_type & pmu_attr->pmu_type;
574058ae30c2SKan Liang }
574158ae30c2SKan Liang
hybrid_events_is_visible(struct kobject * kobj,struct attribute * attr,int i)574258ae30c2SKan Liang static umode_t hybrid_events_is_visible(struct kobject *kobj,
574358ae30c2SKan Liang struct attribute *attr, int i)
574458ae30c2SKan Liang {
574558ae30c2SKan Liang return is_attr_for_this_pmu(kobj, attr) ? attr->mode : 0;
574658ae30c2SKan Liang }
574758ae30c2SKan Liang
hybrid_find_supported_cpu(struct x86_hybrid_pmu * pmu)574858ae30c2SKan Liang static inline int hybrid_find_supported_cpu(struct x86_hybrid_pmu *pmu)
574958ae30c2SKan Liang {
575058ae30c2SKan Liang int cpu = cpumask_first(&pmu->supported_cpus);
575158ae30c2SKan Liang
575258ae30c2SKan Liang return (cpu >= nr_cpu_ids) ? -1 : cpu;
575358ae30c2SKan Liang }
575458ae30c2SKan Liang
hybrid_tsx_is_visible(struct kobject * kobj,struct attribute * attr,int i)575558ae30c2SKan Liang static umode_t hybrid_tsx_is_visible(struct kobject *kobj,
575658ae30c2SKan Liang struct attribute *attr, int i)
575758ae30c2SKan Liang {
575858ae30c2SKan Liang struct device *dev = kobj_to_dev(kobj);
575958ae30c2SKan Liang struct x86_hybrid_pmu *pmu =
576058ae30c2SKan Liang container_of(dev_get_drvdata(dev), struct x86_hybrid_pmu, pmu);
576158ae30c2SKan Liang int cpu = hybrid_find_supported_cpu(pmu);
576258ae30c2SKan Liang
576358ae30c2SKan Liang return (cpu >= 0) && is_attr_for_this_pmu(kobj, attr) && cpu_has(&cpu_data(cpu), X86_FEATURE_RTM) ? attr->mode : 0;
576458ae30c2SKan Liang }
576558ae30c2SKan Liang
hybrid_format_is_visible(struct kobject * kobj,struct attribute * attr,int i)576658ae30c2SKan Liang static umode_t hybrid_format_is_visible(struct kobject *kobj,
576758ae30c2SKan Liang struct attribute *attr, int i)
576858ae30c2SKan Liang {
576958ae30c2SKan Liang struct device *dev = kobj_to_dev(kobj);
577058ae30c2SKan Liang struct x86_hybrid_pmu *pmu =
577158ae30c2SKan Liang container_of(dev_get_drvdata(dev), struct x86_hybrid_pmu, pmu);
577258ae30c2SKan Liang struct perf_pmu_format_hybrid_attr *pmu_attr =
577358ae30c2SKan Liang container_of(attr, struct perf_pmu_format_hybrid_attr, attr.attr);
577458ae30c2SKan Liang int cpu = hybrid_find_supported_cpu(pmu);
577558ae30c2SKan Liang
577658ae30c2SKan Liang return (cpu >= 0) && (pmu->cpu_type & pmu_attr->pmu_type) ? attr->mode : 0;
577758ae30c2SKan Liang }
577858ae30c2SKan Liang
hybrid_td_is_visible(struct kobject * kobj,struct attribute * attr,int i)5779*475d99e6SKan Liang static umode_t hybrid_td_is_visible(struct kobject *kobj,
5780*475d99e6SKan Liang struct attribute *attr, int i)
5781*475d99e6SKan Liang {
5782*475d99e6SKan Liang struct device *dev = kobj_to_dev(kobj);
5783*475d99e6SKan Liang struct x86_hybrid_pmu *pmu =
5784*475d99e6SKan Liang container_of(dev_get_drvdata(dev), struct x86_hybrid_pmu, pmu);
5785*475d99e6SKan Liang
5786*475d99e6SKan Liang if (!is_attr_for_this_pmu(kobj, attr))
5787*475d99e6SKan Liang return 0;
5788*475d99e6SKan Liang
5789*475d99e6SKan Liang
5790*475d99e6SKan Liang /* Only the big core supports perf metrics */
5791*475d99e6SKan Liang if (pmu->cpu_type == hybrid_big)
5792*475d99e6SKan Liang return pmu->intel_cap.perf_metrics ? attr->mode : 0;
5793*475d99e6SKan Liang
5794*475d99e6SKan Liang return attr->mode;
5795*475d99e6SKan Liang }
5796*475d99e6SKan Liang
579758ae30c2SKan Liang static struct attribute_group hybrid_group_events_td = {
579858ae30c2SKan Liang .name = "events",
5799*475d99e6SKan Liang .is_visible = hybrid_td_is_visible,
580058ae30c2SKan Liang };
580158ae30c2SKan Liang
580258ae30c2SKan Liang static struct attribute_group hybrid_group_events_mem = {
580358ae30c2SKan Liang .name = "events",
580458ae30c2SKan Liang .is_visible = hybrid_events_is_visible,
580558ae30c2SKan Liang };
580658ae30c2SKan Liang
580758ae30c2SKan Liang static struct attribute_group hybrid_group_events_tsx = {
580858ae30c2SKan Liang .name = "events",
580958ae30c2SKan Liang .is_visible = hybrid_tsx_is_visible,
581058ae30c2SKan Liang };
581158ae30c2SKan Liang
581258ae30c2SKan Liang static struct attribute_group hybrid_group_format_extra = {
581358ae30c2SKan Liang .name = "format",
581458ae30c2SKan Liang .is_visible = hybrid_format_is_visible,
581558ae30c2SKan Liang };
581658ae30c2SKan Liang
intel_hybrid_get_attr_cpus(struct device * dev,struct device_attribute * attr,char * buf)581758ae30c2SKan Liang static ssize_t intel_hybrid_get_attr_cpus(struct device *dev,
581858ae30c2SKan Liang struct device_attribute *attr,
581958ae30c2SKan Liang char *buf)
582058ae30c2SKan Liang {
582158ae30c2SKan Liang struct x86_hybrid_pmu *pmu =
582258ae30c2SKan Liang container_of(dev_get_drvdata(dev), struct x86_hybrid_pmu, pmu);
582358ae30c2SKan Liang
582458ae30c2SKan Liang return cpumap_print_to_pagebuf(true, buf, &pmu->supported_cpus);
582558ae30c2SKan Liang }
582658ae30c2SKan Liang
582758ae30c2SKan Liang static DEVICE_ATTR(cpus, S_IRUGO, intel_hybrid_get_attr_cpus, NULL);
582858ae30c2SKan Liang static struct attribute *intel_hybrid_cpus_attrs[] = {
582958ae30c2SKan Liang &dev_attr_cpus.attr,
583058ae30c2SKan Liang NULL,
583158ae30c2SKan Liang };
583258ae30c2SKan Liang
583358ae30c2SKan Liang static struct attribute_group hybrid_group_cpus = {
583458ae30c2SKan Liang .attrs = intel_hybrid_cpus_attrs,
583558ae30c2SKan Liang };
583658ae30c2SKan Liang
583758ae30c2SKan Liang static const struct attribute_group *hybrid_attr_update[] = {
583858ae30c2SKan Liang &hybrid_group_events_td,
583958ae30c2SKan Liang &hybrid_group_events_mem,
584058ae30c2SKan Liang &hybrid_group_events_tsx,
584158ae30c2SKan Liang &group_caps_gen,
584258ae30c2SKan Liang &group_caps_lbr,
584358ae30c2SKan Liang &hybrid_group_format_extra,
584458ae30c2SKan Liang &group_default,
584558ae30c2SKan Liang &hybrid_group_cpus,
584658ae30c2SKan Liang NULL,
584758ae30c2SKan Liang };
584858ae30c2SKan Liang
5849baa0c833SJiri Olsa static struct attribute *empty_attrs;
5850baa0c833SJiri Olsa
intel_pmu_check_num_counters(int * num_counters,int * num_counters_fixed,u64 * intel_ctrl,u64 fixed_mask)5851b8c4d1a8SKan Liang static void intel_pmu_check_num_counters(int *num_counters,
5852b8c4d1a8SKan Liang int *num_counters_fixed,
5853b8c4d1a8SKan Liang u64 *intel_ctrl, u64 fixed_mask)
5854b8c4d1a8SKan Liang {
5855b8c4d1a8SKan Liang if (*num_counters > INTEL_PMC_MAX_GENERIC) {
5856b8c4d1a8SKan Liang WARN(1, KERN_ERR "hw perf events %d > max(%d), clipping!",
5857b8c4d1a8SKan Liang *num_counters, INTEL_PMC_MAX_GENERIC);
5858b8c4d1a8SKan Liang *num_counters = INTEL_PMC_MAX_GENERIC;
5859b8c4d1a8SKan Liang }
5860b8c4d1a8SKan Liang *intel_ctrl = (1ULL << *num_counters) - 1;
5861b8c4d1a8SKan Liang
5862b8c4d1a8SKan Liang if (*num_counters_fixed > INTEL_PMC_MAX_FIXED) {
5863b8c4d1a8SKan Liang WARN(1, KERN_ERR "hw perf events fixed %d > max(%d), clipping!",
5864b8c4d1a8SKan Liang *num_counters_fixed, INTEL_PMC_MAX_FIXED);
5865b8c4d1a8SKan Liang *num_counters_fixed = INTEL_PMC_MAX_FIXED;
5866b8c4d1a8SKan Liang }
5867b8c4d1a8SKan Liang
5868b8c4d1a8SKan Liang *intel_ctrl |= fixed_mask << INTEL_PMC_IDX_FIXED;
5869b8c4d1a8SKan Liang }
5870b8c4d1a8SKan Liang
intel_pmu_check_event_constraints(struct event_constraint * event_constraints,int num_counters,int num_counters_fixed,u64 intel_ctrl)5871bc14fe1bSKan Liang static void intel_pmu_check_event_constraints(struct event_constraint *event_constraints,
5872bc14fe1bSKan Liang int num_counters,
5873bc14fe1bSKan Liang int num_counters_fixed,
5874bc14fe1bSKan Liang u64 intel_ctrl)
5875bc14fe1bSKan Liang {
5876bc14fe1bSKan Liang struct event_constraint *c;
5877bc14fe1bSKan Liang
5878bc14fe1bSKan Liang if (!event_constraints)
5879bc14fe1bSKan Liang return;
5880bc14fe1bSKan Liang
5881bc14fe1bSKan Liang /*
5882bc14fe1bSKan Liang * event on fixed counter2 (REF_CYCLES) only works on this
5883bc14fe1bSKan Liang * counter, so do not extend mask to generic counters
5884bc14fe1bSKan Liang */
5885bc14fe1bSKan Liang for_each_event_constraint(c, event_constraints) {
5886bc14fe1bSKan Liang /*
5887bc14fe1bSKan Liang * Don't extend the topdown slots and metrics
5888bc14fe1bSKan Liang * events to the generic counters.
5889bc14fe1bSKan Liang */
5890bc14fe1bSKan Liang if (c->idxmsk64 & INTEL_PMC_MSK_TOPDOWN) {
5891bc14fe1bSKan Liang /*
5892bc14fe1bSKan Liang * Disable topdown slots and metrics events,
5893bc14fe1bSKan Liang * if slots event is not in CPUID.
5894bc14fe1bSKan Liang */
5895bc14fe1bSKan Liang if (!(INTEL_PMC_MSK_FIXED_SLOTS & intel_ctrl))
5896bc14fe1bSKan Liang c->idxmsk64 = 0;
5897bc14fe1bSKan Liang c->weight = hweight64(c->idxmsk64);
5898bc14fe1bSKan Liang continue;
5899bc14fe1bSKan Liang }
5900bc14fe1bSKan Liang
5901bc14fe1bSKan Liang if (c->cmask == FIXED_EVENT_FLAGS) {
5902bc14fe1bSKan Liang /* Disabled fixed counters which are not in CPUID */
5903bc14fe1bSKan Liang c->idxmsk64 &= intel_ctrl;
5904bc14fe1bSKan Liang
59054a263bf3SKan Liang /*
59064a263bf3SKan Liang * Don't extend the pseudo-encoding to the
59074a263bf3SKan Liang * generic counters
59084a263bf3SKan Liang */
59094a263bf3SKan Liang if (!use_fixed_pseudo_encoding(c->code))
5910bc14fe1bSKan Liang c->idxmsk64 |= (1ULL << num_counters) - 1;
5911bc14fe1bSKan Liang }
5912bc14fe1bSKan Liang c->idxmsk64 &=
5913bc14fe1bSKan Liang ~(~0ULL << (INTEL_PMC_IDX_FIXED + num_counters_fixed));
5914bc14fe1bSKan Liang c->weight = hweight64(c->idxmsk64);
5915bc14fe1bSKan Liang }
5916bc14fe1bSKan Liang }
5917bc14fe1bSKan Liang
intel_pmu_check_extra_regs(struct extra_reg * extra_regs)591834d5b61fSKan Liang static void intel_pmu_check_extra_regs(struct extra_reg *extra_regs)
591934d5b61fSKan Liang {
592034d5b61fSKan Liang struct extra_reg *er;
592134d5b61fSKan Liang
592234d5b61fSKan Liang /*
592334d5b61fSKan Liang * Access extra MSR may cause #GP under certain circumstances.
592434d5b61fSKan Liang * E.g. KVM doesn't support offcore event
592534d5b61fSKan Liang * Check all extra_regs here.
592634d5b61fSKan Liang */
592734d5b61fSKan Liang if (!extra_regs)
592834d5b61fSKan Liang return;
592934d5b61fSKan Liang
593034d5b61fSKan Liang for (er = extra_regs; er->msr; er++) {
593134d5b61fSKan Liang er->extra_msr_access = check_msr(er->msr, 0x11UL);
593234d5b61fSKan Liang /* Disable LBR select mapping */
593334d5b61fSKan Liang if ((er->idx == EXTRA_REG_LBR) && !er->extra_msr_access)
593434d5b61fSKan Liang x86_pmu.lbr_sel_map = NULL;
593534d5b61fSKan Liang }
593634d5b61fSKan Liang }
593734d5b61fSKan Liang
intel_pmu_check_hybrid_pmus(u64 fixed_mask)5938d9977c43SKan Liang static void intel_pmu_check_hybrid_pmus(u64 fixed_mask)
5939d9977c43SKan Liang {
5940d9977c43SKan Liang struct x86_hybrid_pmu *pmu;
5941d9977c43SKan Liang int i;
5942d9977c43SKan Liang
5943d9977c43SKan Liang for (i = 0; i < x86_pmu.num_hybrid_pmus; i++) {
5944d9977c43SKan Liang pmu = &x86_pmu.hybrid_pmu[i];
5945d9977c43SKan Liang
5946d9977c43SKan Liang intel_pmu_check_num_counters(&pmu->num_counters,
5947d9977c43SKan Liang &pmu->num_counters_fixed,
5948d9977c43SKan Liang &pmu->intel_ctrl,
5949d9977c43SKan Liang fixed_mask);
5950d9977c43SKan Liang
5951d9977c43SKan Liang if (pmu->intel_cap.perf_metrics) {
5952d9977c43SKan Liang pmu->intel_ctrl |= 1ULL << GLOBAL_CTRL_EN_PERF_METRICS;
5953d9977c43SKan Liang pmu->intel_ctrl |= INTEL_PMC_MSK_FIXED_SLOTS;
5954d9977c43SKan Liang }
5955d9977c43SKan Liang
5956d9977c43SKan Liang if (pmu->intel_cap.pebs_output_pt_available)
5957d9977c43SKan Liang pmu->pmu.capabilities |= PERF_PMU_CAP_AUX_OUTPUT;
5958d9977c43SKan Liang
5959d9977c43SKan Liang intel_pmu_check_event_constraints(pmu->event_constraints,
5960d9977c43SKan Liang pmu->num_counters,
5961d9977c43SKan Liang pmu->num_counters_fixed,
5962d9977c43SKan Liang pmu->intel_ctrl);
5963d9977c43SKan Liang
5964d9977c43SKan Liang intel_pmu_check_extra_regs(pmu->extra_regs);
5965d9977c43SKan Liang }
5966d9977c43SKan Liang }
5967d9977c43SKan Liang
is_mtl(u8 x86_model)596838aaf921SKan Liang static __always_inline bool is_mtl(u8 x86_model)
596938aaf921SKan Liang {
597038aaf921SKan Liang return (x86_model == INTEL_FAM6_METEORLAKE) ||
597138aaf921SKan Liang (x86_model == INTEL_FAM6_METEORLAKE_L);
597238aaf921SKan Liang }
597338aaf921SKan Liang
intel_pmu_init(void)5974e1069839SBorislav Petkov __init int intel_pmu_init(void)
5975e1069839SBorislav Petkov {
5976b6576880SJiri Olsa struct attribute **extra_skl_attr = &empty_attrs;
5977baa0c833SJiri Olsa struct attribute **extra_attr = &empty_attrs;
5978baa0c833SJiri Olsa struct attribute **td_attr = &empty_attrs;
5979baa0c833SJiri Olsa struct attribute **mem_attr = &empty_attrs;
5980baa0c833SJiri Olsa struct attribute **tsx_attr = &empty_attrs;
5981e1069839SBorislav Petkov union cpuid10_edx edx;
5982e1069839SBorislav Petkov union cpuid10_eax eax;
5983e1069839SBorislav Petkov union cpuid10_ebx ebx;
598432451614SKan Liang unsigned int fixed_mask;
5985faaeff98SKan Liang bool pmem = false;
5986e1069839SBorislav Petkov int version, i;
5987b00233b5SAndi Kleen char *name;
5988f83d2f91SKan Liang struct x86_hybrid_pmu *pmu;
5989e1069839SBorislav Petkov
5990e1069839SBorislav Petkov if (!cpu_has(&boot_cpu_data, X86_FEATURE_ARCH_PERFMON)) {
5991e1069839SBorislav Petkov switch (boot_cpu_data.x86) {
5992e1069839SBorislav Petkov case 0x6:
5993e1069839SBorislav Petkov return p6_pmu_init();
5994e1069839SBorislav Petkov case 0xb:
5995e1069839SBorislav Petkov return knc_pmu_init();
5996e1069839SBorislav Petkov case 0xf:
5997e1069839SBorislav Petkov return p4_pmu_init();
5998e1069839SBorislav Petkov }
5999e1069839SBorislav Petkov return -ENODEV;
6000e1069839SBorislav Petkov }
6001e1069839SBorislav Petkov
6002e1069839SBorislav Petkov /*
6003e1069839SBorislav Petkov * Check whether the Architectural PerfMon supports
6004e1069839SBorislav Petkov * Branch Misses Retired hw_event or not.
6005e1069839SBorislav Petkov */
600632451614SKan Liang cpuid(10, &eax.full, &ebx.full, &fixed_mask, &edx.full);
6007e1069839SBorislav Petkov if (eax.split.mask_length < ARCH_PERFMON_EVENTS_COUNT)
6008e1069839SBorislav Petkov return -ENODEV;
6009e1069839SBorislav Petkov
6010e1069839SBorislav Petkov version = eax.split.version_id;
6011e1069839SBorislav Petkov if (version < 2)
6012e1069839SBorislav Petkov x86_pmu = core_pmu;
6013e1069839SBorislav Petkov else
6014e1069839SBorislav Petkov x86_pmu = intel_pmu;
6015e1069839SBorislav Petkov
6016e1069839SBorislav Petkov x86_pmu.version = version;
6017e1069839SBorislav Petkov x86_pmu.num_counters = eax.split.num_counters;
6018e1069839SBorislav Petkov x86_pmu.cntval_bits = eax.split.bit_width;
6019e1069839SBorislav Petkov x86_pmu.cntval_mask = (1ULL << eax.split.bit_width) - 1;
6020e1069839SBorislav Petkov
6021e1069839SBorislav Petkov x86_pmu.events_maskl = ebx.full;
6022e1069839SBorislav Petkov x86_pmu.events_mask_len = eax.split.mask_length;
6023e1069839SBorislav Petkov
6024e1069839SBorislav Petkov x86_pmu.max_pebs_events = min_t(unsigned, MAX_PEBS_EVENTS, x86_pmu.num_counters);
60250d23dc34SPeter Zijlstra (Intel) x86_pmu.pebs_capable = PEBS_COUNTER_MASK;
6026e1069839SBorislav Petkov
6027e1069839SBorislav Petkov /*
6028e1069839SBorislav Petkov * Quirk: v2 perfmon does not report fixed-purpose events, so
6029f92b7604SImre Palik * assume at least 3 events, when not running in a hypervisor:
6030e1069839SBorislav Petkov */
603132451614SKan Liang if (version > 1 && version < 5) {
6032f92b7604SImre Palik int assume = 3 * !boot_cpu_has(X86_FEATURE_HYPERVISOR);
6033f92b7604SImre Palik
6034f92b7604SImre Palik x86_pmu.num_counters_fixed =
6035f92b7604SImre Palik max((int)edx.split.num_counters_fixed, assume);
603632451614SKan Liang
603732451614SKan Liang fixed_mask = (1L << x86_pmu.num_counters_fixed) - 1;
603832451614SKan Liang } else if (version >= 5)
603932451614SKan Liang x86_pmu.num_counters_fixed = fls(fixed_mask);
6040e1069839SBorislav Petkov
6041e1069839SBorislav Petkov if (boot_cpu_has(X86_FEATURE_PDCM)) {
6042e1069839SBorislav Petkov u64 capabilities;
6043e1069839SBorislav Petkov
6044e1069839SBorislav Petkov rdmsrl(MSR_IA32_PERF_CAPABILITIES, capabilities);
6045e1069839SBorislav Petkov x86_pmu.intel_cap.capabilities = capabilities;
6046e1069839SBorislav Petkov }
6047e1069839SBorislav Petkov
6048c301b1d8SKan Liang if (x86_pmu.intel_cap.lbr_format == LBR_FORMAT_32) {
60499f354a72SKan Liang x86_pmu.lbr_reset = intel_pmu_lbr_reset_32;
6050c301b1d8SKan Liang x86_pmu.lbr_read = intel_pmu_lbr_read_32;
6051c301b1d8SKan Liang }
60529f354a72SKan Liang
605347125db2SKan Liang if (boot_cpu_has(X86_FEATURE_ARCH_LBR))
605447125db2SKan Liang intel_pmu_arch_lbr_init();
605547125db2SKan Liang
6056e1069839SBorislav Petkov intel_ds_init();
6057e1069839SBorislav Petkov
6058e1069839SBorislav Petkov x86_add_quirk(intel_arch_events_quirk); /* Install first, so it runs last */
6059e1069839SBorislav Petkov
6060cadbaa03SStephane Eranian if (version >= 5) {
6061cadbaa03SStephane Eranian x86_pmu.intel_cap.anythread_deprecated = edx.split.anythread_deprecated;
6062cadbaa03SStephane Eranian if (x86_pmu.intel_cap.anythread_deprecated)
6063cadbaa03SStephane Eranian pr_cont(" AnyThread deprecated, ");
6064cadbaa03SStephane Eranian }
6065cadbaa03SStephane Eranian
6066e1069839SBorislav Petkov /*
6067e1069839SBorislav Petkov * Install the hw-cache-events table:
6068e1069839SBorislav Petkov */
6069e1069839SBorislav Petkov switch (boot_cpu_data.x86_model) {
6070ef5f9f47SDave Hansen case INTEL_FAM6_CORE_YONAH:
6071e1069839SBorislav Petkov pr_cont("Core events, ");
6072b00233b5SAndi Kleen name = "core";
6073e1069839SBorislav Petkov break;
6074e1069839SBorislav Petkov
6075ef5f9f47SDave Hansen case INTEL_FAM6_CORE2_MEROM:
6076e1069839SBorislav Petkov x86_add_quirk(intel_clovertown_quirk);
6077df561f66SGustavo A. R. Silva fallthrough;
60782b0fc374SGustavo A. R. Silva
6079ef5f9f47SDave Hansen case INTEL_FAM6_CORE2_MEROM_L:
6080ef5f9f47SDave Hansen case INTEL_FAM6_CORE2_PENRYN:
6081ef5f9f47SDave Hansen case INTEL_FAM6_CORE2_DUNNINGTON:
6082e1069839SBorislav Petkov memcpy(hw_cache_event_ids, core2_hw_cache_event_ids,
6083e1069839SBorislav Petkov sizeof(hw_cache_event_ids));
6084e1069839SBorislav Petkov
6085e1069839SBorislav Petkov intel_pmu_lbr_init_core();
6086e1069839SBorislav Petkov
6087e1069839SBorislav Petkov x86_pmu.event_constraints = intel_core2_event_constraints;
6088e1069839SBorislav Petkov x86_pmu.pebs_constraints = intel_core2_pebs_event_constraints;
6089e1069839SBorislav Petkov pr_cont("Core2 events, ");
6090b00233b5SAndi Kleen name = "core2";
6091e1069839SBorislav Petkov break;
6092e1069839SBorislav Petkov
6093ef5f9f47SDave Hansen case INTEL_FAM6_NEHALEM:
6094ef5f9f47SDave Hansen case INTEL_FAM6_NEHALEM_EP:
6095ef5f9f47SDave Hansen case INTEL_FAM6_NEHALEM_EX:
6096e1069839SBorislav Petkov memcpy(hw_cache_event_ids, nehalem_hw_cache_event_ids,
6097e1069839SBorislav Petkov sizeof(hw_cache_event_ids));
6098e1069839SBorislav Petkov memcpy(hw_cache_extra_regs, nehalem_hw_cache_extra_regs,
6099e1069839SBorislav Petkov sizeof(hw_cache_extra_regs));
6100e1069839SBorislav Petkov
6101e1069839SBorislav Petkov intel_pmu_lbr_init_nhm();
6102e1069839SBorislav Petkov
6103e1069839SBorislav Petkov x86_pmu.event_constraints = intel_nehalem_event_constraints;
6104e1069839SBorislav Petkov x86_pmu.pebs_constraints = intel_nehalem_pebs_event_constraints;
6105e1069839SBorislav Petkov x86_pmu.enable_all = intel_pmu_nhm_enable_all;
6106e1069839SBorislav Petkov x86_pmu.extra_regs = intel_nehalem_extra_regs;
610744d3bbb6SJosh Hunt x86_pmu.limit_period = nhm_limit_period;
6108e1069839SBorislav Petkov
6109d4ae5529SJiri Olsa mem_attr = nhm_mem_events_attrs;
6110e1069839SBorislav Petkov
6111e1069839SBorislav Petkov /* UOPS_ISSUED.STALLED_CYCLES */
6112e1069839SBorislav Petkov intel_perfmon_event_map[PERF_COUNT_HW_STALLED_CYCLES_FRONTEND] =
6113e1069839SBorislav Petkov X86_CONFIG(.event=0x0e, .umask=0x01, .inv=1, .cmask=1);
6114e1069839SBorislav Petkov /* UOPS_EXECUTED.CORE_ACTIVE_CYCLES,c=1,i=1 */
6115e1069839SBorislav Petkov intel_perfmon_event_map[PERF_COUNT_HW_STALLED_CYCLES_BACKEND] =
6116e1069839SBorislav Petkov X86_CONFIG(.event=0xb1, .umask=0x3f, .inv=1, .cmask=1);
6117e1069839SBorislav Petkov
6118e17dc653SAndi Kleen intel_pmu_pebs_data_source_nhm();
6119e1069839SBorislav Petkov x86_add_quirk(intel_nehalem_quirk);
612095298355SAndi Kleen x86_pmu.pebs_no_tlb = 1;
6121a5df70c3SAndi Kleen extra_attr = nhm_format_attr;
6122e1069839SBorislav Petkov
6123e1069839SBorislav Petkov pr_cont("Nehalem events, ");
6124b00233b5SAndi Kleen name = "nehalem";
6125e1069839SBorislav Petkov break;
6126e1069839SBorislav Petkov
6127f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_BONNELL:
6128f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_BONNELL_MID:
6129f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_SALTWELL:
6130f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_SALTWELL_MID:
6131f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_SALTWELL_TABLET:
6132e1069839SBorislav Petkov memcpy(hw_cache_event_ids, atom_hw_cache_event_ids,
6133e1069839SBorislav Petkov sizeof(hw_cache_event_ids));
6134e1069839SBorislav Petkov
6135e1069839SBorislav Petkov intel_pmu_lbr_init_atom();
6136e1069839SBorislav Petkov
6137e1069839SBorislav Petkov x86_pmu.event_constraints = intel_gen_event_constraints;
6138e1069839SBorislav Petkov x86_pmu.pebs_constraints = intel_atom_pebs_event_constraints;
6139e1069839SBorislav Petkov x86_pmu.pebs_aliases = intel_pebs_aliases_core2;
6140e1069839SBorislav Petkov pr_cont("Atom events, ");
6141b00233b5SAndi Kleen name = "bonnell";
6142e1069839SBorislav Petkov break;
6143e1069839SBorislav Petkov
6144f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_SILVERMONT:
61455ebb34edSPeter Zijlstra case INTEL_FAM6_ATOM_SILVERMONT_D:
6146f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_SILVERMONT_MID:
6147ef5f9f47SDave Hansen case INTEL_FAM6_ATOM_AIRMONT:
6148f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_AIRMONT_MID:
6149e1069839SBorislav Petkov memcpy(hw_cache_event_ids, slm_hw_cache_event_ids,
6150e1069839SBorislav Petkov sizeof(hw_cache_event_ids));
6151e1069839SBorislav Petkov memcpy(hw_cache_extra_regs, slm_hw_cache_extra_regs,
6152e1069839SBorislav Petkov sizeof(hw_cache_extra_regs));
6153e1069839SBorislav Petkov
6154f21d5adcSKan Liang intel_pmu_lbr_init_slm();
6155e1069839SBorislav Petkov
6156e1069839SBorislav Petkov x86_pmu.event_constraints = intel_slm_event_constraints;
6157e1069839SBorislav Petkov x86_pmu.pebs_constraints = intel_slm_pebs_event_constraints;
6158e1069839SBorislav Petkov x86_pmu.extra_regs = intel_slm_extra_regs;
6159e1069839SBorislav Petkov x86_pmu.flags |= PMU_FL_HAS_RSP_1;
6160baa0c833SJiri Olsa td_attr = slm_events_attrs;
6161a5df70c3SAndi Kleen extra_attr = slm_format_attr;
6162e1069839SBorislav Petkov pr_cont("Silvermont events, ");
6163b00233b5SAndi Kleen name = "silvermont";
6164e1069839SBorislav Petkov break;
6165e1069839SBorislav Petkov
6166ef5f9f47SDave Hansen case INTEL_FAM6_ATOM_GOLDMONT:
61675ebb34edSPeter Zijlstra case INTEL_FAM6_ATOM_GOLDMONT_D:
61688b92c3a7SKan Liang memcpy(hw_cache_event_ids, glm_hw_cache_event_ids,
61698b92c3a7SKan Liang sizeof(hw_cache_event_ids));
61708b92c3a7SKan Liang memcpy(hw_cache_extra_regs, glm_hw_cache_extra_regs,
61718b92c3a7SKan Liang sizeof(hw_cache_extra_regs));
61728b92c3a7SKan Liang
61738b92c3a7SKan Liang intel_pmu_lbr_init_skl();
61748b92c3a7SKan Liang
61758b92c3a7SKan Liang x86_pmu.event_constraints = intel_slm_event_constraints;
61768b92c3a7SKan Liang x86_pmu.pebs_constraints = intel_glm_pebs_event_constraints;
61778b92c3a7SKan Liang x86_pmu.extra_regs = intel_glm_extra_regs;
61788b92c3a7SKan Liang /*
61798b92c3a7SKan Liang * It's recommended to use CPU_CLK_UNHALTED.CORE_P + NPEBS
61808b92c3a7SKan Liang * for precise cycles.
61818b92c3a7SKan Liang * :pp is identical to :ppp
61828b92c3a7SKan Liang */
61838b92c3a7SKan Liang x86_pmu.pebs_aliases = NULL;
61848b92c3a7SKan Liang x86_pmu.pebs_prec_dist = true;
6185ccbebba4SAlexander Shishkin x86_pmu.lbr_pt_coexist = true;
61868b92c3a7SKan Liang x86_pmu.flags |= PMU_FL_HAS_RSP_1;
6187baa0c833SJiri Olsa td_attr = glm_events_attrs;
6188a5df70c3SAndi Kleen extra_attr = slm_format_attr;
61898b92c3a7SKan Liang pr_cont("Goldmont events, ");
6190b00233b5SAndi Kleen name = "goldmont";
61918b92c3a7SKan Liang break;
61928b92c3a7SKan Liang
6193f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_GOLDMONT_PLUS:
6194dd0b06b5SKan Liang memcpy(hw_cache_event_ids, glp_hw_cache_event_ids,
6195dd0b06b5SKan Liang sizeof(hw_cache_event_ids));
6196dd0b06b5SKan Liang memcpy(hw_cache_extra_regs, glp_hw_cache_extra_regs,
6197dd0b06b5SKan Liang sizeof(hw_cache_extra_regs));
6198dd0b06b5SKan Liang
6199dd0b06b5SKan Liang intel_pmu_lbr_init_skl();
6200dd0b06b5SKan Liang
6201dd0b06b5SKan Liang x86_pmu.event_constraints = intel_slm_event_constraints;
6202dd0b06b5SKan Liang x86_pmu.extra_regs = intel_glm_extra_regs;
6203dd0b06b5SKan Liang /*
6204dd0b06b5SKan Liang * It's recommended to use CPU_CLK_UNHALTED.CORE_P + NPEBS
6205dd0b06b5SKan Liang * for precise cycles.
6206dd0b06b5SKan Liang */
6207dd0b06b5SKan Liang x86_pmu.pebs_aliases = NULL;
6208dd0b06b5SKan Liang x86_pmu.pebs_prec_dist = true;
6209dd0b06b5SKan Liang x86_pmu.lbr_pt_coexist = true;
62100d23dc34SPeter Zijlstra (Intel) x86_pmu.pebs_capable = ~0ULL;
6211dd0b06b5SKan Liang x86_pmu.flags |= PMU_FL_HAS_RSP_1;
6212a38b0ba1SKan Liang x86_pmu.flags |= PMU_FL_PEBS_ALL;
6213dd0b06b5SKan Liang x86_pmu.get_event_constraints = glp_get_event_constraints;
6214baa0c833SJiri Olsa td_attr = glm_events_attrs;
6215dd0b06b5SKan Liang /* Goldmont Plus has 4-wide pipeline */
6216dd0b06b5SKan Liang event_attr_td_total_slots_scale_glm.event_str = "4";
6217a5df70c3SAndi Kleen extra_attr = slm_format_attr;
6218dd0b06b5SKan Liang pr_cont("Goldmont plus events, ");
6219b00233b5SAndi Kleen name = "goldmont_plus";
6220dd0b06b5SKan Liang break;
6221dd0b06b5SKan Liang
62225ebb34edSPeter Zijlstra case INTEL_FAM6_ATOM_TREMONT_D:
6223eda23b38SKan Liang case INTEL_FAM6_ATOM_TREMONT:
6224dbfd6388SKan Liang case INTEL_FAM6_ATOM_TREMONT_L:
62256daeb873SKan Liang x86_pmu.late_ack = true;
62266daeb873SKan Liang memcpy(hw_cache_event_ids, glp_hw_cache_event_ids,
62276daeb873SKan Liang sizeof(hw_cache_event_ids));
62286daeb873SKan Liang memcpy(hw_cache_extra_regs, tnt_hw_cache_extra_regs,
62296daeb873SKan Liang sizeof(hw_cache_extra_regs));
62306daeb873SKan Liang hw_cache_event_ids[C(ITLB)][C(OP_READ)][C(RESULT_ACCESS)] = -1;
62316daeb873SKan Liang
62326daeb873SKan Liang intel_pmu_lbr_init_skl();
62336daeb873SKan Liang
62346daeb873SKan Liang x86_pmu.event_constraints = intel_slm_event_constraints;
62356daeb873SKan Liang x86_pmu.extra_regs = intel_tnt_extra_regs;
62366daeb873SKan Liang /*
62376daeb873SKan Liang * It's recommended to use CPU_CLK_UNHALTED.CORE_P + NPEBS
62386daeb873SKan Liang * for precise cycles.
62396daeb873SKan Liang */
62406daeb873SKan Liang x86_pmu.pebs_aliases = NULL;
62416daeb873SKan Liang x86_pmu.pebs_prec_dist = true;
62426daeb873SKan Liang x86_pmu.lbr_pt_coexist = true;
62436daeb873SKan Liang x86_pmu.flags |= PMU_FL_HAS_RSP_1;
62446daeb873SKan Liang x86_pmu.get_event_constraints = tnt_get_event_constraints;
6245c2208046SKan Liang td_attr = tnt_events_attrs;
62466daeb873SKan Liang extra_attr = slm_format_attr;
62476daeb873SKan Liang pr_cont("Tremont events, ");
62486daeb873SKan Liang name = "Tremont";
62496daeb873SKan Liang break;
62506daeb873SKan Liang
6251882cdb06SPeter Zijlstra case INTEL_FAM6_ATOM_GRACEMONT:
625224919fdeSKan Liang x86_pmu.mid_ack = true;
625324919fdeSKan Liang memcpy(hw_cache_event_ids, glp_hw_cache_event_ids,
625424919fdeSKan Liang sizeof(hw_cache_event_ids));
625524919fdeSKan Liang memcpy(hw_cache_extra_regs, tnt_hw_cache_extra_regs,
625624919fdeSKan Liang sizeof(hw_cache_extra_regs));
625724919fdeSKan Liang hw_cache_event_ids[C(ITLB)][C(OP_READ)][C(RESULT_ACCESS)] = -1;
625824919fdeSKan Liang
625924919fdeSKan Liang x86_pmu.event_constraints = intel_slm_event_constraints;
626024919fdeSKan Liang x86_pmu.pebs_constraints = intel_grt_pebs_event_constraints;
626124919fdeSKan Liang x86_pmu.extra_regs = intel_grt_extra_regs;
626224919fdeSKan Liang
626324919fdeSKan Liang x86_pmu.pebs_aliases = NULL;
626424919fdeSKan Liang x86_pmu.pebs_prec_dist = true;
626524919fdeSKan Liang x86_pmu.pebs_block = true;
626624919fdeSKan Liang x86_pmu.lbr_pt_coexist = true;
626724919fdeSKan Liang x86_pmu.flags |= PMU_FL_HAS_RSP_1;
626824919fdeSKan Liang x86_pmu.flags |= PMU_FL_INSTR_LATENCY;
626924919fdeSKan Liang
627024919fdeSKan Liang intel_pmu_pebs_data_source_grt();
627124919fdeSKan Liang x86_pmu.pebs_latency_data = adl_latency_data_small;
627224919fdeSKan Liang x86_pmu.get_event_constraints = tnt_get_event_constraints;
627324919fdeSKan Liang x86_pmu.limit_period = spr_limit_period;
627424919fdeSKan Liang td_attr = tnt_events_attrs;
627524919fdeSKan Liang mem_attr = grt_mem_attrs;
627624919fdeSKan Liang extra_attr = nhm_format_attr;
627724919fdeSKan Liang pr_cont("Gracemont events, ");
627824919fdeSKan Liang name = "gracemont";
627924919fdeSKan Liang break;
628024919fdeSKan Liang
6281a430021fSKan Liang case INTEL_FAM6_ATOM_CRESTMONT:
6282a430021fSKan Liang case INTEL_FAM6_ATOM_CRESTMONT_X:
6283a430021fSKan Liang x86_pmu.mid_ack = true;
6284a430021fSKan Liang memcpy(hw_cache_event_ids, glp_hw_cache_event_ids,
6285a430021fSKan Liang sizeof(hw_cache_event_ids));
6286a430021fSKan Liang memcpy(hw_cache_extra_regs, tnt_hw_cache_extra_regs,
6287a430021fSKan Liang sizeof(hw_cache_extra_regs));
6288a430021fSKan Liang hw_cache_event_ids[C(ITLB)][C(OP_READ)][C(RESULT_ACCESS)] = -1;
6289a430021fSKan Liang
6290a430021fSKan Liang x86_pmu.event_constraints = intel_slm_event_constraints;
6291a430021fSKan Liang x86_pmu.pebs_constraints = intel_grt_pebs_event_constraints;
6292a430021fSKan Liang x86_pmu.extra_regs = intel_cmt_extra_regs;
6293a430021fSKan Liang
6294a430021fSKan Liang x86_pmu.pebs_aliases = NULL;
6295a430021fSKan Liang x86_pmu.pebs_prec_dist = true;
6296a430021fSKan Liang x86_pmu.lbr_pt_coexist = true;
6297a430021fSKan Liang x86_pmu.pebs_block = true;
6298a430021fSKan Liang x86_pmu.flags |= PMU_FL_HAS_RSP_1;
6299a430021fSKan Liang x86_pmu.flags |= PMU_FL_INSTR_LATENCY;
6300a430021fSKan Liang
6301a430021fSKan Liang intel_pmu_pebs_data_source_cmt();
6302a430021fSKan Liang x86_pmu.pebs_latency_data = mtl_latency_data_small;
6303a430021fSKan Liang x86_pmu.get_event_constraints = cmt_get_event_constraints;
6304a430021fSKan Liang x86_pmu.limit_period = spr_limit_period;
6305a430021fSKan Liang td_attr = cmt_events_attrs;
6306a430021fSKan Liang mem_attr = grt_mem_attrs;
6307a430021fSKan Liang extra_attr = cmt_format_attr;
6308a430021fSKan Liang pr_cont("Crestmont events, ");
6309a430021fSKan Liang name = "crestmont";
6310a430021fSKan Liang break;
6311a430021fSKan Liang
6312ef5f9f47SDave Hansen case INTEL_FAM6_WESTMERE:
6313ef5f9f47SDave Hansen case INTEL_FAM6_WESTMERE_EP:
6314ef5f9f47SDave Hansen case INTEL_FAM6_WESTMERE_EX:
6315e1069839SBorislav Petkov memcpy(hw_cache_event_ids, westmere_hw_cache_event_ids,
6316e1069839SBorislav Petkov sizeof(hw_cache_event_ids));
6317e1069839SBorislav Petkov memcpy(hw_cache_extra_regs, nehalem_hw_cache_extra_regs,
6318e1069839SBorislav Petkov sizeof(hw_cache_extra_regs));
6319e1069839SBorislav Petkov
6320e1069839SBorislav Petkov intel_pmu_lbr_init_nhm();
6321e1069839SBorislav Petkov
6322e1069839SBorislav Petkov x86_pmu.event_constraints = intel_westmere_event_constraints;
6323e1069839SBorislav Petkov x86_pmu.enable_all = intel_pmu_nhm_enable_all;
6324e1069839SBorislav Petkov x86_pmu.pebs_constraints = intel_westmere_pebs_event_constraints;
6325e1069839SBorislav Petkov x86_pmu.extra_regs = intel_westmere_extra_regs;
6326e1069839SBorislav Petkov x86_pmu.flags |= PMU_FL_HAS_RSP_1;
6327e1069839SBorislav Petkov
6328d4ae5529SJiri Olsa mem_attr = nhm_mem_events_attrs;
6329e1069839SBorislav Petkov
6330e1069839SBorislav Petkov /* UOPS_ISSUED.STALLED_CYCLES */
6331e1069839SBorislav Petkov intel_perfmon_event_map[PERF_COUNT_HW_STALLED_CYCLES_FRONTEND] =
6332e1069839SBorislav Petkov X86_CONFIG(.event=0x0e, .umask=0x01, .inv=1, .cmask=1);
6333e1069839SBorislav Petkov /* UOPS_EXECUTED.CORE_ACTIVE_CYCLES,c=1,i=1 */
6334e1069839SBorislav Petkov intel_perfmon_event_map[PERF_COUNT_HW_STALLED_CYCLES_BACKEND] =
6335e1069839SBorislav Petkov X86_CONFIG(.event=0xb1, .umask=0x3f, .inv=1, .cmask=1);
6336e1069839SBorislav Petkov
6337e17dc653SAndi Kleen intel_pmu_pebs_data_source_nhm();
6338a5df70c3SAndi Kleen extra_attr = nhm_format_attr;
6339e1069839SBorislav Petkov pr_cont("Westmere events, ");
6340b00233b5SAndi Kleen name = "westmere";
6341e1069839SBorislav Petkov break;
6342e1069839SBorislav Petkov
6343ef5f9f47SDave Hansen case INTEL_FAM6_SANDYBRIDGE:
6344ef5f9f47SDave Hansen case INTEL_FAM6_SANDYBRIDGE_X:
6345e1069839SBorislav Petkov x86_add_quirk(intel_sandybridge_quirk);
6346e1069839SBorislav Petkov x86_add_quirk(intel_ht_bug);
6347e1069839SBorislav Petkov memcpy(hw_cache_event_ids, snb_hw_cache_event_ids,
6348e1069839SBorislav Petkov sizeof(hw_cache_event_ids));
6349e1069839SBorislav Petkov memcpy(hw_cache_extra_regs, snb_hw_cache_extra_regs,
6350e1069839SBorislav Petkov sizeof(hw_cache_extra_regs));
6351e1069839SBorislav Petkov
6352e1069839SBorislav Petkov intel_pmu_lbr_init_snb();
6353e1069839SBorislav Petkov
6354e1069839SBorislav Petkov x86_pmu.event_constraints = intel_snb_event_constraints;
6355e1069839SBorislav Petkov x86_pmu.pebs_constraints = intel_snb_pebs_event_constraints;
6356e1069839SBorislav Petkov x86_pmu.pebs_aliases = intel_pebs_aliases_snb;
6357ef5f9f47SDave Hansen if (boot_cpu_data.x86_model == INTEL_FAM6_SANDYBRIDGE_X)
6358e1069839SBorislav Petkov x86_pmu.extra_regs = intel_snbep_extra_regs;
6359e1069839SBorislav Petkov else
6360e1069839SBorislav Petkov x86_pmu.extra_regs = intel_snb_extra_regs;
6361e1069839SBorislav Petkov
6362e1069839SBorislav Petkov
6363e1069839SBorislav Petkov /* all extra regs are per-cpu when HT is on */
6364e1069839SBorislav Petkov x86_pmu.flags |= PMU_FL_HAS_RSP_1;
6365e1069839SBorislav Petkov x86_pmu.flags |= PMU_FL_NO_HT_SHARING;
6366e1069839SBorislav Petkov
6367baa0c833SJiri Olsa td_attr = snb_events_attrs;
6368d4ae5529SJiri Olsa mem_attr = snb_mem_events_attrs;
6369e1069839SBorislav Petkov
6370e1069839SBorislav Petkov /* UOPS_ISSUED.ANY,c=1,i=1 to count stall cycles */
6371e1069839SBorislav Petkov intel_perfmon_event_map[PERF_COUNT_HW_STALLED_CYCLES_FRONTEND] =
6372e1069839SBorislav Petkov X86_CONFIG(.event=0x0e, .umask=0x01, .inv=1, .cmask=1);
6373e1069839SBorislav Petkov /* UOPS_DISPATCHED.THREAD,c=1,i=1 to count stall cycles*/
6374e1069839SBorislav Petkov intel_perfmon_event_map[PERF_COUNT_HW_STALLED_CYCLES_BACKEND] =
6375e1069839SBorislav Petkov X86_CONFIG(.event=0xb1, .umask=0x01, .inv=1, .cmask=1);
6376e1069839SBorislav Petkov
6377a5df70c3SAndi Kleen extra_attr = nhm_format_attr;
6378a5df70c3SAndi Kleen
6379e1069839SBorislav Petkov pr_cont("SandyBridge events, ");
6380b00233b5SAndi Kleen name = "sandybridge";
6381e1069839SBorislav Petkov break;
6382e1069839SBorislav Petkov
6383ef5f9f47SDave Hansen case INTEL_FAM6_IVYBRIDGE:
6384ef5f9f47SDave Hansen case INTEL_FAM6_IVYBRIDGE_X:
6385e1069839SBorislav Petkov x86_add_quirk(intel_ht_bug);
6386e1069839SBorislav Petkov memcpy(hw_cache_event_ids, snb_hw_cache_event_ids,
6387e1069839SBorislav Petkov sizeof(hw_cache_event_ids));
6388e1069839SBorislav Petkov /* dTLB-load-misses on IVB is different than SNB */
6389e1069839SBorislav Petkov hw_cache_event_ids[C(DTLB)][C(OP_READ)][C(RESULT_MISS)] = 0x8108; /* DTLB_LOAD_MISSES.DEMAND_LD_MISS_CAUSES_A_WALK */
6390e1069839SBorislav Petkov
6391e1069839SBorislav Petkov memcpy(hw_cache_extra_regs, snb_hw_cache_extra_regs,
6392e1069839SBorislav Petkov sizeof(hw_cache_extra_regs));
6393e1069839SBorislav Petkov
6394e1069839SBorislav Petkov intel_pmu_lbr_init_snb();
6395e1069839SBorislav Petkov
6396e1069839SBorislav Petkov x86_pmu.event_constraints = intel_ivb_event_constraints;
6397e1069839SBorislav Petkov x86_pmu.pebs_constraints = intel_ivb_pebs_event_constraints;
6398e1069839SBorislav Petkov x86_pmu.pebs_aliases = intel_pebs_aliases_ivb;
6399e1069839SBorislav Petkov x86_pmu.pebs_prec_dist = true;
6400ef5f9f47SDave Hansen if (boot_cpu_data.x86_model == INTEL_FAM6_IVYBRIDGE_X)
6401e1069839SBorislav Petkov x86_pmu.extra_regs = intel_snbep_extra_regs;
6402e1069839SBorislav Petkov else
6403e1069839SBorislav Petkov x86_pmu.extra_regs = intel_snb_extra_regs;
6404e1069839SBorislav Petkov /* all extra regs are per-cpu when HT is on */
6405e1069839SBorislav Petkov x86_pmu.flags |= PMU_FL_HAS_RSP_1;
6406e1069839SBorislav Petkov x86_pmu.flags |= PMU_FL_NO_HT_SHARING;
6407e1069839SBorislav Petkov
6408baa0c833SJiri Olsa td_attr = snb_events_attrs;
6409d4ae5529SJiri Olsa mem_attr = snb_mem_events_attrs;
6410e1069839SBorislav Petkov
6411e1069839SBorislav Petkov /* UOPS_ISSUED.ANY,c=1,i=1 to count stall cycles */
6412e1069839SBorislav Petkov intel_perfmon_event_map[PERF_COUNT_HW_STALLED_CYCLES_FRONTEND] =
6413e1069839SBorislav Petkov X86_CONFIG(.event=0x0e, .umask=0x01, .inv=1, .cmask=1);
6414e1069839SBorislav Petkov
6415a5df70c3SAndi Kleen extra_attr = nhm_format_attr;
6416a5df70c3SAndi Kleen
6417e1069839SBorislav Petkov pr_cont("IvyBridge events, ");
6418b00233b5SAndi Kleen name = "ivybridge";
6419e1069839SBorislav Petkov break;
6420e1069839SBorislav Petkov
6421e1069839SBorislav Petkov
6422c66f78a6SPeter Zijlstra case INTEL_FAM6_HASWELL:
6423ef5f9f47SDave Hansen case INTEL_FAM6_HASWELL_X:
6424af239c44SPeter Zijlstra case INTEL_FAM6_HASWELL_L:
64255e741407SPeter Zijlstra case INTEL_FAM6_HASWELL_G:
6426e1069839SBorislav Petkov x86_add_quirk(intel_ht_bug);
64279b545c04SAndi Kleen x86_add_quirk(intel_pebs_isolation_quirk);
6428e1069839SBorislav Petkov x86_pmu.late_ack = true;
6429e1069839SBorislav Petkov memcpy(hw_cache_event_ids, hsw_hw_cache_event_ids, sizeof(hw_cache_event_ids));
6430e1069839SBorislav Petkov memcpy(hw_cache_extra_regs, hsw_hw_cache_extra_regs, sizeof(hw_cache_extra_regs));
6431e1069839SBorislav Petkov
6432e1069839SBorislav Petkov intel_pmu_lbr_init_hsw();
6433e1069839SBorislav Petkov
6434e1069839SBorislav Petkov x86_pmu.event_constraints = intel_hsw_event_constraints;
6435e1069839SBorislav Petkov x86_pmu.pebs_constraints = intel_hsw_pebs_event_constraints;
6436e1069839SBorislav Petkov x86_pmu.extra_regs = intel_snbep_extra_regs;
6437e1069839SBorislav Petkov x86_pmu.pebs_aliases = intel_pebs_aliases_ivb;
6438e1069839SBorislav Petkov x86_pmu.pebs_prec_dist = true;
6439e1069839SBorislav Petkov /* all extra regs are per-cpu when HT is on */
6440e1069839SBorislav Petkov x86_pmu.flags |= PMU_FL_HAS_RSP_1;
6441e1069839SBorislav Petkov x86_pmu.flags |= PMU_FL_NO_HT_SHARING;
6442e1069839SBorislav Petkov
6443e1069839SBorislav Petkov x86_pmu.hw_config = hsw_hw_config;
6444e1069839SBorislav Petkov x86_pmu.get_event_constraints = hsw_get_event_constraints;
64450eaf812aSKan Liang x86_pmu.limit_period = hsw_limit_period;
6446e1069839SBorislav Petkov x86_pmu.lbr_double_abort = true;
6447a5df70c3SAndi Kleen extra_attr = boot_cpu_has(X86_FEATURE_RTM) ?
6448a5df70c3SAndi Kleen hsw_format_attr : nhm_format_attr;
6449baa0c833SJiri Olsa td_attr = hsw_events_attrs;
6450d4ae5529SJiri Olsa mem_attr = hsw_mem_events_attrs;
6451d4ae5529SJiri Olsa tsx_attr = hsw_tsx_events_attrs;
6452e1069839SBorislav Petkov pr_cont("Haswell events, ");
6453b00233b5SAndi Kleen name = "haswell";
6454e1069839SBorislav Petkov break;
6455e1069839SBorislav Petkov
6456c66f78a6SPeter Zijlstra case INTEL_FAM6_BROADWELL:
64575ebb34edSPeter Zijlstra case INTEL_FAM6_BROADWELL_D:
64585e741407SPeter Zijlstra case INTEL_FAM6_BROADWELL_G:
6459ef5f9f47SDave Hansen case INTEL_FAM6_BROADWELL_X:
64609b545c04SAndi Kleen x86_add_quirk(intel_pebs_isolation_quirk);
6461e1069839SBorislav Petkov x86_pmu.late_ack = true;
6462e1069839SBorislav Petkov memcpy(hw_cache_event_ids, hsw_hw_cache_event_ids, sizeof(hw_cache_event_ids));
6463e1069839SBorislav Petkov memcpy(hw_cache_extra_regs, hsw_hw_cache_extra_regs, sizeof(hw_cache_extra_regs));
6464e1069839SBorislav Petkov
6465e1069839SBorislav Petkov /* L3_MISS_LOCAL_DRAM is BIT(26) in Broadwell */
6466e1069839SBorislav Petkov hw_cache_extra_regs[C(LL)][C(OP_READ)][C(RESULT_MISS)] = HSW_DEMAND_READ |
6467e1069839SBorislav Petkov BDW_L3_MISS|HSW_SNOOP_DRAM;
6468e1069839SBorislav Petkov hw_cache_extra_regs[C(LL)][C(OP_WRITE)][C(RESULT_MISS)] = HSW_DEMAND_WRITE|BDW_L3_MISS|
6469e1069839SBorislav Petkov HSW_SNOOP_DRAM;
6470e1069839SBorislav Petkov hw_cache_extra_regs[C(NODE)][C(OP_READ)][C(RESULT_ACCESS)] = HSW_DEMAND_READ|
6471e1069839SBorislav Petkov BDW_L3_MISS_LOCAL|HSW_SNOOP_DRAM;
6472e1069839SBorislav Petkov hw_cache_extra_regs[C(NODE)][C(OP_WRITE)][C(RESULT_ACCESS)] = HSW_DEMAND_WRITE|
6473e1069839SBorislav Petkov BDW_L3_MISS_LOCAL|HSW_SNOOP_DRAM;
6474e1069839SBorislav Petkov
6475e1069839SBorislav Petkov intel_pmu_lbr_init_hsw();
6476e1069839SBorislav Petkov
6477e1069839SBorislav Petkov x86_pmu.event_constraints = intel_bdw_event_constraints;
6478b3e62463SStephane Eranian x86_pmu.pebs_constraints = intel_bdw_pebs_event_constraints;
6479e1069839SBorislav Petkov x86_pmu.extra_regs = intel_snbep_extra_regs;
6480e1069839SBorislav Petkov x86_pmu.pebs_aliases = intel_pebs_aliases_ivb;
6481e1069839SBorislav Petkov x86_pmu.pebs_prec_dist = true;
6482e1069839SBorislav Petkov /* all extra regs are per-cpu when HT is on */
6483e1069839SBorislav Petkov x86_pmu.flags |= PMU_FL_HAS_RSP_1;
6484e1069839SBorislav Petkov x86_pmu.flags |= PMU_FL_NO_HT_SHARING;
6485e1069839SBorislav Petkov
6486e1069839SBorislav Petkov x86_pmu.hw_config = hsw_hw_config;
6487e1069839SBorislav Petkov x86_pmu.get_event_constraints = hsw_get_event_constraints;
6488e1069839SBorislav Petkov x86_pmu.limit_period = bdw_limit_period;
6489a5df70c3SAndi Kleen extra_attr = boot_cpu_has(X86_FEATURE_RTM) ?
6490a5df70c3SAndi Kleen hsw_format_attr : nhm_format_attr;
6491baa0c833SJiri Olsa td_attr = hsw_events_attrs;
6492d4ae5529SJiri Olsa mem_attr = hsw_mem_events_attrs;
6493d4ae5529SJiri Olsa tsx_attr = hsw_tsx_events_attrs;
6494e1069839SBorislav Petkov pr_cont("Broadwell events, ");
6495b00233b5SAndi Kleen name = "broadwell";
6496e1069839SBorislav Petkov break;
6497e1069839SBorislav Petkov
6498ef5f9f47SDave Hansen case INTEL_FAM6_XEON_PHI_KNL:
6499608284bfSPiotr Luc case INTEL_FAM6_XEON_PHI_KNM:
6500e1069839SBorislav Petkov memcpy(hw_cache_event_ids,
6501e1069839SBorislav Petkov slm_hw_cache_event_ids, sizeof(hw_cache_event_ids));
6502e1069839SBorislav Petkov memcpy(hw_cache_extra_regs,
6503e1069839SBorislav Petkov knl_hw_cache_extra_regs, sizeof(hw_cache_extra_regs));
6504e1069839SBorislav Petkov intel_pmu_lbr_init_knl();
6505e1069839SBorislav Petkov
6506e1069839SBorislav Petkov x86_pmu.event_constraints = intel_slm_event_constraints;
6507e1069839SBorislav Petkov x86_pmu.pebs_constraints = intel_slm_pebs_event_constraints;
6508e1069839SBorislav Petkov x86_pmu.extra_regs = intel_knl_extra_regs;
6509e1069839SBorislav Petkov
6510e1069839SBorislav Petkov /* all extra regs are per-cpu when HT is on */
6511e1069839SBorislav Petkov x86_pmu.flags |= PMU_FL_HAS_RSP_1;
6512e1069839SBorislav Petkov x86_pmu.flags |= PMU_FL_NO_HT_SHARING;
6513a5df70c3SAndi Kleen extra_attr = slm_format_attr;
6514608284bfSPiotr Luc pr_cont("Knights Landing/Mill events, ");
6515b00233b5SAndi Kleen name = "knights-landing";
6516e1069839SBorislav Petkov break;
6517e1069839SBorislav Petkov
6518faaeff98SKan Liang case INTEL_FAM6_SKYLAKE_X:
6519faaeff98SKan Liang pmem = true;
6520df561f66SGustavo A. R. Silva fallthrough;
6521af239c44SPeter Zijlstra case INTEL_FAM6_SKYLAKE_L:
6522c66f78a6SPeter Zijlstra case INTEL_FAM6_SKYLAKE:
6523af239c44SPeter Zijlstra case INTEL_FAM6_KABYLAKE_L:
6524c66f78a6SPeter Zijlstra case INTEL_FAM6_KABYLAKE:
65259066288bSKan Liang case INTEL_FAM6_COMETLAKE_L:
65269066288bSKan Liang case INTEL_FAM6_COMETLAKE:
65279b545c04SAndi Kleen x86_add_quirk(intel_pebs_isolation_quirk);
6528e1069839SBorislav Petkov x86_pmu.late_ack = true;
6529e1069839SBorislav Petkov memcpy(hw_cache_event_ids, skl_hw_cache_event_ids, sizeof(hw_cache_event_ids));
6530e1069839SBorislav Petkov memcpy(hw_cache_extra_regs, skl_hw_cache_extra_regs, sizeof(hw_cache_extra_regs));
6531e1069839SBorislav Petkov intel_pmu_lbr_init_skl();
6532e1069839SBorislav Petkov
6533a39fcae7SAndi Kleen /* INT_MISC.RECOVERY_CYCLES has umask 1 in Skylake */
6534a39fcae7SAndi Kleen event_attr_td_recovery_bubbles.event_str_noht =
6535a39fcae7SAndi Kleen "event=0xd,umask=0x1,cmask=1";
6536a39fcae7SAndi Kleen event_attr_td_recovery_bubbles.event_str_ht =
6537a39fcae7SAndi Kleen "event=0xd,umask=0x1,cmask=1,any=1";
6538a39fcae7SAndi Kleen
6539e1069839SBorislav Petkov x86_pmu.event_constraints = intel_skl_event_constraints;
6540e1069839SBorislav Petkov x86_pmu.pebs_constraints = intel_skl_pebs_event_constraints;
6541e1069839SBorislav Petkov x86_pmu.extra_regs = intel_skl_extra_regs;
6542e1069839SBorislav Petkov x86_pmu.pebs_aliases = intel_pebs_aliases_skl;
6543e1069839SBorislav Petkov x86_pmu.pebs_prec_dist = true;
6544e1069839SBorislav Petkov /* all extra regs are per-cpu when HT is on */
6545e1069839SBorislav Petkov x86_pmu.flags |= PMU_FL_HAS_RSP_1;
6546e1069839SBorislav Petkov x86_pmu.flags |= PMU_FL_NO_HT_SHARING;
6547e1069839SBorislav Petkov
6548e1069839SBorislav Petkov x86_pmu.hw_config = hsw_hw_config;
6549e1069839SBorislav Petkov x86_pmu.get_event_constraints = hsw_get_event_constraints;
6550a5df70c3SAndi Kleen extra_attr = boot_cpu_has(X86_FEATURE_RTM) ?
6551a5df70c3SAndi Kleen hsw_format_attr : nhm_format_attr;
6552b6576880SJiri Olsa extra_skl_attr = skl_format_attr;
6553baa0c833SJiri Olsa td_attr = hsw_events_attrs;
6554d4ae5529SJiri Olsa mem_attr = hsw_mem_events_attrs;
6555d4ae5529SJiri Olsa tsx_attr = hsw_tsx_events_attrs;
6556faaeff98SKan Liang intel_pmu_pebs_data_source_skl(pmem);
6557400816f6SPeter Zijlstra (Intel)
6558ad3c2e17SPawan Gupta /*
6559ad3c2e17SPawan Gupta * Processors with CPUID.RTM_ALWAYS_ABORT have TSX deprecated by default.
6560ad3c2e17SPawan Gupta * TSX force abort hooks are not required on these systems. Only deploy
6561ad3c2e17SPawan Gupta * workaround when microcode has not enabled X86_FEATURE_RTM_ALWAYS_ABORT.
6562ad3c2e17SPawan Gupta */
6563ad3c2e17SPawan Gupta if (boot_cpu_has(X86_FEATURE_TSX_FORCE_ABORT) &&
6564ad3c2e17SPawan Gupta !boot_cpu_has(X86_FEATURE_RTM_ALWAYS_ABORT)) {
6565400816f6SPeter Zijlstra (Intel) x86_pmu.flags |= PMU_FL_TFA;
6566400816f6SPeter Zijlstra (Intel) x86_pmu.get_event_constraints = tfa_get_event_constraints;
6567400816f6SPeter Zijlstra (Intel) x86_pmu.enable_all = intel_tfa_pmu_enable_all;
6568400816f6SPeter Zijlstra (Intel) x86_pmu.commit_scheduling = intel_tfa_commit_scheduling;
6569400816f6SPeter Zijlstra (Intel) }
6570400816f6SPeter Zijlstra (Intel)
6571e1069839SBorislav Petkov pr_cont("Skylake events, ");
6572b00233b5SAndi Kleen name = "skylake";
6573e1069839SBorislav Petkov break;
6574e1069839SBorislav Petkov
6575faaeff98SKan Liang case INTEL_FAM6_ICELAKE_X:
65765ebb34edSPeter Zijlstra case INTEL_FAM6_ICELAKE_D:
6577fb358e0bSLike Xu x86_pmu.pebs_ept = 1;
6578faaeff98SKan Liang pmem = true;
6579df561f66SGustavo A. R. Silva fallthrough;
6580af239c44SPeter Zijlstra case INTEL_FAM6_ICELAKE_L:
6581c66f78a6SPeter Zijlstra case INTEL_FAM6_ICELAKE:
658223645a76SKan Liang case INTEL_FAM6_TIGERLAKE_L:
658323645a76SKan Liang case INTEL_FAM6_TIGERLAKE:
6584b14d0db5SKan Liang case INTEL_FAM6_ROCKETLAKE:
658560176089SKan Liang x86_pmu.late_ack = true;
658660176089SKan Liang memcpy(hw_cache_event_ids, skl_hw_cache_event_ids, sizeof(hw_cache_event_ids));
658760176089SKan Liang memcpy(hw_cache_extra_regs, skl_hw_cache_extra_regs, sizeof(hw_cache_extra_regs));
658860176089SKan Liang hw_cache_event_ids[C(ITLB)][C(OP_READ)][C(RESULT_ACCESS)] = -1;
658960176089SKan Liang intel_pmu_lbr_init_skl();
659060176089SKan Liang
659160176089SKan Liang x86_pmu.event_constraints = intel_icl_event_constraints;
659260176089SKan Liang x86_pmu.pebs_constraints = intel_icl_pebs_event_constraints;
659360176089SKan Liang x86_pmu.extra_regs = intel_icl_extra_regs;
659460176089SKan Liang x86_pmu.pebs_aliases = NULL;
659560176089SKan Liang x86_pmu.pebs_prec_dist = true;
659660176089SKan Liang x86_pmu.flags |= PMU_FL_HAS_RSP_1;
659760176089SKan Liang x86_pmu.flags |= PMU_FL_NO_HT_SHARING;
659860176089SKan Liang
659960176089SKan Liang x86_pmu.hw_config = hsw_hw_config;
660060176089SKan Liang x86_pmu.get_event_constraints = icl_get_event_constraints;
660160176089SKan Liang extra_attr = boot_cpu_has(X86_FEATURE_RTM) ?
660260176089SKan Liang hsw_format_attr : nhm_format_attr;
6603b6576880SJiri Olsa extra_skl_attr = skl_format_attr;
6604baa0c833SJiri Olsa mem_attr = icl_events_attrs;
660559a854e2SKan Liang td_attr = icl_td_events_attrs;
6606baa0c833SJiri Olsa tsx_attr = icl_tsx_events_attrs;
660746b72e1bSKan Liang x86_pmu.rtm_abort_event = X86_CONFIG(.event=0xc9, .umask=0x04);
660860176089SKan Liang x86_pmu.lbr_pt_coexist = true;
6609faaeff98SKan Liang intel_pmu_pebs_data_source_skl(pmem);
66101ab5f235SKan Liang x86_pmu.num_topdown_events = 4;
66111acab2e0SPeter Zijlstra static_call_update(intel_pmu_update_topdown_event,
66121acab2e0SPeter Zijlstra &icl_update_topdown_event);
661323685167SPeter Zijlstra static_call_update(intel_pmu_set_topdown_event_period,
661423685167SPeter Zijlstra &icl_set_topdown_event_period);
661560176089SKan Liang pr_cont("Icelake events, ");
661660176089SKan Liang name = "icelake";
661760176089SKan Liang break;
661860176089SKan Liang
661961b985e3SKan Liang case INTEL_FAM6_SAPPHIRERAPIDS_X:
66206795e558SKan Liang case INTEL_FAM6_EMERALDRAPIDS_X:
6621bc4000fdSKan Liang x86_pmu.flags |= PMU_FL_MEM_LOADS_AUX;
6622a6742cb9SKan Liang x86_pmu.extra_regs = intel_spr_extra_regs;
6623bc4000fdSKan Liang fallthrough;
6624bc4000fdSKan Liang case INTEL_FAM6_GRANITERAPIDS_X:
6625bc4000fdSKan Liang case INTEL_FAM6_GRANITERAPIDS_D:
662661b985e3SKan Liang pmem = true;
662761b985e3SKan Liang x86_pmu.late_ack = true;
662861b985e3SKan Liang memcpy(hw_cache_event_ids, spr_hw_cache_event_ids, sizeof(hw_cache_event_ids));
662961b985e3SKan Liang memcpy(hw_cache_extra_regs, spr_hw_cache_extra_regs, sizeof(hw_cache_extra_regs));
663061b985e3SKan Liang
663161b985e3SKan Liang x86_pmu.event_constraints = intel_spr_event_constraints;
663261b985e3SKan Liang x86_pmu.pebs_constraints = intel_spr_pebs_event_constraints;
6633a6742cb9SKan Liang if (!x86_pmu.extra_regs)
6634a6742cb9SKan Liang x86_pmu.extra_regs = intel_gnr_extra_regs;
663561b985e3SKan Liang x86_pmu.limit_period = spr_limit_period;
663613738a36SLike Xu x86_pmu.pebs_ept = 1;
663761b985e3SKan Liang x86_pmu.pebs_aliases = NULL;
663861b985e3SKan Liang x86_pmu.pebs_prec_dist = true;
663961b985e3SKan Liang x86_pmu.pebs_block = true;
664061b985e3SKan Liang x86_pmu.flags |= PMU_FL_HAS_RSP_1;
664161b985e3SKan Liang x86_pmu.flags |= PMU_FL_NO_HT_SHARING;
664261b985e3SKan Liang x86_pmu.flags |= PMU_FL_INSTR_LATENCY;
664361b985e3SKan Liang
664461b985e3SKan Liang x86_pmu.hw_config = hsw_hw_config;
664561b985e3SKan Liang x86_pmu.get_event_constraints = spr_get_event_constraints;
664661b985e3SKan Liang extra_attr = boot_cpu_has(X86_FEATURE_RTM) ?
664761b985e3SKan Liang hsw_format_attr : nhm_format_attr;
664861b985e3SKan Liang extra_skl_attr = skl_format_attr;
664961b985e3SKan Liang mem_attr = spr_events_attrs;
665061b985e3SKan Liang td_attr = spr_td_events_attrs;
665161b985e3SKan Liang tsx_attr = spr_tsx_events_attrs;
665261b985e3SKan Liang x86_pmu.rtm_abort_event = X86_CONFIG(.event=0xc9, .umask=0x04);
665361b985e3SKan Liang x86_pmu.lbr_pt_coexist = true;
665461b985e3SKan Liang intel_pmu_pebs_data_source_skl(pmem);
665561b985e3SKan Liang x86_pmu.num_topdown_events = 8;
66561acab2e0SPeter Zijlstra static_call_update(intel_pmu_update_topdown_event,
66571acab2e0SPeter Zijlstra &icl_update_topdown_event);
665823685167SPeter Zijlstra static_call_update(intel_pmu_set_topdown_event_period,
665923685167SPeter Zijlstra &icl_set_topdown_event_period);
666061b985e3SKan Liang pr_cont("Sapphire Rapids events, ");
666161b985e3SKan Liang name = "sapphire_rapids";
666261b985e3SKan Liang break;
666361b985e3SKan Liang
6664f83d2f91SKan Liang case INTEL_FAM6_ALDERLAKE:
6665f83d2f91SKan Liang case INTEL_FAM6_ALDERLAKE_L:
6666c61759e5SKan Liang case INTEL_FAM6_RAPTORLAKE:
6667c2a960f7SKan Liang case INTEL_FAM6_RAPTORLAKE_P:
666850b0c97bSKan Liang case INTEL_FAM6_RAPTORLAKE_S:
666938aaf921SKan Liang case INTEL_FAM6_METEORLAKE:
667038aaf921SKan Liang case INTEL_FAM6_METEORLAKE_L:
6671f83d2f91SKan Liang /*
6672f83d2f91SKan Liang * Alder Lake has 2 types of CPU, core and atom.
6673f83d2f91SKan Liang *
6674f83d2f91SKan Liang * Initialize the common PerfMon capabilities here.
6675f83d2f91SKan Liang */
6676f83d2f91SKan Liang x86_pmu.hybrid_pmu = kcalloc(X86_HYBRID_NUM_PMUS,
6677f83d2f91SKan Liang sizeof(struct x86_hybrid_pmu),
6678f83d2f91SKan Liang GFP_KERNEL);
6679f83d2f91SKan Liang if (!x86_pmu.hybrid_pmu)
6680f83d2f91SKan Liang return -ENOMEM;
6681f83d2f91SKan Liang static_branch_enable(&perf_is_hybrid);
6682f83d2f91SKan Liang x86_pmu.num_hybrid_pmus = X86_HYBRID_NUM_PMUS;
6683f83d2f91SKan Liang
6684f83d2f91SKan Liang x86_pmu.pebs_aliases = NULL;
6685f83d2f91SKan Liang x86_pmu.pebs_prec_dist = true;
6686f83d2f91SKan Liang x86_pmu.pebs_block = true;
6687f83d2f91SKan Liang x86_pmu.flags |= PMU_FL_HAS_RSP_1;
6688f83d2f91SKan Liang x86_pmu.flags |= PMU_FL_NO_HT_SHARING;
6689f83d2f91SKan Liang x86_pmu.flags |= PMU_FL_INSTR_LATENCY;
6690f83d2f91SKan Liang x86_pmu.lbr_pt_coexist = true;
669139a41278SKan Liang x86_pmu.pebs_latency_data = adl_latency_data_small;
6692f83d2f91SKan Liang x86_pmu.num_topdown_events = 8;
66931acab2e0SPeter Zijlstra static_call_update(intel_pmu_update_topdown_event,
66941acab2e0SPeter Zijlstra &adl_update_topdown_event);
669523685167SPeter Zijlstra static_call_update(intel_pmu_set_topdown_event_period,
669623685167SPeter Zijlstra &adl_set_topdown_event_period);
6697f83d2f91SKan Liang
6698bd275681SPeter Zijlstra x86_pmu.filter = intel_pmu_filter;
6699f83d2f91SKan Liang x86_pmu.get_event_constraints = adl_get_event_constraints;
6700f83d2f91SKan Liang x86_pmu.hw_config = adl_hw_config;
6701f83d2f91SKan Liang x86_pmu.limit_period = spr_limit_period;
6702f83d2f91SKan Liang x86_pmu.get_hybrid_cpu_type = adl_get_hybrid_cpu_type;
6703f83d2f91SKan Liang /*
6704f83d2f91SKan Liang * The rtm_abort_event is used to check whether to enable GPRs
6705f83d2f91SKan Liang * for the RTM abort event. Atom doesn't have the RTM abort
6706f83d2f91SKan Liang * event. There is no harmful to set it in the common
6707f83d2f91SKan Liang * x86_pmu.rtm_abort_event.
6708f83d2f91SKan Liang */
6709f83d2f91SKan Liang x86_pmu.rtm_abort_event = X86_CONFIG(.event=0xc9, .umask=0x04);
6710f83d2f91SKan Liang
6711f83d2f91SKan Liang td_attr = adl_hybrid_events_attrs;
6712f83d2f91SKan Liang mem_attr = adl_hybrid_mem_attrs;
6713f83d2f91SKan Liang tsx_attr = adl_hybrid_tsx_attrs;
6714f83d2f91SKan Liang extra_attr = boot_cpu_has(X86_FEATURE_RTM) ?
6715f83d2f91SKan Liang adl_hybrid_extra_attr_rtm : adl_hybrid_extra_attr;
6716f83d2f91SKan Liang
6717f83d2f91SKan Liang /* Initialize big core specific PerfMon capabilities.*/
6718f83d2f91SKan Liang pmu = &x86_pmu.hybrid_pmu[X86_HYBRID_PMU_CORE_IDX];
6719f83d2f91SKan Liang pmu->name = "cpu_core";
6720f83d2f91SKan Liang pmu->cpu_type = hybrid_big;
6721acade637SKan Liang pmu->late_ack = true;
6722ee72a94eSKan Liang if (cpu_feature_enabled(X86_FEATURE_HYBRID_CPU)) {
6723f83d2f91SKan Liang pmu->num_counters = x86_pmu.num_counters + 2;
6724f83d2f91SKan Liang pmu->num_counters_fixed = x86_pmu.num_counters_fixed + 1;
6725ee72a94eSKan Liang } else {
6726ee72a94eSKan Liang pmu->num_counters = x86_pmu.num_counters;
6727ee72a94eSKan Liang pmu->num_counters_fixed = x86_pmu.num_counters_fixed;
6728ee72a94eSKan Liang }
67297fa981caSKan Liang
67307fa981caSKan Liang /*
67317fa981caSKan Liang * Quirk: For some Alder Lake machine, when all E-cores are disabled in
67327fa981caSKan Liang * a BIOS, the leaf 0xA will enumerate all counters of P-cores. However,
67337fa981caSKan Liang * the X86_FEATURE_HYBRID_CPU is still set. The above codes will
67347fa981caSKan Liang * mistakenly add extra counters for P-cores. Correct the number of
67357fa981caSKan Liang * counters here.
67367fa981caSKan Liang */
67377fa981caSKan Liang if ((pmu->num_counters > 8) || (pmu->num_counters_fixed > 4)) {
67387fa981caSKan Liang pmu->num_counters = x86_pmu.num_counters;
67397fa981caSKan Liang pmu->num_counters_fixed = x86_pmu.num_counters_fixed;
67407fa981caSKan Liang }
67417fa981caSKan Liang
6742f83d2f91SKan Liang pmu->max_pebs_events = min_t(unsigned, MAX_PEBS_EVENTS, pmu->num_counters);
6743f83d2f91SKan Liang pmu->unconstrained = (struct event_constraint)
6744f83d2f91SKan Liang __EVENT_CONSTRAINT(0, (1ULL << pmu->num_counters) - 1,
6745f83d2f91SKan Liang 0, pmu->num_counters, 0, 0);
6746f83d2f91SKan Liang pmu->intel_cap.capabilities = x86_pmu.intel_cap.capabilities;
6747f83d2f91SKan Liang pmu->intel_cap.perf_metrics = 1;
6748f83d2f91SKan Liang pmu->intel_cap.pebs_output_pt_available = 0;
6749f83d2f91SKan Liang
6750f83d2f91SKan Liang memcpy(pmu->hw_cache_event_ids, spr_hw_cache_event_ids, sizeof(pmu->hw_cache_event_ids));
6751f83d2f91SKan Liang memcpy(pmu->hw_cache_extra_regs, spr_hw_cache_extra_regs, sizeof(pmu->hw_cache_extra_regs));
6752f83d2f91SKan Liang pmu->event_constraints = intel_spr_event_constraints;
6753f83d2f91SKan Liang pmu->pebs_constraints = intel_spr_pebs_event_constraints;
6754f83d2f91SKan Liang pmu->extra_regs = intel_spr_extra_regs;
6755f83d2f91SKan Liang
6756f83d2f91SKan Liang /* Initialize Atom core specific PerfMon capabilities.*/
6757f83d2f91SKan Liang pmu = &x86_pmu.hybrid_pmu[X86_HYBRID_PMU_ATOM_IDX];
6758f83d2f91SKan Liang pmu->name = "cpu_atom";
6759f83d2f91SKan Liang pmu->cpu_type = hybrid_small;
6760acade637SKan Liang pmu->mid_ack = true;
6761f83d2f91SKan Liang pmu->num_counters = x86_pmu.num_counters;
6762f83d2f91SKan Liang pmu->num_counters_fixed = x86_pmu.num_counters_fixed;
6763f83d2f91SKan Liang pmu->max_pebs_events = x86_pmu.max_pebs_events;
6764f83d2f91SKan Liang pmu->unconstrained = (struct event_constraint)
6765f83d2f91SKan Liang __EVENT_CONSTRAINT(0, (1ULL << pmu->num_counters) - 1,
6766f83d2f91SKan Liang 0, pmu->num_counters, 0, 0);
6767f83d2f91SKan Liang pmu->intel_cap.capabilities = x86_pmu.intel_cap.capabilities;
6768f83d2f91SKan Liang pmu->intel_cap.perf_metrics = 0;
6769f83d2f91SKan Liang pmu->intel_cap.pebs_output_pt_available = 1;
6770f83d2f91SKan Liang
6771f83d2f91SKan Liang memcpy(pmu->hw_cache_event_ids, glp_hw_cache_event_ids, sizeof(pmu->hw_cache_event_ids));
6772f83d2f91SKan Liang memcpy(pmu->hw_cache_extra_regs, tnt_hw_cache_extra_regs, sizeof(pmu->hw_cache_extra_regs));
6773f83d2f91SKan Liang pmu->hw_cache_event_ids[C(ITLB)][C(OP_READ)][C(RESULT_ACCESS)] = -1;
6774f83d2f91SKan Liang pmu->event_constraints = intel_slm_event_constraints;
6775f83d2f91SKan Liang pmu->pebs_constraints = intel_grt_pebs_event_constraints;
6776f83d2f91SKan Liang pmu->extra_regs = intel_grt_extra_regs;
677738aaf921SKan Liang if (is_mtl(boot_cpu_data.x86_model)) {
6778a6742cb9SKan Liang x86_pmu.hybrid_pmu[X86_HYBRID_PMU_CORE_IDX].extra_regs = intel_gnr_extra_regs;
677938aaf921SKan Liang x86_pmu.pebs_latency_data = mtl_latency_data_small;
678038aaf921SKan Liang extra_attr = boot_cpu_has(X86_FEATURE_RTM) ?
678138aaf921SKan Liang mtl_hybrid_extra_attr_rtm : mtl_hybrid_extra_attr;
678238aaf921SKan Liang mem_attr = mtl_hybrid_mem_attrs;
678338aaf921SKan Liang intel_pmu_pebs_data_source_mtl();
678438aaf921SKan Liang x86_pmu.get_event_constraints = mtl_get_event_constraints;
678538aaf921SKan Liang pmu->extra_regs = intel_cmt_extra_regs;
678638aaf921SKan Liang pr_cont("Meteorlake Hybrid events, ");
678738aaf921SKan Liang name = "meteorlake_hybrid";
678838aaf921SKan Liang } else {
678938aaf921SKan Liang x86_pmu.flags |= PMU_FL_MEM_LOADS_AUX;
679038aaf921SKan Liang intel_pmu_pebs_data_source_adl();
6791f83d2f91SKan Liang pr_cont("Alderlake Hybrid events, ");
6792f83d2f91SKan Liang name = "alderlake_hybrid";
679338aaf921SKan Liang }
6794f83d2f91SKan Liang break;
6795f83d2f91SKan Liang
6796e1069839SBorislav Petkov default:
6797e1069839SBorislav Petkov switch (x86_pmu.version) {
6798e1069839SBorislav Petkov case 1:
6799e1069839SBorislav Petkov x86_pmu.event_constraints = intel_v1_event_constraints;
6800e1069839SBorislav Petkov pr_cont("generic architected perfmon v1, ");
6801b00233b5SAndi Kleen name = "generic_arch_v1";
6802e1069839SBorislav Petkov break;
6803ee28855aSKan Liang case 2:
6804ee28855aSKan Liang case 3:
6805ee28855aSKan Liang case 4:
6806e1069839SBorislav Petkov /*
6807e1069839SBorislav Petkov * default constraints for v2 and up
6808e1069839SBorislav Petkov */
6809e1069839SBorislav Petkov x86_pmu.event_constraints = intel_gen_event_constraints;
6810e1069839SBorislav Petkov pr_cont("generic architected perfmon, ");
6811b00233b5SAndi Kleen name = "generic_arch_v2+";
6812e1069839SBorislav Petkov break;
6813ee28855aSKan Liang default:
6814ee28855aSKan Liang /*
6815ee28855aSKan Liang * The default constraints for v5 and up can support up to
6816ee28855aSKan Liang * 16 fixed counters. For the fixed counters 4 and later,
6817ee28855aSKan Liang * the pseudo-encoding is applied.
6818ee28855aSKan Liang * The constraints may be cut according to the CPUID enumeration
6819ee28855aSKan Liang * by inserting the EVENT_CONSTRAINT_END.
6820ee28855aSKan Liang */
6821ee28855aSKan Liang if (x86_pmu.num_counters_fixed > INTEL_PMC_MAX_FIXED)
6822ee28855aSKan Liang x86_pmu.num_counters_fixed = INTEL_PMC_MAX_FIXED;
6823ee28855aSKan Liang intel_v5_gen_event_constraints[x86_pmu.num_counters_fixed].weight = -1;
6824ee28855aSKan Liang x86_pmu.event_constraints = intel_v5_gen_event_constraints;
6825ee28855aSKan Liang pr_cont("generic architected perfmon, ");
6826ee28855aSKan Liang name = "generic_arch_v5+";
6827ee28855aSKan Liang break;
6828e1069839SBorislav Petkov }
6829e1069839SBorislav Petkov }
6830e1069839SBorislav Petkov
68310e96f31eSJordan Borgner snprintf(pmu_name_str, sizeof(pmu_name_str), "%s", name);
6832b00233b5SAndi Kleen
683358ae30c2SKan Liang if (!is_hybrid()) {
6834baa0c833SJiri Olsa group_events_td.attrs = td_attr;
6835baa0c833SJiri Olsa group_events_mem.attrs = mem_attr;
6836baa0c833SJiri Olsa group_events_tsx.attrs = tsx_attr;
68373ea40ac7SJiri Olsa group_format_extra.attrs = extra_attr;
6838b6576880SJiri Olsa group_format_extra_skl.attrs = extra_skl_attr;
6839baa0c833SJiri Olsa
6840baa0c833SJiri Olsa x86_pmu.attr_update = attr_update;
684158ae30c2SKan Liang } else {
684258ae30c2SKan Liang hybrid_group_events_td.attrs = td_attr;
684358ae30c2SKan Liang hybrid_group_events_mem.attrs = mem_attr;
684458ae30c2SKan Liang hybrid_group_events_tsx.attrs = tsx_attr;
684558ae30c2SKan Liang hybrid_group_format_extra.attrs = extra_attr;
6846d4ae5529SJiri Olsa
684758ae30c2SKan Liang x86_pmu.attr_update = hybrid_attr_update;
6848e1069839SBorislav Petkov }
6849e1069839SBorislav Petkov
6850b8c4d1a8SKan Liang intel_pmu_check_num_counters(&x86_pmu.num_counters,
6851b8c4d1a8SKan Liang &x86_pmu.num_counters_fixed,
6852b8c4d1a8SKan Liang &x86_pmu.intel_ctrl,
6853b8c4d1a8SKan Liang (u64)fixed_mask);
6854e1069839SBorislav Petkov
6855cadbaa03SStephane Eranian /* AnyThread may be deprecated on arch perfmon v5 or later */
6856cadbaa03SStephane Eranian if (x86_pmu.intel_cap.anythread_deprecated)
6857cadbaa03SStephane Eranian x86_pmu.format_attrs = intel_arch_formats_attr;
6858cadbaa03SStephane Eranian
6859bc14fe1bSKan Liang intel_pmu_check_event_constraints(x86_pmu.event_constraints,
6860bc14fe1bSKan Liang x86_pmu.num_counters,
6861bc14fe1bSKan Liang x86_pmu.num_counters_fixed,
6862bc14fe1bSKan Liang x86_pmu.intel_ctrl);
6863e1069839SBorislav Petkov /*
6864e1069839SBorislav Petkov * Access LBR MSR may cause #GP under certain circumstances.
686592d80178SLike Xu * Check all LBR MSR here.
6866e1069839SBorislav Petkov * Disable LBR access if any LBR MSRs can not be accessed.
6867e1069839SBorislav Petkov */
68683317c26aSLike Xu if (x86_pmu.lbr_tos && !check_msr(x86_pmu.lbr_tos, 0x3UL))
6869e1069839SBorislav Petkov x86_pmu.lbr_nr = 0;
6870e1069839SBorislav Petkov for (i = 0; i < x86_pmu.lbr_nr; i++) {
6871e1069839SBorislav Petkov if (!(check_msr(x86_pmu.lbr_from + i, 0xffffUL) &&
6872e1069839SBorislav Petkov check_msr(x86_pmu.lbr_to + i, 0xffffUL)))
6873e1069839SBorislav Petkov x86_pmu.lbr_nr = 0;
6874e1069839SBorislav Petkov }
6875e1069839SBorislav Petkov
6876c22ac2a3SSong Liu if (x86_pmu.lbr_nr) {
68771ac7fd81SPeter Zijlstra (Intel) intel_pmu_lbr_init();
68781ac7fd81SPeter Zijlstra (Intel)
6879f09509b9SDavid Carrillo-Cisneros pr_cont("%d-deep LBR, ", x86_pmu.lbr_nr);
6880b00233b5SAndi Kleen
6881c22ac2a3SSong Liu /* only support branch_stack snapshot for perfmon >= v2 */
6882c22ac2a3SSong Liu if (x86_pmu.disable_all == intel_pmu_disable_all) {
6883c22ac2a3SSong Liu if (boot_cpu_has(X86_FEATURE_ARCH_LBR)) {
6884c22ac2a3SSong Liu static_call_update(perf_snapshot_branch_stack,
6885c22ac2a3SSong Liu intel_pmu_snapshot_arch_branch_stack);
6886c22ac2a3SSong Liu } else {
6887c22ac2a3SSong Liu static_call_update(perf_snapshot_branch_stack,
6888c22ac2a3SSong Liu intel_pmu_snapshot_branch_stack);
6889c22ac2a3SSong Liu }
6890c22ac2a3SSong Liu }
6891c22ac2a3SSong Liu }
6892c22ac2a3SSong Liu
689334d5b61fSKan Liang intel_pmu_check_extra_regs(x86_pmu.extra_regs);
6894e1069839SBorislav Petkov
6895e1069839SBorislav Petkov /* Support full width counters using alternative MSR range */
6896e1069839SBorislav Petkov if (x86_pmu.intel_cap.full_width_write) {
68977f612a7fSPeter Zijlstra (Intel) x86_pmu.max_period = x86_pmu.cntval_mask >> 1;
6898e1069839SBorislav Petkov x86_pmu.perfctr = MSR_IA32_PMC0;
6899e1069839SBorislav Petkov pr_cont("full-width counters, ");
6900e1069839SBorislav Petkov }
6901e1069839SBorislav Petkov
6902d0946a88SKan Liang if (!is_hybrid() && x86_pmu.intel_cap.perf_metrics)
690359a854e2SKan Liang x86_pmu.intel_ctrl |= 1ULL << GLOBAL_CTRL_EN_PERF_METRICS;
690459a854e2SKan Liang
6905d9977c43SKan Liang if (is_hybrid())
6906d9977c43SKan Liang intel_pmu_check_hybrid_pmus((u64)fixed_mask);
6907d9977c43SKan Liang
6908c87a3109SKan Liang if (x86_pmu.intel_cap.pebs_timing_info)
6909c87a3109SKan Liang x86_pmu.flags |= PMU_FL_RETIRE_LATENCY;
6910c87a3109SKan Liang
69118b8ff8ccSAdrian Hunter intel_aux_output_init();
69128b8ff8ccSAdrian Hunter
6913e1069839SBorislav Petkov return 0;
6914e1069839SBorislav Petkov }
6915e1069839SBorislav Petkov
6916e1069839SBorislav Petkov /*
6917e1069839SBorislav Petkov * HT bug: phase 2 init
6918e1069839SBorislav Petkov * Called once we have valid topology information to check
6919e1069839SBorislav Petkov * whether or not HT is enabled
6920e1069839SBorislav Petkov * If HT is off, then we disable the workaround
6921e1069839SBorislav Petkov */
fixup_ht_bug(void)6922e1069839SBorislav Petkov static __init int fixup_ht_bug(void)
6923e1069839SBorislav Petkov {
6924030ba6cdSAndi Kleen int c;
6925e1069839SBorislav Petkov /*
6926e1069839SBorislav Petkov * problem not present on this CPU model, nothing to do
6927e1069839SBorislav Petkov */
6928e1069839SBorislav Petkov if (!(x86_pmu.flags & PMU_FL_EXCL_ENABLED))
6929e1069839SBorislav Petkov return 0;
6930e1069839SBorislav Petkov
6931030ba6cdSAndi Kleen if (topology_max_smt_threads() > 1) {
6932e1069839SBorislav Petkov pr_info("PMU erratum BJ122, BV98, HSD29 worked around, HT is on\n");
6933e1069839SBorislav Petkov return 0;
6934e1069839SBorislav Petkov }
6935e1069839SBorislav Petkov
69362406e3b1SPeter Zijlstra cpus_read_lock();
69372406e3b1SPeter Zijlstra
69382406e3b1SPeter Zijlstra hardlockup_detector_perf_stop();
6939e1069839SBorislav Petkov
6940e1069839SBorislav Petkov x86_pmu.flags &= ~(PMU_FL_EXCL_CNTRS | PMU_FL_EXCL_ENABLED);
6941e1069839SBorislav Petkov
6942e1069839SBorislav Petkov x86_pmu.start_scheduling = NULL;
6943e1069839SBorislav Petkov x86_pmu.commit_scheduling = NULL;
6944e1069839SBorislav Petkov x86_pmu.stop_scheduling = NULL;
6945e1069839SBorislav Petkov
69462406e3b1SPeter Zijlstra hardlockup_detector_perf_restart();
6947e1069839SBorislav Petkov
69481ba143a5SSebastian Andrzej Siewior for_each_online_cpu(c)
6949d01b1f96SPeter Zijlstra (Intel) free_excl_cntrs(&per_cpu(cpu_hw_events, c));
6950e1069839SBorislav Petkov
69511ba143a5SSebastian Andrzej Siewior cpus_read_unlock();
6952e1069839SBorislav Petkov pr_info("PMU erratum BJ122, BV98, HSD29 workaround disabled, HT off\n");
6953e1069839SBorislav Petkov return 0;
6954e1069839SBorislav Petkov }
6955e1069839SBorislav Petkov subsys_initcall(fixup_ht_bug)
6956