1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Per core/cpu state 4 * 5 * Used to coordinate shared registers between HT threads or 6 * among events on a single PMU. 7 */ 8 9 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 10 11 #include <linux/stddef.h> 12 #include <linux/types.h> 13 #include <linux/init.h> 14 #include <linux/slab.h> 15 #include <linux/export.h> 16 #include <linux/nmi.h> 17 #include <linux/kvm_host.h> 18 19 #include <asm/cpufeature.h> 20 #include <asm/debugreg.h> 21 #include <asm/hardirq.h> 22 #include <asm/intel-family.h> 23 #include <asm/intel_pt.h> 24 #include <asm/apic.h> 25 #include <asm/cpu_device_id.h> 26 #include <asm/msr.h> 27 28 #include "../perf_event.h" 29 30 /* 31 * Intel PerfMon, used on Core and later. 32 */ 33 static u64 intel_perfmon_event_map[PERF_COUNT_HW_MAX] __read_mostly = 34 { 35 [PERF_COUNT_HW_CPU_CYCLES] = 0x003c, 36 [PERF_COUNT_HW_INSTRUCTIONS] = 0x00c0, 37 [PERF_COUNT_HW_CACHE_REFERENCES] = 0x4f2e, 38 [PERF_COUNT_HW_CACHE_MISSES] = 0x412e, 39 [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = 0x00c4, 40 [PERF_COUNT_HW_BRANCH_MISSES] = 0x00c5, 41 [PERF_COUNT_HW_BUS_CYCLES] = 0x013c, 42 [PERF_COUNT_HW_REF_CPU_CYCLES] = 0x0300, /* pseudo-encoding */ 43 }; 44 45 static struct event_constraint intel_core_event_constraints[] __read_mostly = 46 { 47 INTEL_EVENT_CONSTRAINT(0x11, 0x2), /* FP_ASSIST */ 48 INTEL_EVENT_CONSTRAINT(0x12, 0x2), /* MUL */ 49 INTEL_EVENT_CONSTRAINT(0x13, 0x2), /* DIV */ 50 INTEL_EVENT_CONSTRAINT(0x14, 0x1), /* CYCLES_DIV_BUSY */ 51 INTEL_EVENT_CONSTRAINT(0x19, 0x2), /* DELAYED_BYPASS */ 52 INTEL_EVENT_CONSTRAINT(0xc1, 0x1), /* FP_COMP_INSTR_RET */ 53 EVENT_CONSTRAINT_END 54 }; 55 56 static struct event_constraint intel_core2_event_constraints[] __read_mostly = 57 { 58 FIXED_EVENT_CONSTRAINT(0x00c0, 0), /* INST_RETIRED.ANY */ 59 FIXED_EVENT_CONSTRAINT(0x003c, 1), /* CPU_CLK_UNHALTED.CORE */ 60 FIXED_EVENT_CONSTRAINT(0x0300, 2), /* CPU_CLK_UNHALTED.REF */ 61 INTEL_EVENT_CONSTRAINT(0x10, 0x1), /* FP_COMP_OPS_EXE */ 62 INTEL_EVENT_CONSTRAINT(0x11, 0x2), /* FP_ASSIST */ 63 INTEL_EVENT_CONSTRAINT(0x12, 0x2), /* MUL */ 64 INTEL_EVENT_CONSTRAINT(0x13, 0x2), /* DIV */ 65 INTEL_EVENT_CONSTRAINT(0x14, 0x1), /* CYCLES_DIV_BUSY */ 66 INTEL_EVENT_CONSTRAINT(0x18, 0x1), /* IDLE_DURING_DIV */ 67 INTEL_EVENT_CONSTRAINT(0x19, 0x2), /* DELAYED_BYPASS */ 68 INTEL_EVENT_CONSTRAINT(0xa1, 0x1), /* RS_UOPS_DISPATCH_CYCLES */ 69 INTEL_EVENT_CONSTRAINT(0xc9, 0x1), /* ITLB_MISS_RETIRED (T30-9) */ 70 INTEL_EVENT_CONSTRAINT(0xcb, 0x1), /* MEM_LOAD_RETIRED */ 71 EVENT_CONSTRAINT_END 72 }; 73 74 static struct event_constraint intel_nehalem_event_constraints[] __read_mostly = 75 { 76 FIXED_EVENT_CONSTRAINT(0x00c0, 0), /* INST_RETIRED.ANY */ 77 FIXED_EVENT_CONSTRAINT(0x003c, 1), /* CPU_CLK_UNHALTED.CORE */ 78 FIXED_EVENT_CONSTRAINT(0x0300, 2), /* CPU_CLK_UNHALTED.REF */ 79 INTEL_EVENT_CONSTRAINT(0x40, 0x3), /* L1D_CACHE_LD */ 80 INTEL_EVENT_CONSTRAINT(0x41, 0x3), /* L1D_CACHE_ST */ 81 INTEL_EVENT_CONSTRAINT(0x42, 0x3), /* L1D_CACHE_LOCK */ 82 INTEL_EVENT_CONSTRAINT(0x43, 0x3), /* L1D_ALL_REF */ 83 INTEL_EVENT_CONSTRAINT(0x48, 0x3), /* L1D_PEND_MISS */ 84 INTEL_EVENT_CONSTRAINT(0x4e, 0x3), /* L1D_PREFETCH */ 85 INTEL_EVENT_CONSTRAINT(0x51, 0x3), /* L1D */ 86 INTEL_EVENT_CONSTRAINT(0x63, 0x3), /* CACHE_LOCK_CYCLES */ 87 EVENT_CONSTRAINT_END 88 }; 89 90 static struct extra_reg intel_nehalem_extra_regs[] __read_mostly = 91 { 92 /* must define OFFCORE_RSP_X first, see intel_fixup_er() */ 93 INTEL_UEVENT_EXTRA_REG(0x01b7, MSR_OFFCORE_RSP_0, 0xffff, RSP_0), 94 INTEL_UEVENT_PEBS_LDLAT_EXTRA_REG(0x100b), 95 EVENT_EXTRA_END 96 }; 97 98 static struct event_constraint intel_westmere_event_constraints[] __read_mostly = 99 { 100 FIXED_EVENT_CONSTRAINT(0x00c0, 0), /* INST_RETIRED.ANY */ 101 FIXED_EVENT_CONSTRAINT(0x003c, 1), /* CPU_CLK_UNHALTED.CORE */ 102 FIXED_EVENT_CONSTRAINT(0x0300, 2), /* CPU_CLK_UNHALTED.REF */ 103 INTEL_EVENT_CONSTRAINT(0x51, 0x3), /* L1D */ 104 INTEL_EVENT_CONSTRAINT(0x60, 0x1), /* OFFCORE_REQUESTS_OUTSTANDING */ 105 INTEL_EVENT_CONSTRAINT(0x63, 0x3), /* CACHE_LOCK_CYCLES */ 106 INTEL_EVENT_CONSTRAINT(0xb3, 0x1), /* SNOOPQ_REQUEST_OUTSTANDING */ 107 EVENT_CONSTRAINT_END 108 }; 109 110 static struct event_constraint intel_snb_event_constraints[] __read_mostly = 111 { 112 FIXED_EVENT_CONSTRAINT(0x00c0, 0), /* INST_RETIRED.ANY */ 113 FIXED_EVENT_CONSTRAINT(0x003c, 1), /* CPU_CLK_UNHALTED.CORE */ 114 FIXED_EVENT_CONSTRAINT(0x0300, 2), /* CPU_CLK_UNHALTED.REF */ 115 INTEL_UEVENT_CONSTRAINT(0x04a3, 0xf), /* CYCLE_ACTIVITY.CYCLES_NO_DISPATCH */ 116 INTEL_UEVENT_CONSTRAINT(0x05a3, 0xf), /* CYCLE_ACTIVITY.STALLS_L2_PENDING */ 117 INTEL_UEVENT_CONSTRAINT(0x02a3, 0x4), /* CYCLE_ACTIVITY.CYCLES_L1D_PENDING */ 118 INTEL_UEVENT_CONSTRAINT(0x06a3, 0x4), /* CYCLE_ACTIVITY.STALLS_L1D_PENDING */ 119 INTEL_EVENT_CONSTRAINT(0x48, 0x4), /* L1D_PEND_MISS.PENDING */ 120 INTEL_UEVENT_CONSTRAINT(0x01c0, 0x2), /* INST_RETIRED.PREC_DIST */ 121 INTEL_EVENT_CONSTRAINT(0xcd, 0x8), /* MEM_TRANS_RETIRED.LOAD_LATENCY */ 122 INTEL_UEVENT_CONSTRAINT(0x04a3, 0xf), /* CYCLE_ACTIVITY.CYCLES_NO_DISPATCH */ 123 INTEL_UEVENT_CONSTRAINT(0x02a3, 0x4), /* CYCLE_ACTIVITY.CYCLES_L1D_PENDING */ 124 125 /* 126 * When HT is off these events can only run on the bottom 4 counters 127 * When HT is on, they are impacted by the HT bug and require EXCL access 128 */ 129 INTEL_EXCLEVT_CONSTRAINT(0xd0, 0xf), /* MEM_UOPS_RETIRED.* */ 130 INTEL_EXCLEVT_CONSTRAINT(0xd1, 0xf), /* MEM_LOAD_UOPS_RETIRED.* */ 131 INTEL_EXCLEVT_CONSTRAINT(0xd2, 0xf), /* MEM_LOAD_UOPS_LLC_HIT_RETIRED.* */ 132 INTEL_EXCLEVT_CONSTRAINT(0xd3, 0xf), /* MEM_LOAD_UOPS_LLC_MISS_RETIRED.* */ 133 134 EVENT_CONSTRAINT_END 135 }; 136 137 static struct event_constraint intel_ivb_event_constraints[] __read_mostly = 138 { 139 FIXED_EVENT_CONSTRAINT(0x00c0, 0), /* INST_RETIRED.ANY */ 140 FIXED_EVENT_CONSTRAINT(0x003c, 1), /* CPU_CLK_UNHALTED.CORE */ 141 FIXED_EVENT_CONSTRAINT(0x0300, 2), /* CPU_CLK_UNHALTED.REF */ 142 INTEL_UEVENT_CONSTRAINT(0x0148, 0x4), /* L1D_PEND_MISS.PENDING */ 143 INTEL_UEVENT_CONSTRAINT(0x0279, 0xf), /* IDQ.EMPTY */ 144 INTEL_UEVENT_CONSTRAINT(0x019c, 0xf), /* IDQ_UOPS_NOT_DELIVERED.CORE */ 145 INTEL_UEVENT_CONSTRAINT(0x02a3, 0xf), /* CYCLE_ACTIVITY.CYCLES_LDM_PENDING */ 146 INTEL_UEVENT_CONSTRAINT(0x04a3, 0xf), /* CYCLE_ACTIVITY.CYCLES_NO_EXECUTE */ 147 INTEL_UEVENT_CONSTRAINT(0x05a3, 0xf), /* CYCLE_ACTIVITY.STALLS_L2_PENDING */ 148 INTEL_UEVENT_CONSTRAINT(0x06a3, 0xf), /* CYCLE_ACTIVITY.STALLS_LDM_PENDING */ 149 INTEL_UEVENT_CONSTRAINT(0x08a3, 0x4), /* CYCLE_ACTIVITY.CYCLES_L1D_PENDING */ 150 INTEL_UEVENT_CONSTRAINT(0x0ca3, 0x4), /* CYCLE_ACTIVITY.STALLS_L1D_PENDING */ 151 INTEL_UEVENT_CONSTRAINT(0x01c0, 0x2), /* INST_RETIRED.PREC_DIST */ 152 153 /* 154 * When HT is off these events can only run on the bottom 4 counters 155 * When HT is on, they are impacted by the HT bug and require EXCL access 156 */ 157 INTEL_EXCLEVT_CONSTRAINT(0xd0, 0xf), /* MEM_UOPS_RETIRED.* */ 158 INTEL_EXCLEVT_CONSTRAINT(0xd1, 0xf), /* MEM_LOAD_UOPS_RETIRED.* */ 159 INTEL_EXCLEVT_CONSTRAINT(0xd2, 0xf), /* MEM_LOAD_UOPS_LLC_HIT_RETIRED.* */ 160 INTEL_EXCLEVT_CONSTRAINT(0xd3, 0xf), /* MEM_LOAD_UOPS_LLC_MISS_RETIRED.* */ 161 162 EVENT_CONSTRAINT_END 163 }; 164 165 static struct extra_reg intel_westmere_extra_regs[] __read_mostly = 166 { 167 /* must define OFFCORE_RSP_X first, see intel_fixup_er() */ 168 INTEL_UEVENT_EXTRA_REG(0x01b7, MSR_OFFCORE_RSP_0, 0xffff, RSP_0), 169 INTEL_UEVENT_EXTRA_REG(0x01bb, MSR_OFFCORE_RSP_1, 0xffff, RSP_1), 170 INTEL_UEVENT_PEBS_LDLAT_EXTRA_REG(0x100b), 171 EVENT_EXTRA_END 172 }; 173 174 static struct event_constraint intel_v1_event_constraints[] __read_mostly = 175 { 176 EVENT_CONSTRAINT_END 177 }; 178 179 static struct event_constraint intel_gen_event_constraints[] __read_mostly = 180 { 181 FIXED_EVENT_CONSTRAINT(0x00c0, 0), /* INST_RETIRED.ANY */ 182 FIXED_EVENT_CONSTRAINT(0x003c, 1), /* CPU_CLK_UNHALTED.CORE */ 183 FIXED_EVENT_CONSTRAINT(0x0300, 2), /* CPU_CLK_UNHALTED.REF */ 184 EVENT_CONSTRAINT_END 185 }; 186 187 static struct event_constraint intel_v5_gen_event_constraints[] __read_mostly = 188 { 189 FIXED_EVENT_CONSTRAINT(0x00c0, 0), /* INST_RETIRED.ANY */ 190 FIXED_EVENT_CONSTRAINT(0x003c, 1), /* CPU_CLK_UNHALTED.CORE */ 191 FIXED_EVENT_CONSTRAINT(0x0300, 2), /* CPU_CLK_UNHALTED.REF */ 192 FIXED_EVENT_CONSTRAINT(0x0400, 3), /* SLOTS */ 193 FIXED_EVENT_CONSTRAINT(0x0500, 4), 194 FIXED_EVENT_CONSTRAINT(0x0600, 5), 195 FIXED_EVENT_CONSTRAINT(0x0700, 6), 196 FIXED_EVENT_CONSTRAINT(0x0800, 7), 197 FIXED_EVENT_CONSTRAINT(0x0900, 8), 198 FIXED_EVENT_CONSTRAINT(0x0a00, 9), 199 FIXED_EVENT_CONSTRAINT(0x0b00, 10), 200 FIXED_EVENT_CONSTRAINT(0x0c00, 11), 201 FIXED_EVENT_CONSTRAINT(0x0d00, 12), 202 FIXED_EVENT_CONSTRAINT(0x0e00, 13), 203 FIXED_EVENT_CONSTRAINT(0x0f00, 14), 204 FIXED_EVENT_CONSTRAINT(0x1000, 15), 205 EVENT_CONSTRAINT_END 206 }; 207 208 static struct event_constraint intel_slm_event_constraints[] __read_mostly = 209 { 210 FIXED_EVENT_CONSTRAINT(0x00c0, 0), /* INST_RETIRED.ANY */ 211 FIXED_EVENT_CONSTRAINT(0x003c, 1), /* CPU_CLK_UNHALTED.CORE */ 212 FIXED_EVENT_CONSTRAINT(0x0300, 2), /* pseudo CPU_CLK_UNHALTED.REF */ 213 EVENT_CONSTRAINT_END 214 }; 215 216 static struct event_constraint intel_grt_event_constraints[] __read_mostly = { 217 FIXED_EVENT_CONSTRAINT(0x00c0, 0), /* INST_RETIRED.ANY */ 218 FIXED_EVENT_CONSTRAINT(0x003c, 1), /* CPU_CLK_UNHALTED.CORE */ 219 FIXED_EVENT_CONSTRAINT(0x0300, 2), /* pseudo CPU_CLK_UNHALTED.REF */ 220 FIXED_EVENT_CONSTRAINT(0x013c, 2), /* CPU_CLK_UNHALTED.REF_TSC_P */ 221 EVENT_CONSTRAINT_END 222 }; 223 224 static struct event_constraint intel_skt_event_constraints[] __read_mostly = { 225 FIXED_EVENT_CONSTRAINT(0x00c0, 0), /* INST_RETIRED.ANY */ 226 FIXED_EVENT_CONSTRAINT(0x003c, 1), /* CPU_CLK_UNHALTED.CORE */ 227 FIXED_EVENT_CONSTRAINT(0x0300, 2), /* pseudo CPU_CLK_UNHALTED.REF */ 228 FIXED_EVENT_CONSTRAINT(0x013c, 2), /* CPU_CLK_UNHALTED.REF_TSC_P */ 229 FIXED_EVENT_CONSTRAINT(0x0073, 4), /* TOPDOWN_BAD_SPECULATION.ALL */ 230 FIXED_EVENT_CONSTRAINT(0x019c, 5), /* TOPDOWN_FE_BOUND.ALL */ 231 FIXED_EVENT_CONSTRAINT(0x02c2, 6), /* TOPDOWN_RETIRING.ALL */ 232 EVENT_CONSTRAINT_END 233 }; 234 235 static struct event_constraint intel_arw_event_constraints[] __read_mostly = { 236 FIXED_EVENT_CONSTRAINT(0x00c0, 0), /* INST_RETIRED.ANY */ 237 FIXED_EVENT_CONSTRAINT(0x003c, 1), /* CPU_CLK_UNHALTED.CORE */ 238 FIXED_EVENT_CONSTRAINT(0x0300, 2), /* pseudo CPU_CLK_UNHALTED.REF */ 239 FIXED_EVENT_CONSTRAINT(0x013c, 2), /* CPU_CLK_UNHALTED.REF_TSC_P */ 240 FIXED_EVENT_CONSTRAINT(0x0073, 4), /* TOPDOWN_BAD_SPECULATION.ALL */ 241 FIXED_EVENT_CONSTRAINT(0x019c, 5), /* TOPDOWN_FE_BOUND.ALL */ 242 FIXED_EVENT_CONSTRAINT(0x02c2, 6), /* TOPDOWN_RETIRING.ALL */ 243 INTEL_UEVENT_CONSTRAINT(0x01b7, 0x1), 244 INTEL_UEVENT_CONSTRAINT(0x02b7, 0x2), 245 INTEL_UEVENT_CONSTRAINT(0x04b7, 0x4), 246 INTEL_UEVENT_CONSTRAINT(0x08b7, 0x8), 247 INTEL_UEVENT_CONSTRAINT(0x01d4, 0x1), 248 INTEL_UEVENT_CONSTRAINT(0x02d4, 0x2), 249 INTEL_UEVENT_CONSTRAINT(0x04d4, 0x4), 250 INTEL_UEVENT_CONSTRAINT(0x08d4, 0x8), 251 INTEL_UEVENT_CONSTRAINT(0x0175, 0x1), 252 INTEL_UEVENT_CONSTRAINT(0x0275, 0x2), 253 INTEL_UEVENT_CONSTRAINT(0x21d3, 0x1), 254 INTEL_UEVENT_CONSTRAINT(0x22d3, 0x1), 255 EVENT_CONSTRAINT_END 256 }; 257 258 static struct event_constraint intel_skl_event_constraints[] = { 259 FIXED_EVENT_CONSTRAINT(0x00c0, 0), /* INST_RETIRED.ANY */ 260 FIXED_EVENT_CONSTRAINT(0x003c, 1), /* CPU_CLK_UNHALTED.CORE */ 261 FIXED_EVENT_CONSTRAINT(0x0300, 2), /* CPU_CLK_UNHALTED.REF */ 262 INTEL_UEVENT_CONSTRAINT(0x1c0, 0x2), /* INST_RETIRED.PREC_DIST */ 263 264 /* 265 * when HT is off, these can only run on the bottom 4 counters 266 */ 267 INTEL_EVENT_CONSTRAINT(0xd0, 0xf), /* MEM_INST_RETIRED.* */ 268 INTEL_EVENT_CONSTRAINT(0xd1, 0xf), /* MEM_LOAD_RETIRED.* */ 269 INTEL_EVENT_CONSTRAINT(0xd2, 0xf), /* MEM_LOAD_L3_HIT_RETIRED.* */ 270 INTEL_EVENT_CONSTRAINT(0xcd, 0xf), /* MEM_TRANS_RETIRED.* */ 271 INTEL_EVENT_CONSTRAINT(0xc6, 0xf), /* FRONTEND_RETIRED.* */ 272 273 EVENT_CONSTRAINT_END 274 }; 275 276 static struct extra_reg intel_knl_extra_regs[] __read_mostly = { 277 INTEL_UEVENT_EXTRA_REG(0x01b7, MSR_OFFCORE_RSP_0, 0x799ffbb6e7ull, RSP_0), 278 INTEL_UEVENT_EXTRA_REG(0x02b7, MSR_OFFCORE_RSP_1, 0x399ffbffe7ull, RSP_1), 279 EVENT_EXTRA_END 280 }; 281 282 static struct extra_reg intel_snb_extra_regs[] __read_mostly = { 283 /* must define OFFCORE_RSP_X first, see intel_fixup_er() */ 284 INTEL_UEVENT_EXTRA_REG(0x01b7, MSR_OFFCORE_RSP_0, 0x3f807f8fffull, RSP_0), 285 INTEL_UEVENT_EXTRA_REG(0x01bb, MSR_OFFCORE_RSP_1, 0x3f807f8fffull, RSP_1), 286 INTEL_UEVENT_PEBS_LDLAT_EXTRA_REG(0x01cd), 287 EVENT_EXTRA_END 288 }; 289 290 static struct extra_reg intel_snbep_extra_regs[] __read_mostly = { 291 /* must define OFFCORE_RSP_X first, see intel_fixup_er() */ 292 INTEL_UEVENT_EXTRA_REG(0x01b7, MSR_OFFCORE_RSP_0, 0x3fffff8fffull, RSP_0), 293 INTEL_UEVENT_EXTRA_REG(0x01bb, MSR_OFFCORE_RSP_1, 0x3fffff8fffull, RSP_1), 294 INTEL_UEVENT_PEBS_LDLAT_EXTRA_REG(0x01cd), 295 EVENT_EXTRA_END 296 }; 297 298 static struct extra_reg intel_skl_extra_regs[] __read_mostly = { 299 INTEL_UEVENT_EXTRA_REG(0x01b7, MSR_OFFCORE_RSP_0, 0x3fffff8fffull, RSP_0), 300 INTEL_UEVENT_EXTRA_REG(0x01bb, MSR_OFFCORE_RSP_1, 0x3fffff8fffull, RSP_1), 301 INTEL_UEVENT_PEBS_LDLAT_EXTRA_REG(0x01cd), 302 /* 303 * Note the low 8 bits eventsel code is not a continuous field, containing 304 * some #GPing bits. These are masked out. 305 */ 306 INTEL_UEVENT_EXTRA_REG(0x01c6, MSR_PEBS_FRONTEND, 0x7fff17, FE), 307 EVENT_EXTRA_END 308 }; 309 310 static struct event_constraint intel_icl_event_constraints[] = { 311 FIXED_EVENT_CONSTRAINT(0x00c0, 0), /* INST_RETIRED.ANY */ 312 FIXED_EVENT_CONSTRAINT(0x01c0, 0), /* old INST_RETIRED.PREC_DIST */ 313 FIXED_EVENT_CONSTRAINT(0x0100, 0), /* INST_RETIRED.PREC_DIST */ 314 FIXED_EVENT_CONSTRAINT(0x003c, 1), /* CPU_CLK_UNHALTED.CORE */ 315 FIXED_EVENT_CONSTRAINT(0x0300, 2), /* CPU_CLK_UNHALTED.REF */ 316 FIXED_EVENT_CONSTRAINT(0x0400, 3), /* SLOTS */ 317 METRIC_EVENT_CONSTRAINT(INTEL_TD_METRIC_RETIRING, 0), 318 METRIC_EVENT_CONSTRAINT(INTEL_TD_METRIC_BAD_SPEC, 1), 319 METRIC_EVENT_CONSTRAINT(INTEL_TD_METRIC_FE_BOUND, 2), 320 METRIC_EVENT_CONSTRAINT(INTEL_TD_METRIC_BE_BOUND, 3), 321 INTEL_EVENT_CONSTRAINT_RANGE(0x03, 0x0a, 0xf), 322 INTEL_EVENT_CONSTRAINT_RANGE(0x1f, 0x28, 0xf), 323 INTEL_EVENT_CONSTRAINT(0x32, 0xf), /* SW_PREFETCH_ACCESS.* */ 324 INTEL_EVENT_CONSTRAINT_RANGE(0x48, 0x56, 0xf), 325 INTEL_EVENT_CONSTRAINT_RANGE(0x60, 0x8b, 0xf), 326 INTEL_UEVENT_CONSTRAINT(0x04a3, 0xff), /* CYCLE_ACTIVITY.STALLS_TOTAL */ 327 INTEL_UEVENT_CONSTRAINT(0x10a3, 0xff), /* CYCLE_ACTIVITY.CYCLES_MEM_ANY */ 328 INTEL_UEVENT_CONSTRAINT(0x14a3, 0xff), /* CYCLE_ACTIVITY.STALLS_MEM_ANY */ 329 INTEL_EVENT_CONSTRAINT(0xa3, 0xf), /* CYCLE_ACTIVITY.* */ 330 INTEL_EVENT_CONSTRAINT_RANGE(0xa8, 0xb0, 0xf), 331 INTEL_EVENT_CONSTRAINT_RANGE(0xb7, 0xbd, 0xf), 332 INTEL_EVENT_CONSTRAINT_RANGE(0xd0, 0xe6, 0xf), 333 INTEL_EVENT_CONSTRAINT(0xef, 0xf), 334 INTEL_EVENT_CONSTRAINT_RANGE(0xf0, 0xf4, 0xf), 335 EVENT_CONSTRAINT_END 336 }; 337 338 static struct extra_reg intel_icl_extra_regs[] __read_mostly = { 339 INTEL_UEVENT_EXTRA_REG(0x01b7, MSR_OFFCORE_RSP_0, 0x3fffffbfffull, RSP_0), 340 INTEL_UEVENT_EXTRA_REG(0x01bb, MSR_OFFCORE_RSP_1, 0x3fffffbfffull, RSP_1), 341 INTEL_UEVENT_PEBS_LDLAT_EXTRA_REG(0x01cd), 342 INTEL_UEVENT_EXTRA_REG(0x01c6, MSR_PEBS_FRONTEND, 0x7fff17, FE), 343 EVENT_EXTRA_END 344 }; 345 346 static struct extra_reg intel_glc_extra_regs[] __read_mostly = { 347 INTEL_UEVENT_EXTRA_REG(0x012a, MSR_OFFCORE_RSP_0, 0x3fffffffffull, RSP_0), 348 INTEL_UEVENT_EXTRA_REG(0x012b, MSR_OFFCORE_RSP_1, 0x3fffffffffull, RSP_1), 349 INTEL_UEVENT_PEBS_LDLAT_EXTRA_REG(0x01cd), 350 INTEL_UEVENT_EXTRA_REG(0x01c6, MSR_PEBS_FRONTEND, 0x7fff1f, FE), 351 INTEL_UEVENT_EXTRA_REG(0x40ad, MSR_PEBS_FRONTEND, 0x7, FE), 352 INTEL_UEVENT_EXTRA_REG(0x04c2, MSR_PEBS_FRONTEND, 0x8, FE), 353 EVENT_EXTRA_END 354 }; 355 356 static struct event_constraint intel_glc_event_constraints[] = { 357 FIXED_EVENT_CONSTRAINT(0x00c0, 0), /* INST_RETIRED.ANY */ 358 FIXED_EVENT_CONSTRAINT(0x0100, 0), /* INST_RETIRED.PREC_DIST */ 359 FIXED_EVENT_CONSTRAINT(0x003c, 1), /* CPU_CLK_UNHALTED.CORE */ 360 FIXED_EVENT_CONSTRAINT(0x0300, 2), /* CPU_CLK_UNHALTED.REF */ 361 FIXED_EVENT_CONSTRAINT(0x013c, 2), /* CPU_CLK_UNHALTED.REF_TSC_P */ 362 FIXED_EVENT_CONSTRAINT(0x0400, 3), /* SLOTS */ 363 METRIC_EVENT_CONSTRAINT(INTEL_TD_METRIC_RETIRING, 0), 364 METRIC_EVENT_CONSTRAINT(INTEL_TD_METRIC_BAD_SPEC, 1), 365 METRIC_EVENT_CONSTRAINT(INTEL_TD_METRIC_FE_BOUND, 2), 366 METRIC_EVENT_CONSTRAINT(INTEL_TD_METRIC_BE_BOUND, 3), 367 METRIC_EVENT_CONSTRAINT(INTEL_TD_METRIC_HEAVY_OPS, 4), 368 METRIC_EVENT_CONSTRAINT(INTEL_TD_METRIC_BR_MISPREDICT, 5), 369 METRIC_EVENT_CONSTRAINT(INTEL_TD_METRIC_FETCH_LAT, 6), 370 METRIC_EVENT_CONSTRAINT(INTEL_TD_METRIC_MEM_BOUND, 7), 371 372 INTEL_EVENT_CONSTRAINT(0x2e, 0xff), 373 INTEL_EVENT_CONSTRAINT(0x3c, 0xff), 374 /* 375 * Generally event codes < 0x90 are restricted to counters 0-3. 376 * The 0x2E and 0x3C are exception, which has no restriction. 377 */ 378 INTEL_EVENT_CONSTRAINT_RANGE(0x01, 0x8f, 0xf), 379 380 INTEL_UEVENT_CONSTRAINT(0x01a3, 0xf), 381 INTEL_UEVENT_CONSTRAINT(0x02a3, 0xf), 382 INTEL_UEVENT_CONSTRAINT(0x08a3, 0xf), 383 INTEL_UEVENT_CONSTRAINT(0x04a4, 0x1), 384 INTEL_UEVENT_CONSTRAINT(0x08a4, 0x1), 385 INTEL_UEVENT_CONSTRAINT(0x02cd, 0x1), 386 INTEL_EVENT_CONSTRAINT(0xce, 0x1), 387 INTEL_EVENT_CONSTRAINT_RANGE(0xd0, 0xdf, 0xf), 388 /* 389 * Generally event codes >= 0x90 are likely to have no restrictions. 390 * The exception are defined as above. 391 */ 392 INTEL_EVENT_CONSTRAINT_RANGE(0x90, 0xfe, 0xff), 393 394 EVENT_CONSTRAINT_END 395 }; 396 397 static struct extra_reg intel_rwc_extra_regs[] __read_mostly = { 398 INTEL_UEVENT_EXTRA_REG(0x012a, MSR_OFFCORE_RSP_0, 0x3fffffffffull, RSP_0), 399 INTEL_UEVENT_EXTRA_REG(0x012b, MSR_OFFCORE_RSP_1, 0x3fffffffffull, RSP_1), 400 INTEL_UEVENT_PEBS_LDLAT_EXTRA_REG(0x01cd), 401 INTEL_UEVENT_EXTRA_REG(0x02c6, MSR_PEBS_FRONTEND, 0x9, FE), 402 INTEL_UEVENT_EXTRA_REG(0x03c6, MSR_PEBS_FRONTEND, 0x7fff1f, FE), 403 INTEL_UEVENT_EXTRA_REG(0x40ad, MSR_PEBS_FRONTEND, 0x7, FE), 404 INTEL_UEVENT_EXTRA_REG(0x04c2, MSR_PEBS_FRONTEND, 0x8, FE), 405 EVENT_EXTRA_END 406 }; 407 408 static struct event_constraint intel_lnc_event_constraints[] = { 409 FIXED_EVENT_CONSTRAINT(0x00c0, 0), /* INST_RETIRED.ANY */ 410 FIXED_EVENT_CONSTRAINT(0x0100, 0), /* INST_RETIRED.PREC_DIST */ 411 FIXED_EVENT_CONSTRAINT(0x003c, 1), /* CPU_CLK_UNHALTED.CORE */ 412 FIXED_EVENT_CONSTRAINT(0x0300, 2), /* CPU_CLK_UNHALTED.REF */ 413 FIXED_EVENT_CONSTRAINT(0x013c, 2), /* CPU_CLK_UNHALTED.REF_TSC_P */ 414 FIXED_EVENT_CONSTRAINT(0x0400, 3), /* SLOTS */ 415 METRIC_EVENT_CONSTRAINT(INTEL_TD_METRIC_RETIRING, 0), 416 METRIC_EVENT_CONSTRAINT(INTEL_TD_METRIC_BAD_SPEC, 1), 417 METRIC_EVENT_CONSTRAINT(INTEL_TD_METRIC_FE_BOUND, 2), 418 METRIC_EVENT_CONSTRAINT(INTEL_TD_METRIC_BE_BOUND, 3), 419 METRIC_EVENT_CONSTRAINT(INTEL_TD_METRIC_HEAVY_OPS, 4), 420 METRIC_EVENT_CONSTRAINT(INTEL_TD_METRIC_BR_MISPREDICT, 5), 421 METRIC_EVENT_CONSTRAINT(INTEL_TD_METRIC_FETCH_LAT, 6), 422 METRIC_EVENT_CONSTRAINT(INTEL_TD_METRIC_MEM_BOUND, 7), 423 424 INTEL_EVENT_CONSTRAINT(0x20, 0xf), 425 426 INTEL_UEVENT_CONSTRAINT(0x012a, 0xf), 427 INTEL_UEVENT_CONSTRAINT(0x012b, 0xf), 428 INTEL_UEVENT_CONSTRAINT(0x0148, 0x4), 429 INTEL_UEVENT_CONSTRAINT(0x0175, 0x4), 430 431 INTEL_EVENT_CONSTRAINT(0x2e, 0x3ff), 432 INTEL_EVENT_CONSTRAINT(0x3c, 0x3ff), 433 434 INTEL_UEVENT_CONSTRAINT(0x08a3, 0x4), 435 INTEL_UEVENT_CONSTRAINT(0x0ca3, 0x4), 436 INTEL_UEVENT_CONSTRAINT(0x04a4, 0x1), 437 INTEL_UEVENT_CONSTRAINT(0x08a4, 0x1), 438 INTEL_UEVENT_CONSTRAINT(0x10a4, 0x1), 439 INTEL_UEVENT_CONSTRAINT(0x01b1, 0x8), 440 INTEL_UEVENT_CONSTRAINT(0x01cd, 0x3fc), 441 INTEL_UEVENT_CONSTRAINT(0x02cd, 0x3), 442 443 INTEL_EVENT_CONSTRAINT_RANGE(0xd0, 0xdf, 0xf), 444 445 INTEL_UEVENT_CONSTRAINT(0x00e0, 0xf), 446 447 EVENT_CONSTRAINT_END 448 }; 449 450 static struct extra_reg intel_lnc_extra_regs[] __read_mostly = { 451 INTEL_UEVENT_EXTRA_REG(0x012a, MSR_OFFCORE_RSP_0, 0xfffffffffffull, RSP_0), 452 INTEL_UEVENT_EXTRA_REG(0x012b, MSR_OFFCORE_RSP_1, 0xfffffffffffull, RSP_1), 453 INTEL_UEVENT_PEBS_LDLAT_EXTRA_REG(0x01cd), 454 INTEL_UEVENT_EXTRA_REG(0x02c6, MSR_PEBS_FRONTEND, 0x9, FE), 455 INTEL_UEVENT_EXTRA_REG(0x03c6, MSR_PEBS_FRONTEND, 0x7fff1f, FE), 456 INTEL_UEVENT_EXTRA_REG(0x40ad, MSR_PEBS_FRONTEND, 0xf, FE), 457 INTEL_UEVENT_EXTRA_REG(0x04c2, MSR_PEBS_FRONTEND, 0x8, FE), 458 EVENT_EXTRA_END 459 }; 460 461 static struct event_constraint intel_pnc_event_constraints[] = { 462 FIXED_EVENT_CONSTRAINT(0x00c0, 0), /* INST_RETIRED.ANY */ 463 FIXED_EVENT_CONSTRAINT(0x0100, 0), /* INST_RETIRED.PREC_DIST */ 464 FIXED_EVENT_CONSTRAINT(0x003c, 1), /* CPU_CLK_UNHALTED.CORE */ 465 FIXED_EVENT_CONSTRAINT(0x0300, 2), /* CPU_CLK_UNHALTED.REF */ 466 FIXED_EVENT_CONSTRAINT(0x013c, 2), /* CPU_CLK_UNHALTED.REF_TSC_P */ 467 FIXED_EVENT_CONSTRAINT(0x0400, 3), /* SLOTS */ 468 METRIC_EVENT_CONSTRAINT(INTEL_TD_METRIC_RETIRING, 0), 469 METRIC_EVENT_CONSTRAINT(INTEL_TD_METRIC_BAD_SPEC, 1), 470 METRIC_EVENT_CONSTRAINT(INTEL_TD_METRIC_FE_BOUND, 2), 471 METRIC_EVENT_CONSTRAINT(INTEL_TD_METRIC_BE_BOUND, 3), 472 METRIC_EVENT_CONSTRAINT(INTEL_TD_METRIC_HEAVY_OPS, 4), 473 METRIC_EVENT_CONSTRAINT(INTEL_TD_METRIC_BR_MISPREDICT, 5), 474 METRIC_EVENT_CONSTRAINT(INTEL_TD_METRIC_FETCH_LAT, 6), 475 METRIC_EVENT_CONSTRAINT(INTEL_TD_METRIC_MEM_BOUND, 7), 476 477 INTEL_EVENT_CONSTRAINT(0x20, 0xf), 478 INTEL_EVENT_CONSTRAINT(0x79, 0xf), 479 480 INTEL_UEVENT_CONSTRAINT(0x0275, 0xf), 481 INTEL_UEVENT_CONSTRAINT(0x0176, 0xf), 482 INTEL_UEVENT_CONSTRAINT(0x04a4, 0x1), 483 INTEL_UEVENT_CONSTRAINT(0x08a4, 0x1), 484 INTEL_UEVENT_CONSTRAINT(0x01cd, 0xfc), 485 INTEL_UEVENT_CONSTRAINT(0x02cd, 0x3), 486 487 INTEL_EVENT_CONSTRAINT(0xd0, 0xf), 488 INTEL_EVENT_CONSTRAINT(0xd1, 0xf), 489 INTEL_EVENT_CONSTRAINT(0xd4, 0xf), 490 INTEL_EVENT_CONSTRAINT(0xd6, 0xf), 491 INTEL_EVENT_CONSTRAINT(0xdf, 0xf), 492 INTEL_EVENT_CONSTRAINT(0xce, 0x1), 493 494 INTEL_UEVENT_CONSTRAINT(0x01b1, 0x8), 495 INTEL_UEVENT_CONSTRAINT(0x0847, 0xf), 496 INTEL_UEVENT_CONSTRAINT(0x0446, 0xf), 497 INTEL_UEVENT_CONSTRAINT(0x0846, 0xf), 498 INTEL_UEVENT_CONSTRAINT(0x0148, 0xf), 499 500 EVENT_CONSTRAINT_END 501 }; 502 503 static struct extra_reg intel_pnc_extra_regs[] __read_mostly = { 504 /* must define OMR_X first, see intel_alt_er() */ 505 INTEL_UEVENT_EXTRA_REG(0x012a, MSR_OMR_0, 0x40ffffff0000ffffull, OMR_0), 506 INTEL_UEVENT_EXTRA_REG(0x022a, MSR_OMR_1, 0x40ffffff0000ffffull, OMR_1), 507 INTEL_UEVENT_EXTRA_REG(0x042a, MSR_OMR_2, 0x40ffffff0000ffffull, OMR_2), 508 INTEL_UEVENT_EXTRA_REG(0x082a, MSR_OMR_3, 0x40ffffff0000ffffull, OMR_3), 509 INTEL_UEVENT_PEBS_LDLAT_EXTRA_REG(0x01cd), 510 INTEL_UEVENT_EXTRA_REG(0x02c6, MSR_PEBS_FRONTEND, 0x9, FE), 511 INTEL_UEVENT_EXTRA_REG(0x03c6, MSR_PEBS_FRONTEND, 0x7fff1f, FE), 512 INTEL_UEVENT_EXTRA_REG(0x40ad, MSR_PEBS_FRONTEND, 0xf, FE), 513 INTEL_UEVENT_EXTRA_REG(0x04c2, MSR_PEBS_FRONTEND, 0x8, FE), 514 EVENT_EXTRA_END 515 }; 516 517 EVENT_ATTR_STR(mem-loads, mem_ld_nhm, "event=0x0b,umask=0x10,ldlat=3"); 518 EVENT_ATTR_STR(mem-loads, mem_ld_snb, "event=0xcd,umask=0x1,ldlat=3"); 519 EVENT_ATTR_STR(mem-stores, mem_st_snb, "event=0xcd,umask=0x2"); 520 521 static struct attribute *nhm_mem_events_attrs[] = { 522 EVENT_PTR(mem_ld_nhm), 523 NULL, 524 }; 525 526 /* 527 * topdown events for Intel Core CPUs. 528 * 529 * The events are all in slots, which is a free slot in a 4 wide 530 * pipeline. Some events are already reported in slots, for cycle 531 * events we multiply by the pipeline width (4). 532 * 533 * With Hyper Threading on, topdown metrics are either summed or averaged 534 * between the threads of a core: (count_t0 + count_t1). 535 * 536 * For the average case the metric is always scaled to pipeline width, 537 * so we use factor 2 ((count_t0 + count_t1) / 2 * 4) 538 */ 539 540 EVENT_ATTR_STR_HT(topdown-total-slots, td_total_slots, 541 "event=0x3c,umask=0x0", /* cpu_clk_unhalted.thread */ 542 "event=0x3c,umask=0x0,any=1"); /* cpu_clk_unhalted.thread_any */ 543 EVENT_ATTR_STR_HT(topdown-total-slots.scale, td_total_slots_scale, "4", "2"); 544 EVENT_ATTR_STR(topdown-slots-issued, td_slots_issued, 545 "event=0xe,umask=0x1"); /* uops_issued.any */ 546 EVENT_ATTR_STR(topdown-slots-retired, td_slots_retired, 547 "event=0xc2,umask=0x2"); /* uops_retired.retire_slots */ 548 EVENT_ATTR_STR(topdown-fetch-bubbles, td_fetch_bubbles, 549 "event=0x9c,umask=0x1"); /* idq_uops_not_delivered_core */ 550 EVENT_ATTR_STR_HT(topdown-recovery-bubbles, td_recovery_bubbles, 551 "event=0xd,umask=0x3,cmask=1", /* int_misc.recovery_cycles */ 552 "event=0xd,umask=0x3,cmask=1,any=1"); /* int_misc.recovery_cycles_any */ 553 EVENT_ATTR_STR_HT(topdown-recovery-bubbles.scale, td_recovery_bubbles_scale, 554 "4", "2"); 555 556 EVENT_ATTR_STR(slots, slots, "event=0x00,umask=0x4"); 557 EVENT_ATTR_STR(topdown-retiring, td_retiring, "event=0x00,umask=0x80"); 558 EVENT_ATTR_STR(topdown-bad-spec, td_bad_spec, "event=0x00,umask=0x81"); 559 EVENT_ATTR_STR(topdown-fe-bound, td_fe_bound, "event=0x00,umask=0x82"); 560 EVENT_ATTR_STR(topdown-be-bound, td_be_bound, "event=0x00,umask=0x83"); 561 EVENT_ATTR_STR(topdown-heavy-ops, td_heavy_ops, "event=0x00,umask=0x84"); 562 EVENT_ATTR_STR(topdown-br-mispredict, td_br_mispredict, "event=0x00,umask=0x85"); 563 EVENT_ATTR_STR(topdown-fetch-lat, td_fetch_lat, "event=0x00,umask=0x86"); 564 EVENT_ATTR_STR(topdown-mem-bound, td_mem_bound, "event=0x00,umask=0x87"); 565 566 static struct attribute *snb_events_attrs[] = { 567 EVENT_PTR(td_slots_issued), 568 EVENT_PTR(td_slots_retired), 569 EVENT_PTR(td_fetch_bubbles), 570 EVENT_PTR(td_total_slots), 571 EVENT_PTR(td_total_slots_scale), 572 EVENT_PTR(td_recovery_bubbles), 573 EVENT_PTR(td_recovery_bubbles_scale), 574 NULL, 575 }; 576 577 static struct attribute *snb_mem_events_attrs[] = { 578 EVENT_PTR(mem_ld_snb), 579 EVENT_PTR(mem_st_snb), 580 NULL, 581 }; 582 583 static struct event_constraint intel_hsw_event_constraints[] = { 584 FIXED_EVENT_CONSTRAINT(0x00c0, 0), /* INST_RETIRED.ANY */ 585 FIXED_EVENT_CONSTRAINT(0x003c, 1), /* CPU_CLK_UNHALTED.CORE */ 586 FIXED_EVENT_CONSTRAINT(0x0300, 2), /* CPU_CLK_UNHALTED.REF */ 587 INTEL_UEVENT_CONSTRAINT(0x148, 0x4), /* L1D_PEND_MISS.PENDING */ 588 INTEL_UEVENT_CONSTRAINT(0x01c0, 0x2), /* INST_RETIRED.PREC_DIST */ 589 INTEL_EVENT_CONSTRAINT(0xcd, 0x8), /* MEM_TRANS_RETIRED.LOAD_LATENCY */ 590 /* CYCLE_ACTIVITY.CYCLES_L1D_PENDING */ 591 INTEL_UEVENT_CONSTRAINT(0x08a3, 0x4), 592 /* CYCLE_ACTIVITY.STALLS_L1D_PENDING */ 593 INTEL_UEVENT_CONSTRAINT(0x0ca3, 0x4), 594 /* CYCLE_ACTIVITY.CYCLES_NO_EXECUTE */ 595 INTEL_UEVENT_CONSTRAINT(0x04a3, 0xf), 596 597 /* 598 * When HT is off these events can only run on the bottom 4 counters 599 * When HT is on, they are impacted by the HT bug and require EXCL access 600 */ 601 INTEL_EXCLEVT_CONSTRAINT(0xd0, 0xf), /* MEM_UOPS_RETIRED.* */ 602 INTEL_EXCLEVT_CONSTRAINT(0xd1, 0xf), /* MEM_LOAD_UOPS_RETIRED.* */ 603 INTEL_EXCLEVT_CONSTRAINT(0xd2, 0xf), /* MEM_LOAD_UOPS_LLC_HIT_RETIRED.* */ 604 INTEL_EXCLEVT_CONSTRAINT(0xd3, 0xf), /* MEM_LOAD_UOPS_LLC_MISS_RETIRED.* */ 605 606 EVENT_CONSTRAINT_END 607 }; 608 609 static struct event_constraint intel_bdw_event_constraints[] = { 610 FIXED_EVENT_CONSTRAINT(0x00c0, 0), /* INST_RETIRED.ANY */ 611 FIXED_EVENT_CONSTRAINT(0x003c, 1), /* CPU_CLK_UNHALTED.CORE */ 612 FIXED_EVENT_CONSTRAINT(0x0300, 2), /* CPU_CLK_UNHALTED.REF */ 613 INTEL_UEVENT_CONSTRAINT(0x148, 0x4), /* L1D_PEND_MISS.PENDING */ 614 INTEL_UBIT_EVENT_CONSTRAINT(0x8a3, 0x4), /* CYCLE_ACTIVITY.CYCLES_L1D_MISS */ 615 /* 616 * when HT is off, these can only run on the bottom 4 counters 617 */ 618 INTEL_EVENT_CONSTRAINT(0xd0, 0xf), /* MEM_INST_RETIRED.* */ 619 INTEL_EVENT_CONSTRAINT(0xd1, 0xf), /* MEM_LOAD_RETIRED.* */ 620 INTEL_EVENT_CONSTRAINT(0xd2, 0xf), /* MEM_LOAD_L3_HIT_RETIRED.* */ 621 INTEL_EVENT_CONSTRAINT(0xcd, 0xf), /* MEM_TRANS_RETIRED.* */ 622 EVENT_CONSTRAINT_END 623 }; 624 625 static u64 intel_pmu_event_map(int hw_event) 626 { 627 return intel_perfmon_event_map[hw_event]; 628 } 629 630 static __initconst const u64 glc_hw_cache_event_ids 631 [PERF_COUNT_HW_CACHE_MAX] 632 [PERF_COUNT_HW_CACHE_OP_MAX] 633 [PERF_COUNT_HW_CACHE_RESULT_MAX] = 634 { 635 [ C(L1D ) ] = { 636 [ C(OP_READ) ] = { 637 [ C(RESULT_ACCESS) ] = 0x81d0, 638 [ C(RESULT_MISS) ] = 0xe124, 639 }, 640 [ C(OP_WRITE) ] = { 641 [ C(RESULT_ACCESS) ] = 0x82d0, 642 }, 643 }, 644 [ C(L1I ) ] = { 645 [ C(OP_READ) ] = { 646 [ C(RESULT_MISS) ] = 0xe424, 647 }, 648 [ C(OP_WRITE) ] = { 649 [ C(RESULT_ACCESS) ] = -1, 650 [ C(RESULT_MISS) ] = -1, 651 }, 652 }, 653 [ C(LL ) ] = { 654 [ C(OP_READ) ] = { 655 [ C(RESULT_ACCESS) ] = 0x12a, 656 [ C(RESULT_MISS) ] = 0x12a, 657 }, 658 [ C(OP_WRITE) ] = { 659 [ C(RESULT_ACCESS) ] = 0x12a, 660 [ C(RESULT_MISS) ] = 0x12a, 661 }, 662 }, 663 [ C(DTLB) ] = { 664 [ C(OP_READ) ] = { 665 [ C(RESULT_ACCESS) ] = 0x81d0, 666 [ C(RESULT_MISS) ] = 0xe12, 667 }, 668 [ C(OP_WRITE) ] = { 669 [ C(RESULT_ACCESS) ] = 0x82d0, 670 [ C(RESULT_MISS) ] = 0xe13, 671 }, 672 }, 673 [ C(ITLB) ] = { 674 [ C(OP_READ) ] = { 675 [ C(RESULT_ACCESS) ] = -1, 676 [ C(RESULT_MISS) ] = 0xe11, 677 }, 678 [ C(OP_WRITE) ] = { 679 [ C(RESULT_ACCESS) ] = -1, 680 [ C(RESULT_MISS) ] = -1, 681 }, 682 [ C(OP_PREFETCH) ] = { 683 [ C(RESULT_ACCESS) ] = -1, 684 [ C(RESULT_MISS) ] = -1, 685 }, 686 }, 687 [ C(BPU ) ] = { 688 [ C(OP_READ) ] = { 689 [ C(RESULT_ACCESS) ] = 0x4c4, 690 [ C(RESULT_MISS) ] = 0x4c5, 691 }, 692 [ C(OP_WRITE) ] = { 693 [ C(RESULT_ACCESS) ] = -1, 694 [ C(RESULT_MISS) ] = -1, 695 }, 696 [ C(OP_PREFETCH) ] = { 697 [ C(RESULT_ACCESS) ] = -1, 698 [ C(RESULT_MISS) ] = -1, 699 }, 700 }, 701 [ C(NODE) ] = { 702 [ C(OP_READ) ] = { 703 [ C(RESULT_ACCESS) ] = 0x12a, 704 [ C(RESULT_MISS) ] = 0x12a, 705 }, 706 }, 707 }; 708 709 static __initconst const u64 glc_hw_cache_extra_regs 710 [PERF_COUNT_HW_CACHE_MAX] 711 [PERF_COUNT_HW_CACHE_OP_MAX] 712 [PERF_COUNT_HW_CACHE_RESULT_MAX] = 713 { 714 [ C(LL ) ] = { 715 [ C(OP_READ) ] = { 716 [ C(RESULT_ACCESS) ] = 0x10001, 717 [ C(RESULT_MISS) ] = 0x3fbfc00001, 718 }, 719 [ C(OP_WRITE) ] = { 720 [ C(RESULT_ACCESS) ] = 0x3f3ffc0002, 721 [ C(RESULT_MISS) ] = 0x3f3fc00002, 722 }, 723 }, 724 [ C(NODE) ] = { 725 [ C(OP_READ) ] = { 726 [ C(RESULT_ACCESS) ] = 0x10c000001, 727 [ C(RESULT_MISS) ] = 0x3fb3000001, 728 }, 729 }, 730 }; 731 732 static __initconst const u64 pnc_hw_cache_event_ids 733 [PERF_COUNT_HW_CACHE_MAX] 734 [PERF_COUNT_HW_CACHE_OP_MAX] 735 [PERF_COUNT_HW_CACHE_RESULT_MAX] = 736 { 737 [ C(L1D ) ] = { 738 [ C(OP_READ) ] = { 739 [ C(RESULT_ACCESS) ] = 0x81d0, 740 [ C(RESULT_MISS) ] = 0xe124, 741 }, 742 [ C(OP_WRITE) ] = { 743 [ C(RESULT_ACCESS) ] = 0x82d0, 744 }, 745 }, 746 [ C(L1I ) ] = { 747 [ C(OP_READ) ] = { 748 [ C(RESULT_MISS) ] = 0xe424, 749 }, 750 [ C(OP_WRITE) ] = { 751 [ C(RESULT_ACCESS) ] = -1, 752 [ C(RESULT_MISS) ] = -1, 753 }, 754 }, 755 [ C(LL ) ] = { 756 [ C(OP_READ) ] = { 757 [ C(RESULT_ACCESS) ] = 0x12a, 758 [ C(RESULT_MISS) ] = 0x12a, 759 }, 760 [ C(OP_WRITE) ] = { 761 [ C(RESULT_ACCESS) ] = 0x12a, 762 [ C(RESULT_MISS) ] = 0x12a, 763 }, 764 }, 765 [ C(DTLB) ] = { 766 [ C(OP_READ) ] = { 767 [ C(RESULT_ACCESS) ] = 0x81d0, 768 [ C(RESULT_MISS) ] = 0xe12, 769 }, 770 [ C(OP_WRITE) ] = { 771 [ C(RESULT_ACCESS) ] = 0x82d0, 772 [ C(RESULT_MISS) ] = 0xe13, 773 }, 774 }, 775 [ C(ITLB) ] = { 776 [ C(OP_READ) ] = { 777 [ C(RESULT_ACCESS) ] = -1, 778 [ C(RESULT_MISS) ] = 0xe11, 779 }, 780 [ C(OP_WRITE) ] = { 781 [ C(RESULT_ACCESS) ] = -1, 782 [ C(RESULT_MISS) ] = -1, 783 }, 784 [ C(OP_PREFETCH) ] = { 785 [ C(RESULT_ACCESS) ] = -1, 786 [ C(RESULT_MISS) ] = -1, 787 }, 788 }, 789 [ C(BPU ) ] = { 790 [ C(OP_READ) ] = { 791 [ C(RESULT_ACCESS) ] = 0x4c4, 792 [ C(RESULT_MISS) ] = 0x4c5, 793 }, 794 [ C(OP_WRITE) ] = { 795 [ C(RESULT_ACCESS) ] = -1, 796 [ C(RESULT_MISS) ] = -1, 797 }, 798 [ C(OP_PREFETCH) ] = { 799 [ C(RESULT_ACCESS) ] = -1, 800 [ C(RESULT_MISS) ] = -1, 801 }, 802 }, 803 [ C(NODE) ] = { 804 [ C(OP_READ) ] = { 805 [ C(RESULT_ACCESS) ] = -1, 806 [ C(RESULT_MISS) ] = -1, 807 }, 808 }, 809 }; 810 811 static __initconst const u64 pnc_hw_cache_extra_regs 812 [PERF_COUNT_HW_CACHE_MAX] 813 [PERF_COUNT_HW_CACHE_OP_MAX] 814 [PERF_COUNT_HW_CACHE_RESULT_MAX] = 815 { 816 [ C(LL ) ] = { 817 [ C(OP_READ) ] = { 818 [ C(RESULT_ACCESS) ] = 0x4000000000000001, 819 [ C(RESULT_MISS) ] = 0xFFFFF000000001, 820 }, 821 [ C(OP_WRITE) ] = { 822 [ C(RESULT_ACCESS) ] = 0x4000000000000002, 823 [ C(RESULT_MISS) ] = 0xFFFFF000000002, 824 }, 825 }, 826 }; 827 828 /* 829 * Notes on the events: 830 * - data reads do not include code reads (comparable to earlier tables) 831 * - data counts include speculative execution (except L1 write, dtlb, bpu) 832 * - remote node access includes remote memory, remote cache, remote mmio. 833 * - prefetches are not included in the counts. 834 * - icache miss does not include decoded icache 835 */ 836 837 #define SKL_DEMAND_DATA_RD BIT_ULL(0) 838 #define SKL_DEMAND_RFO BIT_ULL(1) 839 #define SKL_ANY_RESPONSE BIT_ULL(16) 840 #define SKL_SUPPLIER_NONE BIT_ULL(17) 841 #define SKL_L3_MISS_LOCAL_DRAM BIT_ULL(26) 842 #define SKL_L3_MISS_REMOTE_HOP0_DRAM BIT_ULL(27) 843 #define SKL_L3_MISS_REMOTE_HOP1_DRAM BIT_ULL(28) 844 #define SKL_L3_MISS_REMOTE_HOP2P_DRAM BIT_ULL(29) 845 #define SKL_L3_MISS (SKL_L3_MISS_LOCAL_DRAM| \ 846 SKL_L3_MISS_REMOTE_HOP0_DRAM| \ 847 SKL_L3_MISS_REMOTE_HOP1_DRAM| \ 848 SKL_L3_MISS_REMOTE_HOP2P_DRAM) 849 #define SKL_SPL_HIT BIT_ULL(30) 850 #define SKL_SNOOP_NONE BIT_ULL(31) 851 #define SKL_SNOOP_NOT_NEEDED BIT_ULL(32) 852 #define SKL_SNOOP_MISS BIT_ULL(33) 853 #define SKL_SNOOP_HIT_NO_FWD BIT_ULL(34) 854 #define SKL_SNOOP_HIT_WITH_FWD BIT_ULL(35) 855 #define SKL_SNOOP_HITM BIT_ULL(36) 856 #define SKL_SNOOP_NON_DRAM BIT_ULL(37) 857 #define SKL_ANY_SNOOP (SKL_SPL_HIT|SKL_SNOOP_NONE| \ 858 SKL_SNOOP_NOT_NEEDED|SKL_SNOOP_MISS| \ 859 SKL_SNOOP_HIT_NO_FWD|SKL_SNOOP_HIT_WITH_FWD| \ 860 SKL_SNOOP_HITM|SKL_SNOOP_NON_DRAM) 861 #define SKL_DEMAND_READ SKL_DEMAND_DATA_RD 862 #define SKL_SNOOP_DRAM (SKL_SNOOP_NONE| \ 863 SKL_SNOOP_NOT_NEEDED|SKL_SNOOP_MISS| \ 864 SKL_SNOOP_HIT_NO_FWD|SKL_SNOOP_HIT_WITH_FWD| \ 865 SKL_SNOOP_HITM|SKL_SPL_HIT) 866 #define SKL_DEMAND_WRITE SKL_DEMAND_RFO 867 #define SKL_LLC_ACCESS SKL_ANY_RESPONSE 868 #define SKL_L3_MISS_REMOTE (SKL_L3_MISS_REMOTE_HOP0_DRAM| \ 869 SKL_L3_MISS_REMOTE_HOP1_DRAM| \ 870 SKL_L3_MISS_REMOTE_HOP2P_DRAM) 871 872 static __initconst const u64 skl_hw_cache_event_ids 873 [PERF_COUNT_HW_CACHE_MAX] 874 [PERF_COUNT_HW_CACHE_OP_MAX] 875 [PERF_COUNT_HW_CACHE_RESULT_MAX] = 876 { 877 [ C(L1D ) ] = { 878 [ C(OP_READ) ] = { 879 [ C(RESULT_ACCESS) ] = 0x81d0, /* MEM_INST_RETIRED.ALL_LOADS */ 880 [ C(RESULT_MISS) ] = 0x151, /* L1D.REPLACEMENT */ 881 }, 882 [ C(OP_WRITE) ] = { 883 [ C(RESULT_ACCESS) ] = 0x82d0, /* MEM_INST_RETIRED.ALL_STORES */ 884 [ C(RESULT_MISS) ] = 0x0, 885 }, 886 [ C(OP_PREFETCH) ] = { 887 [ C(RESULT_ACCESS) ] = 0x0, 888 [ C(RESULT_MISS) ] = 0x0, 889 }, 890 }, 891 [ C(L1I ) ] = { 892 [ C(OP_READ) ] = { 893 [ C(RESULT_ACCESS) ] = 0x0, 894 [ C(RESULT_MISS) ] = 0x283, /* ICACHE_64B.MISS */ 895 }, 896 [ C(OP_WRITE) ] = { 897 [ C(RESULT_ACCESS) ] = -1, 898 [ C(RESULT_MISS) ] = -1, 899 }, 900 [ C(OP_PREFETCH) ] = { 901 [ C(RESULT_ACCESS) ] = 0x0, 902 [ C(RESULT_MISS) ] = 0x0, 903 }, 904 }, 905 [ C(LL ) ] = { 906 [ C(OP_READ) ] = { 907 [ C(RESULT_ACCESS) ] = 0x1b7, /* OFFCORE_RESPONSE */ 908 [ C(RESULT_MISS) ] = 0x1b7, /* OFFCORE_RESPONSE */ 909 }, 910 [ C(OP_WRITE) ] = { 911 [ C(RESULT_ACCESS) ] = 0x1b7, /* OFFCORE_RESPONSE */ 912 [ C(RESULT_MISS) ] = 0x1b7, /* OFFCORE_RESPONSE */ 913 }, 914 [ C(OP_PREFETCH) ] = { 915 [ C(RESULT_ACCESS) ] = 0x0, 916 [ C(RESULT_MISS) ] = 0x0, 917 }, 918 }, 919 [ C(DTLB) ] = { 920 [ C(OP_READ) ] = { 921 [ C(RESULT_ACCESS) ] = 0x81d0, /* MEM_INST_RETIRED.ALL_LOADS */ 922 [ C(RESULT_MISS) ] = 0xe08, /* DTLB_LOAD_MISSES.WALK_COMPLETED */ 923 }, 924 [ C(OP_WRITE) ] = { 925 [ C(RESULT_ACCESS) ] = 0x82d0, /* MEM_INST_RETIRED.ALL_STORES */ 926 [ C(RESULT_MISS) ] = 0xe49, /* DTLB_STORE_MISSES.WALK_COMPLETED */ 927 }, 928 [ C(OP_PREFETCH) ] = { 929 [ C(RESULT_ACCESS) ] = 0x0, 930 [ C(RESULT_MISS) ] = 0x0, 931 }, 932 }, 933 [ C(ITLB) ] = { 934 [ C(OP_READ) ] = { 935 [ C(RESULT_ACCESS) ] = 0x2085, /* ITLB_MISSES.STLB_HIT */ 936 [ C(RESULT_MISS) ] = 0xe85, /* ITLB_MISSES.WALK_COMPLETED */ 937 }, 938 [ C(OP_WRITE) ] = { 939 [ C(RESULT_ACCESS) ] = -1, 940 [ C(RESULT_MISS) ] = -1, 941 }, 942 [ C(OP_PREFETCH) ] = { 943 [ C(RESULT_ACCESS) ] = -1, 944 [ C(RESULT_MISS) ] = -1, 945 }, 946 }, 947 [ C(BPU ) ] = { 948 [ C(OP_READ) ] = { 949 [ C(RESULT_ACCESS) ] = 0xc4, /* BR_INST_RETIRED.ALL_BRANCHES */ 950 [ C(RESULT_MISS) ] = 0xc5, /* BR_MISP_RETIRED.ALL_BRANCHES */ 951 }, 952 [ C(OP_WRITE) ] = { 953 [ C(RESULT_ACCESS) ] = -1, 954 [ C(RESULT_MISS) ] = -1, 955 }, 956 [ C(OP_PREFETCH) ] = { 957 [ C(RESULT_ACCESS) ] = -1, 958 [ C(RESULT_MISS) ] = -1, 959 }, 960 }, 961 [ C(NODE) ] = { 962 [ C(OP_READ) ] = { 963 [ C(RESULT_ACCESS) ] = 0x1b7, /* OFFCORE_RESPONSE */ 964 [ C(RESULT_MISS) ] = 0x1b7, /* OFFCORE_RESPONSE */ 965 }, 966 [ C(OP_WRITE) ] = { 967 [ C(RESULT_ACCESS) ] = 0x1b7, /* OFFCORE_RESPONSE */ 968 [ C(RESULT_MISS) ] = 0x1b7, /* OFFCORE_RESPONSE */ 969 }, 970 [ C(OP_PREFETCH) ] = { 971 [ C(RESULT_ACCESS) ] = 0x0, 972 [ C(RESULT_MISS) ] = 0x0, 973 }, 974 }, 975 }; 976 977 static __initconst const u64 skl_hw_cache_extra_regs 978 [PERF_COUNT_HW_CACHE_MAX] 979 [PERF_COUNT_HW_CACHE_OP_MAX] 980 [PERF_COUNT_HW_CACHE_RESULT_MAX] = 981 { 982 [ C(LL ) ] = { 983 [ C(OP_READ) ] = { 984 [ C(RESULT_ACCESS) ] = SKL_DEMAND_READ| 985 SKL_LLC_ACCESS|SKL_ANY_SNOOP, 986 [ C(RESULT_MISS) ] = SKL_DEMAND_READ| 987 SKL_L3_MISS|SKL_ANY_SNOOP| 988 SKL_SUPPLIER_NONE, 989 }, 990 [ C(OP_WRITE) ] = { 991 [ C(RESULT_ACCESS) ] = SKL_DEMAND_WRITE| 992 SKL_LLC_ACCESS|SKL_ANY_SNOOP, 993 [ C(RESULT_MISS) ] = SKL_DEMAND_WRITE| 994 SKL_L3_MISS|SKL_ANY_SNOOP| 995 SKL_SUPPLIER_NONE, 996 }, 997 [ C(OP_PREFETCH) ] = { 998 [ C(RESULT_ACCESS) ] = 0x0, 999 [ C(RESULT_MISS) ] = 0x0, 1000 }, 1001 }, 1002 [ C(NODE) ] = { 1003 [ C(OP_READ) ] = { 1004 [ C(RESULT_ACCESS) ] = SKL_DEMAND_READ| 1005 SKL_L3_MISS_LOCAL_DRAM|SKL_SNOOP_DRAM, 1006 [ C(RESULT_MISS) ] = SKL_DEMAND_READ| 1007 SKL_L3_MISS_REMOTE|SKL_SNOOP_DRAM, 1008 }, 1009 [ C(OP_WRITE) ] = { 1010 [ C(RESULT_ACCESS) ] = SKL_DEMAND_WRITE| 1011 SKL_L3_MISS_LOCAL_DRAM|SKL_SNOOP_DRAM, 1012 [ C(RESULT_MISS) ] = SKL_DEMAND_WRITE| 1013 SKL_L3_MISS_REMOTE|SKL_SNOOP_DRAM, 1014 }, 1015 [ C(OP_PREFETCH) ] = { 1016 [ C(RESULT_ACCESS) ] = 0x0, 1017 [ C(RESULT_MISS) ] = 0x0, 1018 }, 1019 }, 1020 }; 1021 1022 #define SNB_DMND_DATA_RD (1ULL << 0) 1023 #define SNB_DMND_RFO (1ULL << 1) 1024 #define SNB_DMND_IFETCH (1ULL << 2) 1025 #define SNB_DMND_WB (1ULL << 3) 1026 #define SNB_PF_DATA_RD (1ULL << 4) 1027 #define SNB_PF_RFO (1ULL << 5) 1028 #define SNB_PF_IFETCH (1ULL << 6) 1029 #define SNB_LLC_DATA_RD (1ULL << 7) 1030 #define SNB_LLC_RFO (1ULL << 8) 1031 #define SNB_LLC_IFETCH (1ULL << 9) 1032 #define SNB_BUS_LOCKS (1ULL << 10) 1033 #define SNB_STRM_ST (1ULL << 11) 1034 #define SNB_OTHER (1ULL << 15) 1035 #define SNB_RESP_ANY (1ULL << 16) 1036 #define SNB_NO_SUPP (1ULL << 17) 1037 #define SNB_LLC_HITM (1ULL << 18) 1038 #define SNB_LLC_HITE (1ULL << 19) 1039 #define SNB_LLC_HITS (1ULL << 20) 1040 #define SNB_LLC_HITF (1ULL << 21) 1041 #define SNB_LOCAL (1ULL << 22) 1042 #define SNB_REMOTE (0xffULL << 23) 1043 #define SNB_SNP_NONE (1ULL << 31) 1044 #define SNB_SNP_NOT_NEEDED (1ULL << 32) 1045 #define SNB_SNP_MISS (1ULL << 33) 1046 #define SNB_NO_FWD (1ULL << 34) 1047 #define SNB_SNP_FWD (1ULL << 35) 1048 #define SNB_HITM (1ULL << 36) 1049 #define SNB_NON_DRAM (1ULL << 37) 1050 1051 #define SNB_DMND_READ (SNB_DMND_DATA_RD|SNB_LLC_DATA_RD) 1052 #define SNB_DMND_WRITE (SNB_DMND_RFO|SNB_LLC_RFO) 1053 #define SNB_DMND_PREFETCH (SNB_PF_DATA_RD|SNB_PF_RFO) 1054 1055 #define SNB_SNP_ANY (SNB_SNP_NONE|SNB_SNP_NOT_NEEDED| \ 1056 SNB_SNP_MISS|SNB_NO_FWD|SNB_SNP_FWD| \ 1057 SNB_HITM) 1058 1059 #define SNB_DRAM_ANY (SNB_LOCAL|SNB_REMOTE|SNB_SNP_ANY) 1060 #define SNB_DRAM_REMOTE (SNB_REMOTE|SNB_SNP_ANY) 1061 1062 #define SNB_L3_ACCESS SNB_RESP_ANY 1063 #define SNB_L3_MISS (SNB_DRAM_ANY|SNB_NON_DRAM) 1064 1065 static __initconst const u64 snb_hw_cache_extra_regs 1066 [PERF_COUNT_HW_CACHE_MAX] 1067 [PERF_COUNT_HW_CACHE_OP_MAX] 1068 [PERF_COUNT_HW_CACHE_RESULT_MAX] = 1069 { 1070 [ C(LL ) ] = { 1071 [ C(OP_READ) ] = { 1072 [ C(RESULT_ACCESS) ] = SNB_DMND_READ|SNB_L3_ACCESS, 1073 [ C(RESULT_MISS) ] = SNB_DMND_READ|SNB_L3_MISS, 1074 }, 1075 [ C(OP_WRITE) ] = { 1076 [ C(RESULT_ACCESS) ] = SNB_DMND_WRITE|SNB_L3_ACCESS, 1077 [ C(RESULT_MISS) ] = SNB_DMND_WRITE|SNB_L3_MISS, 1078 }, 1079 [ C(OP_PREFETCH) ] = { 1080 [ C(RESULT_ACCESS) ] = SNB_DMND_PREFETCH|SNB_L3_ACCESS, 1081 [ C(RESULT_MISS) ] = SNB_DMND_PREFETCH|SNB_L3_MISS, 1082 }, 1083 }, 1084 [ C(NODE) ] = { 1085 [ C(OP_READ) ] = { 1086 [ C(RESULT_ACCESS) ] = SNB_DMND_READ|SNB_DRAM_ANY, 1087 [ C(RESULT_MISS) ] = SNB_DMND_READ|SNB_DRAM_REMOTE, 1088 }, 1089 [ C(OP_WRITE) ] = { 1090 [ C(RESULT_ACCESS) ] = SNB_DMND_WRITE|SNB_DRAM_ANY, 1091 [ C(RESULT_MISS) ] = SNB_DMND_WRITE|SNB_DRAM_REMOTE, 1092 }, 1093 [ C(OP_PREFETCH) ] = { 1094 [ C(RESULT_ACCESS) ] = SNB_DMND_PREFETCH|SNB_DRAM_ANY, 1095 [ C(RESULT_MISS) ] = SNB_DMND_PREFETCH|SNB_DRAM_REMOTE, 1096 }, 1097 }, 1098 }; 1099 1100 static __initconst const u64 snb_hw_cache_event_ids 1101 [PERF_COUNT_HW_CACHE_MAX] 1102 [PERF_COUNT_HW_CACHE_OP_MAX] 1103 [PERF_COUNT_HW_CACHE_RESULT_MAX] = 1104 { 1105 [ C(L1D) ] = { 1106 [ C(OP_READ) ] = { 1107 [ C(RESULT_ACCESS) ] = 0xf1d0, /* MEM_UOP_RETIRED.LOADS */ 1108 [ C(RESULT_MISS) ] = 0x0151, /* L1D.REPLACEMENT */ 1109 }, 1110 [ C(OP_WRITE) ] = { 1111 [ C(RESULT_ACCESS) ] = 0xf2d0, /* MEM_UOP_RETIRED.STORES */ 1112 [ C(RESULT_MISS) ] = 0x0851, /* L1D.ALL_M_REPLACEMENT */ 1113 }, 1114 [ C(OP_PREFETCH) ] = { 1115 [ C(RESULT_ACCESS) ] = 0x0, 1116 [ C(RESULT_MISS) ] = 0x024e, /* HW_PRE_REQ.DL1_MISS */ 1117 }, 1118 }, 1119 [ C(L1I ) ] = { 1120 [ C(OP_READ) ] = { 1121 [ C(RESULT_ACCESS) ] = 0x0, 1122 [ C(RESULT_MISS) ] = 0x0280, /* ICACHE.MISSES */ 1123 }, 1124 [ C(OP_WRITE) ] = { 1125 [ C(RESULT_ACCESS) ] = -1, 1126 [ C(RESULT_MISS) ] = -1, 1127 }, 1128 [ C(OP_PREFETCH) ] = { 1129 [ C(RESULT_ACCESS) ] = 0x0, 1130 [ C(RESULT_MISS) ] = 0x0, 1131 }, 1132 }, 1133 [ C(LL ) ] = { 1134 [ C(OP_READ) ] = { 1135 /* OFFCORE_RESPONSE.ANY_DATA.LOCAL_CACHE */ 1136 [ C(RESULT_ACCESS) ] = 0x01b7, 1137 /* OFFCORE_RESPONSE.ANY_DATA.ANY_LLC_MISS */ 1138 [ C(RESULT_MISS) ] = 0x01b7, 1139 }, 1140 [ C(OP_WRITE) ] = { 1141 /* OFFCORE_RESPONSE.ANY_RFO.LOCAL_CACHE */ 1142 [ C(RESULT_ACCESS) ] = 0x01b7, 1143 /* OFFCORE_RESPONSE.ANY_RFO.ANY_LLC_MISS */ 1144 [ C(RESULT_MISS) ] = 0x01b7, 1145 }, 1146 [ C(OP_PREFETCH) ] = { 1147 /* OFFCORE_RESPONSE.PREFETCH.LOCAL_CACHE */ 1148 [ C(RESULT_ACCESS) ] = 0x01b7, 1149 /* OFFCORE_RESPONSE.PREFETCH.ANY_LLC_MISS */ 1150 [ C(RESULT_MISS) ] = 0x01b7, 1151 }, 1152 }, 1153 [ C(DTLB) ] = { 1154 [ C(OP_READ) ] = { 1155 [ C(RESULT_ACCESS) ] = 0x81d0, /* MEM_UOP_RETIRED.ALL_LOADS */ 1156 [ C(RESULT_MISS) ] = 0x0108, /* DTLB_LOAD_MISSES.CAUSES_A_WALK */ 1157 }, 1158 [ C(OP_WRITE) ] = { 1159 [ C(RESULT_ACCESS) ] = 0x82d0, /* MEM_UOP_RETIRED.ALL_STORES */ 1160 [ C(RESULT_MISS) ] = 0x0149, /* DTLB_STORE_MISSES.MISS_CAUSES_A_WALK */ 1161 }, 1162 [ C(OP_PREFETCH) ] = { 1163 [ C(RESULT_ACCESS) ] = 0x0, 1164 [ C(RESULT_MISS) ] = 0x0, 1165 }, 1166 }, 1167 [ C(ITLB) ] = { 1168 [ C(OP_READ) ] = { 1169 [ C(RESULT_ACCESS) ] = 0x1085, /* ITLB_MISSES.STLB_HIT */ 1170 [ C(RESULT_MISS) ] = 0x0185, /* ITLB_MISSES.CAUSES_A_WALK */ 1171 }, 1172 [ C(OP_WRITE) ] = { 1173 [ C(RESULT_ACCESS) ] = -1, 1174 [ C(RESULT_MISS) ] = -1, 1175 }, 1176 [ C(OP_PREFETCH) ] = { 1177 [ C(RESULT_ACCESS) ] = -1, 1178 [ C(RESULT_MISS) ] = -1, 1179 }, 1180 }, 1181 [ C(BPU ) ] = { 1182 [ C(OP_READ) ] = { 1183 [ C(RESULT_ACCESS) ] = 0x00c4, /* BR_INST_RETIRED.ALL_BRANCHES */ 1184 [ C(RESULT_MISS) ] = 0x00c5, /* BR_MISP_RETIRED.ALL_BRANCHES */ 1185 }, 1186 [ C(OP_WRITE) ] = { 1187 [ C(RESULT_ACCESS) ] = -1, 1188 [ C(RESULT_MISS) ] = -1, 1189 }, 1190 [ C(OP_PREFETCH) ] = { 1191 [ C(RESULT_ACCESS) ] = -1, 1192 [ C(RESULT_MISS) ] = -1, 1193 }, 1194 }, 1195 [ C(NODE) ] = { 1196 [ C(OP_READ) ] = { 1197 [ C(RESULT_ACCESS) ] = 0x01b7, 1198 [ C(RESULT_MISS) ] = 0x01b7, 1199 }, 1200 [ C(OP_WRITE) ] = { 1201 [ C(RESULT_ACCESS) ] = 0x01b7, 1202 [ C(RESULT_MISS) ] = 0x01b7, 1203 }, 1204 [ C(OP_PREFETCH) ] = { 1205 [ C(RESULT_ACCESS) ] = 0x01b7, 1206 [ C(RESULT_MISS) ] = 0x01b7, 1207 }, 1208 }, 1209 1210 }; 1211 1212 /* 1213 * Notes on the events: 1214 * - data reads do not include code reads (comparable to earlier tables) 1215 * - data counts include speculative execution (except L1 write, dtlb, bpu) 1216 * - remote node access includes remote memory, remote cache, remote mmio. 1217 * - prefetches are not included in the counts because they are not 1218 * reliably counted. 1219 */ 1220 1221 #define HSW_DEMAND_DATA_RD BIT_ULL(0) 1222 #define HSW_DEMAND_RFO BIT_ULL(1) 1223 #define HSW_ANY_RESPONSE BIT_ULL(16) 1224 #define HSW_SUPPLIER_NONE BIT_ULL(17) 1225 #define HSW_L3_MISS_LOCAL_DRAM BIT_ULL(22) 1226 #define HSW_L3_MISS_REMOTE_HOP0 BIT_ULL(27) 1227 #define HSW_L3_MISS_REMOTE_HOP1 BIT_ULL(28) 1228 #define HSW_L3_MISS_REMOTE_HOP2P BIT_ULL(29) 1229 #define HSW_L3_MISS (HSW_L3_MISS_LOCAL_DRAM| \ 1230 HSW_L3_MISS_REMOTE_HOP0|HSW_L3_MISS_REMOTE_HOP1| \ 1231 HSW_L3_MISS_REMOTE_HOP2P) 1232 #define HSW_SNOOP_NONE BIT_ULL(31) 1233 #define HSW_SNOOP_NOT_NEEDED BIT_ULL(32) 1234 #define HSW_SNOOP_MISS BIT_ULL(33) 1235 #define HSW_SNOOP_HIT_NO_FWD BIT_ULL(34) 1236 #define HSW_SNOOP_HIT_WITH_FWD BIT_ULL(35) 1237 #define HSW_SNOOP_HITM BIT_ULL(36) 1238 #define HSW_SNOOP_NON_DRAM BIT_ULL(37) 1239 #define HSW_ANY_SNOOP (HSW_SNOOP_NONE| \ 1240 HSW_SNOOP_NOT_NEEDED|HSW_SNOOP_MISS| \ 1241 HSW_SNOOP_HIT_NO_FWD|HSW_SNOOP_HIT_WITH_FWD| \ 1242 HSW_SNOOP_HITM|HSW_SNOOP_NON_DRAM) 1243 #define HSW_SNOOP_DRAM (HSW_ANY_SNOOP & ~HSW_SNOOP_NON_DRAM) 1244 #define HSW_DEMAND_READ HSW_DEMAND_DATA_RD 1245 #define HSW_DEMAND_WRITE HSW_DEMAND_RFO 1246 #define HSW_L3_MISS_REMOTE (HSW_L3_MISS_REMOTE_HOP0|\ 1247 HSW_L3_MISS_REMOTE_HOP1|HSW_L3_MISS_REMOTE_HOP2P) 1248 #define HSW_LLC_ACCESS HSW_ANY_RESPONSE 1249 1250 #define BDW_L3_MISS_LOCAL BIT(26) 1251 #define BDW_L3_MISS (BDW_L3_MISS_LOCAL| \ 1252 HSW_L3_MISS_REMOTE_HOP0|HSW_L3_MISS_REMOTE_HOP1| \ 1253 HSW_L3_MISS_REMOTE_HOP2P) 1254 1255 1256 static __initconst const u64 hsw_hw_cache_event_ids 1257 [PERF_COUNT_HW_CACHE_MAX] 1258 [PERF_COUNT_HW_CACHE_OP_MAX] 1259 [PERF_COUNT_HW_CACHE_RESULT_MAX] = 1260 { 1261 [ C(L1D ) ] = { 1262 [ C(OP_READ) ] = { 1263 [ C(RESULT_ACCESS) ] = 0x81d0, /* MEM_UOPS_RETIRED.ALL_LOADS */ 1264 [ C(RESULT_MISS) ] = 0x151, /* L1D.REPLACEMENT */ 1265 }, 1266 [ C(OP_WRITE) ] = { 1267 [ C(RESULT_ACCESS) ] = 0x82d0, /* MEM_UOPS_RETIRED.ALL_STORES */ 1268 [ C(RESULT_MISS) ] = 0x0, 1269 }, 1270 [ C(OP_PREFETCH) ] = { 1271 [ C(RESULT_ACCESS) ] = 0x0, 1272 [ C(RESULT_MISS) ] = 0x0, 1273 }, 1274 }, 1275 [ C(L1I ) ] = { 1276 [ C(OP_READ) ] = { 1277 [ C(RESULT_ACCESS) ] = 0x0, 1278 [ C(RESULT_MISS) ] = 0x280, /* ICACHE.MISSES */ 1279 }, 1280 [ C(OP_WRITE) ] = { 1281 [ C(RESULT_ACCESS) ] = -1, 1282 [ C(RESULT_MISS) ] = -1, 1283 }, 1284 [ C(OP_PREFETCH) ] = { 1285 [ C(RESULT_ACCESS) ] = 0x0, 1286 [ C(RESULT_MISS) ] = 0x0, 1287 }, 1288 }, 1289 [ C(LL ) ] = { 1290 [ C(OP_READ) ] = { 1291 [ C(RESULT_ACCESS) ] = 0x1b7, /* OFFCORE_RESPONSE */ 1292 [ C(RESULT_MISS) ] = 0x1b7, /* OFFCORE_RESPONSE */ 1293 }, 1294 [ C(OP_WRITE) ] = { 1295 [ C(RESULT_ACCESS) ] = 0x1b7, /* OFFCORE_RESPONSE */ 1296 [ C(RESULT_MISS) ] = 0x1b7, /* OFFCORE_RESPONSE */ 1297 }, 1298 [ C(OP_PREFETCH) ] = { 1299 [ C(RESULT_ACCESS) ] = 0x0, 1300 [ C(RESULT_MISS) ] = 0x0, 1301 }, 1302 }, 1303 [ C(DTLB) ] = { 1304 [ C(OP_READ) ] = { 1305 [ C(RESULT_ACCESS) ] = 0x81d0, /* MEM_UOPS_RETIRED.ALL_LOADS */ 1306 [ C(RESULT_MISS) ] = 0x108, /* DTLB_LOAD_MISSES.MISS_CAUSES_A_WALK */ 1307 }, 1308 [ C(OP_WRITE) ] = { 1309 [ C(RESULT_ACCESS) ] = 0x82d0, /* MEM_UOPS_RETIRED.ALL_STORES */ 1310 [ C(RESULT_MISS) ] = 0x149, /* DTLB_STORE_MISSES.MISS_CAUSES_A_WALK */ 1311 }, 1312 [ C(OP_PREFETCH) ] = { 1313 [ C(RESULT_ACCESS) ] = 0x0, 1314 [ C(RESULT_MISS) ] = 0x0, 1315 }, 1316 }, 1317 [ C(ITLB) ] = { 1318 [ C(OP_READ) ] = { 1319 [ C(RESULT_ACCESS) ] = 0x6085, /* ITLB_MISSES.STLB_HIT */ 1320 [ C(RESULT_MISS) ] = 0x185, /* ITLB_MISSES.MISS_CAUSES_A_WALK */ 1321 }, 1322 [ C(OP_WRITE) ] = { 1323 [ C(RESULT_ACCESS) ] = -1, 1324 [ C(RESULT_MISS) ] = -1, 1325 }, 1326 [ C(OP_PREFETCH) ] = { 1327 [ C(RESULT_ACCESS) ] = -1, 1328 [ C(RESULT_MISS) ] = -1, 1329 }, 1330 }, 1331 [ C(BPU ) ] = { 1332 [ C(OP_READ) ] = { 1333 [ C(RESULT_ACCESS) ] = 0xc4, /* BR_INST_RETIRED.ALL_BRANCHES */ 1334 [ C(RESULT_MISS) ] = 0xc5, /* BR_MISP_RETIRED.ALL_BRANCHES */ 1335 }, 1336 [ C(OP_WRITE) ] = { 1337 [ C(RESULT_ACCESS) ] = -1, 1338 [ C(RESULT_MISS) ] = -1, 1339 }, 1340 [ C(OP_PREFETCH) ] = { 1341 [ C(RESULT_ACCESS) ] = -1, 1342 [ C(RESULT_MISS) ] = -1, 1343 }, 1344 }, 1345 [ C(NODE) ] = { 1346 [ C(OP_READ) ] = { 1347 [ C(RESULT_ACCESS) ] = 0x1b7, /* OFFCORE_RESPONSE */ 1348 [ C(RESULT_MISS) ] = 0x1b7, /* OFFCORE_RESPONSE */ 1349 }, 1350 [ C(OP_WRITE) ] = { 1351 [ C(RESULT_ACCESS) ] = 0x1b7, /* OFFCORE_RESPONSE */ 1352 [ C(RESULT_MISS) ] = 0x1b7, /* OFFCORE_RESPONSE */ 1353 }, 1354 [ C(OP_PREFETCH) ] = { 1355 [ C(RESULT_ACCESS) ] = 0x0, 1356 [ C(RESULT_MISS) ] = 0x0, 1357 }, 1358 }, 1359 }; 1360 1361 static __initconst const u64 hsw_hw_cache_extra_regs 1362 [PERF_COUNT_HW_CACHE_MAX] 1363 [PERF_COUNT_HW_CACHE_OP_MAX] 1364 [PERF_COUNT_HW_CACHE_RESULT_MAX] = 1365 { 1366 [ C(LL ) ] = { 1367 [ C(OP_READ) ] = { 1368 [ C(RESULT_ACCESS) ] = HSW_DEMAND_READ| 1369 HSW_LLC_ACCESS, 1370 [ C(RESULT_MISS) ] = HSW_DEMAND_READ| 1371 HSW_L3_MISS|HSW_ANY_SNOOP, 1372 }, 1373 [ C(OP_WRITE) ] = { 1374 [ C(RESULT_ACCESS) ] = HSW_DEMAND_WRITE| 1375 HSW_LLC_ACCESS, 1376 [ C(RESULT_MISS) ] = HSW_DEMAND_WRITE| 1377 HSW_L3_MISS|HSW_ANY_SNOOP, 1378 }, 1379 [ C(OP_PREFETCH) ] = { 1380 [ C(RESULT_ACCESS) ] = 0x0, 1381 [ C(RESULT_MISS) ] = 0x0, 1382 }, 1383 }, 1384 [ C(NODE) ] = { 1385 [ C(OP_READ) ] = { 1386 [ C(RESULT_ACCESS) ] = HSW_DEMAND_READ| 1387 HSW_L3_MISS_LOCAL_DRAM| 1388 HSW_SNOOP_DRAM, 1389 [ C(RESULT_MISS) ] = HSW_DEMAND_READ| 1390 HSW_L3_MISS_REMOTE| 1391 HSW_SNOOP_DRAM, 1392 }, 1393 [ C(OP_WRITE) ] = { 1394 [ C(RESULT_ACCESS) ] = HSW_DEMAND_WRITE| 1395 HSW_L3_MISS_LOCAL_DRAM| 1396 HSW_SNOOP_DRAM, 1397 [ C(RESULT_MISS) ] = HSW_DEMAND_WRITE| 1398 HSW_L3_MISS_REMOTE| 1399 HSW_SNOOP_DRAM, 1400 }, 1401 [ C(OP_PREFETCH) ] = { 1402 [ C(RESULT_ACCESS) ] = 0x0, 1403 [ C(RESULT_MISS) ] = 0x0, 1404 }, 1405 }, 1406 }; 1407 1408 static __initconst const u64 westmere_hw_cache_event_ids 1409 [PERF_COUNT_HW_CACHE_MAX] 1410 [PERF_COUNT_HW_CACHE_OP_MAX] 1411 [PERF_COUNT_HW_CACHE_RESULT_MAX] = 1412 { 1413 [ C(L1D) ] = { 1414 [ C(OP_READ) ] = { 1415 [ C(RESULT_ACCESS) ] = 0x010b, /* MEM_INST_RETIRED.LOADS */ 1416 [ C(RESULT_MISS) ] = 0x0151, /* L1D.REPL */ 1417 }, 1418 [ C(OP_WRITE) ] = { 1419 [ C(RESULT_ACCESS) ] = 0x020b, /* MEM_INST_RETURED.STORES */ 1420 [ C(RESULT_MISS) ] = 0x0251, /* L1D.M_REPL */ 1421 }, 1422 [ C(OP_PREFETCH) ] = { 1423 [ C(RESULT_ACCESS) ] = 0x014e, /* L1D_PREFETCH.REQUESTS */ 1424 [ C(RESULT_MISS) ] = 0x024e, /* L1D_PREFETCH.MISS */ 1425 }, 1426 }, 1427 [ C(L1I ) ] = { 1428 [ C(OP_READ) ] = { 1429 [ C(RESULT_ACCESS) ] = 0x0380, /* L1I.READS */ 1430 [ C(RESULT_MISS) ] = 0x0280, /* L1I.MISSES */ 1431 }, 1432 [ C(OP_WRITE) ] = { 1433 [ C(RESULT_ACCESS) ] = -1, 1434 [ C(RESULT_MISS) ] = -1, 1435 }, 1436 [ C(OP_PREFETCH) ] = { 1437 [ C(RESULT_ACCESS) ] = 0x0, 1438 [ C(RESULT_MISS) ] = 0x0, 1439 }, 1440 }, 1441 [ C(LL ) ] = { 1442 [ C(OP_READ) ] = { 1443 /* OFFCORE_RESPONSE.ANY_DATA.LOCAL_CACHE */ 1444 [ C(RESULT_ACCESS) ] = 0x01b7, 1445 /* OFFCORE_RESPONSE.ANY_DATA.ANY_LLC_MISS */ 1446 [ C(RESULT_MISS) ] = 0x01b7, 1447 }, 1448 /* 1449 * Use RFO, not WRITEBACK, because a write miss would typically occur 1450 * on RFO. 1451 */ 1452 [ C(OP_WRITE) ] = { 1453 /* OFFCORE_RESPONSE.ANY_RFO.LOCAL_CACHE */ 1454 [ C(RESULT_ACCESS) ] = 0x01b7, 1455 /* OFFCORE_RESPONSE.ANY_RFO.ANY_LLC_MISS */ 1456 [ C(RESULT_MISS) ] = 0x01b7, 1457 }, 1458 [ C(OP_PREFETCH) ] = { 1459 /* OFFCORE_RESPONSE.PREFETCH.LOCAL_CACHE */ 1460 [ C(RESULT_ACCESS) ] = 0x01b7, 1461 /* OFFCORE_RESPONSE.PREFETCH.ANY_LLC_MISS */ 1462 [ C(RESULT_MISS) ] = 0x01b7, 1463 }, 1464 }, 1465 [ C(DTLB) ] = { 1466 [ C(OP_READ) ] = { 1467 [ C(RESULT_ACCESS) ] = 0x010b, /* MEM_INST_RETIRED.LOADS */ 1468 [ C(RESULT_MISS) ] = 0x0108, /* DTLB_LOAD_MISSES.ANY */ 1469 }, 1470 [ C(OP_WRITE) ] = { 1471 [ C(RESULT_ACCESS) ] = 0x020b, /* MEM_INST_RETURED.STORES */ 1472 [ C(RESULT_MISS) ] = 0x010c, /* MEM_STORE_RETIRED.DTLB_MISS */ 1473 }, 1474 [ C(OP_PREFETCH) ] = { 1475 [ C(RESULT_ACCESS) ] = 0x0, 1476 [ C(RESULT_MISS) ] = 0x0, 1477 }, 1478 }, 1479 [ C(ITLB) ] = { 1480 [ C(OP_READ) ] = { 1481 [ C(RESULT_ACCESS) ] = 0x01c0, /* INST_RETIRED.ANY_P */ 1482 [ C(RESULT_MISS) ] = 0x0185, /* ITLB_MISSES.ANY */ 1483 }, 1484 [ C(OP_WRITE) ] = { 1485 [ C(RESULT_ACCESS) ] = -1, 1486 [ C(RESULT_MISS) ] = -1, 1487 }, 1488 [ C(OP_PREFETCH) ] = { 1489 [ C(RESULT_ACCESS) ] = -1, 1490 [ C(RESULT_MISS) ] = -1, 1491 }, 1492 }, 1493 [ C(BPU ) ] = { 1494 [ C(OP_READ) ] = { 1495 [ C(RESULT_ACCESS) ] = 0x00c4, /* BR_INST_RETIRED.ALL_BRANCHES */ 1496 [ C(RESULT_MISS) ] = 0x03e8, /* BPU_CLEARS.ANY */ 1497 }, 1498 [ C(OP_WRITE) ] = { 1499 [ C(RESULT_ACCESS) ] = -1, 1500 [ C(RESULT_MISS) ] = -1, 1501 }, 1502 [ C(OP_PREFETCH) ] = { 1503 [ C(RESULT_ACCESS) ] = -1, 1504 [ C(RESULT_MISS) ] = -1, 1505 }, 1506 }, 1507 [ C(NODE) ] = { 1508 [ C(OP_READ) ] = { 1509 [ C(RESULT_ACCESS) ] = 0x01b7, 1510 [ C(RESULT_MISS) ] = 0x01b7, 1511 }, 1512 [ C(OP_WRITE) ] = { 1513 [ C(RESULT_ACCESS) ] = 0x01b7, 1514 [ C(RESULT_MISS) ] = 0x01b7, 1515 }, 1516 [ C(OP_PREFETCH) ] = { 1517 [ C(RESULT_ACCESS) ] = 0x01b7, 1518 [ C(RESULT_MISS) ] = 0x01b7, 1519 }, 1520 }, 1521 }; 1522 1523 /* 1524 * Nehalem/Westmere MSR_OFFCORE_RESPONSE bits; 1525 * See IA32 SDM Vol 3B 30.6.1.3 1526 */ 1527 1528 #define NHM_DMND_DATA_RD (1 << 0) 1529 #define NHM_DMND_RFO (1 << 1) 1530 #define NHM_DMND_IFETCH (1 << 2) 1531 #define NHM_DMND_WB (1 << 3) 1532 #define NHM_PF_DATA_RD (1 << 4) 1533 #define NHM_PF_DATA_RFO (1 << 5) 1534 #define NHM_PF_IFETCH (1 << 6) 1535 #define NHM_OFFCORE_OTHER (1 << 7) 1536 #define NHM_UNCORE_HIT (1 << 8) 1537 #define NHM_OTHER_CORE_HIT_SNP (1 << 9) 1538 #define NHM_OTHER_CORE_HITM (1 << 10) 1539 /* reserved */ 1540 #define NHM_REMOTE_CACHE_FWD (1 << 12) 1541 #define NHM_REMOTE_DRAM (1 << 13) 1542 #define NHM_LOCAL_DRAM (1 << 14) 1543 #define NHM_NON_DRAM (1 << 15) 1544 1545 #define NHM_LOCAL (NHM_LOCAL_DRAM|NHM_REMOTE_CACHE_FWD) 1546 #define NHM_REMOTE (NHM_REMOTE_DRAM) 1547 1548 #define NHM_DMND_READ (NHM_DMND_DATA_RD) 1549 #define NHM_DMND_WRITE (NHM_DMND_RFO|NHM_DMND_WB) 1550 #define NHM_DMND_PREFETCH (NHM_PF_DATA_RD|NHM_PF_DATA_RFO) 1551 1552 #define NHM_L3_HIT (NHM_UNCORE_HIT|NHM_OTHER_CORE_HIT_SNP|NHM_OTHER_CORE_HITM) 1553 #define NHM_L3_MISS (NHM_NON_DRAM|NHM_LOCAL_DRAM|NHM_REMOTE_DRAM|NHM_REMOTE_CACHE_FWD) 1554 #define NHM_L3_ACCESS (NHM_L3_HIT|NHM_L3_MISS) 1555 1556 static __initconst const u64 nehalem_hw_cache_extra_regs 1557 [PERF_COUNT_HW_CACHE_MAX] 1558 [PERF_COUNT_HW_CACHE_OP_MAX] 1559 [PERF_COUNT_HW_CACHE_RESULT_MAX] = 1560 { 1561 [ C(LL ) ] = { 1562 [ C(OP_READ) ] = { 1563 [ C(RESULT_ACCESS) ] = NHM_DMND_READ|NHM_L3_ACCESS, 1564 [ C(RESULT_MISS) ] = NHM_DMND_READ|NHM_L3_MISS, 1565 }, 1566 [ C(OP_WRITE) ] = { 1567 [ C(RESULT_ACCESS) ] = NHM_DMND_WRITE|NHM_L3_ACCESS, 1568 [ C(RESULT_MISS) ] = NHM_DMND_WRITE|NHM_L3_MISS, 1569 }, 1570 [ C(OP_PREFETCH) ] = { 1571 [ C(RESULT_ACCESS) ] = NHM_DMND_PREFETCH|NHM_L3_ACCESS, 1572 [ C(RESULT_MISS) ] = NHM_DMND_PREFETCH|NHM_L3_MISS, 1573 }, 1574 }, 1575 [ C(NODE) ] = { 1576 [ C(OP_READ) ] = { 1577 [ C(RESULT_ACCESS) ] = NHM_DMND_READ|NHM_LOCAL|NHM_REMOTE, 1578 [ C(RESULT_MISS) ] = NHM_DMND_READ|NHM_REMOTE, 1579 }, 1580 [ C(OP_WRITE) ] = { 1581 [ C(RESULT_ACCESS) ] = NHM_DMND_WRITE|NHM_LOCAL|NHM_REMOTE, 1582 [ C(RESULT_MISS) ] = NHM_DMND_WRITE|NHM_REMOTE, 1583 }, 1584 [ C(OP_PREFETCH) ] = { 1585 [ C(RESULT_ACCESS) ] = NHM_DMND_PREFETCH|NHM_LOCAL|NHM_REMOTE, 1586 [ C(RESULT_MISS) ] = NHM_DMND_PREFETCH|NHM_REMOTE, 1587 }, 1588 }, 1589 }; 1590 1591 static __initconst const u64 nehalem_hw_cache_event_ids 1592 [PERF_COUNT_HW_CACHE_MAX] 1593 [PERF_COUNT_HW_CACHE_OP_MAX] 1594 [PERF_COUNT_HW_CACHE_RESULT_MAX] = 1595 { 1596 [ C(L1D) ] = { 1597 [ C(OP_READ) ] = { 1598 [ C(RESULT_ACCESS) ] = 0x010b, /* MEM_INST_RETIRED.LOADS */ 1599 [ C(RESULT_MISS) ] = 0x0151, /* L1D.REPL */ 1600 }, 1601 [ C(OP_WRITE) ] = { 1602 [ C(RESULT_ACCESS) ] = 0x020b, /* MEM_INST_RETURED.STORES */ 1603 [ C(RESULT_MISS) ] = 0x0251, /* L1D.M_REPL */ 1604 }, 1605 [ C(OP_PREFETCH) ] = { 1606 [ C(RESULT_ACCESS) ] = 0x014e, /* L1D_PREFETCH.REQUESTS */ 1607 [ C(RESULT_MISS) ] = 0x024e, /* L1D_PREFETCH.MISS */ 1608 }, 1609 }, 1610 [ C(L1I ) ] = { 1611 [ C(OP_READ) ] = { 1612 [ C(RESULT_ACCESS) ] = 0x0380, /* L1I.READS */ 1613 [ C(RESULT_MISS) ] = 0x0280, /* L1I.MISSES */ 1614 }, 1615 [ C(OP_WRITE) ] = { 1616 [ C(RESULT_ACCESS) ] = -1, 1617 [ C(RESULT_MISS) ] = -1, 1618 }, 1619 [ C(OP_PREFETCH) ] = { 1620 [ C(RESULT_ACCESS) ] = 0x0, 1621 [ C(RESULT_MISS) ] = 0x0, 1622 }, 1623 }, 1624 [ C(LL ) ] = { 1625 [ C(OP_READ) ] = { 1626 /* OFFCORE_RESPONSE.ANY_DATA.LOCAL_CACHE */ 1627 [ C(RESULT_ACCESS) ] = 0x01b7, 1628 /* OFFCORE_RESPONSE.ANY_DATA.ANY_LLC_MISS */ 1629 [ C(RESULT_MISS) ] = 0x01b7, 1630 }, 1631 /* 1632 * Use RFO, not WRITEBACK, because a write miss would typically occur 1633 * on RFO. 1634 */ 1635 [ C(OP_WRITE) ] = { 1636 /* OFFCORE_RESPONSE.ANY_RFO.LOCAL_CACHE */ 1637 [ C(RESULT_ACCESS) ] = 0x01b7, 1638 /* OFFCORE_RESPONSE.ANY_RFO.ANY_LLC_MISS */ 1639 [ C(RESULT_MISS) ] = 0x01b7, 1640 }, 1641 [ C(OP_PREFETCH) ] = { 1642 /* OFFCORE_RESPONSE.PREFETCH.LOCAL_CACHE */ 1643 [ C(RESULT_ACCESS) ] = 0x01b7, 1644 /* OFFCORE_RESPONSE.PREFETCH.ANY_LLC_MISS */ 1645 [ C(RESULT_MISS) ] = 0x01b7, 1646 }, 1647 }, 1648 [ C(DTLB) ] = { 1649 [ C(OP_READ) ] = { 1650 [ C(RESULT_ACCESS) ] = 0x0f40, /* L1D_CACHE_LD.MESI (alias) */ 1651 [ C(RESULT_MISS) ] = 0x0108, /* DTLB_LOAD_MISSES.ANY */ 1652 }, 1653 [ C(OP_WRITE) ] = { 1654 [ C(RESULT_ACCESS) ] = 0x0f41, /* L1D_CACHE_ST.MESI (alias) */ 1655 [ C(RESULT_MISS) ] = 0x010c, /* MEM_STORE_RETIRED.DTLB_MISS */ 1656 }, 1657 [ C(OP_PREFETCH) ] = { 1658 [ C(RESULT_ACCESS) ] = 0x0, 1659 [ C(RESULT_MISS) ] = 0x0, 1660 }, 1661 }, 1662 [ C(ITLB) ] = { 1663 [ C(OP_READ) ] = { 1664 [ C(RESULT_ACCESS) ] = 0x01c0, /* INST_RETIRED.ANY_P */ 1665 [ C(RESULT_MISS) ] = 0x20c8, /* ITLB_MISS_RETIRED */ 1666 }, 1667 [ C(OP_WRITE) ] = { 1668 [ C(RESULT_ACCESS) ] = -1, 1669 [ C(RESULT_MISS) ] = -1, 1670 }, 1671 [ C(OP_PREFETCH) ] = { 1672 [ C(RESULT_ACCESS) ] = -1, 1673 [ C(RESULT_MISS) ] = -1, 1674 }, 1675 }, 1676 [ C(BPU ) ] = { 1677 [ C(OP_READ) ] = { 1678 [ C(RESULT_ACCESS) ] = 0x00c4, /* BR_INST_RETIRED.ALL_BRANCHES */ 1679 [ C(RESULT_MISS) ] = 0x03e8, /* BPU_CLEARS.ANY */ 1680 }, 1681 [ C(OP_WRITE) ] = { 1682 [ C(RESULT_ACCESS) ] = -1, 1683 [ C(RESULT_MISS) ] = -1, 1684 }, 1685 [ C(OP_PREFETCH) ] = { 1686 [ C(RESULT_ACCESS) ] = -1, 1687 [ C(RESULT_MISS) ] = -1, 1688 }, 1689 }, 1690 [ C(NODE) ] = { 1691 [ C(OP_READ) ] = { 1692 [ C(RESULT_ACCESS) ] = 0x01b7, 1693 [ C(RESULT_MISS) ] = 0x01b7, 1694 }, 1695 [ C(OP_WRITE) ] = { 1696 [ C(RESULT_ACCESS) ] = 0x01b7, 1697 [ C(RESULT_MISS) ] = 0x01b7, 1698 }, 1699 [ C(OP_PREFETCH) ] = { 1700 [ C(RESULT_ACCESS) ] = 0x01b7, 1701 [ C(RESULT_MISS) ] = 0x01b7, 1702 }, 1703 }, 1704 }; 1705 1706 static __initconst const u64 core2_hw_cache_event_ids 1707 [PERF_COUNT_HW_CACHE_MAX] 1708 [PERF_COUNT_HW_CACHE_OP_MAX] 1709 [PERF_COUNT_HW_CACHE_RESULT_MAX] = 1710 { 1711 [ C(L1D) ] = { 1712 [ C(OP_READ) ] = { 1713 [ C(RESULT_ACCESS) ] = 0x0f40, /* L1D_CACHE_LD.MESI */ 1714 [ C(RESULT_MISS) ] = 0x0140, /* L1D_CACHE_LD.I_STATE */ 1715 }, 1716 [ C(OP_WRITE) ] = { 1717 [ C(RESULT_ACCESS) ] = 0x0f41, /* L1D_CACHE_ST.MESI */ 1718 [ C(RESULT_MISS) ] = 0x0141, /* L1D_CACHE_ST.I_STATE */ 1719 }, 1720 [ C(OP_PREFETCH) ] = { 1721 [ C(RESULT_ACCESS) ] = 0x104e, /* L1D_PREFETCH.REQUESTS */ 1722 [ C(RESULT_MISS) ] = 0, 1723 }, 1724 }, 1725 [ C(L1I ) ] = { 1726 [ C(OP_READ) ] = { 1727 [ C(RESULT_ACCESS) ] = 0x0080, /* L1I.READS */ 1728 [ C(RESULT_MISS) ] = 0x0081, /* L1I.MISSES */ 1729 }, 1730 [ C(OP_WRITE) ] = { 1731 [ C(RESULT_ACCESS) ] = -1, 1732 [ C(RESULT_MISS) ] = -1, 1733 }, 1734 [ C(OP_PREFETCH) ] = { 1735 [ C(RESULT_ACCESS) ] = 0, 1736 [ C(RESULT_MISS) ] = 0, 1737 }, 1738 }, 1739 [ C(LL ) ] = { 1740 [ C(OP_READ) ] = { 1741 [ C(RESULT_ACCESS) ] = 0x4f29, /* L2_LD.MESI */ 1742 [ C(RESULT_MISS) ] = 0x4129, /* L2_LD.ISTATE */ 1743 }, 1744 [ C(OP_WRITE) ] = { 1745 [ C(RESULT_ACCESS) ] = 0x4f2A, /* L2_ST.MESI */ 1746 [ C(RESULT_MISS) ] = 0x412A, /* L2_ST.ISTATE */ 1747 }, 1748 [ C(OP_PREFETCH) ] = { 1749 [ C(RESULT_ACCESS) ] = 0, 1750 [ C(RESULT_MISS) ] = 0, 1751 }, 1752 }, 1753 [ C(DTLB) ] = { 1754 [ C(OP_READ) ] = { 1755 [ C(RESULT_ACCESS) ] = 0x0f40, /* L1D_CACHE_LD.MESI (alias) */ 1756 [ C(RESULT_MISS) ] = 0x0208, /* DTLB_MISSES.MISS_LD */ 1757 }, 1758 [ C(OP_WRITE) ] = { 1759 [ C(RESULT_ACCESS) ] = 0x0f41, /* L1D_CACHE_ST.MESI (alias) */ 1760 [ C(RESULT_MISS) ] = 0x0808, /* DTLB_MISSES.MISS_ST */ 1761 }, 1762 [ C(OP_PREFETCH) ] = { 1763 [ C(RESULT_ACCESS) ] = 0, 1764 [ C(RESULT_MISS) ] = 0, 1765 }, 1766 }, 1767 [ C(ITLB) ] = { 1768 [ C(OP_READ) ] = { 1769 [ C(RESULT_ACCESS) ] = 0x00c0, /* INST_RETIRED.ANY_P */ 1770 [ C(RESULT_MISS) ] = 0x1282, /* ITLBMISSES */ 1771 }, 1772 [ C(OP_WRITE) ] = { 1773 [ C(RESULT_ACCESS) ] = -1, 1774 [ C(RESULT_MISS) ] = -1, 1775 }, 1776 [ C(OP_PREFETCH) ] = { 1777 [ C(RESULT_ACCESS) ] = -1, 1778 [ C(RESULT_MISS) ] = -1, 1779 }, 1780 }, 1781 [ C(BPU ) ] = { 1782 [ C(OP_READ) ] = { 1783 [ C(RESULT_ACCESS) ] = 0x00c4, /* BR_INST_RETIRED.ANY */ 1784 [ C(RESULT_MISS) ] = 0x00c5, /* BP_INST_RETIRED.MISPRED */ 1785 }, 1786 [ C(OP_WRITE) ] = { 1787 [ C(RESULT_ACCESS) ] = -1, 1788 [ C(RESULT_MISS) ] = -1, 1789 }, 1790 [ C(OP_PREFETCH) ] = { 1791 [ C(RESULT_ACCESS) ] = -1, 1792 [ C(RESULT_MISS) ] = -1, 1793 }, 1794 }, 1795 }; 1796 1797 static __initconst const u64 atom_hw_cache_event_ids 1798 [PERF_COUNT_HW_CACHE_MAX] 1799 [PERF_COUNT_HW_CACHE_OP_MAX] 1800 [PERF_COUNT_HW_CACHE_RESULT_MAX] = 1801 { 1802 [ C(L1D) ] = { 1803 [ C(OP_READ) ] = { 1804 [ C(RESULT_ACCESS) ] = 0x2140, /* L1D_CACHE.LD */ 1805 [ C(RESULT_MISS) ] = 0, 1806 }, 1807 [ C(OP_WRITE) ] = { 1808 [ C(RESULT_ACCESS) ] = 0x2240, /* L1D_CACHE.ST */ 1809 [ C(RESULT_MISS) ] = 0, 1810 }, 1811 [ C(OP_PREFETCH) ] = { 1812 [ C(RESULT_ACCESS) ] = 0x0, 1813 [ C(RESULT_MISS) ] = 0, 1814 }, 1815 }, 1816 [ C(L1I ) ] = { 1817 [ C(OP_READ) ] = { 1818 [ C(RESULT_ACCESS) ] = 0x0380, /* L1I.READS */ 1819 [ C(RESULT_MISS) ] = 0x0280, /* L1I.MISSES */ 1820 }, 1821 [ C(OP_WRITE) ] = { 1822 [ C(RESULT_ACCESS) ] = -1, 1823 [ C(RESULT_MISS) ] = -1, 1824 }, 1825 [ C(OP_PREFETCH) ] = { 1826 [ C(RESULT_ACCESS) ] = 0, 1827 [ C(RESULT_MISS) ] = 0, 1828 }, 1829 }, 1830 [ C(LL ) ] = { 1831 [ C(OP_READ) ] = { 1832 [ C(RESULT_ACCESS) ] = 0x4f29, /* L2_LD.MESI */ 1833 [ C(RESULT_MISS) ] = 0x4129, /* L2_LD.ISTATE */ 1834 }, 1835 [ C(OP_WRITE) ] = { 1836 [ C(RESULT_ACCESS) ] = 0x4f2A, /* L2_ST.MESI */ 1837 [ C(RESULT_MISS) ] = 0x412A, /* L2_ST.ISTATE */ 1838 }, 1839 [ C(OP_PREFETCH) ] = { 1840 [ C(RESULT_ACCESS) ] = 0, 1841 [ C(RESULT_MISS) ] = 0, 1842 }, 1843 }, 1844 [ C(DTLB) ] = { 1845 [ C(OP_READ) ] = { 1846 [ C(RESULT_ACCESS) ] = 0x2140, /* L1D_CACHE_LD.MESI (alias) */ 1847 [ C(RESULT_MISS) ] = 0x0508, /* DTLB_MISSES.MISS_LD */ 1848 }, 1849 [ C(OP_WRITE) ] = { 1850 [ C(RESULT_ACCESS) ] = 0x2240, /* L1D_CACHE_ST.MESI (alias) */ 1851 [ C(RESULT_MISS) ] = 0x0608, /* DTLB_MISSES.MISS_ST */ 1852 }, 1853 [ C(OP_PREFETCH) ] = { 1854 [ C(RESULT_ACCESS) ] = 0, 1855 [ C(RESULT_MISS) ] = 0, 1856 }, 1857 }, 1858 [ C(ITLB) ] = { 1859 [ C(OP_READ) ] = { 1860 [ C(RESULT_ACCESS) ] = 0x00c0, /* INST_RETIRED.ANY_P */ 1861 [ C(RESULT_MISS) ] = 0x0282, /* ITLB.MISSES */ 1862 }, 1863 [ C(OP_WRITE) ] = { 1864 [ C(RESULT_ACCESS) ] = -1, 1865 [ C(RESULT_MISS) ] = -1, 1866 }, 1867 [ C(OP_PREFETCH) ] = { 1868 [ C(RESULT_ACCESS) ] = -1, 1869 [ C(RESULT_MISS) ] = -1, 1870 }, 1871 }, 1872 [ C(BPU ) ] = { 1873 [ C(OP_READ) ] = { 1874 [ C(RESULT_ACCESS) ] = 0x00c4, /* BR_INST_RETIRED.ANY */ 1875 [ C(RESULT_MISS) ] = 0x00c5, /* BP_INST_RETIRED.MISPRED */ 1876 }, 1877 [ C(OP_WRITE) ] = { 1878 [ C(RESULT_ACCESS) ] = -1, 1879 [ C(RESULT_MISS) ] = -1, 1880 }, 1881 [ C(OP_PREFETCH) ] = { 1882 [ C(RESULT_ACCESS) ] = -1, 1883 [ C(RESULT_MISS) ] = -1, 1884 }, 1885 }, 1886 }; 1887 1888 EVENT_ATTR_STR(topdown-total-slots, td_total_slots_slm, "event=0x3c"); 1889 EVENT_ATTR_STR(topdown-total-slots.scale, td_total_slots_scale_slm, "2"); 1890 /* no_alloc_cycles.not_delivered */ 1891 EVENT_ATTR_STR(topdown-fetch-bubbles, td_fetch_bubbles_slm, 1892 "event=0xca,umask=0x50"); 1893 EVENT_ATTR_STR(topdown-fetch-bubbles.scale, td_fetch_bubbles_scale_slm, "2"); 1894 /* uops_retired.all */ 1895 EVENT_ATTR_STR(topdown-slots-issued, td_slots_issued_slm, 1896 "event=0xc2,umask=0x10"); 1897 /* uops_retired.all */ 1898 EVENT_ATTR_STR(topdown-slots-retired, td_slots_retired_slm, 1899 "event=0xc2,umask=0x10"); 1900 1901 static struct attribute *slm_events_attrs[] = { 1902 EVENT_PTR(td_total_slots_slm), 1903 EVENT_PTR(td_total_slots_scale_slm), 1904 EVENT_PTR(td_fetch_bubbles_slm), 1905 EVENT_PTR(td_fetch_bubbles_scale_slm), 1906 EVENT_PTR(td_slots_issued_slm), 1907 EVENT_PTR(td_slots_retired_slm), 1908 NULL 1909 }; 1910 1911 static struct extra_reg intel_slm_extra_regs[] __read_mostly = 1912 { 1913 /* must define OFFCORE_RSP_X first, see intel_fixup_er() */ 1914 INTEL_UEVENT_EXTRA_REG(0x01b7, MSR_OFFCORE_RSP_0, 0x768005ffffull, RSP_0), 1915 INTEL_UEVENT_EXTRA_REG(0x02b7, MSR_OFFCORE_RSP_1, 0x368005ffffull, RSP_1), 1916 EVENT_EXTRA_END 1917 }; 1918 1919 #define SLM_DMND_READ SNB_DMND_DATA_RD 1920 #define SLM_DMND_WRITE SNB_DMND_RFO 1921 #define SLM_DMND_PREFETCH (SNB_PF_DATA_RD|SNB_PF_RFO) 1922 1923 #define SLM_SNP_ANY (SNB_SNP_NONE|SNB_SNP_MISS|SNB_NO_FWD|SNB_HITM) 1924 #define SLM_LLC_ACCESS SNB_RESP_ANY 1925 #define SLM_LLC_MISS (SLM_SNP_ANY|SNB_NON_DRAM) 1926 1927 static __initconst const u64 slm_hw_cache_extra_regs 1928 [PERF_COUNT_HW_CACHE_MAX] 1929 [PERF_COUNT_HW_CACHE_OP_MAX] 1930 [PERF_COUNT_HW_CACHE_RESULT_MAX] = 1931 { 1932 [ C(LL ) ] = { 1933 [ C(OP_READ) ] = { 1934 [ C(RESULT_ACCESS) ] = SLM_DMND_READ|SLM_LLC_ACCESS, 1935 [ C(RESULT_MISS) ] = 0, 1936 }, 1937 [ C(OP_WRITE) ] = { 1938 [ C(RESULT_ACCESS) ] = SLM_DMND_WRITE|SLM_LLC_ACCESS, 1939 [ C(RESULT_MISS) ] = SLM_DMND_WRITE|SLM_LLC_MISS, 1940 }, 1941 [ C(OP_PREFETCH) ] = { 1942 [ C(RESULT_ACCESS) ] = SLM_DMND_PREFETCH|SLM_LLC_ACCESS, 1943 [ C(RESULT_MISS) ] = SLM_DMND_PREFETCH|SLM_LLC_MISS, 1944 }, 1945 }, 1946 }; 1947 1948 static __initconst const u64 slm_hw_cache_event_ids 1949 [PERF_COUNT_HW_CACHE_MAX] 1950 [PERF_COUNT_HW_CACHE_OP_MAX] 1951 [PERF_COUNT_HW_CACHE_RESULT_MAX] = 1952 { 1953 [ C(L1D) ] = { 1954 [ C(OP_READ) ] = { 1955 [ C(RESULT_ACCESS) ] = 0, 1956 [ C(RESULT_MISS) ] = 0x0104, /* LD_DCU_MISS */ 1957 }, 1958 [ C(OP_WRITE) ] = { 1959 [ C(RESULT_ACCESS) ] = 0, 1960 [ C(RESULT_MISS) ] = 0, 1961 }, 1962 [ C(OP_PREFETCH) ] = { 1963 [ C(RESULT_ACCESS) ] = 0, 1964 [ C(RESULT_MISS) ] = 0, 1965 }, 1966 }, 1967 [ C(L1I ) ] = { 1968 [ C(OP_READ) ] = { 1969 [ C(RESULT_ACCESS) ] = 0x0380, /* ICACHE.ACCESSES */ 1970 [ C(RESULT_MISS) ] = 0x0280, /* ICACGE.MISSES */ 1971 }, 1972 [ C(OP_WRITE) ] = { 1973 [ C(RESULT_ACCESS) ] = -1, 1974 [ C(RESULT_MISS) ] = -1, 1975 }, 1976 [ C(OP_PREFETCH) ] = { 1977 [ C(RESULT_ACCESS) ] = 0, 1978 [ C(RESULT_MISS) ] = 0, 1979 }, 1980 }, 1981 [ C(LL ) ] = { 1982 [ C(OP_READ) ] = { 1983 /* OFFCORE_RESPONSE.ANY_DATA.LOCAL_CACHE */ 1984 [ C(RESULT_ACCESS) ] = 0x01b7, 1985 [ C(RESULT_MISS) ] = 0, 1986 }, 1987 [ C(OP_WRITE) ] = { 1988 /* OFFCORE_RESPONSE.ANY_RFO.LOCAL_CACHE */ 1989 [ C(RESULT_ACCESS) ] = 0x01b7, 1990 /* OFFCORE_RESPONSE.ANY_RFO.ANY_LLC_MISS */ 1991 [ C(RESULT_MISS) ] = 0x01b7, 1992 }, 1993 [ C(OP_PREFETCH) ] = { 1994 /* OFFCORE_RESPONSE.PREFETCH.LOCAL_CACHE */ 1995 [ C(RESULT_ACCESS) ] = 0x01b7, 1996 /* OFFCORE_RESPONSE.PREFETCH.ANY_LLC_MISS */ 1997 [ C(RESULT_MISS) ] = 0x01b7, 1998 }, 1999 }, 2000 [ C(DTLB) ] = { 2001 [ C(OP_READ) ] = { 2002 [ C(RESULT_ACCESS) ] = 0, 2003 [ C(RESULT_MISS) ] = 0x0804, /* LD_DTLB_MISS */ 2004 }, 2005 [ C(OP_WRITE) ] = { 2006 [ C(RESULT_ACCESS) ] = 0, 2007 [ C(RESULT_MISS) ] = 0, 2008 }, 2009 [ C(OP_PREFETCH) ] = { 2010 [ C(RESULT_ACCESS) ] = 0, 2011 [ C(RESULT_MISS) ] = 0, 2012 }, 2013 }, 2014 [ C(ITLB) ] = { 2015 [ C(OP_READ) ] = { 2016 [ C(RESULT_ACCESS) ] = 0x00c0, /* INST_RETIRED.ANY_P */ 2017 [ C(RESULT_MISS) ] = 0x40205, /* PAGE_WALKS.I_SIDE_WALKS */ 2018 }, 2019 [ C(OP_WRITE) ] = { 2020 [ C(RESULT_ACCESS) ] = -1, 2021 [ C(RESULT_MISS) ] = -1, 2022 }, 2023 [ C(OP_PREFETCH) ] = { 2024 [ C(RESULT_ACCESS) ] = -1, 2025 [ C(RESULT_MISS) ] = -1, 2026 }, 2027 }, 2028 [ C(BPU ) ] = { 2029 [ C(OP_READ) ] = { 2030 [ C(RESULT_ACCESS) ] = 0x00c4, /* BR_INST_RETIRED.ANY */ 2031 [ C(RESULT_MISS) ] = 0x00c5, /* BP_INST_RETIRED.MISPRED */ 2032 }, 2033 [ C(OP_WRITE) ] = { 2034 [ C(RESULT_ACCESS) ] = -1, 2035 [ C(RESULT_MISS) ] = -1, 2036 }, 2037 [ C(OP_PREFETCH) ] = { 2038 [ C(RESULT_ACCESS) ] = -1, 2039 [ C(RESULT_MISS) ] = -1, 2040 }, 2041 }, 2042 }; 2043 2044 EVENT_ATTR_STR(topdown-total-slots, td_total_slots_glm, "event=0x3c"); 2045 EVENT_ATTR_STR(topdown-total-slots.scale, td_total_slots_scale_glm, "3"); 2046 /* UOPS_NOT_DELIVERED.ANY */ 2047 EVENT_ATTR_STR(topdown-fetch-bubbles, td_fetch_bubbles_glm, "event=0x9c"); 2048 /* ISSUE_SLOTS_NOT_CONSUMED.RECOVERY */ 2049 EVENT_ATTR_STR(topdown-recovery-bubbles, td_recovery_bubbles_glm, "event=0xca,umask=0x02"); 2050 /* UOPS_RETIRED.ANY */ 2051 EVENT_ATTR_STR(topdown-slots-retired, td_slots_retired_glm, "event=0xc2"); 2052 /* UOPS_ISSUED.ANY */ 2053 EVENT_ATTR_STR(topdown-slots-issued, td_slots_issued_glm, "event=0x0e"); 2054 2055 static struct attribute *glm_events_attrs[] = { 2056 EVENT_PTR(td_total_slots_glm), 2057 EVENT_PTR(td_total_slots_scale_glm), 2058 EVENT_PTR(td_fetch_bubbles_glm), 2059 EVENT_PTR(td_recovery_bubbles_glm), 2060 EVENT_PTR(td_slots_issued_glm), 2061 EVENT_PTR(td_slots_retired_glm), 2062 NULL 2063 }; 2064 2065 static struct extra_reg intel_glm_extra_regs[] __read_mostly = { 2066 /* must define OFFCORE_RSP_X first, see intel_fixup_er() */ 2067 INTEL_UEVENT_EXTRA_REG(0x01b7, MSR_OFFCORE_RSP_0, 0x760005ffbfull, RSP_0), 2068 INTEL_UEVENT_EXTRA_REG(0x02b7, MSR_OFFCORE_RSP_1, 0x360005ffbfull, RSP_1), 2069 EVENT_EXTRA_END 2070 }; 2071 2072 #define GLM_DEMAND_DATA_RD BIT_ULL(0) 2073 #define GLM_DEMAND_RFO BIT_ULL(1) 2074 #define GLM_ANY_RESPONSE BIT_ULL(16) 2075 #define GLM_SNP_NONE_OR_MISS BIT_ULL(33) 2076 #define GLM_DEMAND_READ GLM_DEMAND_DATA_RD 2077 #define GLM_DEMAND_WRITE GLM_DEMAND_RFO 2078 #define GLM_DEMAND_PREFETCH (SNB_PF_DATA_RD|SNB_PF_RFO) 2079 #define GLM_LLC_ACCESS GLM_ANY_RESPONSE 2080 #define GLM_SNP_ANY (GLM_SNP_NONE_OR_MISS|SNB_NO_FWD|SNB_HITM) 2081 #define GLM_LLC_MISS (GLM_SNP_ANY|SNB_NON_DRAM) 2082 2083 static __initconst const u64 glm_hw_cache_event_ids 2084 [PERF_COUNT_HW_CACHE_MAX] 2085 [PERF_COUNT_HW_CACHE_OP_MAX] 2086 [PERF_COUNT_HW_CACHE_RESULT_MAX] = { 2087 [C(L1D)] = { 2088 [C(OP_READ)] = { 2089 [C(RESULT_ACCESS)] = 0x81d0, /* MEM_UOPS_RETIRED.ALL_LOADS */ 2090 [C(RESULT_MISS)] = 0x0, 2091 }, 2092 [C(OP_WRITE)] = { 2093 [C(RESULT_ACCESS)] = 0x82d0, /* MEM_UOPS_RETIRED.ALL_STORES */ 2094 [C(RESULT_MISS)] = 0x0, 2095 }, 2096 [C(OP_PREFETCH)] = { 2097 [C(RESULT_ACCESS)] = 0x0, 2098 [C(RESULT_MISS)] = 0x0, 2099 }, 2100 }, 2101 [C(L1I)] = { 2102 [C(OP_READ)] = { 2103 [C(RESULT_ACCESS)] = 0x0380, /* ICACHE.ACCESSES */ 2104 [C(RESULT_MISS)] = 0x0280, /* ICACHE.MISSES */ 2105 }, 2106 [C(OP_WRITE)] = { 2107 [C(RESULT_ACCESS)] = -1, 2108 [C(RESULT_MISS)] = -1, 2109 }, 2110 [C(OP_PREFETCH)] = { 2111 [C(RESULT_ACCESS)] = 0x0, 2112 [C(RESULT_MISS)] = 0x0, 2113 }, 2114 }, 2115 [C(LL)] = { 2116 [C(OP_READ)] = { 2117 [C(RESULT_ACCESS)] = 0x1b7, /* OFFCORE_RESPONSE */ 2118 [C(RESULT_MISS)] = 0x1b7, /* OFFCORE_RESPONSE */ 2119 }, 2120 [C(OP_WRITE)] = { 2121 [C(RESULT_ACCESS)] = 0x1b7, /* OFFCORE_RESPONSE */ 2122 [C(RESULT_MISS)] = 0x1b7, /* OFFCORE_RESPONSE */ 2123 }, 2124 [C(OP_PREFETCH)] = { 2125 [C(RESULT_ACCESS)] = 0x1b7, /* OFFCORE_RESPONSE */ 2126 [C(RESULT_MISS)] = 0x1b7, /* OFFCORE_RESPONSE */ 2127 }, 2128 }, 2129 [C(DTLB)] = { 2130 [C(OP_READ)] = { 2131 [C(RESULT_ACCESS)] = 0x81d0, /* MEM_UOPS_RETIRED.ALL_LOADS */ 2132 [C(RESULT_MISS)] = 0x0, 2133 }, 2134 [C(OP_WRITE)] = { 2135 [C(RESULT_ACCESS)] = 0x82d0, /* MEM_UOPS_RETIRED.ALL_STORES */ 2136 [C(RESULT_MISS)] = 0x0, 2137 }, 2138 [C(OP_PREFETCH)] = { 2139 [C(RESULT_ACCESS)] = 0x0, 2140 [C(RESULT_MISS)] = 0x0, 2141 }, 2142 }, 2143 [C(ITLB)] = { 2144 [C(OP_READ)] = { 2145 [C(RESULT_ACCESS)] = 0x00c0, /* INST_RETIRED.ANY_P */ 2146 [C(RESULT_MISS)] = 0x0481, /* ITLB.MISS */ 2147 }, 2148 [C(OP_WRITE)] = { 2149 [C(RESULT_ACCESS)] = -1, 2150 [C(RESULT_MISS)] = -1, 2151 }, 2152 [C(OP_PREFETCH)] = { 2153 [C(RESULT_ACCESS)] = -1, 2154 [C(RESULT_MISS)] = -1, 2155 }, 2156 }, 2157 [C(BPU)] = { 2158 [C(OP_READ)] = { 2159 [C(RESULT_ACCESS)] = 0x00c4, /* BR_INST_RETIRED.ALL_BRANCHES */ 2160 [C(RESULT_MISS)] = 0x00c5, /* BR_MISP_RETIRED.ALL_BRANCHES */ 2161 }, 2162 [C(OP_WRITE)] = { 2163 [C(RESULT_ACCESS)] = -1, 2164 [C(RESULT_MISS)] = -1, 2165 }, 2166 [C(OP_PREFETCH)] = { 2167 [C(RESULT_ACCESS)] = -1, 2168 [C(RESULT_MISS)] = -1, 2169 }, 2170 }, 2171 }; 2172 2173 static __initconst const u64 glm_hw_cache_extra_regs 2174 [PERF_COUNT_HW_CACHE_MAX] 2175 [PERF_COUNT_HW_CACHE_OP_MAX] 2176 [PERF_COUNT_HW_CACHE_RESULT_MAX] = { 2177 [C(LL)] = { 2178 [C(OP_READ)] = { 2179 [C(RESULT_ACCESS)] = GLM_DEMAND_READ| 2180 GLM_LLC_ACCESS, 2181 [C(RESULT_MISS)] = GLM_DEMAND_READ| 2182 GLM_LLC_MISS, 2183 }, 2184 [C(OP_WRITE)] = { 2185 [C(RESULT_ACCESS)] = GLM_DEMAND_WRITE| 2186 GLM_LLC_ACCESS, 2187 [C(RESULT_MISS)] = GLM_DEMAND_WRITE| 2188 GLM_LLC_MISS, 2189 }, 2190 [C(OP_PREFETCH)] = { 2191 [C(RESULT_ACCESS)] = GLM_DEMAND_PREFETCH| 2192 GLM_LLC_ACCESS, 2193 [C(RESULT_MISS)] = GLM_DEMAND_PREFETCH| 2194 GLM_LLC_MISS, 2195 }, 2196 }, 2197 }; 2198 2199 static __initconst const u64 glp_hw_cache_event_ids 2200 [PERF_COUNT_HW_CACHE_MAX] 2201 [PERF_COUNT_HW_CACHE_OP_MAX] 2202 [PERF_COUNT_HW_CACHE_RESULT_MAX] = { 2203 [C(L1D)] = { 2204 [C(OP_READ)] = { 2205 [C(RESULT_ACCESS)] = 0x81d0, /* MEM_UOPS_RETIRED.ALL_LOADS */ 2206 [C(RESULT_MISS)] = 0x0, 2207 }, 2208 [C(OP_WRITE)] = { 2209 [C(RESULT_ACCESS)] = 0x82d0, /* MEM_UOPS_RETIRED.ALL_STORES */ 2210 [C(RESULT_MISS)] = 0x0, 2211 }, 2212 [C(OP_PREFETCH)] = { 2213 [C(RESULT_ACCESS)] = 0x0, 2214 [C(RESULT_MISS)] = 0x0, 2215 }, 2216 }, 2217 [C(L1I)] = { 2218 [C(OP_READ)] = { 2219 [C(RESULT_ACCESS)] = 0x0380, /* ICACHE.ACCESSES */ 2220 [C(RESULT_MISS)] = 0x0280, /* ICACHE.MISSES */ 2221 }, 2222 [C(OP_WRITE)] = { 2223 [C(RESULT_ACCESS)] = -1, 2224 [C(RESULT_MISS)] = -1, 2225 }, 2226 [C(OP_PREFETCH)] = { 2227 [C(RESULT_ACCESS)] = 0x0, 2228 [C(RESULT_MISS)] = 0x0, 2229 }, 2230 }, 2231 [C(LL)] = { 2232 [C(OP_READ)] = { 2233 [C(RESULT_ACCESS)] = 0x1b7, /* OFFCORE_RESPONSE */ 2234 [C(RESULT_MISS)] = 0x1b7, /* OFFCORE_RESPONSE */ 2235 }, 2236 [C(OP_WRITE)] = { 2237 [C(RESULT_ACCESS)] = 0x1b7, /* OFFCORE_RESPONSE */ 2238 [C(RESULT_MISS)] = 0x1b7, /* OFFCORE_RESPONSE */ 2239 }, 2240 [C(OP_PREFETCH)] = { 2241 [C(RESULT_ACCESS)] = 0x0, 2242 [C(RESULT_MISS)] = 0x0, 2243 }, 2244 }, 2245 [C(DTLB)] = { 2246 [C(OP_READ)] = { 2247 [C(RESULT_ACCESS)] = 0x81d0, /* MEM_UOPS_RETIRED.ALL_LOADS */ 2248 [C(RESULT_MISS)] = 0xe08, /* DTLB_LOAD_MISSES.WALK_COMPLETED */ 2249 }, 2250 [C(OP_WRITE)] = { 2251 [C(RESULT_ACCESS)] = 0x82d0, /* MEM_UOPS_RETIRED.ALL_STORES */ 2252 [C(RESULT_MISS)] = 0xe49, /* DTLB_STORE_MISSES.WALK_COMPLETED */ 2253 }, 2254 [C(OP_PREFETCH)] = { 2255 [C(RESULT_ACCESS)] = 0x0, 2256 [C(RESULT_MISS)] = 0x0, 2257 }, 2258 }, 2259 [C(ITLB)] = { 2260 [C(OP_READ)] = { 2261 [C(RESULT_ACCESS)] = 0x00c0, /* INST_RETIRED.ANY_P */ 2262 [C(RESULT_MISS)] = 0x0481, /* ITLB.MISS */ 2263 }, 2264 [C(OP_WRITE)] = { 2265 [C(RESULT_ACCESS)] = -1, 2266 [C(RESULT_MISS)] = -1, 2267 }, 2268 [C(OP_PREFETCH)] = { 2269 [C(RESULT_ACCESS)] = -1, 2270 [C(RESULT_MISS)] = -1, 2271 }, 2272 }, 2273 [C(BPU)] = { 2274 [C(OP_READ)] = { 2275 [C(RESULT_ACCESS)] = 0x00c4, /* BR_INST_RETIRED.ALL_BRANCHES */ 2276 [C(RESULT_MISS)] = 0x00c5, /* BR_MISP_RETIRED.ALL_BRANCHES */ 2277 }, 2278 [C(OP_WRITE)] = { 2279 [C(RESULT_ACCESS)] = -1, 2280 [C(RESULT_MISS)] = -1, 2281 }, 2282 [C(OP_PREFETCH)] = { 2283 [C(RESULT_ACCESS)] = -1, 2284 [C(RESULT_MISS)] = -1, 2285 }, 2286 }, 2287 }; 2288 2289 static __initconst const u64 glp_hw_cache_extra_regs 2290 [PERF_COUNT_HW_CACHE_MAX] 2291 [PERF_COUNT_HW_CACHE_OP_MAX] 2292 [PERF_COUNT_HW_CACHE_RESULT_MAX] = { 2293 [C(LL)] = { 2294 [C(OP_READ)] = { 2295 [C(RESULT_ACCESS)] = GLM_DEMAND_READ| 2296 GLM_LLC_ACCESS, 2297 [C(RESULT_MISS)] = GLM_DEMAND_READ| 2298 GLM_LLC_MISS, 2299 }, 2300 [C(OP_WRITE)] = { 2301 [C(RESULT_ACCESS)] = GLM_DEMAND_WRITE| 2302 GLM_LLC_ACCESS, 2303 [C(RESULT_MISS)] = GLM_DEMAND_WRITE| 2304 GLM_LLC_MISS, 2305 }, 2306 [C(OP_PREFETCH)] = { 2307 [C(RESULT_ACCESS)] = 0x0, 2308 [C(RESULT_MISS)] = 0x0, 2309 }, 2310 }, 2311 }; 2312 2313 #define TNT_LOCAL_DRAM BIT_ULL(26) 2314 #define TNT_DEMAND_READ GLM_DEMAND_DATA_RD 2315 #define TNT_DEMAND_WRITE GLM_DEMAND_RFO 2316 #define TNT_LLC_ACCESS GLM_ANY_RESPONSE 2317 #define TNT_SNP_ANY (SNB_SNP_NOT_NEEDED|SNB_SNP_MISS| \ 2318 SNB_NO_FWD|SNB_SNP_FWD|SNB_HITM) 2319 #define TNT_LLC_MISS (TNT_SNP_ANY|SNB_NON_DRAM|TNT_LOCAL_DRAM) 2320 2321 static __initconst const u64 tnt_hw_cache_extra_regs 2322 [PERF_COUNT_HW_CACHE_MAX] 2323 [PERF_COUNT_HW_CACHE_OP_MAX] 2324 [PERF_COUNT_HW_CACHE_RESULT_MAX] = { 2325 [C(LL)] = { 2326 [C(OP_READ)] = { 2327 [C(RESULT_ACCESS)] = TNT_DEMAND_READ| 2328 TNT_LLC_ACCESS, 2329 [C(RESULT_MISS)] = TNT_DEMAND_READ| 2330 TNT_LLC_MISS, 2331 }, 2332 [C(OP_WRITE)] = { 2333 [C(RESULT_ACCESS)] = TNT_DEMAND_WRITE| 2334 TNT_LLC_ACCESS, 2335 [C(RESULT_MISS)] = TNT_DEMAND_WRITE| 2336 TNT_LLC_MISS, 2337 }, 2338 [C(OP_PREFETCH)] = { 2339 [C(RESULT_ACCESS)] = 0x0, 2340 [C(RESULT_MISS)] = 0x0, 2341 }, 2342 }, 2343 }; 2344 2345 static __initconst const u64 arw_hw_cache_extra_regs 2346 [PERF_COUNT_HW_CACHE_MAX] 2347 [PERF_COUNT_HW_CACHE_OP_MAX] 2348 [PERF_COUNT_HW_CACHE_RESULT_MAX] = { 2349 [C(LL)] = { 2350 [C(OP_READ)] = { 2351 [C(RESULT_ACCESS)] = 0x4000000000000001, 2352 [C(RESULT_MISS)] = 0xFFFFF000000001, 2353 }, 2354 [C(OP_WRITE)] = { 2355 [C(RESULT_ACCESS)] = 0x4000000000000002, 2356 [C(RESULT_MISS)] = 0xFFFFF000000002, 2357 }, 2358 [C(OP_PREFETCH)] = { 2359 [C(RESULT_ACCESS)] = 0x0, 2360 [C(RESULT_MISS)] = 0x0, 2361 }, 2362 }, 2363 }; 2364 2365 EVENT_ATTR_STR(topdown-fe-bound, td_fe_bound_tnt, "event=0x71,umask=0x0"); 2366 EVENT_ATTR_STR(topdown-retiring, td_retiring_tnt, "event=0xc2,umask=0x0"); 2367 EVENT_ATTR_STR(topdown-bad-spec, td_bad_spec_tnt, "event=0x73,umask=0x6"); 2368 EVENT_ATTR_STR(topdown-be-bound, td_be_bound_tnt, "event=0x74,umask=0x0"); 2369 2370 static struct attribute *tnt_events_attrs[] = { 2371 EVENT_PTR(td_fe_bound_tnt), 2372 EVENT_PTR(td_retiring_tnt), 2373 EVENT_PTR(td_bad_spec_tnt), 2374 EVENT_PTR(td_be_bound_tnt), 2375 NULL, 2376 }; 2377 2378 static struct extra_reg intel_tnt_extra_regs[] __read_mostly = { 2379 /* must define OFFCORE_RSP_X first, see intel_fixup_er() */ 2380 INTEL_UEVENT_EXTRA_REG(0x01b7, MSR_OFFCORE_RSP_0, 0x800ff0ffffff9fffull, RSP_0), 2381 INTEL_UEVENT_EXTRA_REG(0x02b7, MSR_OFFCORE_RSP_1, 0xff0ffffff9fffull, RSP_1), 2382 EVENT_EXTRA_END 2383 }; 2384 2385 EVENT_ATTR_STR(mem-loads, mem_ld_grt, "event=0xd0,umask=0x5,ldlat=3"); 2386 EVENT_ATTR_STR(mem-stores, mem_st_grt, "event=0xd0,umask=0x6"); 2387 2388 static struct attribute *grt_mem_attrs[] = { 2389 EVENT_PTR(mem_ld_grt), 2390 EVENT_PTR(mem_st_grt), 2391 NULL 2392 }; 2393 2394 static struct extra_reg intel_grt_extra_regs[] __read_mostly = { 2395 /* must define OFFCORE_RSP_X first, see intel_fixup_er() */ 2396 INTEL_UEVENT_EXTRA_REG(0x01b7, MSR_OFFCORE_RSP_0, 0x3fffffffffull, RSP_0), 2397 INTEL_UEVENT_EXTRA_REG(0x02b7, MSR_OFFCORE_RSP_1, 0x3fffffffffull, RSP_1), 2398 INTEL_UEVENT_PEBS_LDLAT_EXTRA_REG(0x5d0), 2399 EVENT_EXTRA_END 2400 }; 2401 2402 EVENT_ATTR_STR(topdown-retiring, td_retiring_cmt, "event=0x72,umask=0x0"); 2403 EVENT_ATTR_STR(topdown-bad-spec, td_bad_spec_cmt, "event=0x73,umask=0x0"); 2404 2405 static struct attribute *cmt_events_attrs[] = { 2406 EVENT_PTR(td_fe_bound_tnt), 2407 EVENT_PTR(td_retiring_cmt), 2408 EVENT_PTR(td_bad_spec_cmt), 2409 EVENT_PTR(td_be_bound_tnt), 2410 NULL 2411 }; 2412 2413 static struct extra_reg intel_cmt_extra_regs[] __read_mostly = { 2414 /* must define OFFCORE_RSP_X first, see intel_fixup_er() */ 2415 INTEL_UEVENT_EXTRA_REG(0x01b7, MSR_OFFCORE_RSP_0, 0x800ff3ffffffffffull, RSP_0), 2416 INTEL_UEVENT_EXTRA_REG(0x02b7, MSR_OFFCORE_RSP_1, 0xff3ffffffffffull, RSP_1), 2417 INTEL_UEVENT_PEBS_LDLAT_EXTRA_REG(0x5d0), 2418 INTEL_UEVENT_EXTRA_REG(0x0127, MSR_SNOOP_RSP_0, 0xffffffffffffffffull, SNOOP_0), 2419 INTEL_UEVENT_EXTRA_REG(0x0227, MSR_SNOOP_RSP_1, 0xffffffffffffffffull, SNOOP_1), 2420 EVENT_EXTRA_END 2421 }; 2422 2423 static struct extra_reg intel_arw_extra_regs[] __read_mostly = { 2424 /* must define OMR_X first, see intel_alt_er() */ 2425 INTEL_UEVENT_EXTRA_REG(0x01b7, MSR_OMR_0, 0xc0ffffffffffffffull, OMR_0), 2426 INTEL_UEVENT_EXTRA_REG(0x02b7, MSR_OMR_1, 0xc0ffffffffffffffull, OMR_1), 2427 INTEL_UEVENT_EXTRA_REG(0x04b7, MSR_OMR_2, 0xc0ffffffffffffffull, OMR_2), 2428 INTEL_UEVENT_EXTRA_REG(0x08b7, MSR_OMR_3, 0xc0ffffffffffffffull, OMR_3), 2429 INTEL_UEVENT_EXTRA_REG(0x01d4, MSR_OMR_0, 0xc0ffffffffffffffull, OMR_0), 2430 INTEL_UEVENT_EXTRA_REG(0x02d4, MSR_OMR_1, 0xc0ffffffffffffffull, OMR_1), 2431 INTEL_UEVENT_EXTRA_REG(0x04d4, MSR_OMR_2, 0xc0ffffffffffffffull, OMR_2), 2432 INTEL_UEVENT_EXTRA_REG(0x08d4, MSR_OMR_3, 0xc0ffffffffffffffull, OMR_3), 2433 INTEL_UEVENT_PEBS_LDLAT_EXTRA_REG(0x5d0), 2434 INTEL_UEVENT_EXTRA_REG(0x0127, MSR_SNOOP_RSP_0, 0xffffffffffffffffull, SNOOP_0), 2435 INTEL_UEVENT_EXTRA_REG(0x0227, MSR_SNOOP_RSP_1, 0xffffffffffffffffull, SNOOP_1), 2436 EVENT_EXTRA_END 2437 }; 2438 2439 EVENT_ATTR_STR(topdown-fe-bound, td_fe_bound_skt, "event=0x9c,umask=0x01"); 2440 EVENT_ATTR_STR(topdown-retiring, td_retiring_skt, "event=0xc2,umask=0x02"); 2441 EVENT_ATTR_STR(topdown-be-bound, td_be_bound_skt, "event=0xa4,umask=0x02"); 2442 2443 static struct attribute *skt_events_attrs[] = { 2444 EVENT_PTR(td_fe_bound_skt), 2445 EVENT_PTR(td_retiring_skt), 2446 EVENT_PTR(td_bad_spec_cmt), 2447 EVENT_PTR(td_be_bound_skt), 2448 NULL, 2449 }; 2450 2451 #define KNL_OT_L2_HITE BIT_ULL(19) /* Other Tile L2 Hit */ 2452 #define KNL_OT_L2_HITF BIT_ULL(20) /* Other Tile L2 Hit */ 2453 #define KNL_MCDRAM_LOCAL BIT_ULL(21) 2454 #define KNL_MCDRAM_FAR BIT_ULL(22) 2455 #define KNL_DDR_LOCAL BIT_ULL(23) 2456 #define KNL_DDR_FAR BIT_ULL(24) 2457 #define KNL_DRAM_ANY (KNL_MCDRAM_LOCAL | KNL_MCDRAM_FAR | \ 2458 KNL_DDR_LOCAL | KNL_DDR_FAR) 2459 #define KNL_L2_READ SLM_DMND_READ 2460 #define KNL_L2_WRITE SLM_DMND_WRITE 2461 #define KNL_L2_PREFETCH SLM_DMND_PREFETCH 2462 #define KNL_L2_ACCESS SLM_LLC_ACCESS 2463 #define KNL_L2_MISS (KNL_OT_L2_HITE | KNL_OT_L2_HITF | \ 2464 KNL_DRAM_ANY | SNB_SNP_ANY | \ 2465 SNB_NON_DRAM) 2466 2467 static __initconst const u64 knl_hw_cache_extra_regs 2468 [PERF_COUNT_HW_CACHE_MAX] 2469 [PERF_COUNT_HW_CACHE_OP_MAX] 2470 [PERF_COUNT_HW_CACHE_RESULT_MAX] = { 2471 [C(LL)] = { 2472 [C(OP_READ)] = { 2473 [C(RESULT_ACCESS)] = KNL_L2_READ | KNL_L2_ACCESS, 2474 [C(RESULT_MISS)] = 0, 2475 }, 2476 [C(OP_WRITE)] = { 2477 [C(RESULT_ACCESS)] = KNL_L2_WRITE | KNL_L2_ACCESS, 2478 [C(RESULT_MISS)] = KNL_L2_WRITE | KNL_L2_MISS, 2479 }, 2480 [C(OP_PREFETCH)] = { 2481 [C(RESULT_ACCESS)] = KNL_L2_PREFETCH | KNL_L2_ACCESS, 2482 [C(RESULT_MISS)] = KNL_L2_PREFETCH | KNL_L2_MISS, 2483 }, 2484 }, 2485 }; 2486 2487 /* 2488 * Used from PMIs where the LBRs are already disabled. 2489 * 2490 * This function could be called consecutively. It is required to remain in 2491 * disabled state if called consecutively. 2492 * 2493 * During consecutive calls, the same disable value will be written to related 2494 * registers, so the PMU state remains unchanged. 2495 * 2496 * intel_bts events don't coexist with intel PMU's BTS events because of 2497 * x86_add_exclusive(x86_lbr_exclusive_lbr); there's no need to keep them 2498 * disabled around intel PMU's event batching etc, only inside the PMI handler. 2499 * 2500 * Avoid PEBS_ENABLE MSR access in PMIs. 2501 * The GLOBAL_CTRL has been disabled. All the counters do not count anymore. 2502 * It doesn't matter if the PEBS is enabled or not. 2503 * Usually, the PEBS status are not changed in PMIs. It's unnecessary to 2504 * access PEBS_ENABLE MSR in disable_all()/enable_all(). 2505 * However, there are some cases which may change PEBS status, e.g. PMI 2506 * throttle. The PEBS_ENABLE should be updated where the status changes. 2507 */ 2508 static __always_inline void __intel_pmu_disable_all(bool bts) 2509 { 2510 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events); 2511 2512 wrmsrq(MSR_CORE_PERF_GLOBAL_CTRL, 0); 2513 2514 if (bts && test_bit(INTEL_PMC_IDX_FIXED_BTS, cpuc->active_mask)) 2515 intel_pmu_disable_bts(); 2516 } 2517 2518 static __always_inline void intel_pmu_disable_all(void) 2519 { 2520 __intel_pmu_disable_all(true); 2521 static_call_cond(x86_pmu_pebs_disable_all)(); 2522 intel_pmu_lbr_disable_all(); 2523 } 2524 2525 static void __intel_pmu_enable_all(int added, bool pmi) 2526 { 2527 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events); 2528 u64 intel_ctrl = hybrid(cpuc->pmu, intel_ctrl); 2529 2530 intel_pmu_lbr_enable_all(pmi); 2531 2532 if (cpuc->fixed_ctrl_val != cpuc->active_fixed_ctrl_val) { 2533 wrmsrq(MSR_ARCH_PERFMON_FIXED_CTR_CTRL, cpuc->fixed_ctrl_val); 2534 cpuc->active_fixed_ctrl_val = cpuc->fixed_ctrl_val; 2535 } 2536 2537 wrmsrq(MSR_CORE_PERF_GLOBAL_CTRL, 2538 intel_ctrl & ~cpuc->intel_ctrl_guest_mask); 2539 2540 if (test_bit(INTEL_PMC_IDX_FIXED_BTS, cpuc->active_mask)) { 2541 struct perf_event *event = 2542 cpuc->events[INTEL_PMC_IDX_FIXED_BTS]; 2543 2544 if (WARN_ON_ONCE(!event)) 2545 return; 2546 2547 intel_pmu_enable_bts(event->hw.config); 2548 } 2549 } 2550 2551 static void intel_pmu_enable_all(int added) 2552 { 2553 static_call_cond(x86_pmu_pebs_enable_all)(); 2554 __intel_pmu_enable_all(added, false); 2555 } 2556 2557 static noinline int 2558 __intel_pmu_snapshot_branch_stack(struct perf_branch_entry *entries, 2559 unsigned int cnt, unsigned long flags) 2560 { 2561 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events); 2562 2563 intel_pmu_lbr_read(); 2564 cnt = min_t(unsigned int, cnt, x86_pmu.lbr_nr); 2565 2566 memcpy(entries, cpuc->lbr_entries, sizeof(struct perf_branch_entry) * cnt); 2567 intel_pmu_enable_all(0); 2568 local_irq_restore(flags); 2569 return cnt; 2570 } 2571 2572 static int 2573 intel_pmu_snapshot_branch_stack(struct perf_branch_entry *entries, unsigned int cnt) 2574 { 2575 unsigned long flags; 2576 2577 /* must not have branches... */ 2578 local_irq_save(flags); 2579 __intel_pmu_disable_all(false); /* we don't care about BTS */ 2580 __intel_pmu_lbr_disable(); 2581 /* ... until here */ 2582 return __intel_pmu_snapshot_branch_stack(entries, cnt, flags); 2583 } 2584 2585 static int 2586 intel_pmu_snapshot_arch_branch_stack(struct perf_branch_entry *entries, unsigned int cnt) 2587 { 2588 unsigned long flags; 2589 2590 /* must not have branches... */ 2591 local_irq_save(flags); 2592 __intel_pmu_disable_all(false); /* we don't care about BTS */ 2593 __intel_pmu_arch_lbr_disable(); 2594 /* ... until here */ 2595 return __intel_pmu_snapshot_branch_stack(entries, cnt, flags); 2596 } 2597 2598 /* 2599 * Workaround for: 2600 * Intel Errata AAK100 (model 26) 2601 * Intel Errata AAP53 (model 30) 2602 * Intel Errata BD53 (model 44) 2603 * 2604 * The official story: 2605 * These chips need to be 'reset' when adding counters by programming the 2606 * magic three (non-counting) events 0x4300B5, 0x4300D2, and 0x4300B1 either 2607 * in sequence on the same PMC or on different PMCs. 2608 * 2609 * In practice it appears some of these events do in fact count, and 2610 * we need to program all 4 events. 2611 */ 2612 static void intel_pmu_nhm_workaround(void) 2613 { 2614 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events); 2615 static const unsigned long nhm_magic[4] = { 2616 0x4300B5, 2617 0x4300D2, 2618 0x4300B1, 2619 0x4300B1 2620 }; 2621 struct perf_event *event; 2622 int i; 2623 2624 /* 2625 * The Errata requires below steps: 2626 * 1) Clear MSR_IA32_PEBS_ENABLE and MSR_CORE_PERF_GLOBAL_CTRL; 2627 * 2) Configure 4 PERFEVTSELx with the magic events and clear 2628 * the corresponding PMCx; 2629 * 3) set bit0~bit3 of MSR_CORE_PERF_GLOBAL_CTRL; 2630 * 4) Clear MSR_CORE_PERF_GLOBAL_CTRL; 2631 * 5) Clear 4 pairs of ERFEVTSELx and PMCx; 2632 */ 2633 2634 /* 2635 * The real steps we choose are a little different from above. 2636 * A) To reduce MSR operations, we don't run step 1) as they 2637 * are already cleared before this function is called; 2638 * B) Call x86_perf_event_update to save PMCx before configuring 2639 * PERFEVTSELx with magic number; 2640 * C) With step 5), we do clear only when the PERFEVTSELx is 2641 * not used currently. 2642 * D) Call x86_perf_event_set_period to restore PMCx; 2643 */ 2644 2645 /* We always operate 4 pairs of PERF Counters */ 2646 for (i = 0; i < 4; i++) { 2647 event = cpuc->events[i]; 2648 if (event) 2649 static_call(x86_pmu_update)(event); 2650 } 2651 2652 for (i = 0; i < 4; i++) { 2653 wrmsrq(MSR_ARCH_PERFMON_EVENTSEL0 + i, nhm_magic[i]); 2654 wrmsrq(MSR_ARCH_PERFMON_PERFCTR0 + i, 0x0); 2655 } 2656 2657 wrmsrq(MSR_CORE_PERF_GLOBAL_CTRL, 0xf); 2658 wrmsrq(MSR_CORE_PERF_GLOBAL_CTRL, 0x0); 2659 2660 for (i = 0; i < 4; i++) { 2661 event = cpuc->events[i]; 2662 2663 if (event) { 2664 static_call(x86_pmu_set_period)(event); 2665 __x86_pmu_enable_event(&event->hw, 2666 ARCH_PERFMON_EVENTSEL_ENABLE); 2667 } else 2668 wrmsrq(MSR_ARCH_PERFMON_EVENTSEL0 + i, 0x0); 2669 } 2670 } 2671 2672 static void intel_pmu_nhm_enable_all(int added) 2673 { 2674 if (added) 2675 intel_pmu_nhm_workaround(); 2676 intel_pmu_enable_all(added); 2677 } 2678 2679 static void intel_set_tfa(struct cpu_hw_events *cpuc, bool on) 2680 { 2681 u64 val = on ? MSR_TFA_RTM_FORCE_ABORT : 0; 2682 2683 if (cpuc->tfa_shadow != val) { 2684 cpuc->tfa_shadow = val; 2685 wrmsrq(MSR_TSX_FORCE_ABORT, val); 2686 } 2687 } 2688 2689 static void intel_tfa_commit_scheduling(struct cpu_hw_events *cpuc, int idx, int cntr) 2690 { 2691 /* 2692 * We're going to use PMC3, make sure TFA is set before we touch it. 2693 */ 2694 if (cntr == 3) 2695 intel_set_tfa(cpuc, true); 2696 } 2697 2698 static void intel_tfa_pmu_enable_all(int added) 2699 { 2700 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events); 2701 2702 /* 2703 * If we find PMC3 is no longer used when we enable the PMU, we can 2704 * clear TFA. 2705 */ 2706 if (!test_bit(3, cpuc->active_mask)) 2707 intel_set_tfa(cpuc, false); 2708 2709 intel_pmu_enable_all(added); 2710 } 2711 2712 static inline u64 intel_pmu_get_status(void) 2713 { 2714 u64 status; 2715 2716 rdmsrq(MSR_CORE_PERF_GLOBAL_STATUS, status); 2717 2718 return status; 2719 } 2720 2721 static inline void intel_pmu_ack_status(u64 ack) 2722 { 2723 wrmsrq(MSR_CORE_PERF_GLOBAL_OVF_CTRL, ack); 2724 } 2725 2726 static inline bool event_is_checkpointed(struct perf_event *event) 2727 { 2728 return unlikely(event->hw.config & HSW_IN_TX_CHECKPOINTED) != 0; 2729 } 2730 2731 static inline void intel_set_masks(struct perf_event *event, int idx) 2732 { 2733 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events); 2734 2735 if (event->attr.exclude_host) 2736 __set_bit(idx, (unsigned long *)&cpuc->intel_ctrl_guest_mask); 2737 if (event->attr.exclude_guest) 2738 __set_bit(idx, (unsigned long *)&cpuc->intel_ctrl_host_mask); 2739 if (event_is_checkpointed(event)) 2740 __set_bit(idx, (unsigned long *)&cpuc->intel_cp_status); 2741 } 2742 2743 static inline void intel_clear_masks(struct perf_event *event, int idx) 2744 { 2745 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events); 2746 2747 __clear_bit(idx, (unsigned long *)&cpuc->intel_ctrl_guest_mask); 2748 __clear_bit(idx, (unsigned long *)&cpuc->intel_ctrl_host_mask); 2749 __clear_bit(idx, (unsigned long *)&cpuc->intel_cp_status); 2750 } 2751 2752 static void intel_pmu_disable_fixed(struct perf_event *event) 2753 { 2754 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events); 2755 struct hw_perf_event *hwc = &event->hw; 2756 int idx = hwc->idx; 2757 u64 mask; 2758 2759 if (is_topdown_idx(idx)) { 2760 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events); 2761 2762 /* 2763 * When there are other active TopDown events, 2764 * don't disable the fixed counter 3. 2765 */ 2766 if (*(u64 *)cpuc->active_mask & INTEL_PMC_OTHER_TOPDOWN_BITS(idx)) 2767 return; 2768 idx = INTEL_PMC_IDX_FIXED_SLOTS; 2769 } 2770 2771 intel_clear_masks(event, idx); 2772 2773 mask = intel_fixed_bits_by_idx(idx - INTEL_PMC_IDX_FIXED, INTEL_FIXED_BITS_MASK); 2774 cpuc->fixed_ctrl_val &= ~mask; 2775 } 2776 2777 static inline void __intel_pmu_update_event_ext(int idx, u64 ext) 2778 { 2779 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events); 2780 u32 msr; 2781 2782 if (idx < INTEL_PMC_IDX_FIXED) { 2783 msr = MSR_IA32_PMC_V6_GP0_CFG_C + 2784 x86_pmu.addr_offset(idx, false); 2785 } else { 2786 msr = MSR_IA32_PMC_V6_FX0_CFG_C + 2787 x86_pmu.addr_offset(idx - INTEL_PMC_IDX_FIXED, false); 2788 } 2789 2790 cpuc->cfg_c_val[idx] = ext; 2791 wrmsrq(msr, ext); 2792 } 2793 2794 static void intel_pmu_disable_event_ext(struct perf_event *event) 2795 { 2796 /* 2797 * Only clear CFG_C MSR for PEBS counter group events, 2798 * it avoids the HW counter's value to be added into 2799 * other PEBS records incorrectly after PEBS counter 2800 * group events are disabled. 2801 * 2802 * For other events, it's unnecessary to clear CFG_C MSRs 2803 * since CFG_C doesn't take effect if counter is in 2804 * disabled state. That helps to reduce the WRMSR overhead 2805 * in context switches. 2806 */ 2807 if (!is_pebs_counter_event_group(event)) 2808 return; 2809 2810 __intel_pmu_update_event_ext(event->hw.idx, 0); 2811 } 2812 2813 DEFINE_STATIC_CALL_NULL(intel_pmu_disable_event_ext, intel_pmu_disable_event_ext); 2814 2815 static void intel_pmu_disable_event(struct perf_event *event) 2816 { 2817 struct hw_perf_event *hwc = &event->hw; 2818 int idx = hwc->idx; 2819 2820 switch (idx) { 2821 case 0 ... INTEL_PMC_IDX_FIXED - 1: 2822 intel_clear_masks(event, idx); 2823 static_call_cond(intel_pmu_disable_event_ext)(event); 2824 x86_pmu_disable_event(event); 2825 break; 2826 case INTEL_PMC_IDX_FIXED ... INTEL_PMC_IDX_FIXED_BTS - 1: 2827 static_call_cond(intel_pmu_disable_event_ext)(event); 2828 fallthrough; 2829 case INTEL_PMC_IDX_METRIC_BASE ... INTEL_PMC_IDX_METRIC_END: 2830 intel_pmu_disable_fixed(event); 2831 break; 2832 case INTEL_PMC_IDX_FIXED_BTS: 2833 intel_pmu_disable_bts(); 2834 intel_pmu_drain_bts_buffer(); 2835 return; 2836 case INTEL_PMC_IDX_FIXED_VLBR: 2837 intel_clear_masks(event, idx); 2838 break; 2839 default: 2840 intel_clear_masks(event, idx); 2841 pr_warn("Failed to disable the event with invalid index %d\n", 2842 idx); 2843 return; 2844 } 2845 2846 /* 2847 * Needs to be called after x86_pmu_disable_event, 2848 * so we don't trigger the event without PEBS bit set. 2849 */ 2850 if (unlikely(event->attr.precise_ip)) 2851 static_call(x86_pmu_pebs_disable)(event); 2852 } 2853 2854 static void intel_pmu_assign_event(struct perf_event *event, int idx) 2855 { 2856 if (is_pebs_pt(event)) 2857 perf_report_aux_output_id(event, idx); 2858 } 2859 2860 static __always_inline bool intel_pmu_needs_branch_stack(struct perf_event *event) 2861 { 2862 return event->hw.flags & PERF_X86_EVENT_NEEDS_BRANCH_STACK; 2863 } 2864 2865 static void intel_pmu_del_event(struct perf_event *event) 2866 { 2867 if (intel_pmu_needs_branch_stack(event)) 2868 intel_pmu_lbr_del(event); 2869 if (event->attr.precise_ip) 2870 intel_pmu_pebs_del(event); 2871 if (is_pebs_counter_event_group(event) || 2872 is_acr_event_group(event)) 2873 this_cpu_ptr(&cpu_hw_events)->n_late_setup--; 2874 } 2875 2876 static int icl_set_topdown_event_period(struct perf_event *event) 2877 { 2878 struct hw_perf_event *hwc = &event->hw; 2879 s64 left = local64_read(&hwc->period_left); 2880 2881 /* 2882 * The values in PERF_METRICS MSR are derived from fixed counter 3. 2883 * Software should start both registers, PERF_METRICS and fixed 2884 * counter 3, from zero. 2885 * Clear PERF_METRICS and Fixed counter 3 in initialization. 2886 * After that, both MSRs will be cleared for each read. 2887 * Don't need to clear them again. 2888 */ 2889 if (left == x86_pmu.max_period) { 2890 wrmsrq(MSR_CORE_PERF_FIXED_CTR3, 0); 2891 wrmsrq(MSR_PERF_METRICS, 0); 2892 hwc->saved_slots = 0; 2893 hwc->saved_metric = 0; 2894 } 2895 2896 if ((hwc->saved_slots) && is_slots_event(event)) { 2897 wrmsrq(MSR_CORE_PERF_FIXED_CTR3, hwc->saved_slots); 2898 wrmsrq(MSR_PERF_METRICS, hwc->saved_metric); 2899 } 2900 2901 perf_event_update_userpage(event); 2902 2903 return 0; 2904 } 2905 2906 DEFINE_STATIC_CALL(intel_pmu_set_topdown_event_period, x86_perf_event_set_period); 2907 2908 static inline u64 icl_get_metrics_event_value(u64 metric, u64 slots, int idx) 2909 { 2910 u32 val; 2911 2912 /* 2913 * The metric is reported as an 8bit integer fraction 2914 * summing up to 0xff. 2915 * slots-in-metric = (Metric / 0xff) * slots 2916 */ 2917 val = (metric >> ((idx - INTEL_PMC_IDX_METRIC_BASE) * 8)) & 0xff; 2918 return mul_u64_u32_div(slots, val, 0xff); 2919 } 2920 2921 static u64 icl_get_topdown_value(struct perf_event *event, 2922 u64 slots, u64 metrics) 2923 { 2924 int idx = event->hw.idx; 2925 u64 delta; 2926 2927 if (is_metric_idx(idx)) 2928 delta = icl_get_metrics_event_value(metrics, slots, idx); 2929 else 2930 delta = slots; 2931 2932 return delta; 2933 } 2934 2935 static void __icl_update_topdown_event(struct perf_event *event, 2936 u64 slots, u64 metrics, 2937 u64 last_slots, u64 last_metrics) 2938 { 2939 u64 delta, last = 0; 2940 2941 delta = icl_get_topdown_value(event, slots, metrics); 2942 if (last_slots) 2943 last = icl_get_topdown_value(event, last_slots, last_metrics); 2944 2945 /* 2946 * The 8bit integer fraction of metric may be not accurate, 2947 * especially when the changes is very small. 2948 * For example, if only a few bad_spec happens, the fraction 2949 * may be reduced from 1 to 0. If so, the bad_spec event value 2950 * will be 0 which is definitely less than the last value. 2951 * Avoid update event->count for this case. 2952 */ 2953 if (delta > last) { 2954 delta -= last; 2955 local64_add(delta, &event->count); 2956 } 2957 } 2958 2959 static void update_saved_topdown_regs(struct perf_event *event, u64 slots, 2960 u64 metrics, int metric_end) 2961 { 2962 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events); 2963 struct perf_event *other; 2964 int idx; 2965 2966 event->hw.saved_slots = slots; 2967 event->hw.saved_metric = metrics; 2968 2969 for_each_set_bit(idx, cpuc->active_mask, metric_end + 1) { 2970 if (!is_topdown_idx(idx)) 2971 continue; 2972 other = cpuc->events[idx]; 2973 other->hw.saved_slots = slots; 2974 other->hw.saved_metric = metrics; 2975 } 2976 } 2977 2978 /* 2979 * Update all active Topdown events. 2980 * 2981 * The PERF_METRICS and Fixed counter 3 are read separately. The values may be 2982 * modify by a NMI. PMU has to be disabled before calling this function. 2983 */ 2984 2985 static u64 intel_update_topdown_event(struct perf_event *event, int metric_end, u64 *val) 2986 { 2987 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events); 2988 struct perf_event *other; 2989 u64 slots, metrics; 2990 bool reset = true; 2991 int idx; 2992 2993 if (!val) { 2994 /* read Fixed counter 3 */ 2995 slots = rdpmc(3 | INTEL_PMC_FIXED_RDPMC_BASE); 2996 if (!slots) 2997 return 0; 2998 2999 /* read PERF_METRICS */ 3000 metrics = rdpmc(INTEL_PMC_FIXED_RDPMC_METRICS); 3001 } else { 3002 slots = val[0]; 3003 metrics = val[1]; 3004 /* 3005 * Don't reset the PERF_METRICS and Fixed counter 3 3006 * for each PEBS record read. Utilize the RDPMC metrics 3007 * clear mode. 3008 */ 3009 reset = false; 3010 } 3011 3012 for_each_set_bit(idx, cpuc->active_mask, metric_end + 1) { 3013 if (!is_topdown_idx(idx)) 3014 continue; 3015 other = cpuc->events[idx]; 3016 __icl_update_topdown_event(other, slots, metrics, 3017 event ? event->hw.saved_slots : 0, 3018 event ? event->hw.saved_metric : 0); 3019 } 3020 3021 /* 3022 * Check and update this event, which may have been cleared 3023 * in active_mask e.g. x86_pmu_stop() 3024 */ 3025 if (event && !test_bit(event->hw.idx, cpuc->active_mask)) { 3026 __icl_update_topdown_event(event, slots, metrics, 3027 event->hw.saved_slots, 3028 event->hw.saved_metric); 3029 3030 /* 3031 * In x86_pmu_stop(), the event is cleared in active_mask first, 3032 * then drain the delta, which indicates context switch for 3033 * counting. 3034 * Save metric and slots for context switch. 3035 * Don't need to reset the PERF_METRICS and Fixed counter 3. 3036 * Because the values will be restored in next schedule in. 3037 */ 3038 update_saved_topdown_regs(event, slots, metrics, metric_end); 3039 reset = false; 3040 } 3041 3042 if (reset) { 3043 /* The fixed counter 3 has to be written before the PERF_METRICS. */ 3044 wrmsrq(MSR_CORE_PERF_FIXED_CTR3, 0); 3045 wrmsrq(MSR_PERF_METRICS, 0); 3046 if (event) 3047 update_saved_topdown_regs(event, 0, 0, metric_end); 3048 } 3049 3050 return slots; 3051 } 3052 3053 static u64 icl_update_topdown_event(struct perf_event *event, u64 *val) 3054 { 3055 return intel_update_topdown_event(event, INTEL_PMC_IDX_METRIC_BASE + 3056 x86_pmu.num_topdown_events - 1, 3057 val); 3058 } 3059 3060 DEFINE_STATIC_CALL(intel_pmu_update_topdown_event, intel_pmu_topdown_event_update); 3061 3062 static void intel_pmu_read_event(struct perf_event *event) 3063 { 3064 if (event->hw.flags & (PERF_X86_EVENT_AUTO_RELOAD | PERF_X86_EVENT_TOPDOWN) || 3065 is_pebs_counter_event_group(event)) { 3066 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events); 3067 bool pmu_enabled = cpuc->enabled; 3068 3069 /* Only need to call update_topdown_event() once for group read. */ 3070 if (is_metric_event(event) && (cpuc->txn_flags & PERF_PMU_TXN_READ)) 3071 return; 3072 3073 cpuc->enabled = 0; 3074 if (pmu_enabled) 3075 intel_pmu_disable_all(); 3076 3077 /* 3078 * If the PEBS counters snapshotting is enabled, 3079 * the topdown event is available in PEBS records. 3080 */ 3081 if (is_topdown_count(event) && !is_pebs_counter_event_group(event)) 3082 static_call(intel_pmu_update_topdown_event)(event, NULL); 3083 else 3084 intel_pmu_drain_pebs_buffer(); 3085 3086 cpuc->enabled = pmu_enabled; 3087 if (pmu_enabled) 3088 intel_pmu_enable_all(0); 3089 3090 return; 3091 } 3092 3093 x86_perf_event_update(event); 3094 } 3095 3096 static void intel_pmu_enable_fixed(struct perf_event *event) 3097 { 3098 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events); 3099 struct hw_perf_event *hwc = &event->hw; 3100 int idx = hwc->idx; 3101 u64 bits = 0; 3102 3103 if (is_topdown_idx(idx)) { 3104 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events); 3105 /* 3106 * When there are other active TopDown events, 3107 * don't enable the fixed counter 3 again. 3108 */ 3109 if (*(u64 *)cpuc->active_mask & INTEL_PMC_OTHER_TOPDOWN_BITS(idx)) 3110 return; 3111 3112 idx = INTEL_PMC_IDX_FIXED_SLOTS; 3113 3114 if (event->attr.config1 & INTEL_TD_CFG_METRIC_CLEAR) 3115 bits |= INTEL_FIXED_3_METRICS_CLEAR; 3116 } 3117 3118 intel_set_masks(event, idx); 3119 3120 /* 3121 * Enable IRQ generation (0x8), if not PEBS, 3122 * and enable ring-3 counting (0x2) and ring-0 counting (0x1) 3123 * if requested: 3124 */ 3125 if (!event->attr.precise_ip) 3126 bits |= INTEL_FIXED_0_ENABLE_PMI; 3127 if (hwc->config & ARCH_PERFMON_EVENTSEL_USR) 3128 bits |= INTEL_FIXED_0_USER; 3129 if (hwc->config & ARCH_PERFMON_EVENTSEL_OS) 3130 bits |= INTEL_FIXED_0_KERNEL; 3131 if (hwc->config & ARCH_PERFMON_EVENTSEL_RDPMC_USER_DISABLE) 3132 bits |= INTEL_FIXED_0_RDPMC_USER_DISABLE; 3133 3134 /* 3135 * ANY bit is supported in v3 and up 3136 */ 3137 if (x86_pmu.version > 2 && hwc->config & ARCH_PERFMON_EVENTSEL_ANY) 3138 bits |= INTEL_FIXED_0_ANYTHREAD; 3139 3140 idx -= INTEL_PMC_IDX_FIXED; 3141 bits = intel_fixed_bits_by_idx(idx, bits); 3142 if (x86_pmu.intel_cap.pebs_baseline && event->attr.precise_ip) 3143 bits |= intel_fixed_bits_by_idx(idx, ICL_FIXED_0_ADAPTIVE); 3144 3145 cpuc->fixed_ctrl_val &= ~intel_fixed_bits_by_idx(idx, INTEL_FIXED_BITS_MASK); 3146 cpuc->fixed_ctrl_val |= bits; 3147 } 3148 3149 static void intel_pmu_config_acr(int idx, u64 mask, u32 reload) 3150 { 3151 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events); 3152 int msr_b, msr_c; 3153 int msr_offset; 3154 3155 if (!mask && !cpuc->acr_cfg_b[idx]) 3156 return; 3157 3158 if (idx < INTEL_PMC_IDX_FIXED) { 3159 msr_b = MSR_IA32_PMC_V6_GP0_CFG_B; 3160 msr_c = MSR_IA32_PMC_V6_GP0_CFG_C; 3161 msr_offset = x86_pmu.addr_offset(idx, false); 3162 } else { 3163 msr_b = MSR_IA32_PMC_V6_FX0_CFG_B; 3164 msr_c = MSR_IA32_PMC_V6_FX0_CFG_C; 3165 msr_offset = x86_pmu.addr_offset(idx - INTEL_PMC_IDX_FIXED, false); 3166 } 3167 3168 if (cpuc->acr_cfg_b[idx] != mask) { 3169 wrmsrl(msr_b + msr_offset, mask); 3170 cpuc->acr_cfg_b[idx] = mask; 3171 } 3172 /* Only need to update the reload value when there is a valid config value. */ 3173 if (mask && cpuc->acr_cfg_c[idx] != reload) { 3174 wrmsrl(msr_c + msr_offset, reload); 3175 cpuc->acr_cfg_c[idx] = reload; 3176 } 3177 } 3178 3179 static void intel_pmu_enable_acr(struct perf_event *event) 3180 { 3181 struct hw_perf_event *hwc = &event->hw; 3182 3183 if (!is_acr_event_group(event) || !event->attr.config2) { 3184 /* 3185 * The disable doesn't clear the ACR CFG register. 3186 * Check and clear the ACR CFG register. 3187 */ 3188 intel_pmu_config_acr(hwc->idx, 0, 0); 3189 return; 3190 } 3191 3192 intel_pmu_config_acr(hwc->idx, hwc->config1, -hwc->sample_period); 3193 } 3194 3195 DEFINE_STATIC_CALL_NULL(intel_pmu_enable_acr_event, intel_pmu_enable_acr); 3196 3197 static void intel_pmu_enable_event_ext(struct perf_event *event) 3198 { 3199 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events); 3200 struct hw_perf_event *hwc = &event->hw; 3201 union arch_pebs_index old, new; 3202 struct arch_pebs_cap cap; 3203 u64 ext = 0; 3204 3205 cap = hybrid(cpuc->pmu, arch_pebs_cap); 3206 3207 if (event->attr.precise_ip) { 3208 u64 pebs_data_cfg = intel_get_arch_pebs_data_config(event); 3209 3210 ext |= ARCH_PEBS_EN; 3211 if (hwc->flags & PERF_X86_EVENT_AUTO_RELOAD) 3212 ext |= (-hwc->sample_period) & ARCH_PEBS_RELOAD; 3213 3214 if (pebs_data_cfg && cap.caps) { 3215 if (pebs_data_cfg & PEBS_DATACFG_MEMINFO) 3216 ext |= ARCH_PEBS_AUX & cap.caps; 3217 3218 if (pebs_data_cfg & PEBS_DATACFG_GP) 3219 ext |= ARCH_PEBS_GPR & cap.caps; 3220 3221 if (pebs_data_cfg & PEBS_DATACFG_XMMS) 3222 ext |= ARCH_PEBS_VECR_XMM & cap.caps; 3223 3224 if (pebs_data_cfg & PEBS_DATACFG_LBRS) 3225 ext |= ARCH_PEBS_LBR & cap.caps; 3226 3227 if (pebs_data_cfg & 3228 (PEBS_DATACFG_CNTR_MASK << PEBS_DATACFG_CNTR_SHIFT)) 3229 ext |= ARCH_PEBS_CNTR_GP & cap.caps; 3230 3231 if (pebs_data_cfg & 3232 (PEBS_DATACFG_FIX_MASK << PEBS_DATACFG_FIX_SHIFT)) 3233 ext |= ARCH_PEBS_CNTR_FIXED & cap.caps; 3234 3235 if (pebs_data_cfg & PEBS_DATACFG_METRICS) 3236 ext |= ARCH_PEBS_CNTR_METRICS & cap.caps; 3237 } 3238 3239 if (cpuc->n_pebs == cpuc->n_large_pebs) 3240 new.thresh = ARCH_PEBS_THRESH_MULTI; 3241 else 3242 new.thresh = ARCH_PEBS_THRESH_SINGLE; 3243 3244 rdmsrq(MSR_IA32_PEBS_INDEX, old.whole); 3245 if (new.thresh != old.thresh || !old.en) { 3246 if (old.thresh == ARCH_PEBS_THRESH_MULTI && old.wr > 0) { 3247 /* 3248 * Large PEBS was enabled. 3249 * Drain PEBS buffer before applying the single PEBS. 3250 */ 3251 intel_pmu_drain_pebs_buffer(); 3252 } else { 3253 new.wr = 0; 3254 new.full = 0; 3255 new.en = 1; 3256 wrmsrq(MSR_IA32_PEBS_INDEX, new.whole); 3257 } 3258 } 3259 } 3260 3261 if (is_pebs_counter_event_group(event)) 3262 ext |= ARCH_PEBS_CNTR_ALLOW; 3263 3264 if (cpuc->cfg_c_val[hwc->idx] != ext) 3265 __intel_pmu_update_event_ext(hwc->idx, ext); 3266 } 3267 3268 static void intel_pmu_update_rdpmc_user_disable(struct perf_event *event) 3269 { 3270 if (!x86_pmu_has_rdpmc_user_disable(event->pmu)) 3271 return; 3272 3273 /* 3274 * Counter scope's user-space rdpmc is disabled by default 3275 * except two cases. 3276 * a. rdpmc = 2 (user space rdpmc enabled unconditionally) 3277 * b. rdpmc = 1 and the event is not a system-wide event. 3278 * The count of non-system-wide events would be cleared when 3279 * context switches, so no count data is leaked. 3280 */ 3281 if (x86_pmu.attr_rdpmc == X86_USER_RDPMC_ALWAYS_ENABLE || 3282 (x86_pmu.attr_rdpmc == X86_USER_RDPMC_CONDITIONAL_ENABLE && 3283 event->ctx->task)) 3284 event->hw.config &= ~ARCH_PERFMON_EVENTSEL_RDPMC_USER_DISABLE; 3285 else 3286 event->hw.config |= ARCH_PERFMON_EVENTSEL_RDPMC_USER_DISABLE; 3287 } 3288 3289 DEFINE_STATIC_CALL_NULL(intel_pmu_enable_event_ext, intel_pmu_enable_event_ext); 3290 3291 static void intel_pmu_enable_event(struct perf_event *event) 3292 { 3293 u64 enable_mask = ARCH_PERFMON_EVENTSEL_ENABLE; 3294 struct hw_perf_event *hwc = &event->hw; 3295 int idx = hwc->idx; 3296 3297 intel_pmu_update_rdpmc_user_disable(event); 3298 3299 if (unlikely(event->attr.precise_ip)) 3300 static_call(x86_pmu_pebs_enable)(event); 3301 3302 switch (idx) { 3303 case 0 ... INTEL_PMC_IDX_FIXED - 1: 3304 if (branch_sample_counters(event)) 3305 enable_mask |= ARCH_PERFMON_EVENTSEL_BR_CNTR; 3306 intel_set_masks(event, idx); 3307 static_call_cond(intel_pmu_enable_acr_event)(event); 3308 static_call_cond(intel_pmu_enable_event_ext)(event); 3309 __x86_pmu_enable_event(hwc, enable_mask); 3310 break; 3311 case INTEL_PMC_IDX_FIXED ... INTEL_PMC_IDX_FIXED_BTS - 1: 3312 static_call_cond(intel_pmu_enable_acr_event)(event); 3313 static_call_cond(intel_pmu_enable_event_ext)(event); 3314 fallthrough; 3315 case INTEL_PMC_IDX_METRIC_BASE ... INTEL_PMC_IDX_METRIC_END: 3316 intel_pmu_enable_fixed(event); 3317 break; 3318 case INTEL_PMC_IDX_FIXED_BTS: 3319 if (!__this_cpu_read(cpu_hw_events.enabled)) 3320 return; 3321 intel_pmu_enable_bts(hwc->config); 3322 break; 3323 case INTEL_PMC_IDX_FIXED_VLBR: 3324 intel_set_masks(event, idx); 3325 break; 3326 default: 3327 pr_warn("Failed to enable the event with invalid index %d\n", 3328 idx); 3329 } 3330 } 3331 3332 static void intel_pmu_acr_late_setup(struct cpu_hw_events *cpuc) 3333 { 3334 struct perf_event *event, *leader; 3335 int i, j, idx; 3336 3337 for (i = 0; i < cpuc->n_events; i++) { 3338 leader = cpuc->event_list[i]; 3339 if (!is_acr_event_group(leader)) 3340 continue; 3341 3342 /* The ACR events must be contiguous. */ 3343 for (j = i; j < cpuc->n_events; j++) { 3344 event = cpuc->event_list[j]; 3345 if (event->group_leader != leader->group_leader) 3346 break; 3347 for_each_set_bit(idx, (unsigned long *)&event->attr.config2, X86_PMC_IDX_MAX) { 3348 if (i + idx >= cpuc->n_events || 3349 !is_acr_event_group(cpuc->event_list[i + idx])) 3350 return; 3351 __set_bit(cpuc->assign[i + idx], (unsigned long *)&event->hw.config1); 3352 } 3353 } 3354 i = j - 1; 3355 } 3356 } 3357 3358 void intel_pmu_late_setup(void) 3359 { 3360 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events); 3361 3362 if (!cpuc->n_late_setup) 3363 return; 3364 3365 intel_pmu_pebs_late_setup(cpuc); 3366 intel_pmu_acr_late_setup(cpuc); 3367 } 3368 3369 static void intel_pmu_add_event(struct perf_event *event) 3370 { 3371 if (event->attr.precise_ip) 3372 intel_pmu_pebs_add(event); 3373 if (intel_pmu_needs_branch_stack(event)) 3374 intel_pmu_lbr_add(event); 3375 if (is_pebs_counter_event_group(event) || 3376 is_acr_event_group(event)) 3377 this_cpu_ptr(&cpu_hw_events)->n_late_setup++; 3378 } 3379 3380 /* 3381 * Save and restart an expired event. Called by NMI contexts, 3382 * so it has to be careful about preempting normal event ops: 3383 */ 3384 int intel_pmu_save_and_restart(struct perf_event *event) 3385 { 3386 static_call(x86_pmu_update)(event); 3387 /* 3388 * For a checkpointed counter always reset back to 0. This 3389 * avoids a situation where the counter overflows, aborts the 3390 * transaction and is then set back to shortly before the 3391 * overflow, and overflows and aborts again. 3392 */ 3393 if (unlikely(event_is_checkpointed(event))) { 3394 /* No race with NMIs because the counter should not be armed */ 3395 wrmsrq(event->hw.event_base, 0); 3396 local64_set(&event->hw.prev_count, 0); 3397 } 3398 return static_call(x86_pmu_set_period)(event); 3399 } 3400 3401 static int intel_pmu_set_period(struct perf_event *event) 3402 { 3403 if (unlikely(is_topdown_count(event))) 3404 return static_call(intel_pmu_set_topdown_event_period)(event); 3405 3406 return x86_perf_event_set_period(event); 3407 } 3408 3409 static u64 intel_pmu_update(struct perf_event *event) 3410 { 3411 if (unlikely(is_topdown_count(event))) 3412 return static_call(intel_pmu_update_topdown_event)(event, NULL); 3413 3414 return x86_perf_event_update(event); 3415 } 3416 3417 static void intel_pmu_reset(void) 3418 { 3419 struct debug_store *ds = __this_cpu_read(cpu_hw_events.ds); 3420 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events); 3421 unsigned long *cntr_mask = hybrid(cpuc->pmu, cntr_mask); 3422 unsigned long *fixed_cntr_mask = hybrid(cpuc->pmu, fixed_cntr_mask); 3423 unsigned long flags; 3424 int idx; 3425 3426 if (!*(u64 *)cntr_mask) 3427 return; 3428 3429 local_irq_save(flags); 3430 3431 pr_info("clearing PMU state on CPU#%d\n", smp_processor_id()); 3432 3433 for_each_set_bit(idx, cntr_mask, INTEL_PMC_MAX_GENERIC) { 3434 wrmsrq_safe(x86_pmu_config_addr(idx), 0ull); 3435 wrmsrq_safe(x86_pmu_event_addr(idx), 0ull); 3436 } 3437 for_each_set_bit(idx, fixed_cntr_mask, INTEL_PMC_MAX_FIXED) { 3438 if (fixed_counter_disabled(idx, cpuc->pmu)) 3439 continue; 3440 wrmsrq_safe(x86_pmu_fixed_ctr_addr(idx), 0ull); 3441 } 3442 3443 if (ds) 3444 ds->bts_index = ds->bts_buffer_base; 3445 3446 /* Ack all overflows and disable fixed counters */ 3447 if (x86_pmu.version >= 2) { 3448 intel_pmu_ack_status(intel_pmu_get_status()); 3449 wrmsrq(MSR_CORE_PERF_GLOBAL_CTRL, 0); 3450 } 3451 3452 /* Reset LBRs and LBR freezing */ 3453 if (x86_pmu.lbr_nr) { 3454 update_debugctlmsr(get_debugctlmsr() & 3455 ~(DEBUGCTLMSR_FREEZE_LBRS_ON_PMI|DEBUGCTLMSR_LBR)); 3456 } 3457 3458 local_irq_restore(flags); 3459 } 3460 3461 /* 3462 * We may be running with guest PEBS events created by KVM, and the 3463 * PEBS records are logged into the guest's DS and invisible to host. 3464 * 3465 * In the case of guest PEBS overflow, we only trigger a fake event 3466 * to emulate the PEBS overflow PMI for guest PEBS counters in KVM. 3467 * The guest will then vm-entry and check the guest DS area to read 3468 * the guest PEBS records. 3469 * 3470 * The contents and other behavior of the guest event do not matter. 3471 */ 3472 static void x86_pmu_handle_guest_pebs(struct pt_regs *regs, 3473 struct perf_sample_data *data) 3474 { 3475 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events); 3476 u64 guest_pebs_idxs = cpuc->pebs_enabled & ~cpuc->intel_ctrl_host_mask; 3477 struct perf_event *event = NULL; 3478 int bit; 3479 3480 if (!unlikely(perf_guest_state())) 3481 return; 3482 3483 if (!x86_pmu.pebs_ept || !x86_pmu.pebs_active || 3484 !guest_pebs_idxs) 3485 return; 3486 3487 for_each_set_bit(bit, (unsigned long *)&guest_pebs_idxs, X86_PMC_IDX_MAX) { 3488 event = cpuc->events[bit]; 3489 if (!event->attr.precise_ip) 3490 continue; 3491 3492 perf_sample_data_init(data, 0, event->hw.last_period); 3493 perf_event_overflow(event, data, regs); 3494 3495 /* Inject one fake event is enough. */ 3496 break; 3497 } 3498 } 3499 3500 static int handle_pmi_common(struct pt_regs *regs, u64 status) 3501 { 3502 struct perf_sample_data data; 3503 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events); 3504 int bit; 3505 int handled = 0; 3506 3507 inc_irq_stat(apic_perf_irqs); 3508 3509 /* 3510 * Ignore a range of extra bits in status that do not indicate 3511 * overflow by themselves. 3512 */ 3513 status &= ~(GLOBAL_STATUS_COND_CHG | 3514 GLOBAL_STATUS_ASIF | 3515 GLOBAL_STATUS_LBRS_FROZEN); 3516 if (!status) 3517 return 0; 3518 /* 3519 * In case multiple PEBS events are sampled at the same time, 3520 * it is possible to have GLOBAL_STATUS bit 62 set indicating 3521 * PEBS buffer overflow and also seeing at most 3 PEBS counters 3522 * having their bits set in the status register. This is a sign 3523 * that there was at least one PEBS record pending at the time 3524 * of the PMU interrupt. PEBS counters must only be processed 3525 * via the drain_pebs() calls and not via the regular sample 3526 * processing loop coming after that the function, otherwise 3527 * phony regular samples may be generated in the sampling buffer 3528 * not marked with the EXACT tag. Another possibility is to have 3529 * one PEBS event and at least one non-PEBS event which overflows 3530 * while PEBS has armed. In this case, bit 62 of GLOBAL_STATUS will 3531 * not be set, yet the overflow status bit for the PEBS counter will 3532 * be on Skylake. 3533 * 3534 * To avoid this problem, we systematically ignore the PEBS-enabled 3535 * counters from the GLOBAL_STATUS mask and we always process PEBS 3536 * events via drain_pebs(). 3537 */ 3538 status &= ~(cpuc->pebs_enabled & x86_pmu.pebs_capable); 3539 3540 /* 3541 * PEBS overflow sets bit 62 in the global status register 3542 */ 3543 if (__test_and_clear_bit(GLOBAL_STATUS_BUFFER_OVF_BIT, (unsigned long *)&status)) { 3544 u64 pebs_enabled = cpuc->pebs_enabled; 3545 3546 handled++; 3547 x86_pmu_handle_guest_pebs(regs, &data); 3548 static_call(x86_pmu_drain_pebs)(regs, &data); 3549 3550 /* 3551 * PMI throttle may be triggered, which stops the PEBS event. 3552 * Although cpuc->pebs_enabled is updated accordingly, the 3553 * MSR_IA32_PEBS_ENABLE is not updated. Because the 3554 * cpuc->enabled has been forced to 0 in PMI. 3555 * Update the MSR if pebs_enabled is changed. 3556 */ 3557 if (pebs_enabled != cpuc->pebs_enabled) 3558 wrmsrq(MSR_IA32_PEBS_ENABLE, cpuc->pebs_enabled); 3559 3560 /* 3561 * Above PEBS handler (PEBS counters snapshotting) has updated fixed 3562 * counter 3 and perf metrics counts if they are in counter group, 3563 * unnecessary to update again. 3564 */ 3565 if (cpuc->events[INTEL_PMC_IDX_FIXED_SLOTS] && 3566 is_pebs_counter_event_group(cpuc->events[INTEL_PMC_IDX_FIXED_SLOTS])) 3567 status &= ~GLOBAL_STATUS_PERF_METRICS_OVF_BIT; 3568 } 3569 3570 /* 3571 * Arch PEBS sets bit 54 in the global status register 3572 */ 3573 if (__test_and_clear_bit(GLOBAL_STATUS_ARCH_PEBS_THRESHOLD_BIT, 3574 (unsigned long *)&status)) { 3575 handled++; 3576 static_call(x86_pmu_drain_pebs)(regs, &data); 3577 3578 if (cpuc->events[INTEL_PMC_IDX_FIXED_SLOTS] && 3579 is_pebs_counter_event_group(cpuc->events[INTEL_PMC_IDX_FIXED_SLOTS])) 3580 status &= ~GLOBAL_STATUS_PERF_METRICS_OVF_BIT; 3581 } 3582 3583 /* 3584 * Intel PT 3585 */ 3586 if (__test_and_clear_bit(GLOBAL_STATUS_TRACE_TOPAPMI_BIT, (unsigned long *)&status)) { 3587 handled++; 3588 if (!perf_guest_handle_intel_pt_intr()) 3589 intel_pt_interrupt(); 3590 } 3591 3592 /* 3593 * Intel Perf metrics 3594 */ 3595 if (__test_and_clear_bit(GLOBAL_STATUS_PERF_METRICS_OVF_BIT, (unsigned long *)&status)) { 3596 handled++; 3597 static_call(intel_pmu_update_topdown_event)(NULL, NULL); 3598 } 3599 3600 status &= hybrid(cpuc->pmu, intel_ctrl); 3601 3602 /* 3603 * Checkpointed counters can lead to 'spurious' PMIs because the 3604 * rollback caused by the PMI will have cleared the overflow status 3605 * bit. Therefore always force probe these counters. 3606 */ 3607 status |= cpuc->intel_cp_status; 3608 3609 for_each_set_bit(bit, (unsigned long *)&status, X86_PMC_IDX_MAX) { 3610 struct perf_event *event = cpuc->events[bit]; 3611 u64 last_period; 3612 3613 handled++; 3614 3615 if (!test_bit(bit, cpuc->active_mask)) 3616 continue; 3617 /* Event may have already been cleared: */ 3618 if (!event) 3619 continue; 3620 3621 /* 3622 * There may be unprocessed PEBS records in the PEBS buffer, 3623 * which still stores the previous values. 3624 * Process those records first before handling the latest value. 3625 * For example, 3626 * A is a regular counter 3627 * B is a PEBS event which reads A 3628 * C is a PEBS event 3629 * 3630 * The following can happen: 3631 * B-assist A=1 3632 * C A=2 3633 * B-assist A=3 3634 * A-overflow-PMI A=4 3635 * C-assist-PMI (PEBS buffer) A=5 3636 * 3637 * The PEBS buffer has to be drained before handling the A-PMI 3638 */ 3639 if (is_pebs_counter_event_group(event)) 3640 static_call(x86_pmu_drain_pebs)(regs, &data); 3641 3642 last_period = event->hw.last_period; 3643 3644 if (!intel_pmu_save_and_restart(event)) 3645 continue; 3646 3647 perf_sample_data_init(&data, 0, last_period); 3648 3649 if (has_branch_stack(event)) 3650 intel_pmu_lbr_save_brstack(&data, cpuc, event); 3651 3652 perf_event_overflow(event, &data, regs); 3653 } 3654 3655 return handled; 3656 } 3657 3658 /* 3659 * This handler is triggered by the local APIC, so the APIC IRQ handling 3660 * rules apply: 3661 */ 3662 static int intel_pmu_handle_irq(struct pt_regs *regs) 3663 { 3664 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events); 3665 bool late_ack = hybrid_bit(cpuc->pmu, late_ack); 3666 bool mid_ack = hybrid_bit(cpuc->pmu, mid_ack); 3667 int loops; 3668 u64 status; 3669 int handled; 3670 int pmu_enabled; 3671 3672 /* 3673 * Save the PMU state. 3674 * It needs to be restored when leaving the handler. 3675 */ 3676 pmu_enabled = cpuc->enabled; 3677 /* 3678 * In general, the early ACK is only applied for old platforms. 3679 * For the big core starts from Haswell, the late ACK should be 3680 * applied. 3681 * For the small core after Tremont, we have to do the ACK right 3682 * before re-enabling counters, which is in the middle of the 3683 * NMI handler. 3684 */ 3685 if (!late_ack && !mid_ack) 3686 apic_write(APIC_LVTPC, APIC_DM_NMI); 3687 intel_bts_disable_local(); 3688 cpuc->enabled = 0; 3689 __intel_pmu_disable_all(true); 3690 handled = intel_pmu_drain_bts_buffer(); 3691 handled += intel_bts_interrupt(); 3692 status = intel_pmu_get_status(); 3693 if (!status) 3694 goto done; 3695 3696 loops = 0; 3697 again: 3698 intel_pmu_lbr_read(); 3699 intel_pmu_ack_status(status); 3700 if (++loops > 100) { 3701 static bool warned; 3702 3703 if (!warned) { 3704 WARN(1, "perfevents: irq loop stuck!\n"); 3705 perf_event_print_debug(); 3706 warned = true; 3707 } 3708 intel_pmu_reset(); 3709 goto done; 3710 } 3711 3712 handled += handle_pmi_common(regs, status); 3713 3714 /* 3715 * Repeat if there is more work to be done: 3716 */ 3717 status = intel_pmu_get_status(); 3718 if (status) 3719 goto again; 3720 3721 done: 3722 if (mid_ack) 3723 apic_write(APIC_LVTPC, APIC_DM_NMI); 3724 /* Only restore PMU state when it's active. See x86_pmu_disable(). */ 3725 cpuc->enabled = pmu_enabled; 3726 if (pmu_enabled) 3727 __intel_pmu_enable_all(0, true); 3728 intel_bts_enable_local(); 3729 3730 /* 3731 * Only unmask the NMI after the overflow counters 3732 * have been reset. This avoids spurious NMIs on 3733 * Haswell CPUs. 3734 */ 3735 if (late_ack) 3736 apic_write(APIC_LVTPC, APIC_DM_NMI); 3737 return handled; 3738 } 3739 3740 static struct event_constraint * 3741 intel_bts_constraints(struct perf_event *event) 3742 { 3743 if (unlikely(intel_pmu_has_bts(event))) 3744 return &bts_constraint; 3745 3746 return NULL; 3747 } 3748 3749 /* 3750 * Note: matches a fake event, like Fixed2. 3751 */ 3752 static struct event_constraint * 3753 intel_vlbr_constraints(struct perf_event *event) 3754 { 3755 struct event_constraint *c = &vlbr_constraint; 3756 3757 if (unlikely(constraint_match(c, event->hw.config))) { 3758 event->hw.flags |= c->flags; 3759 return c; 3760 } 3761 3762 return NULL; 3763 } 3764 3765 static int intel_alt_er(struct cpu_hw_events *cpuc, 3766 int idx, u64 config) 3767 { 3768 struct extra_reg *extra_regs = hybrid(cpuc->pmu, extra_regs); 3769 int alt_idx = idx; 3770 3771 switch (idx) { 3772 case EXTRA_REG_RSP_0 ... EXTRA_REG_RSP_1: 3773 if (!(x86_pmu.flags & PMU_FL_HAS_RSP_1)) 3774 return idx; 3775 if (++alt_idx > EXTRA_REG_RSP_1) 3776 alt_idx = EXTRA_REG_RSP_0; 3777 if (config & ~extra_regs[alt_idx].valid_mask) 3778 return idx; 3779 break; 3780 3781 case EXTRA_REG_OMR_0 ... EXTRA_REG_OMR_3: 3782 if (!(x86_pmu.flags & PMU_FL_HAS_OMR)) 3783 return idx; 3784 if (++alt_idx > EXTRA_REG_OMR_3) 3785 alt_idx = EXTRA_REG_OMR_0; 3786 /* 3787 * Subtracting EXTRA_REG_OMR_0 ensures to get correct 3788 * OMR extra_reg entries which start from 0. 3789 */ 3790 if (config & ~extra_regs[alt_idx - EXTRA_REG_OMR_0].valid_mask) 3791 return idx; 3792 break; 3793 3794 default: 3795 break; 3796 } 3797 3798 return alt_idx; 3799 } 3800 3801 static void intel_fixup_er(struct perf_event *event, int idx) 3802 { 3803 struct extra_reg *extra_regs = hybrid(event->pmu, extra_regs); 3804 int er_idx; 3805 3806 event->hw.extra_reg.idx = idx; 3807 switch (idx) { 3808 case EXTRA_REG_RSP_0 ... EXTRA_REG_RSP_1: 3809 er_idx = idx - EXTRA_REG_RSP_0; 3810 event->hw.config &= ~INTEL_ARCH_EVENT_MASK; 3811 event->hw.config |= extra_regs[er_idx].event; 3812 event->hw.extra_reg.reg = MSR_OFFCORE_RSP_0 + er_idx; 3813 break; 3814 3815 case EXTRA_REG_OMR_0 ... EXTRA_REG_OMR_3: 3816 er_idx = idx - EXTRA_REG_OMR_0; 3817 event->hw.config &= ~ARCH_PERFMON_EVENTSEL_UMASK; 3818 event->hw.config |= 1ULL << (8 + er_idx); 3819 event->hw.extra_reg.reg = MSR_OMR_0 + er_idx; 3820 break; 3821 3822 default: 3823 pr_warn("The extra reg idx %d is not supported.\n", idx); 3824 } 3825 } 3826 3827 /* 3828 * manage allocation of shared extra msr for certain events 3829 * 3830 * sharing can be: 3831 * per-cpu: to be shared between the various events on a single PMU 3832 * per-core: per-cpu + shared by HT threads 3833 */ 3834 static struct event_constraint * 3835 __intel_shared_reg_get_constraints(struct cpu_hw_events *cpuc, 3836 struct perf_event *event, 3837 struct hw_perf_event_extra *reg) 3838 { 3839 struct event_constraint *c = &emptyconstraint; 3840 struct er_account *era; 3841 unsigned long flags; 3842 int idx = reg->idx; 3843 3844 /* 3845 * reg->alloc can be set due to existing state, so for fake cpuc we 3846 * need to ignore this, otherwise we might fail to allocate proper fake 3847 * state for this extra reg constraint. Also see the comment below. 3848 */ 3849 if (reg->alloc && !cpuc->is_fake) 3850 return NULL; /* call x86_get_event_constraint() */ 3851 3852 again: 3853 era = &cpuc->shared_regs->regs[idx]; 3854 /* 3855 * we use spin_lock_irqsave() to avoid lockdep issues when 3856 * passing a fake cpuc 3857 */ 3858 raw_spin_lock_irqsave(&era->lock, flags); 3859 3860 if (!atomic_read(&era->ref) || era->config == reg->config) { 3861 3862 /* 3863 * If its a fake cpuc -- as per validate_{group,event}() we 3864 * shouldn't touch event state and we can avoid doing so 3865 * since both will only call get_event_constraints() once 3866 * on each event, this avoids the need for reg->alloc. 3867 * 3868 * Not doing the ER fixup will only result in era->reg being 3869 * wrong, but since we won't actually try and program hardware 3870 * this isn't a problem either. 3871 */ 3872 if (!cpuc->is_fake) { 3873 if (idx != reg->idx) 3874 intel_fixup_er(event, idx); 3875 3876 /* 3877 * x86_schedule_events() can call get_event_constraints() 3878 * multiple times on events in the case of incremental 3879 * scheduling(). reg->alloc ensures we only do the ER 3880 * allocation once. 3881 */ 3882 reg->alloc = 1; 3883 } 3884 3885 /* lock in msr value */ 3886 era->config = reg->config; 3887 era->reg = reg->reg; 3888 3889 /* one more user */ 3890 atomic_inc(&era->ref); 3891 3892 /* 3893 * need to call x86_get_event_constraint() 3894 * to check if associated event has constraints 3895 */ 3896 c = NULL; 3897 } else { 3898 idx = intel_alt_er(cpuc, idx, reg->config); 3899 if (idx != reg->idx) { 3900 raw_spin_unlock_irqrestore(&era->lock, flags); 3901 goto again; 3902 } 3903 } 3904 raw_spin_unlock_irqrestore(&era->lock, flags); 3905 3906 return c; 3907 } 3908 3909 static void 3910 __intel_shared_reg_put_constraints(struct cpu_hw_events *cpuc, 3911 struct hw_perf_event_extra *reg) 3912 { 3913 struct er_account *era; 3914 3915 /* 3916 * Only put constraint if extra reg was actually allocated. Also takes 3917 * care of event which do not use an extra shared reg. 3918 * 3919 * Also, if this is a fake cpuc we shouldn't touch any event state 3920 * (reg->alloc) and we don't care about leaving inconsistent cpuc state 3921 * either since it'll be thrown out. 3922 */ 3923 if (!reg->alloc || cpuc->is_fake) 3924 return; 3925 3926 era = &cpuc->shared_regs->regs[reg->idx]; 3927 3928 /* one fewer user */ 3929 atomic_dec(&era->ref); 3930 3931 /* allocate again next time */ 3932 reg->alloc = 0; 3933 } 3934 3935 static struct event_constraint * 3936 intel_shared_regs_constraints(struct cpu_hw_events *cpuc, 3937 struct perf_event *event) 3938 { 3939 struct event_constraint *c = NULL, *d; 3940 struct hw_perf_event_extra *xreg, *breg; 3941 3942 xreg = &event->hw.extra_reg; 3943 if (xreg->idx != EXTRA_REG_NONE) { 3944 c = __intel_shared_reg_get_constraints(cpuc, event, xreg); 3945 if (c == &emptyconstraint) 3946 return c; 3947 } 3948 breg = &event->hw.branch_reg; 3949 if (breg->idx != EXTRA_REG_NONE) { 3950 d = __intel_shared_reg_get_constraints(cpuc, event, breg); 3951 if (d == &emptyconstraint) { 3952 __intel_shared_reg_put_constraints(cpuc, xreg); 3953 c = d; 3954 } 3955 } 3956 return c; 3957 } 3958 3959 struct event_constraint * 3960 x86_get_event_constraints(struct cpu_hw_events *cpuc, int idx, 3961 struct perf_event *event) 3962 { 3963 struct event_constraint *event_constraints = hybrid(cpuc->pmu, event_constraints); 3964 struct event_constraint *c; 3965 3966 if (event_constraints) { 3967 for_each_event_constraint(c, event_constraints) { 3968 if (constraint_match(c, event->hw.config)) { 3969 event->hw.flags |= c->flags; 3970 return c; 3971 } 3972 } 3973 } 3974 3975 return &hybrid_var(cpuc->pmu, unconstrained); 3976 } 3977 3978 static struct event_constraint * 3979 __intel_get_event_constraints(struct cpu_hw_events *cpuc, int idx, 3980 struct perf_event *event) 3981 { 3982 struct event_constraint *c; 3983 3984 c = intel_vlbr_constraints(event); 3985 if (c) 3986 return c; 3987 3988 c = intel_bts_constraints(event); 3989 if (c) 3990 return c; 3991 3992 c = intel_shared_regs_constraints(cpuc, event); 3993 if (c) 3994 return c; 3995 3996 c = intel_pebs_constraints(event); 3997 if (c) 3998 return c; 3999 4000 return x86_get_event_constraints(cpuc, idx, event); 4001 } 4002 4003 static void 4004 intel_start_scheduling(struct cpu_hw_events *cpuc) 4005 { 4006 struct intel_excl_cntrs *excl_cntrs = cpuc->excl_cntrs; 4007 struct intel_excl_states *xl; 4008 int tid = cpuc->excl_thread_id; 4009 4010 /* 4011 * nothing needed if in group validation mode 4012 */ 4013 if (cpuc->is_fake || !is_ht_workaround_enabled()) 4014 return; 4015 4016 /* 4017 * no exclusion needed 4018 */ 4019 if (WARN_ON_ONCE(!excl_cntrs)) 4020 return; 4021 4022 xl = &excl_cntrs->states[tid]; 4023 4024 xl->sched_started = true; 4025 /* 4026 * lock shared state until we are done scheduling 4027 * in stop_event_scheduling() 4028 * makes scheduling appear as a transaction 4029 */ 4030 raw_spin_lock(&excl_cntrs->lock); 4031 } 4032 4033 static void intel_commit_scheduling(struct cpu_hw_events *cpuc, int idx, int cntr) 4034 { 4035 struct intel_excl_cntrs *excl_cntrs = cpuc->excl_cntrs; 4036 struct event_constraint *c = cpuc->event_constraint[idx]; 4037 struct intel_excl_states *xl; 4038 int tid = cpuc->excl_thread_id; 4039 4040 if (cpuc->is_fake || !is_ht_workaround_enabled()) 4041 return; 4042 4043 if (WARN_ON_ONCE(!excl_cntrs)) 4044 return; 4045 4046 if (!(c->flags & PERF_X86_EVENT_DYNAMIC)) 4047 return; 4048 4049 xl = &excl_cntrs->states[tid]; 4050 4051 lockdep_assert_held(&excl_cntrs->lock); 4052 4053 if (c->flags & PERF_X86_EVENT_EXCL) 4054 xl->state[cntr] = INTEL_EXCL_EXCLUSIVE; 4055 else 4056 xl->state[cntr] = INTEL_EXCL_SHARED; 4057 } 4058 4059 static void 4060 intel_stop_scheduling(struct cpu_hw_events *cpuc) 4061 { 4062 struct intel_excl_cntrs *excl_cntrs = cpuc->excl_cntrs; 4063 struct intel_excl_states *xl; 4064 int tid = cpuc->excl_thread_id; 4065 4066 /* 4067 * nothing needed if in group validation mode 4068 */ 4069 if (cpuc->is_fake || !is_ht_workaround_enabled()) 4070 return; 4071 /* 4072 * no exclusion needed 4073 */ 4074 if (WARN_ON_ONCE(!excl_cntrs)) 4075 return; 4076 4077 xl = &excl_cntrs->states[tid]; 4078 4079 xl->sched_started = false; 4080 /* 4081 * release shared state lock (acquired in intel_start_scheduling()) 4082 */ 4083 raw_spin_unlock(&excl_cntrs->lock); 4084 } 4085 4086 static struct event_constraint * 4087 dyn_constraint(struct cpu_hw_events *cpuc, struct event_constraint *c, int idx) 4088 { 4089 WARN_ON_ONCE(!cpuc->constraint_list); 4090 4091 if (!(c->flags & PERF_X86_EVENT_DYNAMIC)) { 4092 struct event_constraint *cx; 4093 4094 /* 4095 * grab pre-allocated constraint entry 4096 */ 4097 cx = &cpuc->constraint_list[idx]; 4098 4099 /* 4100 * initialize dynamic constraint 4101 * with static constraint 4102 */ 4103 *cx = *c; 4104 4105 /* 4106 * mark constraint as dynamic 4107 */ 4108 cx->flags |= PERF_X86_EVENT_DYNAMIC; 4109 c = cx; 4110 } 4111 4112 return c; 4113 } 4114 4115 static struct event_constraint * 4116 intel_get_excl_constraints(struct cpu_hw_events *cpuc, struct perf_event *event, 4117 int idx, struct event_constraint *c) 4118 { 4119 struct intel_excl_cntrs *excl_cntrs = cpuc->excl_cntrs; 4120 struct intel_excl_states *xlo; 4121 int tid = cpuc->excl_thread_id; 4122 int is_excl, i, w; 4123 4124 /* 4125 * validating a group does not require 4126 * enforcing cross-thread exclusion 4127 */ 4128 if (cpuc->is_fake || !is_ht_workaround_enabled()) 4129 return c; 4130 4131 /* 4132 * no exclusion needed 4133 */ 4134 if (WARN_ON_ONCE(!excl_cntrs)) 4135 return c; 4136 4137 /* 4138 * because we modify the constraint, we need 4139 * to make a copy. Static constraints come 4140 * from static const tables. 4141 * 4142 * only needed when constraint has not yet 4143 * been cloned (marked dynamic) 4144 */ 4145 c = dyn_constraint(cpuc, c, idx); 4146 4147 /* 4148 * From here on, the constraint is dynamic. 4149 * Either it was just allocated above, or it 4150 * was allocated during a earlier invocation 4151 * of this function 4152 */ 4153 4154 /* 4155 * state of sibling HT 4156 */ 4157 xlo = &excl_cntrs->states[tid ^ 1]; 4158 4159 /* 4160 * event requires exclusive counter access 4161 * across HT threads 4162 */ 4163 is_excl = c->flags & PERF_X86_EVENT_EXCL; 4164 if (is_excl && !(event->hw.flags & PERF_X86_EVENT_EXCL_ACCT)) { 4165 event->hw.flags |= PERF_X86_EVENT_EXCL_ACCT; 4166 if (!cpuc->n_excl++) 4167 WRITE_ONCE(excl_cntrs->has_exclusive[tid], 1); 4168 } 4169 4170 /* 4171 * Modify static constraint with current dynamic 4172 * state of thread 4173 * 4174 * EXCLUSIVE: sibling counter measuring exclusive event 4175 * SHARED : sibling counter measuring non-exclusive event 4176 * UNUSED : sibling counter unused 4177 */ 4178 w = c->weight; 4179 for_each_set_bit(i, c->idxmsk, X86_PMC_IDX_MAX) { 4180 /* 4181 * exclusive event in sibling counter 4182 * our corresponding counter cannot be used 4183 * regardless of our event 4184 */ 4185 if (xlo->state[i] == INTEL_EXCL_EXCLUSIVE) { 4186 __clear_bit(i, c->idxmsk); 4187 w--; 4188 continue; 4189 } 4190 /* 4191 * if measuring an exclusive event, sibling 4192 * measuring non-exclusive, then counter cannot 4193 * be used 4194 */ 4195 if (is_excl && xlo->state[i] == INTEL_EXCL_SHARED) { 4196 __clear_bit(i, c->idxmsk); 4197 w--; 4198 continue; 4199 } 4200 } 4201 4202 /* 4203 * if we return an empty mask, then switch 4204 * back to static empty constraint to avoid 4205 * the cost of freeing later on 4206 */ 4207 if (!w) 4208 c = &emptyconstraint; 4209 4210 c->weight = w; 4211 4212 return c; 4213 } 4214 4215 static struct event_constraint * 4216 intel_get_event_constraints(struct cpu_hw_events *cpuc, int idx, 4217 struct perf_event *event) 4218 { 4219 struct event_constraint *c1, *c2; 4220 4221 c1 = cpuc->event_constraint[idx]; 4222 4223 /* 4224 * first time only 4225 * - static constraint: no change across incremental scheduling calls 4226 * - dynamic constraint: handled by intel_get_excl_constraints() 4227 */ 4228 c2 = __intel_get_event_constraints(cpuc, idx, event); 4229 if (c1) { 4230 WARN_ON_ONCE(!(c1->flags & PERF_X86_EVENT_DYNAMIC)); 4231 bitmap_copy(c1->idxmsk, c2->idxmsk, X86_PMC_IDX_MAX); 4232 c1->weight = c2->weight; 4233 c2 = c1; 4234 } 4235 4236 if (cpuc->excl_cntrs) 4237 return intel_get_excl_constraints(cpuc, event, idx, c2); 4238 4239 if (event->hw.dyn_constraint != ~0ULL) { 4240 c2 = dyn_constraint(cpuc, c2, idx); 4241 c2->idxmsk64 &= event->hw.dyn_constraint; 4242 c2->weight = hweight64(c2->idxmsk64); 4243 } 4244 4245 return c2; 4246 } 4247 4248 static void intel_put_excl_constraints(struct cpu_hw_events *cpuc, 4249 struct perf_event *event) 4250 { 4251 struct hw_perf_event *hwc = &event->hw; 4252 struct intel_excl_cntrs *excl_cntrs = cpuc->excl_cntrs; 4253 int tid = cpuc->excl_thread_id; 4254 struct intel_excl_states *xl; 4255 4256 /* 4257 * nothing needed if in group validation mode 4258 */ 4259 if (cpuc->is_fake) 4260 return; 4261 4262 if (WARN_ON_ONCE(!excl_cntrs)) 4263 return; 4264 4265 if (hwc->flags & PERF_X86_EVENT_EXCL_ACCT) { 4266 hwc->flags &= ~PERF_X86_EVENT_EXCL_ACCT; 4267 if (!--cpuc->n_excl) 4268 WRITE_ONCE(excl_cntrs->has_exclusive[tid], 0); 4269 } 4270 4271 /* 4272 * If event was actually assigned, then mark the counter state as 4273 * unused now. 4274 */ 4275 if (hwc->idx >= 0) { 4276 xl = &excl_cntrs->states[tid]; 4277 4278 /* 4279 * put_constraint may be called from x86_schedule_events() 4280 * which already has the lock held so here make locking 4281 * conditional. 4282 */ 4283 if (!xl->sched_started) 4284 raw_spin_lock(&excl_cntrs->lock); 4285 4286 xl->state[hwc->idx] = INTEL_EXCL_UNUSED; 4287 4288 if (!xl->sched_started) 4289 raw_spin_unlock(&excl_cntrs->lock); 4290 } 4291 } 4292 4293 static void 4294 intel_put_shared_regs_event_constraints(struct cpu_hw_events *cpuc, 4295 struct perf_event *event) 4296 { 4297 struct hw_perf_event_extra *reg; 4298 4299 reg = &event->hw.extra_reg; 4300 if (reg->idx != EXTRA_REG_NONE) 4301 __intel_shared_reg_put_constraints(cpuc, reg); 4302 4303 reg = &event->hw.branch_reg; 4304 if (reg->idx != EXTRA_REG_NONE) 4305 __intel_shared_reg_put_constraints(cpuc, reg); 4306 } 4307 4308 static void intel_put_event_constraints(struct cpu_hw_events *cpuc, 4309 struct perf_event *event) 4310 { 4311 intel_put_shared_regs_event_constraints(cpuc, event); 4312 4313 /* 4314 * is PMU has exclusive counter restrictions, then 4315 * all events are subject to and must call the 4316 * put_excl_constraints() routine 4317 */ 4318 if (cpuc->excl_cntrs) 4319 intel_put_excl_constraints(cpuc, event); 4320 } 4321 4322 static void intel_pebs_aliases_core2(struct perf_event *event) 4323 { 4324 if ((event->hw.config & X86_RAW_EVENT_MASK) == 0x003c) { 4325 /* 4326 * Use an alternative encoding for CPU_CLK_UNHALTED.THREAD_P 4327 * (0x003c) so that we can use it with PEBS. 4328 * 4329 * The regular CPU_CLK_UNHALTED.THREAD_P event (0x003c) isn't 4330 * PEBS capable. However we can use INST_RETIRED.ANY_P 4331 * (0x00c0), which is a PEBS capable event, to get the same 4332 * count. 4333 * 4334 * INST_RETIRED.ANY_P counts the number of cycles that retires 4335 * CNTMASK instructions. By setting CNTMASK to a value (16) 4336 * larger than the maximum number of instructions that can be 4337 * retired per cycle (4) and then inverting the condition, we 4338 * count all cycles that retire 16 or less instructions, which 4339 * is every cycle. 4340 * 4341 * Thereby we gain a PEBS capable cycle counter. 4342 */ 4343 u64 alt_config = X86_CONFIG(.event=0xc0, .inv=1, .cmask=16); 4344 4345 alt_config |= (event->hw.config & ~X86_RAW_EVENT_MASK); 4346 event->hw.config = alt_config; 4347 } 4348 } 4349 4350 static void intel_pebs_aliases_snb(struct perf_event *event) 4351 { 4352 if ((event->hw.config & X86_RAW_EVENT_MASK) == 0x003c) { 4353 /* 4354 * Use an alternative encoding for CPU_CLK_UNHALTED.THREAD_P 4355 * (0x003c) so that we can use it with PEBS. 4356 * 4357 * The regular CPU_CLK_UNHALTED.THREAD_P event (0x003c) isn't 4358 * PEBS capable. However we can use UOPS_RETIRED.ALL 4359 * (0x01c2), which is a PEBS capable event, to get the same 4360 * count. 4361 * 4362 * UOPS_RETIRED.ALL counts the number of cycles that retires 4363 * CNTMASK micro-ops. By setting CNTMASK to a value (16) 4364 * larger than the maximum number of micro-ops that can be 4365 * retired per cycle (4) and then inverting the condition, we 4366 * count all cycles that retire 16 or less micro-ops, which 4367 * is every cycle. 4368 * 4369 * Thereby we gain a PEBS capable cycle counter. 4370 */ 4371 u64 alt_config = X86_CONFIG(.event=0xc2, .umask=0x01, .inv=1, .cmask=16); 4372 4373 alt_config |= (event->hw.config & ~X86_RAW_EVENT_MASK); 4374 event->hw.config = alt_config; 4375 } 4376 } 4377 4378 static void intel_pebs_aliases_precdist(struct perf_event *event) 4379 { 4380 if ((event->hw.config & X86_RAW_EVENT_MASK) == 0x003c) { 4381 /* 4382 * Use an alternative encoding for CPU_CLK_UNHALTED.THREAD_P 4383 * (0x003c) so that we can use it with PEBS. 4384 * 4385 * The regular CPU_CLK_UNHALTED.THREAD_P event (0x003c) isn't 4386 * PEBS capable. However we can use INST_RETIRED.PREC_DIST 4387 * (0x01c0), which is a PEBS capable event, to get the same 4388 * count. 4389 * 4390 * The PREC_DIST event has special support to minimize sample 4391 * shadowing effects. One drawback is that it can be 4392 * only programmed on counter 1, but that seems like an 4393 * acceptable trade off. 4394 */ 4395 u64 alt_config = X86_CONFIG(.event=0xc0, .umask=0x01, .inv=1, .cmask=16); 4396 4397 alt_config |= (event->hw.config & ~X86_RAW_EVENT_MASK); 4398 event->hw.config = alt_config; 4399 } 4400 } 4401 4402 static void intel_pebs_aliases_ivb(struct perf_event *event) 4403 { 4404 if (event->attr.precise_ip < 3) 4405 return intel_pebs_aliases_snb(event); 4406 return intel_pebs_aliases_precdist(event); 4407 } 4408 4409 static void intel_pebs_aliases_skl(struct perf_event *event) 4410 { 4411 if (event->attr.precise_ip < 3) 4412 return intel_pebs_aliases_core2(event); 4413 return intel_pebs_aliases_precdist(event); 4414 } 4415 4416 static unsigned long intel_pmu_large_pebs_flags(struct perf_event *event) 4417 { 4418 unsigned long flags = x86_pmu.large_pebs_flags; 4419 4420 if (event->attr.use_clockid) 4421 flags &= ~PERF_SAMPLE_TIME; 4422 if (!event->attr.exclude_kernel) 4423 flags &= ~PERF_SAMPLE_REGS_USER; 4424 if (event->attr.sample_regs_user & ~PEBS_GP_REGS) 4425 flags &= ~PERF_SAMPLE_REGS_USER; 4426 if (event->attr.sample_regs_intr & ~PEBS_GP_REGS) 4427 flags &= ~PERF_SAMPLE_REGS_INTR; 4428 return flags; 4429 } 4430 4431 static int intel_pmu_bts_config(struct perf_event *event) 4432 { 4433 struct perf_event_attr *attr = &event->attr; 4434 4435 if (unlikely(intel_pmu_has_bts(event))) { 4436 /* BTS is not supported by this architecture. */ 4437 if (!x86_pmu.bts_active) 4438 return -EOPNOTSUPP; 4439 4440 /* BTS is currently only allowed for user-mode. */ 4441 if (!attr->exclude_kernel) 4442 return -EOPNOTSUPP; 4443 4444 /* BTS is not allowed for precise events. */ 4445 if (attr->precise_ip) 4446 return -EOPNOTSUPP; 4447 4448 /* disallow bts if conflicting events are present */ 4449 if (x86_add_exclusive(x86_lbr_exclusive_lbr)) 4450 return -EBUSY; 4451 4452 event->destroy = hw_perf_lbr_event_destroy; 4453 } 4454 4455 return 0; 4456 } 4457 4458 static int core_pmu_hw_config(struct perf_event *event) 4459 { 4460 int ret = x86_pmu_hw_config(event); 4461 4462 if (ret) 4463 return ret; 4464 4465 return intel_pmu_bts_config(event); 4466 } 4467 4468 #define INTEL_TD_METRIC_AVAILABLE_MAX (INTEL_TD_METRIC_RETIRING + \ 4469 ((x86_pmu.num_topdown_events - 1) << 8)) 4470 4471 static bool is_available_metric_event(struct perf_event *event) 4472 { 4473 return is_metric_event(event) && 4474 event->attr.config <= INTEL_TD_METRIC_AVAILABLE_MAX; 4475 } 4476 4477 static inline bool is_mem_loads_event(struct perf_event *event) 4478 { 4479 return (event->attr.config & INTEL_ARCH_EVENT_MASK) == X86_CONFIG(.event=0xcd, .umask=0x01); 4480 } 4481 4482 static inline bool is_mem_loads_aux_event(struct perf_event *event) 4483 { 4484 return (event->attr.config & INTEL_ARCH_EVENT_MASK) == X86_CONFIG(.event=0x03, .umask=0x82); 4485 } 4486 4487 static inline bool require_mem_loads_aux_event(struct perf_event *event) 4488 { 4489 if (!(x86_pmu.flags & PMU_FL_MEM_LOADS_AUX)) 4490 return false; 4491 4492 if (is_hybrid()) 4493 return hybrid_pmu(event->pmu)->pmu_type == hybrid_big; 4494 4495 return true; 4496 } 4497 4498 static inline bool intel_pmu_has_cap(struct perf_event *event, int idx) 4499 { 4500 union perf_capabilities *intel_cap = &hybrid(event->pmu, intel_cap); 4501 4502 return test_bit(idx, (unsigned long *)&intel_cap->capabilities); 4503 } 4504 4505 static u64 intel_pmu_freq_start_period(struct perf_event *event) 4506 { 4507 int type = event->attr.type; 4508 u64 config, factor; 4509 s64 start; 4510 4511 /* 4512 * The 127 is the lowest possible recommended SAV (sample after value) 4513 * for a 4000 freq (default freq), according to the event list JSON file. 4514 * Also, assume the workload is idle 50% time. 4515 */ 4516 factor = 64 * 4000; 4517 if (type != PERF_TYPE_HARDWARE && type != PERF_TYPE_HW_CACHE) 4518 goto end; 4519 4520 /* 4521 * The estimation of the start period in the freq mode is 4522 * based on the below assumption. 4523 * 4524 * For a cycles or an instructions event, 1GHZ of the 4525 * underlying platform, 1 IPC. The workload is idle 50% time. 4526 * The start period = 1,000,000,000 * 1 / freq / 2. 4527 * = 500,000,000 / freq 4528 * 4529 * Usually, the branch-related events occur less than the 4530 * instructions event. According to the Intel event list JSON 4531 * file, the SAV (sample after value) of a branch-related event 4532 * is usually 1/4 of an instruction event. 4533 * The start period of branch-related events = 125,000,000 / freq. 4534 * 4535 * The cache-related events occurs even less. The SAV is usually 4536 * 1/20 of an instruction event. 4537 * The start period of cache-related events = 25,000,000 / freq. 4538 */ 4539 config = event->attr.config & PERF_HW_EVENT_MASK; 4540 if (type == PERF_TYPE_HARDWARE) { 4541 switch (config) { 4542 case PERF_COUNT_HW_CPU_CYCLES: 4543 case PERF_COUNT_HW_INSTRUCTIONS: 4544 case PERF_COUNT_HW_BUS_CYCLES: 4545 case PERF_COUNT_HW_STALLED_CYCLES_FRONTEND: 4546 case PERF_COUNT_HW_STALLED_CYCLES_BACKEND: 4547 case PERF_COUNT_HW_REF_CPU_CYCLES: 4548 factor = 500000000; 4549 break; 4550 case PERF_COUNT_HW_BRANCH_INSTRUCTIONS: 4551 case PERF_COUNT_HW_BRANCH_MISSES: 4552 factor = 125000000; 4553 break; 4554 case PERF_COUNT_HW_CACHE_REFERENCES: 4555 case PERF_COUNT_HW_CACHE_MISSES: 4556 factor = 25000000; 4557 break; 4558 default: 4559 goto end; 4560 } 4561 } 4562 4563 if (type == PERF_TYPE_HW_CACHE) 4564 factor = 25000000; 4565 end: 4566 /* 4567 * Usually, a prime or a number with less factors (close to prime) 4568 * is chosen as an SAV, which makes it less likely that the sampling 4569 * period synchronizes with some periodic event in the workload. 4570 * Minus 1 to make it at least avoiding values near power of twos 4571 * for the default freq. 4572 */ 4573 start = DIV_ROUND_UP_ULL(factor, event->attr.sample_freq) - 1; 4574 4575 if (start > x86_pmu.max_period) 4576 start = x86_pmu.max_period; 4577 4578 if (x86_pmu.limit_period) 4579 x86_pmu.limit_period(event, &start); 4580 4581 return start; 4582 } 4583 4584 static inline bool intel_pmu_has_acr(struct pmu *pmu) 4585 { 4586 return !!hybrid(pmu, acr_cause_mask64); 4587 } 4588 4589 static bool intel_pmu_is_acr_group(struct perf_event *event) 4590 { 4591 /* The group leader has the ACR flag set */ 4592 if (is_acr_event_group(event)) 4593 return true; 4594 4595 /* The acr_mask is set */ 4596 if (event->attr.config2) 4597 return true; 4598 4599 return false; 4600 } 4601 4602 static inline bool intel_pmu_has_pebs_counter_group(struct pmu *pmu) 4603 { 4604 u64 caps; 4605 4606 if (x86_pmu.intel_cap.pebs_format >= 6 && x86_pmu.intel_cap.pebs_baseline) 4607 return true; 4608 4609 caps = hybrid(pmu, arch_pebs_cap).caps; 4610 if (x86_pmu.arch_pebs && (caps & ARCH_PEBS_CNTR_MASK)) 4611 return true; 4612 4613 return false; 4614 } 4615 4616 static inline void intel_pmu_set_acr_cntr_constr(struct perf_event *event, 4617 u64 *cause_mask, int *num) 4618 { 4619 event->hw.dyn_constraint &= hybrid(event->pmu, acr_cntr_mask64); 4620 *cause_mask |= event->attr.config2; 4621 *num += 1; 4622 } 4623 4624 static inline void intel_pmu_set_acr_caused_constr(struct perf_event *event, 4625 int idx, u64 cause_mask) 4626 { 4627 if (test_bit(idx, (unsigned long *)&cause_mask)) 4628 event->hw.dyn_constraint &= hybrid(event->pmu, acr_cause_mask64); 4629 } 4630 4631 static inline int intel_set_branch_counter_constr(struct perf_event *event, 4632 int *num) 4633 { 4634 if (branch_sample_call_stack(event)) 4635 return -EINVAL; 4636 if (branch_sample_counters(event)) { 4637 (*num)++; 4638 event->hw.dyn_constraint &= x86_pmu.lbr_counters; 4639 } 4640 4641 return 0; 4642 } 4643 4644 static int intel_pmu_hw_config(struct perf_event *event) 4645 { 4646 int ret = x86_pmu_hw_config(event); 4647 4648 if (ret) 4649 return ret; 4650 4651 ret = intel_pmu_bts_config(event); 4652 if (ret) 4653 return ret; 4654 4655 if (event->attr.freq && event->attr.sample_freq) { 4656 event->hw.sample_period = intel_pmu_freq_start_period(event); 4657 event->hw.last_period = event->hw.sample_period; 4658 local64_set(&event->hw.period_left, event->hw.sample_period); 4659 } 4660 4661 if (event->attr.precise_ip) { 4662 struct arch_pebs_cap pebs_cap = hybrid(event->pmu, arch_pebs_cap); 4663 4664 if ((event->attr.config & INTEL_ARCH_EVENT_MASK) == INTEL_FIXED_VLBR_EVENT) 4665 return -EINVAL; 4666 4667 if (!(event->attr.freq || (event->attr.wakeup_events && !event->attr.watermark))) { 4668 event->hw.flags |= PERF_X86_EVENT_AUTO_RELOAD; 4669 if (!(event->attr.sample_type & ~intel_pmu_large_pebs_flags(event)) && 4670 !has_aux_action(event)) { 4671 event->hw.flags |= PERF_X86_EVENT_LARGE_PEBS; 4672 event->attach_state |= PERF_ATTACH_SCHED_CB; 4673 } 4674 } 4675 if (x86_pmu.pebs_aliases) 4676 x86_pmu.pebs_aliases(event); 4677 4678 if (x86_pmu.arch_pebs) { 4679 u64 cntr_mask = hybrid(event->pmu, intel_ctrl) & 4680 ~GLOBAL_CTRL_EN_PERF_METRICS; 4681 u64 pebs_mask = event->attr.precise_ip >= 3 ? 4682 pebs_cap.pdists : pebs_cap.counters; 4683 if (cntr_mask != pebs_mask) 4684 event->hw.dyn_constraint &= pebs_mask; 4685 } 4686 } 4687 4688 if (needs_branch_stack(event)) { 4689 /* Avoid branch stack setup for counting events in SAMPLE READ */ 4690 if (is_sampling_event(event) || 4691 !(event->attr.sample_type & PERF_SAMPLE_READ)) 4692 event->hw.flags |= PERF_X86_EVENT_NEEDS_BRANCH_STACK; 4693 } 4694 4695 if (branch_sample_counters(event)) { 4696 struct perf_event *leader, *sibling; 4697 int num = 0; 4698 4699 if (!(x86_pmu.flags & PMU_FL_BR_CNTR) || 4700 (event->attr.config & ~INTEL_ARCH_EVENT_MASK)) 4701 return -EINVAL; 4702 4703 /* 4704 * The branch counter logging is not supported in the call stack 4705 * mode yet, since we cannot simply flush the LBR during e.g., 4706 * multiplexing. Also, there is no obvious usage with the call 4707 * stack mode. Simply forbids it for now. 4708 * 4709 * If any events in the group enable the branch counter logging 4710 * feature, the group is treated as a branch counter logging 4711 * group, which requires the extra space to store the counters. 4712 */ 4713 leader = event->group_leader; 4714 if (intel_set_branch_counter_constr(leader, &num)) 4715 return -EINVAL; 4716 leader->hw.flags |= PERF_X86_EVENT_BRANCH_COUNTERS; 4717 4718 for_each_sibling_event(sibling, leader) { 4719 if (intel_set_branch_counter_constr(sibling, &num)) 4720 return -EINVAL; 4721 } 4722 4723 /* event isn't installed as a sibling yet. */ 4724 if (event != leader) { 4725 if (intel_set_branch_counter_constr(event, &num)) 4726 return -EINVAL; 4727 } 4728 4729 if (num > fls(x86_pmu.lbr_counters)) 4730 return -EINVAL; 4731 /* 4732 * Only applying the PERF_SAMPLE_BRANCH_COUNTERS doesn't 4733 * require any branch stack setup. 4734 * Clear the bit to avoid unnecessary branch stack setup. 4735 */ 4736 if (0 == (event->attr.branch_sample_type & 4737 ~(PERF_SAMPLE_BRANCH_PLM_ALL | 4738 PERF_SAMPLE_BRANCH_COUNTERS))) 4739 event->hw.flags &= ~PERF_X86_EVENT_NEEDS_BRANCH_STACK; 4740 4741 /* 4742 * Force the leader to be a LBR event. So LBRs can be reset 4743 * with the leader event. See intel_pmu_lbr_del() for details. 4744 */ 4745 if (!intel_pmu_needs_branch_stack(leader)) 4746 return -EINVAL; 4747 } 4748 4749 if (intel_pmu_needs_branch_stack(event)) { 4750 ret = intel_pmu_setup_lbr_filter(event); 4751 if (ret) 4752 return ret; 4753 event->attach_state |= PERF_ATTACH_SCHED_CB; 4754 4755 /* 4756 * BTS is set up earlier in this path, so don't account twice 4757 */ 4758 if (!unlikely(intel_pmu_has_bts(event))) { 4759 /* disallow lbr if conflicting events are present */ 4760 if (x86_add_exclusive(x86_lbr_exclusive_lbr)) 4761 return -EBUSY; 4762 4763 event->destroy = hw_perf_lbr_event_destroy; 4764 } 4765 } 4766 4767 if (event->attr.aux_output) { 4768 if (!event->attr.precise_ip) 4769 return -EINVAL; 4770 4771 event->hw.flags |= PERF_X86_EVENT_PEBS_VIA_PT; 4772 } 4773 4774 if ((event->attr.sample_type & PERF_SAMPLE_READ) && 4775 intel_pmu_has_pebs_counter_group(event->pmu) && 4776 is_sampling_event(event) && 4777 event->attr.precise_ip) 4778 event->group_leader->hw.flags |= PERF_X86_EVENT_PEBS_CNTR; 4779 4780 if (intel_pmu_has_acr(event->pmu) && intel_pmu_is_acr_group(event)) { 4781 struct perf_event *sibling, *leader = event->group_leader; 4782 struct pmu *pmu = event->pmu; 4783 bool has_sw_event = false; 4784 int num = 0, idx = 0; 4785 u64 cause_mask = 0; 4786 4787 /* Not support perf metrics */ 4788 if (is_metric_event(event)) 4789 return -EINVAL; 4790 4791 /* Not support freq mode */ 4792 if (event->attr.freq) 4793 return -EINVAL; 4794 4795 /* PDist is not supported */ 4796 if (event->attr.config2 && event->attr.precise_ip > 2) 4797 return -EINVAL; 4798 4799 /* The reload value cannot exceeds the max period */ 4800 if (event->attr.sample_period > x86_pmu.max_period) 4801 return -EINVAL; 4802 /* 4803 * The counter-constraints of each event cannot be finalized 4804 * unless the whole group is scanned. However, it's hard 4805 * to know whether the event is the last one of the group. 4806 * Recalculate the counter-constraints for each event when 4807 * adding a new event. 4808 * 4809 * The group is traversed twice, which may be optimized later. 4810 * In the first round, 4811 * - Find all events which do reload when other events 4812 * overflow and set the corresponding counter-constraints 4813 * - Add all events, which can cause other events reload, 4814 * in the cause_mask 4815 * - Error out if the number of events exceeds the HW limit 4816 * - The ACR events must be contiguous. 4817 * Error out if there are non-X86 events between ACR events. 4818 * This is not a HW limit, but a SW limit. 4819 * With the assumption, the intel_pmu_acr_late_setup() can 4820 * easily convert the event idx to counter idx without 4821 * traversing the whole event list. 4822 */ 4823 if (!is_x86_event(leader)) 4824 return -EINVAL; 4825 4826 if (leader->attr.config2) 4827 intel_pmu_set_acr_cntr_constr(leader, &cause_mask, &num); 4828 4829 if (leader->nr_siblings) { 4830 for_each_sibling_event(sibling, leader) { 4831 if (!is_x86_event(sibling)) { 4832 has_sw_event = true; 4833 continue; 4834 } 4835 if (!sibling->attr.config2) 4836 continue; 4837 if (has_sw_event) 4838 return -EINVAL; 4839 intel_pmu_set_acr_cntr_constr(sibling, &cause_mask, &num); 4840 } 4841 } 4842 if (leader != event && event->attr.config2) { 4843 if (has_sw_event) 4844 return -EINVAL; 4845 intel_pmu_set_acr_cntr_constr(event, &cause_mask, &num); 4846 } 4847 4848 if (hweight64(cause_mask) > hweight64(hybrid(pmu, acr_cause_mask64)) || 4849 num > hweight64(hybrid(event->pmu, acr_cntr_mask64))) 4850 return -EINVAL; 4851 /* 4852 * In the second round, apply the counter-constraints for 4853 * the events which can cause other events reload. 4854 */ 4855 intel_pmu_set_acr_caused_constr(leader, idx++, cause_mask); 4856 4857 if (leader->nr_siblings) { 4858 for_each_sibling_event(sibling, leader) 4859 intel_pmu_set_acr_caused_constr(sibling, idx++, cause_mask); 4860 } 4861 4862 if (leader != event) 4863 intel_pmu_set_acr_caused_constr(event, idx, cause_mask); 4864 4865 leader->hw.flags |= PERF_X86_EVENT_ACR; 4866 } 4867 4868 if ((event->attr.type == PERF_TYPE_HARDWARE) || 4869 (event->attr.type == PERF_TYPE_HW_CACHE)) 4870 return 0; 4871 4872 /* 4873 * Config Topdown slots and metric events 4874 * 4875 * The slots event on Fixed Counter 3 can support sampling, 4876 * which will be handled normally in x86_perf_event_update(). 4877 * 4878 * Metric events don't support sampling and require being paired 4879 * with a slots event as group leader. When the slots event 4880 * is used in a metrics group, it too cannot support sampling. 4881 */ 4882 if (intel_pmu_has_cap(event, PERF_CAP_METRICS_IDX) && is_topdown_event(event)) { 4883 /* The metrics_clear can only be set for the slots event */ 4884 if (event->attr.config1 && 4885 (!is_slots_event(event) || (event->attr.config1 & ~INTEL_TD_CFG_METRIC_CLEAR))) 4886 return -EINVAL; 4887 4888 if (event->attr.config2) 4889 return -EINVAL; 4890 4891 /* 4892 * The TopDown metrics events and slots event don't 4893 * support any filters. 4894 */ 4895 if (event->attr.config & X86_ALL_EVENT_FLAGS) 4896 return -EINVAL; 4897 4898 if (is_available_metric_event(event)) { 4899 struct perf_event *leader = event->group_leader; 4900 4901 /* The metric events don't support sampling. */ 4902 if (is_sampling_event(event)) 4903 return -EINVAL; 4904 4905 /* The metric events require a slots group leader. */ 4906 if (!is_slots_event(leader)) 4907 return -EINVAL; 4908 4909 /* 4910 * The leader/SLOTS must not be a sampling event for 4911 * metric use; hardware requires it starts at 0 when used 4912 * in conjunction with MSR_PERF_METRICS. 4913 */ 4914 if (is_sampling_event(leader)) 4915 return -EINVAL; 4916 4917 event->event_caps |= PERF_EV_CAP_SIBLING; 4918 /* 4919 * Only once we have a METRICs sibling do we 4920 * need TopDown magic. 4921 */ 4922 leader->hw.flags |= PERF_X86_EVENT_TOPDOWN; 4923 event->hw.flags |= PERF_X86_EVENT_TOPDOWN; 4924 } 4925 } 4926 4927 /* 4928 * The load latency event X86_CONFIG(.event=0xcd, .umask=0x01) on SPR 4929 * doesn't function quite right. As a work-around it needs to always be 4930 * co-scheduled with a auxiliary event X86_CONFIG(.event=0x03, .umask=0x82). 4931 * The actual count of this second event is irrelevant it just needs 4932 * to be active to make the first event function correctly. 4933 * 4934 * In a group, the auxiliary event must be in front of the load latency 4935 * event. The rule is to simplify the implementation of the check. 4936 * That's because perf cannot have a complete group at the moment. 4937 */ 4938 if (require_mem_loads_aux_event(event) && 4939 (event->attr.sample_type & PERF_SAMPLE_DATA_SRC) && 4940 is_mem_loads_event(event)) { 4941 struct perf_event *leader = event->group_leader; 4942 struct perf_event *sibling = NULL; 4943 4944 /* 4945 * When this memload event is also the first event (no group 4946 * exists yet), then there is no aux event before it. 4947 */ 4948 if (leader == event) 4949 return -ENODATA; 4950 4951 if (!is_mem_loads_aux_event(leader)) { 4952 for_each_sibling_event(sibling, leader) { 4953 if (is_mem_loads_aux_event(sibling)) 4954 break; 4955 } 4956 if (list_entry_is_head(sibling, &leader->sibling_list, sibling_list)) 4957 return -ENODATA; 4958 } 4959 } 4960 4961 if (!(event->attr.config & ARCH_PERFMON_EVENTSEL_ANY)) 4962 return 0; 4963 4964 if (x86_pmu.version < 3) 4965 return -EINVAL; 4966 4967 ret = perf_allow_cpu(); 4968 if (ret) 4969 return ret; 4970 4971 event->hw.config |= ARCH_PERFMON_EVENTSEL_ANY; 4972 4973 return 0; 4974 } 4975 4976 /* 4977 * Currently, the only caller of this function is the atomic_switch_perf_msrs(). 4978 * The host perf context helps to prepare the values of the real hardware for 4979 * a set of msrs that need to be switched atomically in a vmx transaction. 4980 * 4981 * For example, the pseudocode needed to add a new msr should look like: 4982 * 4983 * arr[(*nr)++] = (struct perf_guest_switch_msr){ 4984 * .msr = the hardware msr address, 4985 * .host = the value the hardware has when it doesn't run a guest, 4986 * .guest = the value the hardware has when it runs a guest, 4987 * }; 4988 * 4989 * These values have nothing to do with the emulated values the guest sees 4990 * when it uses {RD,WR}MSR, which should be handled by the KVM context, 4991 * specifically in the intel_pmu_{get,set}_msr(). 4992 */ 4993 static struct perf_guest_switch_msr *intel_guest_get_msrs(int *nr, void *data) 4994 { 4995 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events); 4996 struct perf_guest_switch_msr *arr = cpuc->guest_switch_msrs; 4997 struct kvm_pmu *kvm_pmu = (struct kvm_pmu *)data; 4998 u64 intel_ctrl = hybrid(cpuc->pmu, intel_ctrl); 4999 u64 pebs_mask = cpuc->pebs_enabled & x86_pmu.pebs_capable; 5000 int global_ctrl, pebs_enable; 5001 5002 /* 5003 * In addition to obeying exclude_guest/exclude_host, remove bits being 5004 * used for PEBS when running a guest, because PEBS writes to virtual 5005 * addresses (not physical addresses). 5006 */ 5007 *nr = 0; 5008 global_ctrl = (*nr)++; 5009 arr[global_ctrl] = (struct perf_guest_switch_msr){ 5010 .msr = MSR_CORE_PERF_GLOBAL_CTRL, 5011 .host = intel_ctrl & ~cpuc->intel_ctrl_guest_mask, 5012 .guest = intel_ctrl & ~cpuc->intel_ctrl_host_mask & ~pebs_mask, 5013 }; 5014 5015 if (!x86_pmu.ds_pebs) 5016 return arr; 5017 5018 /* 5019 * If PMU counter has PEBS enabled it is not enough to 5020 * disable counter on a guest entry since PEBS memory 5021 * write can overshoot guest entry and corrupt guest 5022 * memory. Disabling PEBS solves the problem. 5023 * 5024 * Don't do this if the CPU already enforces it. 5025 */ 5026 if (x86_pmu.pebs_no_isolation) { 5027 arr[(*nr)++] = (struct perf_guest_switch_msr){ 5028 .msr = MSR_IA32_PEBS_ENABLE, 5029 .host = cpuc->pebs_enabled, 5030 .guest = 0, 5031 }; 5032 return arr; 5033 } 5034 5035 if (!kvm_pmu || !x86_pmu.pebs_ept) 5036 return arr; 5037 5038 arr[(*nr)++] = (struct perf_guest_switch_msr){ 5039 .msr = MSR_IA32_DS_AREA, 5040 .host = (unsigned long)cpuc->ds, 5041 .guest = kvm_pmu->ds_area, 5042 }; 5043 5044 if (x86_pmu.intel_cap.pebs_baseline) { 5045 arr[(*nr)++] = (struct perf_guest_switch_msr){ 5046 .msr = MSR_PEBS_DATA_CFG, 5047 .host = cpuc->active_pebs_data_cfg, 5048 .guest = kvm_pmu->pebs_data_cfg, 5049 }; 5050 } 5051 5052 pebs_enable = (*nr)++; 5053 arr[pebs_enable] = (struct perf_guest_switch_msr){ 5054 .msr = MSR_IA32_PEBS_ENABLE, 5055 .host = cpuc->pebs_enabled & ~cpuc->intel_ctrl_guest_mask, 5056 .guest = pebs_mask & ~cpuc->intel_ctrl_host_mask & kvm_pmu->pebs_enable, 5057 }; 5058 5059 if (arr[pebs_enable].host) { 5060 /* Disable guest PEBS if host PEBS is enabled. */ 5061 arr[pebs_enable].guest = 0; 5062 } else { 5063 /* Disable guest PEBS thoroughly for cross-mapped PEBS counters. */ 5064 arr[pebs_enable].guest &= ~kvm_pmu->host_cross_mapped_mask; 5065 arr[global_ctrl].guest &= ~kvm_pmu->host_cross_mapped_mask; 5066 /* Set hw GLOBAL_CTRL bits for PEBS counter when it runs for guest */ 5067 arr[global_ctrl].guest |= arr[pebs_enable].guest; 5068 } 5069 5070 return arr; 5071 } 5072 5073 static struct perf_guest_switch_msr *core_guest_get_msrs(int *nr, void *data) 5074 { 5075 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events); 5076 struct perf_guest_switch_msr *arr = cpuc->guest_switch_msrs; 5077 int idx; 5078 5079 for_each_set_bit(idx, x86_pmu.cntr_mask, X86_PMC_IDX_MAX) { 5080 struct perf_event *event = cpuc->events[idx]; 5081 5082 arr[idx].msr = x86_pmu_config_addr(idx); 5083 arr[idx].host = arr[idx].guest = 0; 5084 5085 if (!test_bit(idx, cpuc->active_mask)) 5086 continue; 5087 5088 arr[idx].host = arr[idx].guest = 5089 event->hw.config | ARCH_PERFMON_EVENTSEL_ENABLE; 5090 5091 if (event->attr.exclude_host) 5092 arr[idx].host &= ~ARCH_PERFMON_EVENTSEL_ENABLE; 5093 else if (event->attr.exclude_guest) 5094 arr[idx].guest &= ~ARCH_PERFMON_EVENTSEL_ENABLE; 5095 } 5096 5097 *nr = x86_pmu_max_num_counters(cpuc->pmu); 5098 return arr; 5099 } 5100 5101 static void core_pmu_enable_event(struct perf_event *event) 5102 { 5103 if (!event->attr.exclude_host) 5104 x86_pmu_enable_event(event); 5105 } 5106 5107 static void core_pmu_enable_all(int added) 5108 { 5109 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events); 5110 int idx; 5111 5112 for_each_set_bit(idx, x86_pmu.cntr_mask, X86_PMC_IDX_MAX) { 5113 struct hw_perf_event *hwc = &cpuc->events[idx]->hw; 5114 5115 if (!test_bit(idx, cpuc->active_mask) || 5116 cpuc->events[idx]->attr.exclude_host) 5117 continue; 5118 5119 __x86_pmu_enable_event(hwc, ARCH_PERFMON_EVENTSEL_ENABLE); 5120 } 5121 } 5122 5123 static int hsw_hw_config(struct perf_event *event) 5124 { 5125 int ret = intel_pmu_hw_config(event); 5126 5127 if (ret) 5128 return ret; 5129 if (!boot_cpu_has(X86_FEATURE_RTM) && !boot_cpu_has(X86_FEATURE_HLE)) 5130 return 0; 5131 event->hw.config |= event->attr.config & (HSW_IN_TX|HSW_IN_TX_CHECKPOINTED); 5132 5133 /* 5134 * IN_TX/IN_TX-CP filters are not supported by the Haswell PMU with 5135 * PEBS or in ANY thread mode. Since the results are non-sensical forbid 5136 * this combination. 5137 */ 5138 if ((event->hw.config & (HSW_IN_TX|HSW_IN_TX_CHECKPOINTED)) && 5139 ((event->hw.config & ARCH_PERFMON_EVENTSEL_ANY) || 5140 event->attr.precise_ip > 0)) 5141 return -EOPNOTSUPP; 5142 5143 if (event_is_checkpointed(event)) { 5144 /* 5145 * Sampling of checkpointed events can cause situations where 5146 * the CPU constantly aborts because of a overflow, which is 5147 * then checkpointed back and ignored. Forbid checkpointing 5148 * for sampling. 5149 * 5150 * But still allow a long sampling period, so that perf stat 5151 * from KVM works. 5152 */ 5153 if (event->attr.sample_period > 0 && 5154 event->attr.sample_period < 0x7fffffff) 5155 return -EOPNOTSUPP; 5156 } 5157 return 0; 5158 } 5159 5160 static struct event_constraint counter0_constraint = 5161 INTEL_ALL_EVENT_CONSTRAINT(0, 0x1); 5162 5163 static struct event_constraint counter1_constraint = 5164 INTEL_ALL_EVENT_CONSTRAINT(0, 0x2); 5165 5166 static struct event_constraint counter0_1_constraint = 5167 INTEL_ALL_EVENT_CONSTRAINT(0, 0x3); 5168 5169 static struct event_constraint counter2_constraint = 5170 EVENT_CONSTRAINT(0, 0x4, 0); 5171 5172 static struct event_constraint fixed0_constraint = 5173 FIXED_EVENT_CONSTRAINT(0x00c0, 0); 5174 5175 static struct event_constraint fixed0_counter0_constraint = 5176 INTEL_ALL_EVENT_CONSTRAINT(0, 0x100000001ULL); 5177 5178 static struct event_constraint fixed0_counter0_1_constraint = 5179 INTEL_ALL_EVENT_CONSTRAINT(0, 0x100000003ULL); 5180 5181 static struct event_constraint counters_1_7_constraint = 5182 INTEL_ALL_EVENT_CONSTRAINT(0, 0xfeULL); 5183 5184 static struct event_constraint * 5185 hsw_get_event_constraints(struct cpu_hw_events *cpuc, int idx, 5186 struct perf_event *event) 5187 { 5188 struct event_constraint *c; 5189 5190 c = intel_get_event_constraints(cpuc, idx, event); 5191 5192 /* Handle special quirk on in_tx_checkpointed only in counter 2 */ 5193 if (event->hw.config & HSW_IN_TX_CHECKPOINTED) { 5194 if (c->idxmsk64 & (1U << 2)) 5195 return &counter2_constraint; 5196 return &emptyconstraint; 5197 } 5198 5199 return c; 5200 } 5201 5202 static struct event_constraint * 5203 icl_get_event_constraints(struct cpu_hw_events *cpuc, int idx, 5204 struct perf_event *event) 5205 { 5206 /* 5207 * Fixed counter 0 has less skid. 5208 * Force instruction:ppp in Fixed counter 0 5209 */ 5210 if ((event->attr.precise_ip == 3) && 5211 constraint_match(&fixed0_constraint, event->hw.config)) 5212 return &fixed0_constraint; 5213 5214 return hsw_get_event_constraints(cpuc, idx, event); 5215 } 5216 5217 static struct event_constraint * 5218 glc_get_event_constraints(struct cpu_hw_events *cpuc, int idx, 5219 struct perf_event *event) 5220 { 5221 struct event_constraint *c; 5222 5223 c = icl_get_event_constraints(cpuc, idx, event); 5224 5225 /* 5226 * The :ppp indicates the Precise Distribution (PDist) facility, which 5227 * is only supported on the GP counter 0. If a :ppp event which is not 5228 * available on the GP counter 0, error out. 5229 * Exception: Instruction PDIR is only available on the fixed counter 0. 5230 */ 5231 if ((event->attr.precise_ip == 3) && 5232 !constraint_match(&fixed0_constraint, event->hw.config)) { 5233 if (c->idxmsk64 & BIT_ULL(0)) 5234 return &counter0_constraint; 5235 5236 return &emptyconstraint; 5237 } 5238 5239 return c; 5240 } 5241 5242 static struct event_constraint * 5243 glp_get_event_constraints(struct cpu_hw_events *cpuc, int idx, 5244 struct perf_event *event) 5245 { 5246 struct event_constraint *c; 5247 5248 /* :ppp means to do reduced skid PEBS which is PMC0 only. */ 5249 if (event->attr.precise_ip == 3) 5250 return &counter0_constraint; 5251 5252 c = intel_get_event_constraints(cpuc, idx, event); 5253 5254 return c; 5255 } 5256 5257 static struct event_constraint * 5258 tnt_get_event_constraints(struct cpu_hw_events *cpuc, int idx, 5259 struct perf_event *event) 5260 { 5261 struct event_constraint *c; 5262 5263 c = intel_get_event_constraints(cpuc, idx, event); 5264 5265 /* 5266 * :ppp means to do reduced skid PEBS, 5267 * which is available on PMC0 and fixed counter 0. 5268 */ 5269 if (event->attr.precise_ip == 3) { 5270 /* Force instruction:ppp on PMC0 and Fixed counter 0 */ 5271 if (constraint_match(&fixed0_constraint, event->hw.config)) 5272 return &fixed0_counter0_constraint; 5273 5274 return &counter0_constraint; 5275 } 5276 5277 return c; 5278 } 5279 5280 static bool allow_tsx_force_abort = true; 5281 5282 static struct event_constraint * 5283 tfa_get_event_constraints(struct cpu_hw_events *cpuc, int idx, 5284 struct perf_event *event) 5285 { 5286 struct event_constraint *c = hsw_get_event_constraints(cpuc, idx, event); 5287 5288 /* 5289 * Without TFA we must not use PMC3. 5290 */ 5291 if (!allow_tsx_force_abort && test_bit(3, c->idxmsk)) { 5292 c = dyn_constraint(cpuc, c, idx); 5293 c->idxmsk64 &= ~(1ULL << 3); 5294 c->weight--; 5295 } 5296 5297 return c; 5298 } 5299 5300 static struct event_constraint * 5301 adl_get_event_constraints(struct cpu_hw_events *cpuc, int idx, 5302 struct perf_event *event) 5303 { 5304 struct x86_hybrid_pmu *pmu = hybrid_pmu(event->pmu); 5305 5306 if (pmu->pmu_type == hybrid_big) 5307 return glc_get_event_constraints(cpuc, idx, event); 5308 else if (pmu->pmu_type == hybrid_small) 5309 return tnt_get_event_constraints(cpuc, idx, event); 5310 5311 WARN_ON(1); 5312 return &emptyconstraint; 5313 } 5314 5315 static struct event_constraint * 5316 cmt_get_event_constraints(struct cpu_hw_events *cpuc, int idx, 5317 struct perf_event *event) 5318 { 5319 struct event_constraint *c; 5320 5321 c = intel_get_event_constraints(cpuc, idx, event); 5322 5323 /* 5324 * The :ppp indicates the Precise Distribution (PDist) facility, which 5325 * is only supported on the GP counter 0 & 1 and Fixed counter 0. 5326 * If a :ppp event which is not available on the above eligible counters, 5327 * error out. 5328 */ 5329 if (event->attr.precise_ip == 3) { 5330 /* Force instruction:ppp on PMC0, 1 and Fixed counter 0 */ 5331 if (constraint_match(&fixed0_constraint, event->hw.config)) { 5332 /* The fixed counter 0 doesn't support LBR event logging. */ 5333 if (branch_sample_counters(event)) 5334 return &counter0_1_constraint; 5335 else 5336 return &fixed0_counter0_1_constraint; 5337 } 5338 5339 switch (c->idxmsk64 & 0x3ull) { 5340 case 0x1: 5341 return &counter0_constraint; 5342 case 0x2: 5343 return &counter1_constraint; 5344 case 0x3: 5345 return &counter0_1_constraint; 5346 } 5347 return &emptyconstraint; 5348 } 5349 5350 return c; 5351 } 5352 5353 static struct event_constraint * 5354 rwc_get_event_constraints(struct cpu_hw_events *cpuc, int idx, 5355 struct perf_event *event) 5356 { 5357 struct event_constraint *c; 5358 5359 c = glc_get_event_constraints(cpuc, idx, event); 5360 5361 /* The Retire Latency is not supported by the fixed counter 0. */ 5362 if (event->attr.precise_ip && 5363 (event->attr.sample_type & PERF_SAMPLE_WEIGHT_TYPE) && 5364 constraint_match(&fixed0_constraint, event->hw.config)) { 5365 /* 5366 * The Instruction PDIR is only available 5367 * on the fixed counter 0. Error out for this case. 5368 */ 5369 if (event->attr.precise_ip == 3) 5370 return &emptyconstraint; 5371 return &counters_1_7_constraint; 5372 } 5373 5374 return c; 5375 } 5376 5377 static struct event_constraint * 5378 mtl_get_event_constraints(struct cpu_hw_events *cpuc, int idx, 5379 struct perf_event *event) 5380 { 5381 struct x86_hybrid_pmu *pmu = hybrid_pmu(event->pmu); 5382 5383 if (pmu->pmu_type == hybrid_big) 5384 return rwc_get_event_constraints(cpuc, idx, event); 5385 if (pmu->pmu_type == hybrid_small) 5386 return cmt_get_event_constraints(cpuc, idx, event); 5387 5388 WARN_ON(1); 5389 return &emptyconstraint; 5390 } 5391 5392 static int adl_hw_config(struct perf_event *event) 5393 { 5394 struct x86_hybrid_pmu *pmu = hybrid_pmu(event->pmu); 5395 5396 if (pmu->pmu_type == hybrid_big) 5397 return hsw_hw_config(event); 5398 else if (pmu->pmu_type == hybrid_small) 5399 return intel_pmu_hw_config(event); 5400 5401 WARN_ON(1); 5402 return -EOPNOTSUPP; 5403 } 5404 5405 static enum intel_cpu_type adl_get_hybrid_cpu_type(void) 5406 { 5407 return INTEL_CPU_TYPE_CORE; 5408 } 5409 5410 static inline bool erratum_hsw11(struct perf_event *event) 5411 { 5412 return (event->hw.config & INTEL_ARCH_EVENT_MASK) == 5413 X86_CONFIG(.event=0xc0, .umask=0x01); 5414 } 5415 5416 static struct event_constraint * 5417 arl_h_get_event_constraints(struct cpu_hw_events *cpuc, int idx, 5418 struct perf_event *event) 5419 { 5420 struct x86_hybrid_pmu *pmu = hybrid_pmu(event->pmu); 5421 5422 if (pmu->pmu_type == hybrid_tiny) 5423 return cmt_get_event_constraints(cpuc, idx, event); 5424 5425 return mtl_get_event_constraints(cpuc, idx, event); 5426 } 5427 5428 static int arl_h_hw_config(struct perf_event *event) 5429 { 5430 struct x86_hybrid_pmu *pmu = hybrid_pmu(event->pmu); 5431 5432 if (pmu->pmu_type == hybrid_tiny) 5433 return intel_pmu_hw_config(event); 5434 5435 return adl_hw_config(event); 5436 } 5437 5438 /* 5439 * The HSW11 requires a period larger than 100 which is the same as the BDM11. 5440 * A minimum period of 128 is enforced as well for the INST_RETIRED.ALL. 5441 * 5442 * The message 'interrupt took too long' can be observed on any counter which 5443 * was armed with a period < 32 and two events expired in the same NMI. 5444 * A minimum period of 32 is enforced for the rest of the events. 5445 */ 5446 static void hsw_limit_period(struct perf_event *event, s64 *left) 5447 { 5448 *left = max(*left, erratum_hsw11(event) ? 128 : 32); 5449 } 5450 5451 /* 5452 * Broadwell: 5453 * 5454 * The INST_RETIRED.ALL period always needs to have lowest 6 bits cleared 5455 * (BDM55) and it must not use a period smaller than 100 (BDM11). We combine 5456 * the two to enforce a minimum period of 128 (the smallest value that has bits 5457 * 0-5 cleared and >= 100). 5458 * 5459 * Because of how the code in x86_perf_event_set_period() works, the truncation 5460 * of the lower 6 bits is 'harmless' as we'll occasionally add a longer period 5461 * to make up for the 'lost' events due to carrying the 'error' in period_left. 5462 * 5463 * Therefore the effective (average) period matches the requested period, 5464 * despite coarser hardware granularity. 5465 */ 5466 static void bdw_limit_period(struct perf_event *event, s64 *left) 5467 { 5468 if (erratum_hsw11(event)) { 5469 if (*left < 128) 5470 *left = 128; 5471 *left &= ~0x3fULL; 5472 } 5473 } 5474 5475 static void nhm_limit_period(struct perf_event *event, s64 *left) 5476 { 5477 *left = max(*left, 32LL); 5478 } 5479 5480 static void glc_limit_period(struct perf_event *event, s64 *left) 5481 { 5482 if (event->attr.precise_ip == 3) 5483 *left = max(*left, 128LL); 5484 } 5485 5486 PMU_FORMAT_ATTR(event, "config:0-7" ); 5487 PMU_FORMAT_ATTR(umask, "config:8-15" ); 5488 PMU_FORMAT_ATTR(edge, "config:18" ); 5489 PMU_FORMAT_ATTR(pc, "config:19" ); 5490 PMU_FORMAT_ATTR(any, "config:21" ); /* v3 + */ 5491 PMU_FORMAT_ATTR(inv, "config:23" ); 5492 PMU_FORMAT_ATTR(cmask, "config:24-31" ); 5493 PMU_FORMAT_ATTR(in_tx, "config:32" ); 5494 PMU_FORMAT_ATTR(in_tx_cp, "config:33" ); 5495 PMU_FORMAT_ATTR(eq, "config:36" ); /* v6 + */ 5496 5497 PMU_FORMAT_ATTR(metrics_clear, "config1:0"); /* PERF_CAPABILITIES.RDPMC_METRICS_CLEAR */ 5498 5499 static ssize_t umask2_show(struct device *dev, 5500 struct device_attribute *attr, 5501 char *page) 5502 { 5503 u64 mask = hybrid(dev_get_drvdata(dev), config_mask) & ARCH_PERFMON_EVENTSEL_UMASK2; 5504 5505 if (mask == ARCH_PERFMON_EVENTSEL_UMASK2) 5506 return sprintf(page, "config:8-15,40-47\n"); 5507 5508 /* Roll back to the old format if umask2 is not supported. */ 5509 return sprintf(page, "config:8-15\n"); 5510 } 5511 5512 static struct device_attribute format_attr_umask2 = 5513 __ATTR(umask, 0444, umask2_show, NULL); 5514 5515 static struct attribute *format_evtsel_ext_attrs[] = { 5516 &format_attr_umask2.attr, 5517 &format_attr_eq.attr, 5518 &format_attr_metrics_clear.attr, 5519 NULL 5520 }; 5521 5522 static umode_t 5523 evtsel_ext_is_visible(struct kobject *kobj, struct attribute *attr, int i) 5524 { 5525 struct device *dev = kobj_to_dev(kobj); 5526 u64 mask; 5527 5528 /* 5529 * The umask and umask2 have different formats but share the 5530 * same attr name. In update mode, the previous value of the 5531 * umask is unconditionally removed before is_visible. If 5532 * umask2 format is not enumerated, it's impossible to roll 5533 * back to the old format. 5534 * Does the check in umask2_show rather than is_visible. 5535 */ 5536 if (i == 0) 5537 return attr->mode; 5538 5539 mask = hybrid(dev_get_drvdata(dev), config_mask); 5540 if (i == 1) 5541 return (mask & ARCH_PERFMON_EVENTSEL_EQ) ? attr->mode : 0; 5542 5543 /* PERF_CAPABILITIES.RDPMC_METRICS_CLEAR */ 5544 if (i == 2) { 5545 union perf_capabilities intel_cap = hybrid(dev_get_drvdata(dev), intel_cap); 5546 5547 return intel_cap.rdpmc_metrics_clear ? attr->mode : 0; 5548 } 5549 5550 return 0; 5551 } 5552 5553 static struct attribute *intel_arch_formats_attr[] = { 5554 &format_attr_event.attr, 5555 &format_attr_umask.attr, 5556 &format_attr_edge.attr, 5557 &format_attr_pc.attr, 5558 &format_attr_inv.attr, 5559 &format_attr_cmask.attr, 5560 NULL, 5561 }; 5562 5563 ssize_t intel_event_sysfs_show(char *page, u64 config) 5564 { 5565 u64 event = (config & ARCH_PERFMON_EVENTSEL_EVENT); 5566 5567 return x86_event_sysfs_show(page, config, event); 5568 } 5569 5570 static struct intel_shared_regs *allocate_shared_regs(int cpu) 5571 { 5572 struct intel_shared_regs *regs; 5573 int i; 5574 5575 regs = kzalloc_node(sizeof(struct intel_shared_regs), 5576 GFP_KERNEL, cpu_to_node(cpu)); 5577 if (regs) { 5578 /* 5579 * initialize the locks to keep lockdep happy 5580 */ 5581 for (i = 0; i < EXTRA_REG_MAX; i++) 5582 raw_spin_lock_init(®s->regs[i].lock); 5583 5584 regs->core_id = -1; 5585 } 5586 return regs; 5587 } 5588 5589 static struct intel_excl_cntrs *allocate_excl_cntrs(int cpu) 5590 { 5591 struct intel_excl_cntrs *c; 5592 5593 c = kzalloc_node(sizeof(struct intel_excl_cntrs), 5594 GFP_KERNEL, cpu_to_node(cpu)); 5595 if (c) { 5596 raw_spin_lock_init(&c->lock); 5597 c->core_id = -1; 5598 } 5599 return c; 5600 } 5601 5602 5603 int intel_cpuc_prepare(struct cpu_hw_events *cpuc, int cpu) 5604 { 5605 cpuc->pebs_record_size = x86_pmu.pebs_record_size; 5606 5607 if (is_hybrid() || x86_pmu.extra_regs || x86_pmu.lbr_sel_map) { 5608 cpuc->shared_regs = allocate_shared_regs(cpu); 5609 if (!cpuc->shared_regs) 5610 goto err; 5611 } 5612 5613 if (x86_pmu.flags & (PMU_FL_EXCL_CNTRS | PMU_FL_TFA | PMU_FL_DYN_CONSTRAINT)) { 5614 size_t sz = X86_PMC_IDX_MAX * sizeof(struct event_constraint); 5615 5616 cpuc->constraint_list = kzalloc_node(sz, GFP_KERNEL, cpu_to_node(cpu)); 5617 if (!cpuc->constraint_list) 5618 goto err_shared_regs; 5619 } 5620 5621 if (x86_pmu.flags & PMU_FL_EXCL_CNTRS) { 5622 cpuc->excl_cntrs = allocate_excl_cntrs(cpu); 5623 if (!cpuc->excl_cntrs) 5624 goto err_constraint_list; 5625 5626 cpuc->excl_thread_id = 0; 5627 } 5628 5629 return 0; 5630 5631 err_constraint_list: 5632 kfree(cpuc->constraint_list); 5633 cpuc->constraint_list = NULL; 5634 5635 err_shared_regs: 5636 kfree(cpuc->shared_regs); 5637 cpuc->shared_regs = NULL; 5638 5639 err: 5640 return -ENOMEM; 5641 } 5642 5643 static int intel_pmu_cpu_prepare(int cpu) 5644 { 5645 int ret; 5646 5647 ret = intel_cpuc_prepare(&per_cpu(cpu_hw_events, cpu), cpu); 5648 if (ret) 5649 return ret; 5650 5651 return alloc_arch_pebs_buf_on_cpu(cpu); 5652 } 5653 5654 static void flip_smm_bit(void *data) 5655 { 5656 unsigned long set = *(unsigned long *)data; 5657 5658 if (set > 0) { 5659 msr_set_bit(MSR_IA32_DEBUGCTLMSR, 5660 DEBUGCTLMSR_FREEZE_IN_SMM_BIT); 5661 } else { 5662 msr_clear_bit(MSR_IA32_DEBUGCTLMSR, 5663 DEBUGCTLMSR_FREEZE_IN_SMM_BIT); 5664 } 5665 } 5666 5667 static void intel_pmu_check_counters_mask(u64 *cntr_mask, 5668 u64 *fixed_cntr_mask, 5669 u64 *intel_ctrl) 5670 { 5671 unsigned int bit; 5672 5673 bit = fls64(*cntr_mask); 5674 if (bit > INTEL_PMC_MAX_GENERIC) { 5675 WARN(1, KERN_ERR "hw perf events %d > max(%d), clipping!", 5676 bit, INTEL_PMC_MAX_GENERIC); 5677 *cntr_mask &= GENMASK_ULL(INTEL_PMC_MAX_GENERIC - 1, 0); 5678 } 5679 *intel_ctrl = *cntr_mask; 5680 5681 bit = fls64(*fixed_cntr_mask); 5682 if (bit > INTEL_PMC_MAX_FIXED) { 5683 WARN(1, KERN_ERR "hw perf events fixed %d > max(%d), clipping!", 5684 bit, INTEL_PMC_MAX_FIXED); 5685 *fixed_cntr_mask &= GENMASK_ULL(INTEL_PMC_MAX_FIXED - 1, 0); 5686 } 5687 5688 *intel_ctrl |= *fixed_cntr_mask << INTEL_PMC_IDX_FIXED; 5689 } 5690 5691 static void intel_pmu_check_event_constraints(struct event_constraint *event_constraints, 5692 u64 cntr_mask, 5693 u64 fixed_cntr_mask, 5694 u64 intel_ctrl); 5695 5696 enum dyn_constr_type { 5697 DYN_CONSTR_NONE, 5698 DYN_CONSTR_BR_CNTR, 5699 DYN_CONSTR_ACR_CNTR, 5700 DYN_CONSTR_ACR_CAUSE, 5701 DYN_CONSTR_PEBS, 5702 DYN_CONSTR_PDIST, 5703 5704 DYN_CONSTR_MAX, 5705 }; 5706 5707 static const char * const dyn_constr_type_name[] = { 5708 [DYN_CONSTR_NONE] = "a normal event", 5709 [DYN_CONSTR_BR_CNTR] = "a branch counter logging event", 5710 [DYN_CONSTR_ACR_CNTR] = "an auto-counter reload event", 5711 [DYN_CONSTR_ACR_CAUSE] = "an auto-counter reload cause event", 5712 [DYN_CONSTR_PEBS] = "a PEBS event", 5713 [DYN_CONSTR_PDIST] = "a PEBS PDIST event", 5714 }; 5715 5716 static void __intel_pmu_check_dyn_constr(struct event_constraint *constr, 5717 enum dyn_constr_type type, u64 mask) 5718 { 5719 struct event_constraint *c1, *c2; 5720 int new_weight, check_weight; 5721 u64 new_mask, check_mask; 5722 5723 for_each_event_constraint(c1, constr) { 5724 new_mask = c1->idxmsk64 & mask; 5725 new_weight = hweight64(new_mask); 5726 5727 /* ignore topdown perf metrics event */ 5728 if (c1->idxmsk64 & INTEL_PMC_MSK_TOPDOWN) 5729 continue; 5730 5731 if (!new_weight && fls64(c1->idxmsk64) < INTEL_PMC_IDX_FIXED) { 5732 pr_info("The event 0x%llx is not supported as %s.\n", 5733 c1->code, dyn_constr_type_name[type]); 5734 } 5735 5736 if (new_weight <= 1) 5737 continue; 5738 5739 for_each_event_constraint(c2, c1 + 1) { 5740 bool check_fail = false; 5741 5742 check_mask = c2->idxmsk64 & mask; 5743 check_weight = hweight64(check_mask); 5744 5745 if (c2->idxmsk64 & INTEL_PMC_MSK_TOPDOWN || 5746 !check_weight) 5747 continue; 5748 5749 /* The same constraints or no overlap */ 5750 if (new_mask == check_mask || 5751 (new_mask ^ check_mask) == (new_mask | check_mask)) 5752 continue; 5753 5754 /* 5755 * A scheduler issue may be triggered in the following cases. 5756 * - Two overlap constraints have the same weight. 5757 * E.g., A constraints: 0x3, B constraints: 0x6 5758 * event counter failure case 5759 * B PMC[2:1] 1 5760 * A PMC[1:0] 0 5761 * A PMC[1:0] FAIL 5762 * - Two overlap constraints have different weight. 5763 * The constraint has a low weight, but has high last bit. 5764 * E.g., A constraints: 0x7, B constraints: 0xC 5765 * event counter failure case 5766 * B PMC[3:2] 2 5767 * A PMC[2:0] 0 5768 * A PMC[2:0] 1 5769 * A PMC[2:0] FAIL 5770 */ 5771 if (new_weight == check_weight) { 5772 check_fail = true; 5773 } else if (new_weight < check_weight) { 5774 if ((new_mask | check_mask) != check_mask && 5775 fls64(new_mask) > fls64(check_mask)) 5776 check_fail = true; 5777 } else { 5778 if ((new_mask | check_mask) != new_mask && 5779 fls64(new_mask) < fls64(check_mask)) 5780 check_fail = true; 5781 } 5782 5783 if (check_fail) { 5784 pr_info("The two events 0x%llx and 0x%llx may not be " 5785 "fully scheduled under some circumstances as " 5786 "%s.\n", 5787 c1->code, c2->code, dyn_constr_type_name[type]); 5788 } 5789 } 5790 } 5791 } 5792 5793 static void intel_pmu_check_dyn_constr(struct pmu *pmu, 5794 struct event_constraint *constr, 5795 u64 cntr_mask) 5796 { 5797 enum dyn_constr_type i; 5798 u64 mask; 5799 5800 for (i = DYN_CONSTR_NONE; i < DYN_CONSTR_MAX; i++) { 5801 mask = 0; 5802 switch (i) { 5803 case DYN_CONSTR_NONE: 5804 mask = cntr_mask; 5805 break; 5806 case DYN_CONSTR_BR_CNTR: 5807 if (x86_pmu.flags & PMU_FL_BR_CNTR) 5808 mask = x86_pmu.lbr_counters; 5809 break; 5810 case DYN_CONSTR_ACR_CNTR: 5811 mask = hybrid(pmu, acr_cntr_mask64) & GENMASK_ULL(INTEL_PMC_MAX_GENERIC - 1, 0); 5812 break; 5813 case DYN_CONSTR_ACR_CAUSE: 5814 if (hybrid(pmu, acr_cntr_mask64) == hybrid(pmu, acr_cause_mask64)) 5815 continue; 5816 mask = hybrid(pmu, acr_cause_mask64) & GENMASK_ULL(INTEL_PMC_MAX_GENERIC - 1, 0); 5817 break; 5818 case DYN_CONSTR_PEBS: 5819 if (x86_pmu.arch_pebs) 5820 mask = hybrid(pmu, arch_pebs_cap).counters; 5821 break; 5822 case DYN_CONSTR_PDIST: 5823 if (x86_pmu.arch_pebs) 5824 mask = hybrid(pmu, arch_pebs_cap).pdists; 5825 break; 5826 default: 5827 pr_warn("Unsupported dynamic constraint type %d\n", i); 5828 } 5829 5830 if (mask) 5831 __intel_pmu_check_dyn_constr(constr, i, mask); 5832 } 5833 } 5834 5835 static void intel_pmu_check_event_constraints_all(struct pmu *pmu) 5836 { 5837 struct event_constraint *event_constraints = hybrid(pmu, event_constraints); 5838 struct event_constraint *pebs_constraints = hybrid(pmu, pebs_constraints); 5839 u64 cntr_mask = hybrid(pmu, cntr_mask64); 5840 u64 fixed_cntr_mask = hybrid(pmu, fixed_cntr_mask64); 5841 u64 intel_ctrl = hybrid(pmu, intel_ctrl); 5842 5843 intel_pmu_check_event_constraints(event_constraints, cntr_mask, 5844 fixed_cntr_mask, intel_ctrl); 5845 5846 if (event_constraints) 5847 intel_pmu_check_dyn_constr(pmu, event_constraints, cntr_mask); 5848 5849 if (pebs_constraints) 5850 intel_pmu_check_dyn_constr(pmu, pebs_constraints, cntr_mask); 5851 } 5852 5853 static void intel_pmu_check_extra_regs(struct extra_reg *extra_regs); 5854 5855 static inline bool intel_pmu_broken_perf_cap(void) 5856 { 5857 /* The Perf Metric (Bit 15) is always cleared */ 5858 if (boot_cpu_data.x86_vfm == INTEL_METEORLAKE || 5859 boot_cpu_data.x86_vfm == INTEL_METEORLAKE_L) 5860 return true; 5861 5862 return false; 5863 } 5864 5865 static inline void __intel_update_pmu_caps(struct pmu *pmu) 5866 { 5867 struct pmu *dest_pmu = pmu ? pmu : x86_get_pmu(smp_processor_id()); 5868 5869 if (hybrid(pmu, arch_pebs_cap).caps & ARCH_PEBS_VECR_XMM) 5870 dest_pmu->capabilities |= PERF_PMU_CAP_EXTENDED_REGS; 5871 } 5872 5873 static inline void __intel_update_large_pebs_flags(struct pmu *pmu) 5874 { 5875 u64 caps = hybrid(pmu, arch_pebs_cap).caps; 5876 5877 x86_pmu.large_pebs_flags |= PERF_SAMPLE_TIME; 5878 if (caps & ARCH_PEBS_LBR) 5879 x86_pmu.large_pebs_flags |= PERF_SAMPLE_BRANCH_STACK; 5880 if (caps & ARCH_PEBS_CNTR_MASK) 5881 x86_pmu.large_pebs_flags |= PERF_SAMPLE_READ; 5882 5883 if (!(caps & ARCH_PEBS_AUX)) 5884 x86_pmu.large_pebs_flags &= ~PERF_SAMPLE_DATA_SRC; 5885 if (!(caps & ARCH_PEBS_GPR)) { 5886 x86_pmu.large_pebs_flags &= 5887 ~(PERF_SAMPLE_REGS_INTR | PERF_SAMPLE_REGS_USER); 5888 } 5889 } 5890 5891 #define counter_mask(_gp, _fixed) ((_gp) | ((u64)(_fixed) << INTEL_PMC_IDX_FIXED)) 5892 5893 static void update_pmu_cap(struct pmu *pmu) 5894 { 5895 unsigned int eax, ebx, ecx, edx; 5896 union cpuid35_eax eax_0; 5897 union cpuid35_ebx ebx_0; 5898 u64 cntrs_mask = 0; 5899 u64 pebs_mask = 0; 5900 u64 pdists_mask = 0; 5901 5902 cpuid(ARCH_PERFMON_EXT_LEAF, &eax_0.full, &ebx_0.full, &ecx, &edx); 5903 5904 if (ebx_0.split.umask2) 5905 hybrid(pmu, config_mask) |= ARCH_PERFMON_EVENTSEL_UMASK2; 5906 if (ebx_0.split.eq) 5907 hybrid(pmu, config_mask) |= ARCH_PERFMON_EVENTSEL_EQ; 5908 if (ebx_0.split.rdpmc_user_disable) 5909 hybrid(pmu, config_mask) |= ARCH_PERFMON_EVENTSEL_RDPMC_USER_DISABLE; 5910 5911 if (eax_0.split.cntr_subleaf) { 5912 cpuid_count(ARCH_PERFMON_EXT_LEAF, ARCH_PERFMON_NUM_COUNTER_LEAF, 5913 &eax, &ebx, &ecx, &edx); 5914 hybrid(pmu, cntr_mask64) = eax; 5915 hybrid(pmu, fixed_cntr_mask64) = ebx; 5916 cntrs_mask = counter_mask(eax, ebx); 5917 } 5918 5919 if (eax_0.split.acr_subleaf) { 5920 cpuid_count(ARCH_PERFMON_EXT_LEAF, ARCH_PERFMON_ACR_LEAF, 5921 &eax, &ebx, &ecx, &edx); 5922 /* The mask of the counters which can be reloaded */ 5923 hybrid(pmu, acr_cntr_mask64) = counter_mask(eax, ebx); 5924 /* The mask of the counters which can cause a reload of reloadable counters */ 5925 hybrid(pmu, acr_cause_mask64) = counter_mask(ecx, edx); 5926 } 5927 5928 /* Bits[5:4] should be set simultaneously if arch-PEBS is supported */ 5929 if (eax_0.split.pebs_caps_subleaf && eax_0.split.pebs_cnts_subleaf) { 5930 cpuid_count(ARCH_PERFMON_EXT_LEAF, ARCH_PERFMON_PEBS_CAP_LEAF, 5931 &eax, &ebx, &ecx, &edx); 5932 hybrid(pmu, arch_pebs_cap).caps = (u64)ebx << 32; 5933 5934 cpuid_count(ARCH_PERFMON_EXT_LEAF, ARCH_PERFMON_PEBS_COUNTER_LEAF, 5935 &eax, &ebx, &ecx, &edx); 5936 pebs_mask = counter_mask(eax, ecx); 5937 pdists_mask = counter_mask(ebx, edx); 5938 hybrid(pmu, arch_pebs_cap).counters = pebs_mask; 5939 hybrid(pmu, arch_pebs_cap).pdists = pdists_mask; 5940 5941 if (WARN_ON((pebs_mask | pdists_mask) & ~cntrs_mask)) { 5942 x86_pmu.arch_pebs = 0; 5943 } else { 5944 __intel_update_pmu_caps(pmu); 5945 __intel_update_large_pebs_flags(pmu); 5946 } 5947 } else { 5948 WARN_ON(x86_pmu.arch_pebs == 1); 5949 x86_pmu.arch_pebs = 0; 5950 } 5951 5952 if (!intel_pmu_broken_perf_cap()) { 5953 /* Perf Metric (Bit 15) and PEBS via PT (Bit 16) are hybrid enumeration */ 5954 rdmsrq(MSR_IA32_PERF_CAPABILITIES, hybrid(pmu, intel_cap).capabilities); 5955 } 5956 } 5957 5958 static void intel_pmu_check_hybrid_pmus(struct x86_hybrid_pmu *pmu) 5959 { 5960 intel_pmu_check_counters_mask(&pmu->cntr_mask64, &pmu->fixed_cntr_mask64, 5961 &pmu->intel_ctrl); 5962 pmu->pebs_events_mask = intel_pmu_pebs_mask(pmu->cntr_mask64); 5963 pmu->unconstrained = (struct event_constraint) 5964 __EVENT_CONSTRAINT(0, pmu->cntr_mask64, 5965 0, x86_pmu_num_counters(&pmu->pmu), 0, 0); 5966 5967 if (pmu->intel_cap.perf_metrics) 5968 pmu->intel_ctrl |= GLOBAL_CTRL_EN_PERF_METRICS; 5969 else 5970 pmu->intel_ctrl &= ~GLOBAL_CTRL_EN_PERF_METRICS; 5971 5972 pmu->pmu.capabilities |= PERF_PMU_CAP_MEDIATED_VPMU; 5973 5974 intel_pmu_check_event_constraints_all(&pmu->pmu); 5975 5976 intel_pmu_check_extra_regs(pmu->extra_regs); 5977 } 5978 5979 static struct x86_hybrid_pmu *find_hybrid_pmu_for_cpu(void) 5980 { 5981 struct cpuinfo_x86 *c = &cpu_data(smp_processor_id()); 5982 enum intel_cpu_type cpu_type = c->topo.intel_type; 5983 int i; 5984 5985 /* 5986 * This is running on a CPU model that is known to have hybrid 5987 * configurations. But the CPU told us it is not hybrid, shame 5988 * on it. There should be a fixup function provided for these 5989 * troublesome CPUs (->get_hybrid_cpu_type). 5990 */ 5991 if (cpu_type == INTEL_CPU_TYPE_UNKNOWN) { 5992 if (x86_pmu.get_hybrid_cpu_type) 5993 cpu_type = x86_pmu.get_hybrid_cpu_type(); 5994 else 5995 return NULL; 5996 } 5997 5998 /* 5999 * This essentially just maps between the 'hybrid_cpu_type' 6000 * and 'hybrid_pmu_type' enums except for ARL-H processor 6001 * which needs to compare atom uarch native id since ARL-H 6002 * contains two different atom uarchs. 6003 */ 6004 for (i = 0; i < x86_pmu.num_hybrid_pmus; i++) { 6005 enum hybrid_pmu_type pmu_type = x86_pmu.hybrid_pmu[i].pmu_type; 6006 u32 native_id; 6007 6008 if (cpu_type == INTEL_CPU_TYPE_CORE && pmu_type == hybrid_big) 6009 return &x86_pmu.hybrid_pmu[i]; 6010 if (cpu_type == INTEL_CPU_TYPE_ATOM) { 6011 if (x86_pmu.num_hybrid_pmus == 2 && pmu_type == hybrid_small) 6012 return &x86_pmu.hybrid_pmu[i]; 6013 6014 native_id = c->topo.intel_native_model_id; 6015 if (native_id == INTEL_ATOM_SKT_NATIVE_ID && pmu_type == hybrid_small) 6016 return &x86_pmu.hybrid_pmu[i]; 6017 if (native_id == INTEL_ATOM_CMT_NATIVE_ID && pmu_type == hybrid_tiny) 6018 return &x86_pmu.hybrid_pmu[i]; 6019 } 6020 } 6021 6022 return NULL; 6023 } 6024 6025 static bool init_hybrid_pmu(int cpu) 6026 { 6027 struct cpu_hw_events *cpuc = &per_cpu(cpu_hw_events, cpu); 6028 struct x86_hybrid_pmu *pmu = find_hybrid_pmu_for_cpu(); 6029 6030 if (WARN_ON_ONCE(!pmu || (pmu->pmu.type == -1))) { 6031 cpuc->pmu = NULL; 6032 return false; 6033 } 6034 6035 /* Only check and dump the PMU information for the first CPU */ 6036 if (!cpumask_empty(&pmu->supported_cpus)) 6037 goto end; 6038 6039 if (this_cpu_has(X86_FEATURE_ARCH_PERFMON_EXT)) 6040 update_pmu_cap(&pmu->pmu); 6041 6042 intel_pmu_check_hybrid_pmus(pmu); 6043 6044 if (!check_hw_exists(&pmu->pmu, pmu->cntr_mask, pmu->fixed_cntr_mask)) 6045 return false; 6046 6047 pr_info("%s PMU driver: ", pmu->name); 6048 6049 pr_cont("\n"); 6050 6051 x86_pmu_show_pmu_cap(&pmu->pmu); 6052 6053 end: 6054 cpumask_set_cpu(cpu, &pmu->supported_cpus); 6055 cpuc->pmu = &pmu->pmu; 6056 6057 return true; 6058 } 6059 6060 static void intel_pmu_cpu_starting(int cpu) 6061 { 6062 struct cpu_hw_events *cpuc = &per_cpu(cpu_hw_events, cpu); 6063 int core_id = topology_core_id(cpu); 6064 int i; 6065 6066 if (is_hybrid() && !init_hybrid_pmu(cpu)) 6067 return; 6068 6069 init_debug_store_on_cpu(cpu); 6070 init_arch_pebs_on_cpu(cpu); 6071 /* 6072 * Deal with CPUs that don't clear their LBRs on power-up, and that may 6073 * even boot with LBRs enabled. 6074 */ 6075 if (!static_cpu_has(X86_FEATURE_ARCH_LBR) && x86_pmu.lbr_nr) 6076 msr_clear_bit(MSR_IA32_DEBUGCTLMSR, DEBUGCTLMSR_LBR_BIT); 6077 intel_pmu_lbr_reset(); 6078 6079 cpuc->lbr_sel = NULL; 6080 6081 if (x86_pmu.flags & PMU_FL_TFA) { 6082 WARN_ON_ONCE(cpuc->tfa_shadow); 6083 cpuc->tfa_shadow = ~0ULL; 6084 intel_set_tfa(cpuc, false); 6085 } 6086 6087 if (x86_pmu.version > 1) 6088 flip_smm_bit(&x86_pmu.attr_freeze_on_smi); 6089 6090 /* 6091 * Disable perf metrics if any added CPU doesn't support it. 6092 * 6093 * Turn off the check for a hybrid architecture, because the 6094 * architecture MSR, MSR_IA32_PERF_CAPABILITIES, only indicate 6095 * the architecture features. The perf metrics is a model-specific 6096 * feature for now. The corresponding bit should always be 0 on 6097 * a hybrid platform, e.g., Alder Lake. 6098 */ 6099 if (!is_hybrid() && x86_pmu.intel_cap.perf_metrics) { 6100 union perf_capabilities perf_cap; 6101 6102 rdmsrq(MSR_IA32_PERF_CAPABILITIES, perf_cap.capabilities); 6103 if (!perf_cap.perf_metrics) { 6104 x86_pmu.intel_cap.perf_metrics = 0; 6105 x86_pmu.intel_ctrl &= ~GLOBAL_CTRL_EN_PERF_METRICS; 6106 } 6107 } 6108 6109 __intel_update_pmu_caps(cpuc->pmu); 6110 6111 if (!cpuc->shared_regs) 6112 return; 6113 6114 if (!(x86_pmu.flags & PMU_FL_NO_HT_SHARING)) { 6115 for_each_cpu(i, topology_sibling_cpumask(cpu)) { 6116 struct intel_shared_regs *pc; 6117 6118 pc = per_cpu(cpu_hw_events, i).shared_regs; 6119 if (pc && pc->core_id == core_id) { 6120 cpuc->kfree_on_online[0] = cpuc->shared_regs; 6121 cpuc->shared_regs = pc; 6122 break; 6123 } 6124 } 6125 cpuc->shared_regs->core_id = core_id; 6126 cpuc->shared_regs->refcnt++; 6127 } 6128 6129 if (x86_pmu.lbr_sel_map) 6130 cpuc->lbr_sel = &cpuc->shared_regs->regs[EXTRA_REG_LBR]; 6131 6132 if (x86_pmu.flags & PMU_FL_EXCL_CNTRS) { 6133 for_each_cpu(i, topology_sibling_cpumask(cpu)) { 6134 struct cpu_hw_events *sibling; 6135 struct intel_excl_cntrs *c; 6136 6137 sibling = &per_cpu(cpu_hw_events, i); 6138 c = sibling->excl_cntrs; 6139 if (c && c->core_id == core_id) { 6140 cpuc->kfree_on_online[1] = cpuc->excl_cntrs; 6141 cpuc->excl_cntrs = c; 6142 if (!sibling->excl_thread_id) 6143 cpuc->excl_thread_id = 1; 6144 break; 6145 } 6146 } 6147 cpuc->excl_cntrs->core_id = core_id; 6148 cpuc->excl_cntrs->refcnt++; 6149 } 6150 } 6151 6152 static void free_excl_cntrs(struct cpu_hw_events *cpuc) 6153 { 6154 struct intel_excl_cntrs *c; 6155 6156 c = cpuc->excl_cntrs; 6157 if (c) { 6158 if (c->core_id == -1 || --c->refcnt == 0) 6159 kfree(c); 6160 cpuc->excl_cntrs = NULL; 6161 } 6162 6163 kfree(cpuc->constraint_list); 6164 cpuc->constraint_list = NULL; 6165 } 6166 6167 static void intel_pmu_cpu_dying(int cpu) 6168 { 6169 fini_debug_store_on_cpu(cpu); 6170 fini_arch_pebs_on_cpu(cpu); 6171 } 6172 6173 void intel_cpuc_finish(struct cpu_hw_events *cpuc) 6174 { 6175 struct intel_shared_regs *pc; 6176 6177 pc = cpuc->shared_regs; 6178 if (pc) { 6179 if (pc->core_id == -1 || --pc->refcnt == 0) 6180 kfree(pc); 6181 cpuc->shared_regs = NULL; 6182 } 6183 6184 free_excl_cntrs(cpuc); 6185 } 6186 6187 static void intel_pmu_cpu_dead(int cpu) 6188 { 6189 struct cpu_hw_events *cpuc = &per_cpu(cpu_hw_events, cpu); 6190 6191 release_arch_pebs_buf_on_cpu(cpu); 6192 intel_cpuc_finish(cpuc); 6193 6194 if (is_hybrid() && cpuc->pmu) 6195 cpumask_clear_cpu(cpu, &hybrid_pmu(cpuc->pmu)->supported_cpus); 6196 } 6197 6198 static void intel_pmu_sched_task(struct perf_event_pmu_context *pmu_ctx, 6199 struct task_struct *task, bool sched_in) 6200 { 6201 intel_pmu_pebs_sched_task(pmu_ctx, sched_in); 6202 intel_pmu_lbr_sched_task(pmu_ctx, task, sched_in); 6203 } 6204 6205 static int intel_pmu_check_period(struct perf_event *event, u64 value) 6206 { 6207 return intel_pmu_has_bts_period(event, value) ? -EINVAL : 0; 6208 } 6209 6210 static void intel_aux_output_init(void) 6211 { 6212 /* Refer also intel_pmu_aux_output_match() */ 6213 if (x86_pmu.intel_cap.pebs_output_pt_available) 6214 x86_pmu.assign = intel_pmu_assign_event; 6215 } 6216 6217 static int intel_pmu_aux_output_match(struct perf_event *event) 6218 { 6219 /* intel_pmu_assign_event() is needed, refer intel_aux_output_init() */ 6220 if (!x86_pmu.intel_cap.pebs_output_pt_available) 6221 return 0; 6222 6223 return is_intel_pt_event(event); 6224 } 6225 6226 static void intel_pmu_filter(struct pmu *pmu, int cpu, bool *ret) 6227 { 6228 struct x86_hybrid_pmu *hpmu = hybrid_pmu(pmu); 6229 6230 *ret = !cpumask_test_cpu(cpu, &hpmu->supported_cpus); 6231 } 6232 6233 PMU_FORMAT_ATTR(offcore_rsp, "config1:0-63"); 6234 6235 PMU_FORMAT_ATTR(ldlat, "config1:0-15"); 6236 6237 PMU_FORMAT_ATTR(frontend, "config1:0-23"); 6238 6239 PMU_FORMAT_ATTR(snoop_rsp, "config1:0-63"); 6240 6241 static struct attribute *intel_arch3_formats_attr[] = { 6242 &format_attr_event.attr, 6243 &format_attr_umask.attr, 6244 &format_attr_edge.attr, 6245 &format_attr_pc.attr, 6246 &format_attr_any.attr, 6247 &format_attr_inv.attr, 6248 &format_attr_cmask.attr, 6249 NULL, 6250 }; 6251 6252 static struct attribute *hsw_format_attr[] = { 6253 &format_attr_in_tx.attr, 6254 &format_attr_in_tx_cp.attr, 6255 &format_attr_offcore_rsp.attr, 6256 &format_attr_ldlat.attr, 6257 NULL 6258 }; 6259 6260 static struct attribute *nhm_format_attr[] = { 6261 &format_attr_offcore_rsp.attr, 6262 &format_attr_ldlat.attr, 6263 NULL 6264 }; 6265 6266 static struct attribute *slm_format_attr[] = { 6267 &format_attr_offcore_rsp.attr, 6268 NULL 6269 }; 6270 6271 static struct attribute *cmt_format_attr[] = { 6272 &format_attr_offcore_rsp.attr, 6273 &format_attr_ldlat.attr, 6274 &format_attr_snoop_rsp.attr, 6275 NULL 6276 }; 6277 6278 static struct attribute *skl_format_attr[] = { 6279 &format_attr_frontend.attr, 6280 NULL, 6281 }; 6282 6283 static __initconst const struct x86_pmu core_pmu = { 6284 .name = "core", 6285 .handle_irq = x86_pmu_handle_irq, 6286 .disable_all = x86_pmu_disable_all, 6287 .enable_all = core_pmu_enable_all, 6288 .enable = core_pmu_enable_event, 6289 .disable = x86_pmu_disable_event, 6290 .hw_config = core_pmu_hw_config, 6291 .schedule_events = x86_schedule_events, 6292 .eventsel = MSR_ARCH_PERFMON_EVENTSEL0, 6293 .perfctr = MSR_ARCH_PERFMON_PERFCTR0, 6294 .fixedctr = MSR_ARCH_PERFMON_FIXED_CTR0, 6295 .event_map = intel_pmu_event_map, 6296 .max_events = ARRAY_SIZE(intel_perfmon_event_map), 6297 .apic = 1, 6298 .large_pebs_flags = LARGE_PEBS_FLAGS, 6299 6300 /* 6301 * Intel PMCs cannot be accessed sanely above 32-bit width, 6302 * so we install an artificial 1<<31 period regardless of 6303 * the generic event period: 6304 */ 6305 .max_period = (1ULL<<31) - 1, 6306 .get_event_constraints = intel_get_event_constraints, 6307 .put_event_constraints = intel_put_event_constraints, 6308 .event_constraints = intel_core_event_constraints, 6309 .guest_get_msrs = core_guest_get_msrs, 6310 .format_attrs = intel_arch_formats_attr, 6311 .events_sysfs_show = intel_event_sysfs_show, 6312 6313 /* 6314 * Virtual (or funny metal) CPU can define x86_pmu.extra_regs 6315 * together with PMU version 1 and thus be using core_pmu with 6316 * shared_regs. We need following callbacks here to allocate 6317 * it properly. 6318 */ 6319 .cpu_prepare = intel_pmu_cpu_prepare, 6320 .cpu_starting = intel_pmu_cpu_starting, 6321 .cpu_dying = intel_pmu_cpu_dying, 6322 .cpu_dead = intel_pmu_cpu_dead, 6323 6324 .check_period = intel_pmu_check_period, 6325 6326 .lbr_reset = intel_pmu_lbr_reset_64, 6327 .lbr_read = intel_pmu_lbr_read_64, 6328 .lbr_save = intel_pmu_lbr_save, 6329 .lbr_restore = intel_pmu_lbr_restore, 6330 }; 6331 6332 static __initconst const struct x86_pmu intel_pmu = { 6333 .name = "Intel", 6334 .handle_irq = intel_pmu_handle_irq, 6335 .disable_all = intel_pmu_disable_all, 6336 .enable_all = intel_pmu_enable_all, 6337 .enable = intel_pmu_enable_event, 6338 .disable = intel_pmu_disable_event, 6339 .add = intel_pmu_add_event, 6340 .del = intel_pmu_del_event, 6341 .read = intel_pmu_read_event, 6342 .set_period = intel_pmu_set_period, 6343 .update = intel_pmu_update, 6344 .hw_config = intel_pmu_hw_config, 6345 .schedule_events = x86_schedule_events, 6346 .eventsel = MSR_ARCH_PERFMON_EVENTSEL0, 6347 .perfctr = MSR_ARCH_PERFMON_PERFCTR0, 6348 .fixedctr = MSR_ARCH_PERFMON_FIXED_CTR0, 6349 .event_map = intel_pmu_event_map, 6350 .max_events = ARRAY_SIZE(intel_perfmon_event_map), 6351 .apic = 1, 6352 .large_pebs_flags = LARGE_PEBS_FLAGS, 6353 /* 6354 * Intel PMCs cannot be accessed sanely above 32 bit width, 6355 * so we install an artificial 1<<31 period regardless of 6356 * the generic event period: 6357 */ 6358 .max_period = (1ULL << 31) - 1, 6359 .get_event_constraints = intel_get_event_constraints, 6360 .put_event_constraints = intel_put_event_constraints, 6361 .pebs_aliases = intel_pebs_aliases_core2, 6362 6363 .format_attrs = intel_arch3_formats_attr, 6364 .events_sysfs_show = intel_event_sysfs_show, 6365 6366 .cpu_prepare = intel_pmu_cpu_prepare, 6367 .cpu_starting = intel_pmu_cpu_starting, 6368 .cpu_dying = intel_pmu_cpu_dying, 6369 .cpu_dead = intel_pmu_cpu_dead, 6370 6371 .guest_get_msrs = intel_guest_get_msrs, 6372 .sched_task = intel_pmu_sched_task, 6373 6374 .check_period = intel_pmu_check_period, 6375 6376 .aux_output_match = intel_pmu_aux_output_match, 6377 6378 .lbr_reset = intel_pmu_lbr_reset_64, 6379 .lbr_read = intel_pmu_lbr_read_64, 6380 .lbr_save = intel_pmu_lbr_save, 6381 .lbr_restore = intel_pmu_lbr_restore, 6382 6383 /* 6384 * SMM has access to all 4 rings and while traditionally SMM code only 6385 * ran in CPL0, 2021-era firmware is starting to make use of CPL3 in SMM. 6386 * 6387 * Since the EVENTSEL.{USR,OS} CPL filtering makes no distinction 6388 * between SMM or not, this results in what should be pure userspace 6389 * counters including SMM data. 6390 * 6391 * This is a clear privilege issue, therefore globally disable 6392 * counting SMM by default. 6393 */ 6394 .attr_freeze_on_smi = 1, 6395 }; 6396 6397 static __init void intel_clovertown_quirk(void) 6398 { 6399 /* 6400 * PEBS is unreliable due to: 6401 * 6402 * AJ67 - PEBS may experience CPL leaks 6403 * AJ68 - PEBS PMI may be delayed by one event 6404 * AJ69 - GLOBAL_STATUS[62] will only be set when DEBUGCTL[12] 6405 * AJ106 - FREEZE_LBRS_ON_PMI doesn't work in combination with PEBS 6406 * 6407 * AJ67 could be worked around by restricting the OS/USR flags. 6408 * AJ69 could be worked around by setting PMU_FREEZE_ON_PMI. 6409 * 6410 * AJ106 could possibly be worked around by not allowing LBR 6411 * usage from PEBS, including the fixup. 6412 * AJ68 could possibly be worked around by always programming 6413 * a pebs_event_reset[0] value and coping with the lost events. 6414 * 6415 * But taken together it might just make sense to not enable PEBS on 6416 * these chips. 6417 */ 6418 pr_warn("PEBS disabled due to CPU errata\n"); 6419 x86_pmu.ds_pebs = 0; 6420 x86_pmu.pebs_constraints = NULL; 6421 } 6422 6423 static const struct x86_cpu_id isolation_ucodes[] = { 6424 X86_MATCH_VFM_STEPS(INTEL_HASWELL, 3, 3, 0x0000001f), 6425 X86_MATCH_VFM_STEPS(INTEL_HASWELL_L, 1, 1, 0x0000001e), 6426 X86_MATCH_VFM_STEPS(INTEL_HASWELL_G, 1, 1, 0x00000015), 6427 X86_MATCH_VFM_STEPS(INTEL_HASWELL_X, 2, 2, 0x00000037), 6428 X86_MATCH_VFM_STEPS(INTEL_HASWELL_X, 4, 4, 0x0000000a), 6429 X86_MATCH_VFM_STEPS(INTEL_BROADWELL, 4, 4, 0x00000023), 6430 X86_MATCH_VFM_STEPS(INTEL_BROADWELL_G, 1, 1, 0x00000014), 6431 X86_MATCH_VFM_STEPS(INTEL_BROADWELL_D, 2, 2, 0x00000010), 6432 X86_MATCH_VFM_STEPS(INTEL_BROADWELL_D, 3, 3, 0x07000009), 6433 X86_MATCH_VFM_STEPS(INTEL_BROADWELL_D, 4, 4, 0x0f000009), 6434 X86_MATCH_VFM_STEPS(INTEL_BROADWELL_D, 5, 5, 0x0e000002), 6435 X86_MATCH_VFM_STEPS(INTEL_BROADWELL_X, 1, 1, 0x0b000014), 6436 X86_MATCH_VFM_STEPS(INTEL_SKYLAKE_X, 3, 3, 0x00000021), 6437 X86_MATCH_VFM_STEPS(INTEL_SKYLAKE_X, 4, 7, 0x00000000), 6438 X86_MATCH_VFM_STEPS(INTEL_SKYLAKE_X, 11, 11, 0x00000000), 6439 X86_MATCH_VFM_STEPS(INTEL_SKYLAKE_L, 3, 3, 0x0000007c), 6440 X86_MATCH_VFM_STEPS(INTEL_SKYLAKE, 3, 3, 0x0000007c), 6441 X86_MATCH_VFM_STEPS(INTEL_KABYLAKE, 9, 13, 0x0000004e), 6442 X86_MATCH_VFM_STEPS(INTEL_KABYLAKE_L, 9, 12, 0x0000004e), 6443 {} 6444 }; 6445 6446 static void intel_check_pebs_isolation(void) 6447 { 6448 x86_pmu.pebs_no_isolation = !x86_match_min_microcode_rev(isolation_ucodes); 6449 } 6450 6451 static __init void intel_pebs_isolation_quirk(void) 6452 { 6453 WARN_ON_ONCE(x86_pmu.check_microcode); 6454 x86_pmu.check_microcode = intel_check_pebs_isolation; 6455 intel_check_pebs_isolation(); 6456 } 6457 6458 static const struct x86_cpu_id pebs_ucodes[] = { 6459 X86_MATCH_VFM_STEPS(INTEL_SANDYBRIDGE, 7, 7, 0x00000028), 6460 X86_MATCH_VFM_STEPS(INTEL_SANDYBRIDGE_X, 6, 6, 0x00000618), 6461 X86_MATCH_VFM_STEPS(INTEL_SANDYBRIDGE_X, 7, 7, 0x0000070c), 6462 {} 6463 }; 6464 6465 static bool intel_snb_pebs_broken(void) 6466 { 6467 return !x86_match_min_microcode_rev(pebs_ucodes); 6468 } 6469 6470 static void intel_snb_check_microcode(void) 6471 { 6472 if (intel_snb_pebs_broken() == x86_pmu.pebs_broken) 6473 return; 6474 6475 /* 6476 * Serialized by the microcode lock.. 6477 */ 6478 if (x86_pmu.pebs_broken) { 6479 pr_info("PEBS enabled due to microcode update\n"); 6480 x86_pmu.pebs_broken = 0; 6481 } else { 6482 pr_info("PEBS disabled due to CPU errata, please upgrade microcode\n"); 6483 x86_pmu.pebs_broken = 1; 6484 } 6485 } 6486 6487 static bool is_lbr_from(unsigned long msr) 6488 { 6489 unsigned long lbr_from_nr = x86_pmu.lbr_from + x86_pmu.lbr_nr; 6490 6491 return x86_pmu.lbr_from <= msr && msr < lbr_from_nr; 6492 } 6493 6494 /* 6495 * Under certain circumstances, access certain MSR may cause #GP. 6496 * The function tests if the input MSR can be safely accessed. 6497 */ 6498 static bool check_msr(unsigned long msr, u64 mask) 6499 { 6500 u64 val_old, val_new, val_tmp; 6501 6502 /* 6503 * Disable the check for real HW, so we don't 6504 * mess with potentially enabled registers: 6505 */ 6506 if (!boot_cpu_has(X86_FEATURE_HYPERVISOR)) 6507 return true; 6508 6509 /* 6510 * Read the current value, change it and read it back to see if it 6511 * matches, this is needed to detect certain hardware emulators 6512 * (qemu/kvm) that don't trap on the MSR access and always return 0s. 6513 */ 6514 if (rdmsrq_safe(msr, &val_old)) 6515 return false; 6516 6517 /* 6518 * Only change the bits which can be updated by wrmsrq. 6519 */ 6520 val_tmp = val_old ^ mask; 6521 6522 if (is_lbr_from(msr)) 6523 val_tmp = lbr_from_signext_quirk_wr(val_tmp); 6524 6525 if (wrmsrq_safe(msr, val_tmp) || 6526 rdmsrq_safe(msr, &val_new)) 6527 return false; 6528 6529 /* 6530 * Quirk only affects validation in wrmsr(), so wrmsrq()'s value 6531 * should equal rdmsrq()'s even with the quirk. 6532 */ 6533 if (val_new != val_tmp) 6534 return false; 6535 6536 if (is_lbr_from(msr)) 6537 val_old = lbr_from_signext_quirk_wr(val_old); 6538 6539 /* Here it's sure that the MSR can be safely accessed. 6540 * Restore the old value and return. 6541 */ 6542 wrmsrq(msr, val_old); 6543 6544 return true; 6545 } 6546 6547 static __init void intel_sandybridge_quirk(void) 6548 { 6549 x86_pmu.check_microcode = intel_snb_check_microcode; 6550 cpus_read_lock(); 6551 intel_snb_check_microcode(); 6552 cpus_read_unlock(); 6553 } 6554 6555 static const struct { int id; char *name; } intel_arch_events_map[] __initconst = { 6556 { PERF_COUNT_HW_CPU_CYCLES, "cpu cycles" }, 6557 { PERF_COUNT_HW_INSTRUCTIONS, "instructions" }, 6558 { PERF_COUNT_HW_BUS_CYCLES, "bus cycles" }, 6559 { PERF_COUNT_HW_CACHE_REFERENCES, "cache references" }, 6560 { PERF_COUNT_HW_CACHE_MISSES, "cache misses" }, 6561 { PERF_COUNT_HW_BRANCH_INSTRUCTIONS, "branch instructions" }, 6562 { PERF_COUNT_HW_BRANCH_MISSES, "branch misses" }, 6563 }; 6564 6565 static __init void intel_arch_events_quirk(void) 6566 { 6567 int bit; 6568 6569 /* disable event that reported as not present by cpuid */ 6570 for_each_set_bit(bit, x86_pmu.events_mask, ARRAY_SIZE(intel_arch_events_map)) { 6571 intel_perfmon_event_map[intel_arch_events_map[bit].id] = 0; 6572 pr_warn("CPUID marked event: \'%s\' unavailable\n", 6573 intel_arch_events_map[bit].name); 6574 } 6575 } 6576 6577 static __init void intel_nehalem_quirk(void) 6578 { 6579 union cpuid10_ebx ebx; 6580 6581 ebx.full = x86_pmu.events_maskl; 6582 if (ebx.split.no_branch_misses_retired) { 6583 /* 6584 * Erratum AAJ80 detected, we work it around by using 6585 * the BR_MISP_EXEC.ANY event. This will over-count 6586 * branch-misses, but it's still much better than the 6587 * architectural event which is often completely bogus: 6588 */ 6589 intel_perfmon_event_map[PERF_COUNT_HW_BRANCH_MISSES] = 0x7f89; 6590 ebx.split.no_branch_misses_retired = 0; 6591 x86_pmu.events_maskl = ebx.full; 6592 pr_info("CPU erratum AAJ80 worked around\n"); 6593 } 6594 } 6595 6596 /* 6597 * enable software workaround for errata: 6598 * SNB: BJ122 6599 * IVB: BV98 6600 * HSW: HSD29 6601 * 6602 * Only needed when HT is enabled. However detecting 6603 * if HT is enabled is difficult (model specific). So instead, 6604 * we enable the workaround in the early boot, and verify if 6605 * it is needed in a later initcall phase once we have valid 6606 * topology information to check if HT is actually enabled 6607 */ 6608 static __init void intel_ht_bug(void) 6609 { 6610 x86_pmu.flags |= PMU_FL_EXCL_CNTRS | PMU_FL_EXCL_ENABLED; 6611 6612 x86_pmu.start_scheduling = intel_start_scheduling; 6613 x86_pmu.commit_scheduling = intel_commit_scheduling; 6614 x86_pmu.stop_scheduling = intel_stop_scheduling; 6615 } 6616 6617 EVENT_ATTR_STR(mem-loads, mem_ld_hsw, "event=0xcd,umask=0x1,ldlat=3"); 6618 EVENT_ATTR_STR(mem-stores, mem_st_hsw, "event=0xd0,umask=0x82") 6619 6620 /* Haswell special events */ 6621 EVENT_ATTR_STR(tx-start, tx_start, "event=0xc9,umask=0x1"); 6622 EVENT_ATTR_STR(tx-commit, tx_commit, "event=0xc9,umask=0x2"); 6623 EVENT_ATTR_STR(tx-abort, tx_abort, "event=0xc9,umask=0x4"); 6624 EVENT_ATTR_STR(tx-capacity, tx_capacity, "event=0x54,umask=0x2"); 6625 EVENT_ATTR_STR(tx-conflict, tx_conflict, "event=0x54,umask=0x1"); 6626 EVENT_ATTR_STR(el-start, el_start, "event=0xc8,umask=0x1"); 6627 EVENT_ATTR_STR(el-commit, el_commit, "event=0xc8,umask=0x2"); 6628 EVENT_ATTR_STR(el-abort, el_abort, "event=0xc8,umask=0x4"); 6629 EVENT_ATTR_STR(el-capacity, el_capacity, "event=0x54,umask=0x2"); 6630 EVENT_ATTR_STR(el-conflict, el_conflict, "event=0x54,umask=0x1"); 6631 EVENT_ATTR_STR(cycles-t, cycles_t, "event=0x3c,in_tx=1"); 6632 EVENT_ATTR_STR(cycles-ct, cycles_ct, "event=0x3c,in_tx=1,in_tx_cp=1"); 6633 6634 static struct attribute *hsw_events_attrs[] = { 6635 EVENT_PTR(td_slots_issued), 6636 EVENT_PTR(td_slots_retired), 6637 EVENT_PTR(td_fetch_bubbles), 6638 EVENT_PTR(td_total_slots), 6639 EVENT_PTR(td_total_slots_scale), 6640 EVENT_PTR(td_recovery_bubbles), 6641 EVENT_PTR(td_recovery_bubbles_scale), 6642 NULL 6643 }; 6644 6645 static struct attribute *hsw_mem_events_attrs[] = { 6646 EVENT_PTR(mem_ld_hsw), 6647 EVENT_PTR(mem_st_hsw), 6648 NULL, 6649 }; 6650 6651 static struct attribute *hsw_tsx_events_attrs[] = { 6652 EVENT_PTR(tx_start), 6653 EVENT_PTR(tx_commit), 6654 EVENT_PTR(tx_abort), 6655 EVENT_PTR(tx_capacity), 6656 EVENT_PTR(tx_conflict), 6657 EVENT_PTR(el_start), 6658 EVENT_PTR(el_commit), 6659 EVENT_PTR(el_abort), 6660 EVENT_PTR(el_capacity), 6661 EVENT_PTR(el_conflict), 6662 EVENT_PTR(cycles_t), 6663 EVENT_PTR(cycles_ct), 6664 NULL 6665 }; 6666 6667 EVENT_ATTR_STR(tx-capacity-read, tx_capacity_read, "event=0x54,umask=0x80"); 6668 EVENT_ATTR_STR(tx-capacity-write, tx_capacity_write, "event=0x54,umask=0x2"); 6669 EVENT_ATTR_STR(el-capacity-read, el_capacity_read, "event=0x54,umask=0x80"); 6670 EVENT_ATTR_STR(el-capacity-write, el_capacity_write, "event=0x54,umask=0x2"); 6671 6672 static struct attribute *icl_events_attrs[] = { 6673 EVENT_PTR(mem_ld_hsw), 6674 EVENT_PTR(mem_st_hsw), 6675 NULL, 6676 }; 6677 6678 static struct attribute *icl_td_events_attrs[] = { 6679 EVENT_PTR(slots), 6680 EVENT_PTR(td_retiring), 6681 EVENT_PTR(td_bad_spec), 6682 EVENT_PTR(td_fe_bound), 6683 EVENT_PTR(td_be_bound), 6684 NULL, 6685 }; 6686 6687 static struct attribute *icl_tsx_events_attrs[] = { 6688 EVENT_PTR(tx_start), 6689 EVENT_PTR(tx_abort), 6690 EVENT_PTR(tx_commit), 6691 EVENT_PTR(tx_capacity_read), 6692 EVENT_PTR(tx_capacity_write), 6693 EVENT_PTR(tx_conflict), 6694 EVENT_PTR(el_start), 6695 EVENT_PTR(el_abort), 6696 EVENT_PTR(el_commit), 6697 EVENT_PTR(el_capacity_read), 6698 EVENT_PTR(el_capacity_write), 6699 EVENT_PTR(el_conflict), 6700 EVENT_PTR(cycles_t), 6701 EVENT_PTR(cycles_ct), 6702 NULL, 6703 }; 6704 6705 6706 EVENT_ATTR_STR(mem-stores, mem_st_spr, "event=0xcd,umask=0x2"); 6707 EVENT_ATTR_STR(mem-loads-aux, mem_ld_aux, "event=0x03,umask=0x82"); 6708 6709 static struct attribute *glc_events_attrs[] = { 6710 EVENT_PTR(mem_ld_hsw), 6711 EVENT_PTR(mem_st_spr), 6712 EVENT_PTR(mem_ld_aux), 6713 NULL, 6714 }; 6715 6716 static struct attribute *glc_td_events_attrs[] = { 6717 EVENT_PTR(slots), 6718 EVENT_PTR(td_retiring), 6719 EVENT_PTR(td_bad_spec), 6720 EVENT_PTR(td_fe_bound), 6721 EVENT_PTR(td_be_bound), 6722 EVENT_PTR(td_heavy_ops), 6723 EVENT_PTR(td_br_mispredict), 6724 EVENT_PTR(td_fetch_lat), 6725 EVENT_PTR(td_mem_bound), 6726 NULL, 6727 }; 6728 6729 static struct attribute *glc_tsx_events_attrs[] = { 6730 EVENT_PTR(tx_start), 6731 EVENT_PTR(tx_abort), 6732 EVENT_PTR(tx_commit), 6733 EVENT_PTR(tx_capacity_read), 6734 EVENT_PTR(tx_capacity_write), 6735 EVENT_PTR(tx_conflict), 6736 EVENT_PTR(cycles_t), 6737 EVENT_PTR(cycles_ct), 6738 NULL, 6739 }; 6740 6741 static ssize_t freeze_on_smi_show(struct device *cdev, 6742 struct device_attribute *attr, 6743 char *buf) 6744 { 6745 return sprintf(buf, "%lu\n", x86_pmu.attr_freeze_on_smi); 6746 } 6747 6748 static DEFINE_MUTEX(freeze_on_smi_mutex); 6749 6750 static ssize_t freeze_on_smi_store(struct device *cdev, 6751 struct device_attribute *attr, 6752 const char *buf, size_t count) 6753 { 6754 unsigned long val; 6755 ssize_t ret; 6756 6757 ret = kstrtoul(buf, 0, &val); 6758 if (ret) 6759 return ret; 6760 6761 if (val > 1) 6762 return -EINVAL; 6763 6764 mutex_lock(&freeze_on_smi_mutex); 6765 6766 if (x86_pmu.attr_freeze_on_smi == val) 6767 goto done; 6768 6769 x86_pmu.attr_freeze_on_smi = val; 6770 6771 cpus_read_lock(); 6772 on_each_cpu(flip_smm_bit, &val, 1); 6773 cpus_read_unlock(); 6774 done: 6775 mutex_unlock(&freeze_on_smi_mutex); 6776 6777 return count; 6778 } 6779 6780 static void update_tfa_sched(void *ignored) 6781 { 6782 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events); 6783 6784 /* 6785 * check if PMC3 is used 6786 * and if so force schedule out for all event types all contexts 6787 */ 6788 if (test_bit(3, cpuc->active_mask)) 6789 perf_pmu_resched(x86_get_pmu(smp_processor_id())); 6790 } 6791 6792 static ssize_t show_sysctl_tfa(struct device *cdev, 6793 struct device_attribute *attr, 6794 char *buf) 6795 { 6796 return snprintf(buf, 40, "%d\n", allow_tsx_force_abort); 6797 } 6798 6799 static ssize_t set_sysctl_tfa(struct device *cdev, 6800 struct device_attribute *attr, 6801 const char *buf, size_t count) 6802 { 6803 bool val; 6804 ssize_t ret; 6805 6806 ret = kstrtobool(buf, &val); 6807 if (ret) 6808 return ret; 6809 6810 /* no change */ 6811 if (val == allow_tsx_force_abort) 6812 return count; 6813 6814 allow_tsx_force_abort = val; 6815 6816 cpus_read_lock(); 6817 on_each_cpu(update_tfa_sched, NULL, 1); 6818 cpus_read_unlock(); 6819 6820 return count; 6821 } 6822 6823 6824 static DEVICE_ATTR_RW(freeze_on_smi); 6825 6826 static ssize_t branches_show(struct device *cdev, 6827 struct device_attribute *attr, 6828 char *buf) 6829 { 6830 return snprintf(buf, PAGE_SIZE, "%d\n", x86_pmu.lbr_nr); 6831 } 6832 6833 static DEVICE_ATTR_RO(branches); 6834 6835 static ssize_t branch_counter_nr_show(struct device *cdev, 6836 struct device_attribute *attr, 6837 char *buf) 6838 { 6839 return snprintf(buf, PAGE_SIZE, "%d\n", fls(x86_pmu.lbr_counters)); 6840 } 6841 6842 static DEVICE_ATTR_RO(branch_counter_nr); 6843 6844 static ssize_t branch_counter_width_show(struct device *cdev, 6845 struct device_attribute *attr, 6846 char *buf) 6847 { 6848 return snprintf(buf, PAGE_SIZE, "%d\n", LBR_INFO_BR_CNTR_BITS); 6849 } 6850 6851 static DEVICE_ATTR_RO(branch_counter_width); 6852 6853 static struct attribute *lbr_attrs[] = { 6854 &dev_attr_branches.attr, 6855 &dev_attr_branch_counter_nr.attr, 6856 &dev_attr_branch_counter_width.attr, 6857 NULL 6858 }; 6859 6860 static umode_t 6861 lbr_is_visible(struct kobject *kobj, struct attribute *attr, int i) 6862 { 6863 /* branches */ 6864 if (i == 0) 6865 return x86_pmu.lbr_nr ? attr->mode : 0; 6866 6867 return (x86_pmu.flags & PMU_FL_BR_CNTR) ? attr->mode : 0; 6868 } 6869 6870 static char pmu_name_str[30]; 6871 6872 static DEVICE_STRING_ATTR_RO(pmu_name, 0444, pmu_name_str); 6873 6874 static struct attribute *intel_pmu_caps_attrs[] = { 6875 &dev_attr_pmu_name.attr.attr, 6876 NULL 6877 }; 6878 6879 static DEVICE_ATTR(allow_tsx_force_abort, 0644, 6880 show_sysctl_tfa, 6881 set_sysctl_tfa); 6882 6883 static struct attribute *intel_pmu_attrs[] = { 6884 &dev_attr_freeze_on_smi.attr, 6885 &dev_attr_allow_tsx_force_abort.attr, 6886 NULL, 6887 }; 6888 6889 static umode_t 6890 default_is_visible(struct kobject *kobj, struct attribute *attr, int i) 6891 { 6892 if (attr == &dev_attr_allow_tsx_force_abort.attr) 6893 return x86_pmu.flags & PMU_FL_TFA ? attr->mode : 0; 6894 6895 return attr->mode; 6896 } 6897 6898 static umode_t 6899 tsx_is_visible(struct kobject *kobj, struct attribute *attr, int i) 6900 { 6901 return boot_cpu_has(X86_FEATURE_RTM) ? attr->mode : 0; 6902 } 6903 6904 static umode_t 6905 pebs_is_visible(struct kobject *kobj, struct attribute *attr, int i) 6906 { 6907 return intel_pmu_has_pebs() ? attr->mode : 0; 6908 } 6909 6910 static umode_t 6911 mem_is_visible(struct kobject *kobj, struct attribute *attr, int i) 6912 { 6913 if (attr == &event_attr_mem_ld_aux.attr.attr) 6914 return x86_pmu.flags & PMU_FL_MEM_LOADS_AUX ? attr->mode : 0; 6915 6916 return pebs_is_visible(kobj, attr, i); 6917 } 6918 6919 static umode_t 6920 exra_is_visible(struct kobject *kobj, struct attribute *attr, int i) 6921 { 6922 return x86_pmu.version >= 2 ? attr->mode : 0; 6923 } 6924 6925 static umode_t 6926 td_is_visible(struct kobject *kobj, struct attribute *attr, int i) 6927 { 6928 /* 6929 * Hide the perf metrics topdown events 6930 * if the feature is not enumerated. 6931 */ 6932 if (x86_pmu.num_topdown_events) 6933 return x86_pmu.intel_cap.perf_metrics ? attr->mode : 0; 6934 6935 return attr->mode; 6936 } 6937 6938 PMU_FORMAT_ATTR(acr_mask, "config2:0-63"); 6939 6940 static struct attribute *format_acr_attrs[] = { 6941 &format_attr_acr_mask.attr, 6942 NULL 6943 }; 6944 6945 static umode_t 6946 acr_is_visible(struct kobject *kobj, struct attribute *attr, int i) 6947 { 6948 struct device *dev = kobj_to_dev(kobj); 6949 6950 return intel_pmu_has_acr(dev_get_drvdata(dev)) ? attr->mode : 0; 6951 } 6952 6953 static struct attribute_group group_events_td = { 6954 .name = "events", 6955 .is_visible = td_is_visible, 6956 }; 6957 6958 static struct attribute_group group_events_mem = { 6959 .name = "events", 6960 .is_visible = mem_is_visible, 6961 }; 6962 6963 static struct attribute_group group_events_tsx = { 6964 .name = "events", 6965 .is_visible = tsx_is_visible, 6966 }; 6967 6968 static struct attribute_group group_caps_gen = { 6969 .name = "caps", 6970 .attrs = intel_pmu_caps_attrs, 6971 }; 6972 6973 static struct attribute_group group_caps_lbr = { 6974 .name = "caps", 6975 .attrs = lbr_attrs, 6976 .is_visible = lbr_is_visible, 6977 }; 6978 6979 static struct attribute_group group_format_extra = { 6980 .name = "format", 6981 .is_visible = exra_is_visible, 6982 }; 6983 6984 static struct attribute_group group_format_extra_skl = { 6985 .name = "format", 6986 .is_visible = exra_is_visible, 6987 }; 6988 6989 static struct attribute_group group_format_evtsel_ext = { 6990 .name = "format", 6991 .attrs = format_evtsel_ext_attrs, 6992 .is_visible = evtsel_ext_is_visible, 6993 }; 6994 6995 static struct attribute_group group_format_acr = { 6996 .name = "format", 6997 .attrs = format_acr_attrs, 6998 .is_visible = acr_is_visible, 6999 }; 7000 7001 static struct attribute_group group_default = { 7002 .attrs = intel_pmu_attrs, 7003 .is_visible = default_is_visible, 7004 }; 7005 7006 static const struct attribute_group *attr_update[] = { 7007 &group_events_td, 7008 &group_events_mem, 7009 &group_events_tsx, 7010 &group_caps_gen, 7011 &group_caps_lbr, 7012 &group_format_extra, 7013 &group_format_extra_skl, 7014 &group_format_evtsel_ext, 7015 &group_format_acr, 7016 &group_default, 7017 NULL, 7018 }; 7019 7020 EVENT_ATTR_STR_HYBRID(slots, slots_adl, "event=0x00,umask=0x4", hybrid_big); 7021 EVENT_ATTR_STR_HYBRID(topdown-retiring, td_retiring_adl, "event=0xc2,umask=0x0;event=0x00,umask=0x80", hybrid_big_small); 7022 EVENT_ATTR_STR_HYBRID(topdown-bad-spec, td_bad_spec_adl, "event=0x73,umask=0x0;event=0x00,umask=0x81", hybrid_big_small); 7023 EVENT_ATTR_STR_HYBRID(topdown-fe-bound, td_fe_bound_adl, "event=0x71,umask=0x0;event=0x00,umask=0x82", hybrid_big_small); 7024 EVENT_ATTR_STR_HYBRID(topdown-be-bound, td_be_bound_adl, "event=0x74,umask=0x0;event=0x00,umask=0x83", hybrid_big_small); 7025 EVENT_ATTR_STR_HYBRID(topdown-heavy-ops, td_heavy_ops_adl, "event=0x00,umask=0x84", hybrid_big); 7026 EVENT_ATTR_STR_HYBRID(topdown-br-mispredict, td_br_mis_adl, "event=0x00,umask=0x85", hybrid_big); 7027 EVENT_ATTR_STR_HYBRID(topdown-fetch-lat, td_fetch_lat_adl, "event=0x00,umask=0x86", hybrid_big); 7028 EVENT_ATTR_STR_HYBRID(topdown-mem-bound, td_mem_bound_adl, "event=0x00,umask=0x87", hybrid_big); 7029 7030 static struct attribute *adl_hybrid_events_attrs[] = { 7031 EVENT_PTR(slots_adl), 7032 EVENT_PTR(td_retiring_adl), 7033 EVENT_PTR(td_bad_spec_adl), 7034 EVENT_PTR(td_fe_bound_adl), 7035 EVENT_PTR(td_be_bound_adl), 7036 EVENT_PTR(td_heavy_ops_adl), 7037 EVENT_PTR(td_br_mis_adl), 7038 EVENT_PTR(td_fetch_lat_adl), 7039 EVENT_PTR(td_mem_bound_adl), 7040 NULL, 7041 }; 7042 7043 EVENT_ATTR_STR_HYBRID(topdown-retiring, td_retiring_lnl, "event=0xc2,umask=0x02;event=0x00,umask=0x80", hybrid_big_small); 7044 EVENT_ATTR_STR_HYBRID(topdown-fe-bound, td_fe_bound_lnl, "event=0x9c,umask=0x01;event=0x00,umask=0x82", hybrid_big_small); 7045 EVENT_ATTR_STR_HYBRID(topdown-be-bound, td_be_bound_lnl, "event=0xa4,umask=0x02;event=0x00,umask=0x83", hybrid_big_small); 7046 7047 static struct attribute *lnl_hybrid_events_attrs[] = { 7048 EVENT_PTR(slots_adl), 7049 EVENT_PTR(td_retiring_lnl), 7050 EVENT_PTR(td_bad_spec_adl), 7051 EVENT_PTR(td_fe_bound_lnl), 7052 EVENT_PTR(td_be_bound_lnl), 7053 EVENT_PTR(td_heavy_ops_adl), 7054 EVENT_PTR(td_br_mis_adl), 7055 EVENT_PTR(td_fetch_lat_adl), 7056 EVENT_PTR(td_mem_bound_adl), 7057 NULL 7058 }; 7059 7060 /* The event string must be in PMU IDX order. */ 7061 EVENT_ATTR_STR_HYBRID(topdown-retiring, 7062 td_retiring_arl_h, 7063 "event=0xc2,umask=0x02;event=0x00,umask=0x80;event=0xc2,umask=0x0", 7064 hybrid_big_small_tiny); 7065 EVENT_ATTR_STR_HYBRID(topdown-bad-spec, 7066 td_bad_spec_arl_h, 7067 "event=0x73,umask=0x0;event=0x00,umask=0x81;event=0x73,umask=0x0", 7068 hybrid_big_small_tiny); 7069 EVENT_ATTR_STR_HYBRID(topdown-fe-bound, 7070 td_fe_bound_arl_h, 7071 "event=0x9c,umask=0x01;event=0x00,umask=0x82;event=0x71,umask=0x0", 7072 hybrid_big_small_tiny); 7073 EVENT_ATTR_STR_HYBRID(topdown-be-bound, 7074 td_be_bound_arl_h, 7075 "event=0xa4,umask=0x02;event=0x00,umask=0x83;event=0x74,umask=0x0", 7076 hybrid_big_small_tiny); 7077 7078 static struct attribute *arl_h_hybrid_events_attrs[] = { 7079 EVENT_PTR(slots_adl), 7080 EVENT_PTR(td_retiring_arl_h), 7081 EVENT_PTR(td_bad_spec_arl_h), 7082 EVENT_PTR(td_fe_bound_arl_h), 7083 EVENT_PTR(td_be_bound_arl_h), 7084 EVENT_PTR(td_heavy_ops_adl), 7085 EVENT_PTR(td_br_mis_adl), 7086 EVENT_PTR(td_fetch_lat_adl), 7087 EVENT_PTR(td_mem_bound_adl), 7088 NULL, 7089 }; 7090 7091 /* Must be in IDX order */ 7092 EVENT_ATTR_STR_HYBRID(mem-loads, mem_ld_adl, "event=0xd0,umask=0x5,ldlat=3;event=0xcd,umask=0x1,ldlat=3", hybrid_big_small); 7093 EVENT_ATTR_STR_HYBRID(mem-stores, mem_st_adl, "event=0xd0,umask=0x6;event=0xcd,umask=0x2", hybrid_big_small); 7094 EVENT_ATTR_STR_HYBRID(mem-loads-aux, mem_ld_aux_adl, "event=0x03,umask=0x82", hybrid_big); 7095 7096 static struct attribute *adl_hybrid_mem_attrs[] = { 7097 EVENT_PTR(mem_ld_adl), 7098 EVENT_PTR(mem_st_adl), 7099 EVENT_PTR(mem_ld_aux_adl), 7100 NULL, 7101 }; 7102 7103 static struct attribute *mtl_hybrid_mem_attrs[] = { 7104 EVENT_PTR(mem_ld_adl), 7105 EVENT_PTR(mem_st_adl), 7106 NULL 7107 }; 7108 7109 EVENT_ATTR_STR_HYBRID(mem-loads, 7110 mem_ld_arl_h, 7111 "event=0xd0,umask=0x5,ldlat=3;event=0xcd,umask=0x1,ldlat=3;event=0xd0,umask=0x5,ldlat=3", 7112 hybrid_big_small_tiny); 7113 EVENT_ATTR_STR_HYBRID(mem-stores, 7114 mem_st_arl_h, 7115 "event=0xd0,umask=0x6;event=0xcd,umask=0x2;event=0xd0,umask=0x6", 7116 hybrid_big_small_tiny); 7117 7118 static struct attribute *arl_h_hybrid_mem_attrs[] = { 7119 EVENT_PTR(mem_ld_arl_h), 7120 EVENT_PTR(mem_st_arl_h), 7121 NULL, 7122 }; 7123 7124 EVENT_ATTR_STR_HYBRID(tx-start, tx_start_adl, "event=0xc9,umask=0x1", hybrid_big); 7125 EVENT_ATTR_STR_HYBRID(tx-commit, tx_commit_adl, "event=0xc9,umask=0x2", hybrid_big); 7126 EVENT_ATTR_STR_HYBRID(tx-abort, tx_abort_adl, "event=0xc9,umask=0x4", hybrid_big); 7127 EVENT_ATTR_STR_HYBRID(tx-conflict, tx_conflict_adl, "event=0x54,umask=0x1", hybrid_big); 7128 EVENT_ATTR_STR_HYBRID(cycles-t, cycles_t_adl, "event=0x3c,in_tx=1", hybrid_big); 7129 EVENT_ATTR_STR_HYBRID(cycles-ct, cycles_ct_adl, "event=0x3c,in_tx=1,in_tx_cp=1", hybrid_big); 7130 EVENT_ATTR_STR_HYBRID(tx-capacity-read, tx_capacity_read_adl, "event=0x54,umask=0x80", hybrid_big); 7131 EVENT_ATTR_STR_HYBRID(tx-capacity-write, tx_capacity_write_adl, "event=0x54,umask=0x2", hybrid_big); 7132 7133 static struct attribute *adl_hybrid_tsx_attrs[] = { 7134 EVENT_PTR(tx_start_adl), 7135 EVENT_PTR(tx_abort_adl), 7136 EVENT_PTR(tx_commit_adl), 7137 EVENT_PTR(tx_capacity_read_adl), 7138 EVENT_PTR(tx_capacity_write_adl), 7139 EVENT_PTR(tx_conflict_adl), 7140 EVENT_PTR(cycles_t_adl), 7141 EVENT_PTR(cycles_ct_adl), 7142 NULL, 7143 }; 7144 7145 FORMAT_ATTR_HYBRID(in_tx, hybrid_big); 7146 FORMAT_ATTR_HYBRID(in_tx_cp, hybrid_big); 7147 FORMAT_ATTR_HYBRID(offcore_rsp, hybrid_big_small_tiny); 7148 FORMAT_ATTR_HYBRID(ldlat, hybrid_big_small_tiny); 7149 FORMAT_ATTR_HYBRID(frontend, hybrid_big); 7150 7151 #define ADL_HYBRID_RTM_FORMAT_ATTR \ 7152 FORMAT_HYBRID_PTR(in_tx), \ 7153 FORMAT_HYBRID_PTR(in_tx_cp) 7154 7155 #define ADL_HYBRID_FORMAT_ATTR \ 7156 FORMAT_HYBRID_PTR(offcore_rsp), \ 7157 FORMAT_HYBRID_PTR(ldlat), \ 7158 FORMAT_HYBRID_PTR(frontend) 7159 7160 static struct attribute *adl_hybrid_extra_attr_rtm[] = { 7161 ADL_HYBRID_RTM_FORMAT_ATTR, 7162 ADL_HYBRID_FORMAT_ATTR, 7163 NULL 7164 }; 7165 7166 static struct attribute *adl_hybrid_extra_attr[] = { 7167 ADL_HYBRID_FORMAT_ATTR, 7168 NULL 7169 }; 7170 7171 FORMAT_ATTR_HYBRID(snoop_rsp, hybrid_small_tiny); 7172 7173 static struct attribute *mtl_hybrid_extra_attr_rtm[] = { 7174 ADL_HYBRID_RTM_FORMAT_ATTR, 7175 ADL_HYBRID_FORMAT_ATTR, 7176 FORMAT_HYBRID_PTR(snoop_rsp), 7177 NULL 7178 }; 7179 7180 static struct attribute *mtl_hybrid_extra_attr[] = { 7181 ADL_HYBRID_FORMAT_ATTR, 7182 FORMAT_HYBRID_PTR(snoop_rsp), 7183 NULL 7184 }; 7185 7186 static bool is_attr_for_this_pmu(struct kobject *kobj, struct attribute *attr) 7187 { 7188 struct device *dev = kobj_to_dev(kobj); 7189 struct x86_hybrid_pmu *pmu = 7190 container_of(dev_get_drvdata(dev), struct x86_hybrid_pmu, pmu); 7191 struct perf_pmu_events_hybrid_attr *pmu_attr = 7192 container_of(attr, struct perf_pmu_events_hybrid_attr, attr.attr); 7193 7194 return pmu->pmu_type & pmu_attr->pmu_type; 7195 } 7196 7197 static umode_t hybrid_events_is_visible(struct kobject *kobj, 7198 struct attribute *attr, int i) 7199 { 7200 return is_attr_for_this_pmu(kobj, attr) ? attr->mode : 0; 7201 } 7202 7203 static inline int hybrid_find_supported_cpu(struct x86_hybrid_pmu *pmu) 7204 { 7205 int cpu = cpumask_first(&pmu->supported_cpus); 7206 7207 return (cpu >= nr_cpu_ids) ? -1 : cpu; 7208 } 7209 7210 static umode_t hybrid_tsx_is_visible(struct kobject *kobj, 7211 struct attribute *attr, int i) 7212 { 7213 struct device *dev = kobj_to_dev(kobj); 7214 struct x86_hybrid_pmu *pmu = 7215 container_of(dev_get_drvdata(dev), struct x86_hybrid_pmu, pmu); 7216 int cpu = hybrid_find_supported_cpu(pmu); 7217 7218 return (cpu >= 0) && is_attr_for_this_pmu(kobj, attr) && cpu_has(&cpu_data(cpu), X86_FEATURE_RTM) ? attr->mode : 0; 7219 } 7220 7221 static umode_t hybrid_format_is_visible(struct kobject *kobj, 7222 struct attribute *attr, int i) 7223 { 7224 struct device *dev = kobj_to_dev(kobj); 7225 struct x86_hybrid_pmu *pmu = 7226 container_of(dev_get_drvdata(dev), struct x86_hybrid_pmu, pmu); 7227 struct perf_pmu_format_hybrid_attr *pmu_attr = 7228 container_of(attr, struct perf_pmu_format_hybrid_attr, attr.attr); 7229 int cpu = hybrid_find_supported_cpu(pmu); 7230 7231 return (cpu >= 0) && (pmu->pmu_type & pmu_attr->pmu_type) ? attr->mode : 0; 7232 } 7233 7234 static umode_t hybrid_td_is_visible(struct kobject *kobj, 7235 struct attribute *attr, int i) 7236 { 7237 struct device *dev = kobj_to_dev(kobj); 7238 struct x86_hybrid_pmu *pmu = 7239 container_of(dev_get_drvdata(dev), struct x86_hybrid_pmu, pmu); 7240 7241 if (!is_attr_for_this_pmu(kobj, attr)) 7242 return 0; 7243 7244 7245 /* Only the big core supports perf metrics */ 7246 if (pmu->pmu_type == hybrid_big) 7247 return pmu->intel_cap.perf_metrics ? attr->mode : 0; 7248 7249 return attr->mode; 7250 } 7251 7252 static struct attribute_group hybrid_group_events_td = { 7253 .name = "events", 7254 .is_visible = hybrid_td_is_visible, 7255 }; 7256 7257 static struct attribute_group hybrid_group_events_mem = { 7258 .name = "events", 7259 .is_visible = hybrid_events_is_visible, 7260 }; 7261 7262 static struct attribute_group hybrid_group_events_tsx = { 7263 .name = "events", 7264 .is_visible = hybrid_tsx_is_visible, 7265 }; 7266 7267 static struct attribute_group hybrid_group_format_extra = { 7268 .name = "format", 7269 .is_visible = hybrid_format_is_visible, 7270 }; 7271 7272 static ssize_t intel_hybrid_get_attr_cpus(struct device *dev, 7273 struct device_attribute *attr, 7274 char *buf) 7275 { 7276 struct x86_hybrid_pmu *pmu = 7277 container_of(dev_get_drvdata(dev), struct x86_hybrid_pmu, pmu); 7278 7279 return cpumap_print_to_pagebuf(true, buf, &pmu->supported_cpus); 7280 } 7281 7282 static DEVICE_ATTR(cpus, S_IRUGO, intel_hybrid_get_attr_cpus, NULL); 7283 static struct attribute *intel_hybrid_cpus_attrs[] = { 7284 &dev_attr_cpus.attr, 7285 NULL, 7286 }; 7287 7288 static struct attribute_group hybrid_group_cpus = { 7289 .attrs = intel_hybrid_cpus_attrs, 7290 }; 7291 7292 static const struct attribute_group *hybrid_attr_update[] = { 7293 &hybrid_group_events_td, 7294 &hybrid_group_events_mem, 7295 &hybrid_group_events_tsx, 7296 &group_caps_gen, 7297 &group_caps_lbr, 7298 &hybrid_group_format_extra, 7299 &group_format_evtsel_ext, 7300 &group_format_acr, 7301 &group_default, 7302 &hybrid_group_cpus, 7303 NULL, 7304 }; 7305 7306 static struct attribute *empty_attrs; 7307 7308 static void intel_pmu_check_event_constraints(struct event_constraint *event_constraints, 7309 u64 cntr_mask, 7310 u64 fixed_cntr_mask, 7311 u64 intel_ctrl) 7312 { 7313 struct event_constraint *c; 7314 7315 if (!event_constraints) 7316 return; 7317 7318 /* 7319 * event on fixed counter2 (REF_CYCLES) only works on this 7320 * counter, so do not extend mask to generic counters 7321 */ 7322 for_each_event_constraint(c, event_constraints) { 7323 /* 7324 * Don't extend the topdown slots and metrics 7325 * events to the generic counters. 7326 */ 7327 if (c->idxmsk64 & INTEL_PMC_MSK_TOPDOWN) { 7328 /* 7329 * Disable topdown slots and metrics events, 7330 * if slots event is not in CPUID. 7331 */ 7332 if (!(INTEL_PMC_MSK_FIXED_SLOTS & intel_ctrl)) 7333 c->idxmsk64 = 0; 7334 c->weight = hweight64(c->idxmsk64); 7335 continue; 7336 } 7337 7338 if (c->cmask == FIXED_EVENT_FLAGS) { 7339 /* Disabled fixed counters which are not in CPUID */ 7340 c->idxmsk64 &= intel_ctrl; 7341 7342 /* 7343 * Don't extend the pseudo-encoding to the 7344 * generic counters 7345 */ 7346 if (!use_fixed_pseudo_encoding(c->code)) 7347 c->idxmsk64 |= cntr_mask; 7348 } 7349 c->idxmsk64 &= cntr_mask | (fixed_cntr_mask << INTEL_PMC_IDX_FIXED); 7350 c->weight = hweight64(c->idxmsk64); 7351 } 7352 } 7353 7354 static void intel_pmu_check_extra_regs(struct extra_reg *extra_regs) 7355 { 7356 struct extra_reg *er; 7357 7358 /* 7359 * Access extra MSR may cause #GP under certain circumstances. 7360 * E.g. KVM doesn't support offcore event 7361 * Check all extra_regs here. 7362 */ 7363 if (!extra_regs) 7364 return; 7365 7366 for (er = extra_regs; er->msr; er++) { 7367 er->extra_msr_access = check_msr(er->msr, 0x11UL); 7368 /* Disable LBR select mapping */ 7369 if ((er->idx == EXTRA_REG_LBR) && !er->extra_msr_access) 7370 x86_pmu.lbr_sel_map = NULL; 7371 } 7372 } 7373 7374 static inline int intel_pmu_v6_addr_offset(int index, bool eventsel) 7375 { 7376 return MSR_IA32_PMC_V6_STEP * index; 7377 } 7378 7379 static const struct { enum hybrid_pmu_type id; char *name; } intel_hybrid_pmu_type_map[] __initconst = { 7380 { hybrid_small, "cpu_atom" }, 7381 { hybrid_big, "cpu_core" }, 7382 { hybrid_tiny, "cpu_lowpower" }, 7383 }; 7384 7385 static __always_inline int intel_pmu_init_hybrid(enum hybrid_pmu_type pmus) 7386 { 7387 unsigned long pmus_mask = pmus; 7388 struct x86_hybrid_pmu *pmu; 7389 int idx = 0, bit; 7390 7391 x86_pmu.num_hybrid_pmus = hweight_long(pmus_mask); 7392 x86_pmu.hybrid_pmu = kzalloc_objs(struct x86_hybrid_pmu, 7393 x86_pmu.num_hybrid_pmus); 7394 if (!x86_pmu.hybrid_pmu) 7395 return -ENOMEM; 7396 7397 static_branch_enable(&perf_is_hybrid); 7398 x86_pmu.filter = intel_pmu_filter; 7399 7400 for_each_set_bit(bit, &pmus_mask, ARRAY_SIZE(intel_hybrid_pmu_type_map)) { 7401 pmu = &x86_pmu.hybrid_pmu[idx++]; 7402 pmu->pmu_type = intel_hybrid_pmu_type_map[bit].id; 7403 pmu->name = intel_hybrid_pmu_type_map[bit].name; 7404 7405 pmu->cntr_mask64 = x86_pmu.cntr_mask64; 7406 pmu->fixed_cntr_mask64 = x86_pmu.fixed_cntr_mask64; 7407 pmu->pebs_events_mask = intel_pmu_pebs_mask(pmu->cntr_mask64); 7408 pmu->config_mask = X86_RAW_EVENT_MASK; 7409 pmu->unconstrained = (struct event_constraint) 7410 __EVENT_CONSTRAINT(0, pmu->cntr_mask64, 7411 0, x86_pmu_num_counters(&pmu->pmu), 0, 0); 7412 7413 pmu->intel_cap.capabilities = x86_pmu.intel_cap.capabilities; 7414 if (pmu->pmu_type & hybrid_small_tiny) { 7415 pmu->intel_cap.perf_metrics = 0; 7416 pmu->mid_ack = true; 7417 } else if (pmu->pmu_type & hybrid_big) { 7418 pmu->intel_cap.perf_metrics = 1; 7419 pmu->late_ack = true; 7420 } 7421 } 7422 7423 return 0; 7424 } 7425 7426 static __always_inline void intel_pmu_ref_cycles_ext(void) 7427 { 7428 if (!(x86_pmu.events_maskl & (INTEL_PMC_MSK_FIXED_REF_CYCLES >> INTEL_PMC_IDX_FIXED))) 7429 intel_perfmon_event_map[PERF_COUNT_HW_REF_CPU_CYCLES] = 0x013c; 7430 } 7431 7432 static __always_inline void intel_pmu_init_glc(struct pmu *pmu) 7433 { 7434 x86_pmu.late_ack = true; 7435 x86_pmu.limit_period = glc_limit_period; 7436 x86_pmu.pebs_aliases = NULL; 7437 x86_pmu.pebs_prec_dist = true; 7438 x86_pmu.pebs_block = true; 7439 x86_pmu.flags |= PMU_FL_HAS_RSP_1; 7440 x86_pmu.flags |= PMU_FL_NO_HT_SHARING; 7441 x86_pmu.flags |= PMU_FL_INSTR_LATENCY; 7442 x86_pmu.rtm_abort_event = X86_CONFIG(.event=0xc9, .umask=0x04); 7443 x86_pmu.lbr_pt_coexist = true; 7444 x86_pmu.num_topdown_events = 8; 7445 static_call_update(intel_pmu_update_topdown_event, 7446 &icl_update_topdown_event); 7447 static_call_update(intel_pmu_set_topdown_event_period, 7448 &icl_set_topdown_event_period); 7449 7450 memcpy(hybrid_var(pmu, hw_cache_event_ids), glc_hw_cache_event_ids, sizeof(hw_cache_event_ids)); 7451 memcpy(hybrid_var(pmu, hw_cache_extra_regs), glc_hw_cache_extra_regs, sizeof(hw_cache_extra_regs)); 7452 hybrid(pmu, event_constraints) = intel_glc_event_constraints; 7453 hybrid(pmu, pebs_constraints) = intel_glc_pebs_event_constraints; 7454 7455 intel_pmu_ref_cycles_ext(); 7456 } 7457 7458 static __always_inline void intel_pmu_init_grt(struct pmu *pmu) 7459 { 7460 x86_pmu.mid_ack = true; 7461 x86_pmu.limit_period = glc_limit_period; 7462 x86_pmu.pebs_aliases = NULL; 7463 x86_pmu.pebs_prec_dist = true; 7464 x86_pmu.pebs_block = true; 7465 x86_pmu.lbr_pt_coexist = true; 7466 x86_pmu.flags |= PMU_FL_HAS_RSP_1; 7467 x86_pmu.flags |= PMU_FL_INSTR_LATENCY; 7468 7469 memcpy(hybrid_var(pmu, hw_cache_event_ids), glp_hw_cache_event_ids, sizeof(hw_cache_event_ids)); 7470 memcpy(hybrid_var(pmu, hw_cache_extra_regs), tnt_hw_cache_extra_regs, sizeof(hw_cache_extra_regs)); 7471 hybrid_var(pmu, hw_cache_event_ids)[C(ITLB)][C(OP_READ)][C(RESULT_ACCESS)] = -1; 7472 hybrid(pmu, event_constraints) = intel_grt_event_constraints; 7473 hybrid(pmu, pebs_constraints) = intel_grt_pebs_event_constraints; 7474 hybrid(pmu, extra_regs) = intel_grt_extra_regs; 7475 7476 intel_pmu_ref_cycles_ext(); 7477 } 7478 7479 static __always_inline void intel_pmu_init_lnc(struct pmu *pmu) 7480 { 7481 intel_pmu_init_glc(pmu); 7482 hybrid(pmu, event_constraints) = intel_lnc_event_constraints; 7483 hybrid(pmu, pebs_constraints) = intel_lnc_pebs_event_constraints; 7484 hybrid(pmu, extra_regs) = intel_lnc_extra_regs; 7485 } 7486 7487 static __always_inline void intel_pmu_init_pnc(struct pmu *pmu) 7488 { 7489 intel_pmu_init_glc(pmu); 7490 x86_pmu.flags &= ~PMU_FL_HAS_RSP_1; 7491 x86_pmu.flags |= PMU_FL_HAS_OMR; 7492 memcpy(hybrid_var(pmu, hw_cache_event_ids), 7493 pnc_hw_cache_event_ids, sizeof(hw_cache_event_ids)); 7494 memcpy(hybrid_var(pmu, hw_cache_extra_regs), 7495 pnc_hw_cache_extra_regs, sizeof(hw_cache_extra_regs)); 7496 hybrid(pmu, event_constraints) = intel_pnc_event_constraints; 7497 hybrid(pmu, pebs_constraints) = intel_pnc_pebs_event_constraints; 7498 hybrid(pmu, extra_regs) = intel_pnc_extra_regs; 7499 } 7500 7501 static __always_inline void intel_pmu_init_skt(struct pmu *pmu) 7502 { 7503 intel_pmu_init_grt(pmu); 7504 hybrid(pmu, event_constraints) = intel_skt_event_constraints; 7505 hybrid(pmu, extra_regs) = intel_cmt_extra_regs; 7506 static_call_update(intel_pmu_enable_acr_event, intel_pmu_enable_acr); 7507 } 7508 7509 static __always_inline void intel_pmu_init_arw(struct pmu *pmu) 7510 { 7511 intel_pmu_init_grt(pmu); 7512 x86_pmu.flags &= ~PMU_FL_HAS_RSP_1; 7513 x86_pmu.flags |= PMU_FL_HAS_OMR; 7514 memcpy(hybrid_var(pmu, hw_cache_extra_regs), 7515 arw_hw_cache_extra_regs, sizeof(hw_cache_extra_regs)); 7516 hybrid(pmu, event_constraints) = intel_arw_event_constraints; 7517 hybrid(pmu, pebs_constraints) = intel_arw_pebs_event_constraints; 7518 hybrid(pmu, extra_regs) = intel_arw_extra_regs; 7519 static_call_update(intel_pmu_enable_acr_event, intel_pmu_enable_acr); 7520 } 7521 7522 __init int intel_pmu_init(void) 7523 { 7524 struct attribute **extra_skl_attr = &empty_attrs; 7525 struct attribute **extra_attr = &empty_attrs; 7526 struct attribute **td_attr = &empty_attrs; 7527 struct attribute **mem_attr = &empty_attrs; 7528 struct attribute **tsx_attr = &empty_attrs; 7529 union cpuid10_edx edx; 7530 union cpuid10_eax eax; 7531 union cpuid10_ebx ebx; 7532 unsigned int fixed_mask; 7533 bool pmem = false; 7534 int version, i; 7535 char *name; 7536 struct x86_hybrid_pmu *pmu; 7537 7538 /* Architectural Perfmon was introduced starting with Core "Yonah" */ 7539 if (!cpu_has(&boot_cpu_data, X86_FEATURE_ARCH_PERFMON)) { 7540 switch (boot_cpu_data.x86) { 7541 case 6: 7542 if (boot_cpu_data.x86_vfm < INTEL_CORE_YONAH) 7543 return p6_pmu_init(); 7544 break; 7545 case 11: 7546 return knc_pmu_init(); 7547 case 15: 7548 return p4_pmu_init(); 7549 } 7550 7551 pr_cont("unsupported CPU family %d model %d ", 7552 boot_cpu_data.x86, boot_cpu_data.x86_model); 7553 return -ENODEV; 7554 } 7555 7556 /* 7557 * Check whether the Architectural PerfMon supports 7558 * Branch Misses Retired hw_event or not. 7559 */ 7560 cpuid(10, &eax.full, &ebx.full, &fixed_mask, &edx.full); 7561 if (eax.split.mask_length < ARCH_PERFMON_EVENTS_COUNT) 7562 return -ENODEV; 7563 7564 version = eax.split.version_id; 7565 if (version < 2) 7566 x86_pmu = core_pmu; 7567 else 7568 x86_pmu = intel_pmu; 7569 7570 x86_pmu.version = version; 7571 x86_pmu.cntr_mask64 = GENMASK_ULL(eax.split.num_counters - 1, 0); 7572 x86_pmu.cntval_bits = eax.split.bit_width; 7573 x86_pmu.cntval_mask = (1ULL << eax.split.bit_width) - 1; 7574 7575 x86_pmu.events_maskl = ebx.full; 7576 x86_pmu.events_mask_len = eax.split.mask_length; 7577 7578 x86_pmu.pebs_events_mask = intel_pmu_pebs_mask(x86_pmu.cntr_mask64); 7579 x86_pmu.pebs_capable = PEBS_COUNTER_MASK; 7580 x86_pmu.config_mask = X86_RAW_EVENT_MASK; 7581 7582 /* 7583 * Quirk: v2 perfmon does not report fixed-purpose events, so 7584 * assume at least 3 events, when not running in a hypervisor: 7585 */ 7586 if (version > 1 && version < 5) { 7587 int assume = 3 * !boot_cpu_has(X86_FEATURE_HYPERVISOR); 7588 7589 x86_pmu.fixed_cntr_mask64 = 7590 GENMASK_ULL(max((int)edx.split.num_counters_fixed, assume) - 1, 0); 7591 } else if (version >= 5) 7592 x86_pmu.fixed_cntr_mask64 = fixed_mask; 7593 7594 if (boot_cpu_has(X86_FEATURE_PDCM)) { 7595 u64 capabilities; 7596 7597 rdmsrq(MSR_IA32_PERF_CAPABILITIES, capabilities); 7598 x86_pmu.intel_cap.capabilities = capabilities; 7599 } 7600 7601 if (x86_pmu.intel_cap.lbr_format == LBR_FORMAT_32) { 7602 x86_pmu.lbr_reset = intel_pmu_lbr_reset_32; 7603 x86_pmu.lbr_read = intel_pmu_lbr_read_32; 7604 } 7605 7606 if (boot_cpu_has(X86_FEATURE_ARCH_LBR)) 7607 intel_pmu_arch_lbr_init(); 7608 7609 intel_pebs_init(); 7610 7611 x86_add_quirk(intel_arch_events_quirk); /* Install first, so it runs last */ 7612 7613 if (version >= 5) { 7614 x86_pmu.intel_cap.anythread_deprecated = edx.split.anythread_deprecated; 7615 if (x86_pmu.intel_cap.anythread_deprecated) 7616 pr_cont(" AnyThread deprecated, "); 7617 } 7618 7619 /* The perf side of core PMU is ready to support the mediated vPMU. */ 7620 x86_get_pmu(smp_processor_id())->capabilities |= PERF_PMU_CAP_MEDIATED_VPMU; 7621 7622 /* 7623 * Many features on and after V6 require dynamic constraint, 7624 * e.g., Arch PEBS, ACR. 7625 */ 7626 if (version >= 6) { 7627 x86_pmu.flags |= PMU_FL_DYN_CONSTRAINT; 7628 x86_pmu.late_setup = intel_pmu_late_setup; 7629 } 7630 7631 /* 7632 * Install the hw-cache-events table: 7633 */ 7634 switch (boot_cpu_data.x86_vfm) { 7635 case INTEL_CORE_YONAH: 7636 pr_cont("Core events, "); 7637 name = "core"; 7638 break; 7639 7640 case INTEL_CORE2_MEROM: 7641 x86_add_quirk(intel_clovertown_quirk); 7642 fallthrough; 7643 7644 case INTEL_CORE2_MEROM_L: 7645 case INTEL_CORE2_PENRYN: 7646 case INTEL_CORE2_DUNNINGTON: 7647 memcpy(hw_cache_event_ids, core2_hw_cache_event_ids, 7648 sizeof(hw_cache_event_ids)); 7649 7650 intel_pmu_lbr_init_core(); 7651 7652 x86_pmu.event_constraints = intel_core2_event_constraints; 7653 x86_pmu.pebs_constraints = intel_core2_pebs_event_constraints; 7654 pr_cont("Core2 events, "); 7655 name = "core2"; 7656 break; 7657 7658 case INTEL_NEHALEM: 7659 case INTEL_NEHALEM_EP: 7660 case INTEL_NEHALEM_EX: 7661 memcpy(hw_cache_event_ids, nehalem_hw_cache_event_ids, 7662 sizeof(hw_cache_event_ids)); 7663 memcpy(hw_cache_extra_regs, nehalem_hw_cache_extra_regs, 7664 sizeof(hw_cache_extra_regs)); 7665 7666 intel_pmu_lbr_init_nhm(); 7667 7668 x86_pmu.event_constraints = intel_nehalem_event_constraints; 7669 x86_pmu.pebs_constraints = intel_nehalem_pebs_event_constraints; 7670 x86_pmu.enable_all = intel_pmu_nhm_enable_all; 7671 x86_pmu.extra_regs = intel_nehalem_extra_regs; 7672 x86_pmu.limit_period = nhm_limit_period; 7673 7674 mem_attr = nhm_mem_events_attrs; 7675 7676 /* UOPS_ISSUED.STALLED_CYCLES */ 7677 intel_perfmon_event_map[PERF_COUNT_HW_STALLED_CYCLES_FRONTEND] = 7678 X86_CONFIG(.event=0x0e, .umask=0x01, .inv=1, .cmask=1); 7679 /* UOPS_EXECUTED.CORE_ACTIVE_CYCLES,c=1,i=1 */ 7680 intel_perfmon_event_map[PERF_COUNT_HW_STALLED_CYCLES_BACKEND] = 7681 X86_CONFIG(.event=0xb1, .umask=0x3f, .inv=1, .cmask=1); 7682 7683 intel_pmu_pebs_data_source_nhm(); 7684 x86_add_quirk(intel_nehalem_quirk); 7685 x86_pmu.pebs_no_tlb = 1; 7686 extra_attr = nhm_format_attr; 7687 7688 pr_cont("Nehalem events, "); 7689 name = "nehalem"; 7690 break; 7691 7692 case INTEL_ATOM_BONNELL: 7693 case INTEL_ATOM_BONNELL_MID: 7694 case INTEL_ATOM_SALTWELL: 7695 case INTEL_ATOM_SALTWELL_MID: 7696 case INTEL_ATOM_SALTWELL_TABLET: 7697 memcpy(hw_cache_event_ids, atom_hw_cache_event_ids, 7698 sizeof(hw_cache_event_ids)); 7699 7700 intel_pmu_lbr_init_atom(); 7701 7702 x86_pmu.event_constraints = intel_gen_event_constraints; 7703 x86_pmu.pebs_constraints = intel_atom_pebs_event_constraints; 7704 x86_pmu.pebs_aliases = intel_pebs_aliases_core2; 7705 pr_cont("Atom events, "); 7706 name = "bonnell"; 7707 break; 7708 7709 case INTEL_ATOM_SILVERMONT: 7710 case INTEL_ATOM_SILVERMONT_D: 7711 case INTEL_ATOM_SILVERMONT_MID: 7712 case INTEL_ATOM_AIRMONT: 7713 case INTEL_ATOM_AIRMONT_NP: 7714 case INTEL_ATOM_SILVERMONT_MID2: 7715 memcpy(hw_cache_event_ids, slm_hw_cache_event_ids, 7716 sizeof(hw_cache_event_ids)); 7717 memcpy(hw_cache_extra_regs, slm_hw_cache_extra_regs, 7718 sizeof(hw_cache_extra_regs)); 7719 7720 intel_pmu_lbr_init_slm(); 7721 7722 x86_pmu.event_constraints = intel_slm_event_constraints; 7723 x86_pmu.pebs_constraints = intel_slm_pebs_event_constraints; 7724 x86_pmu.extra_regs = intel_slm_extra_regs; 7725 x86_pmu.flags |= PMU_FL_HAS_RSP_1; 7726 td_attr = slm_events_attrs; 7727 extra_attr = slm_format_attr; 7728 pr_cont("Silvermont events, "); 7729 name = "silvermont"; 7730 break; 7731 7732 case INTEL_ATOM_GOLDMONT: 7733 case INTEL_ATOM_GOLDMONT_D: 7734 memcpy(hw_cache_event_ids, glm_hw_cache_event_ids, 7735 sizeof(hw_cache_event_ids)); 7736 memcpy(hw_cache_extra_regs, glm_hw_cache_extra_regs, 7737 sizeof(hw_cache_extra_regs)); 7738 7739 intel_pmu_lbr_init_skl(); 7740 7741 x86_pmu.event_constraints = intel_slm_event_constraints; 7742 x86_pmu.pebs_constraints = intel_glm_pebs_event_constraints; 7743 x86_pmu.extra_regs = intel_glm_extra_regs; 7744 /* 7745 * It's recommended to use CPU_CLK_UNHALTED.CORE_P + NPEBS 7746 * for precise cycles. 7747 * :pp is identical to :ppp 7748 */ 7749 x86_pmu.pebs_aliases = NULL; 7750 x86_pmu.pebs_prec_dist = true; 7751 x86_pmu.lbr_pt_coexist = true; 7752 x86_pmu.flags |= PMU_FL_HAS_RSP_1; 7753 td_attr = glm_events_attrs; 7754 extra_attr = slm_format_attr; 7755 pr_cont("Goldmont events, "); 7756 name = "goldmont"; 7757 break; 7758 7759 case INTEL_ATOM_GOLDMONT_PLUS: 7760 memcpy(hw_cache_event_ids, glp_hw_cache_event_ids, 7761 sizeof(hw_cache_event_ids)); 7762 memcpy(hw_cache_extra_regs, glp_hw_cache_extra_regs, 7763 sizeof(hw_cache_extra_regs)); 7764 7765 intel_pmu_lbr_init_skl(); 7766 7767 x86_pmu.event_constraints = intel_slm_event_constraints; 7768 x86_pmu.extra_regs = intel_glm_extra_regs; 7769 /* 7770 * It's recommended to use CPU_CLK_UNHALTED.CORE_P + NPEBS 7771 * for precise cycles. 7772 */ 7773 x86_pmu.pebs_aliases = NULL; 7774 x86_pmu.pebs_prec_dist = true; 7775 x86_pmu.lbr_pt_coexist = true; 7776 x86_pmu.pebs_capable = ~0ULL; 7777 x86_pmu.flags |= PMU_FL_HAS_RSP_1; 7778 x86_pmu.flags |= PMU_FL_PEBS_ALL; 7779 x86_pmu.get_event_constraints = glp_get_event_constraints; 7780 td_attr = glm_events_attrs; 7781 /* Goldmont Plus has 4-wide pipeline */ 7782 event_attr_td_total_slots_scale_glm.event_str = "4"; 7783 extra_attr = slm_format_attr; 7784 pr_cont("Goldmont plus events, "); 7785 name = "goldmont_plus"; 7786 break; 7787 7788 case INTEL_ATOM_TREMONT_D: 7789 case INTEL_ATOM_TREMONT: 7790 case INTEL_ATOM_TREMONT_L: 7791 x86_pmu.late_ack = true; 7792 memcpy(hw_cache_event_ids, glp_hw_cache_event_ids, 7793 sizeof(hw_cache_event_ids)); 7794 memcpy(hw_cache_extra_regs, tnt_hw_cache_extra_regs, 7795 sizeof(hw_cache_extra_regs)); 7796 hw_cache_event_ids[C(ITLB)][C(OP_READ)][C(RESULT_ACCESS)] = -1; 7797 7798 intel_pmu_lbr_init_skl(); 7799 7800 x86_pmu.event_constraints = intel_slm_event_constraints; 7801 x86_pmu.extra_regs = intel_tnt_extra_regs; 7802 /* 7803 * It's recommended to use CPU_CLK_UNHALTED.CORE_P + NPEBS 7804 * for precise cycles. 7805 */ 7806 x86_pmu.pebs_aliases = NULL; 7807 x86_pmu.pebs_prec_dist = true; 7808 x86_pmu.lbr_pt_coexist = true; 7809 x86_pmu.flags |= PMU_FL_HAS_RSP_1; 7810 x86_pmu.get_event_constraints = tnt_get_event_constraints; 7811 td_attr = tnt_events_attrs; 7812 extra_attr = slm_format_attr; 7813 pr_cont("Tremont events, "); 7814 name = "Tremont"; 7815 break; 7816 7817 case INTEL_ATOM_GRACEMONT: 7818 intel_pmu_init_grt(NULL); 7819 intel_pmu_pebs_data_source_grt(); 7820 x86_pmu.pebs_latency_data = grt_latency_data; 7821 x86_pmu.get_event_constraints = tnt_get_event_constraints; 7822 td_attr = tnt_events_attrs; 7823 mem_attr = grt_mem_attrs; 7824 extra_attr = nhm_format_attr; 7825 pr_cont("Gracemont events, "); 7826 name = "gracemont"; 7827 break; 7828 7829 case INTEL_ATOM_CRESTMONT: 7830 case INTEL_ATOM_CRESTMONT_X: 7831 intel_pmu_init_grt(NULL); 7832 x86_pmu.extra_regs = intel_cmt_extra_regs; 7833 intel_pmu_pebs_data_source_cmt(); 7834 x86_pmu.pebs_latency_data = cmt_latency_data; 7835 x86_pmu.get_event_constraints = cmt_get_event_constraints; 7836 td_attr = cmt_events_attrs; 7837 mem_attr = grt_mem_attrs; 7838 extra_attr = cmt_format_attr; 7839 pr_cont("Crestmont events, "); 7840 name = "crestmont"; 7841 break; 7842 7843 case INTEL_ATOM_DARKMONT_X: 7844 intel_pmu_init_skt(NULL); 7845 intel_pmu_pebs_data_source_cmt(); 7846 x86_pmu.pebs_latency_data = cmt_latency_data; 7847 x86_pmu.get_event_constraints = cmt_get_event_constraints; 7848 td_attr = skt_events_attrs; 7849 mem_attr = grt_mem_attrs; 7850 extra_attr = cmt_format_attr; 7851 pr_cont("Darkmont events, "); 7852 name = "darkmont"; 7853 break; 7854 7855 case INTEL_WESTMERE: 7856 case INTEL_WESTMERE_EP: 7857 case INTEL_WESTMERE_EX: 7858 memcpy(hw_cache_event_ids, westmere_hw_cache_event_ids, 7859 sizeof(hw_cache_event_ids)); 7860 memcpy(hw_cache_extra_regs, nehalem_hw_cache_extra_regs, 7861 sizeof(hw_cache_extra_regs)); 7862 7863 intel_pmu_lbr_init_nhm(); 7864 7865 x86_pmu.event_constraints = intel_westmere_event_constraints; 7866 x86_pmu.enable_all = intel_pmu_nhm_enable_all; 7867 x86_pmu.pebs_constraints = intel_westmere_pebs_event_constraints; 7868 x86_pmu.extra_regs = intel_westmere_extra_regs; 7869 x86_pmu.flags |= PMU_FL_HAS_RSP_1; 7870 7871 mem_attr = nhm_mem_events_attrs; 7872 7873 /* UOPS_ISSUED.STALLED_CYCLES */ 7874 intel_perfmon_event_map[PERF_COUNT_HW_STALLED_CYCLES_FRONTEND] = 7875 X86_CONFIG(.event=0x0e, .umask=0x01, .inv=1, .cmask=1); 7876 /* UOPS_EXECUTED.CORE_ACTIVE_CYCLES,c=1,i=1 */ 7877 intel_perfmon_event_map[PERF_COUNT_HW_STALLED_CYCLES_BACKEND] = 7878 X86_CONFIG(.event=0xb1, .umask=0x3f, .inv=1, .cmask=1); 7879 7880 intel_pmu_pebs_data_source_nhm(); 7881 extra_attr = nhm_format_attr; 7882 pr_cont("Westmere events, "); 7883 name = "westmere"; 7884 break; 7885 7886 case INTEL_SANDYBRIDGE: 7887 case INTEL_SANDYBRIDGE_X: 7888 x86_add_quirk(intel_sandybridge_quirk); 7889 x86_add_quirk(intel_ht_bug); 7890 memcpy(hw_cache_event_ids, snb_hw_cache_event_ids, 7891 sizeof(hw_cache_event_ids)); 7892 memcpy(hw_cache_extra_regs, snb_hw_cache_extra_regs, 7893 sizeof(hw_cache_extra_regs)); 7894 7895 intel_pmu_lbr_init_snb(); 7896 7897 x86_pmu.event_constraints = intel_snb_event_constraints; 7898 x86_pmu.pebs_constraints = intel_snb_pebs_event_constraints; 7899 x86_pmu.pebs_aliases = intel_pebs_aliases_snb; 7900 if (boot_cpu_data.x86_vfm == INTEL_SANDYBRIDGE_X) 7901 x86_pmu.extra_regs = intel_snbep_extra_regs; 7902 else 7903 x86_pmu.extra_regs = intel_snb_extra_regs; 7904 7905 7906 /* all extra regs are per-cpu when HT is on */ 7907 x86_pmu.flags |= PMU_FL_HAS_RSP_1; 7908 x86_pmu.flags |= PMU_FL_NO_HT_SHARING; 7909 7910 td_attr = snb_events_attrs; 7911 mem_attr = snb_mem_events_attrs; 7912 7913 /* UOPS_ISSUED.ANY,c=1,i=1 to count stall cycles */ 7914 intel_perfmon_event_map[PERF_COUNT_HW_STALLED_CYCLES_FRONTEND] = 7915 X86_CONFIG(.event=0x0e, .umask=0x01, .inv=1, .cmask=1); 7916 /* UOPS_DISPATCHED.THREAD,c=1,i=1 to count stall cycles*/ 7917 intel_perfmon_event_map[PERF_COUNT_HW_STALLED_CYCLES_BACKEND] = 7918 X86_CONFIG(.event=0xb1, .umask=0x01, .inv=1, .cmask=1); 7919 7920 extra_attr = nhm_format_attr; 7921 7922 pr_cont("SandyBridge events, "); 7923 name = "sandybridge"; 7924 break; 7925 7926 case INTEL_IVYBRIDGE: 7927 case INTEL_IVYBRIDGE_X: 7928 x86_add_quirk(intel_ht_bug); 7929 memcpy(hw_cache_event_ids, snb_hw_cache_event_ids, 7930 sizeof(hw_cache_event_ids)); 7931 /* dTLB-load-misses on IVB is different than SNB */ 7932 hw_cache_event_ids[C(DTLB)][C(OP_READ)][C(RESULT_MISS)] = 0x8108; /* DTLB_LOAD_MISSES.DEMAND_LD_MISS_CAUSES_A_WALK */ 7933 7934 memcpy(hw_cache_extra_regs, snb_hw_cache_extra_regs, 7935 sizeof(hw_cache_extra_regs)); 7936 7937 intel_pmu_lbr_init_snb(); 7938 7939 x86_pmu.event_constraints = intel_ivb_event_constraints; 7940 x86_pmu.pebs_constraints = intel_ivb_pebs_event_constraints; 7941 x86_pmu.pebs_aliases = intel_pebs_aliases_ivb; 7942 x86_pmu.pebs_prec_dist = true; 7943 if (boot_cpu_data.x86_vfm == INTEL_IVYBRIDGE_X) 7944 x86_pmu.extra_regs = intel_snbep_extra_regs; 7945 else 7946 x86_pmu.extra_regs = intel_snb_extra_regs; 7947 /* all extra regs are per-cpu when HT is on */ 7948 x86_pmu.flags |= PMU_FL_HAS_RSP_1; 7949 x86_pmu.flags |= PMU_FL_NO_HT_SHARING; 7950 7951 td_attr = snb_events_attrs; 7952 mem_attr = snb_mem_events_attrs; 7953 7954 /* UOPS_ISSUED.ANY,c=1,i=1 to count stall cycles */ 7955 intel_perfmon_event_map[PERF_COUNT_HW_STALLED_CYCLES_FRONTEND] = 7956 X86_CONFIG(.event=0x0e, .umask=0x01, .inv=1, .cmask=1); 7957 7958 extra_attr = nhm_format_attr; 7959 7960 pr_cont("IvyBridge events, "); 7961 name = "ivybridge"; 7962 break; 7963 7964 7965 case INTEL_HASWELL: 7966 case INTEL_HASWELL_X: 7967 case INTEL_HASWELL_L: 7968 case INTEL_HASWELL_G: 7969 x86_add_quirk(intel_ht_bug); 7970 x86_add_quirk(intel_pebs_isolation_quirk); 7971 x86_pmu.late_ack = true; 7972 memcpy(hw_cache_event_ids, hsw_hw_cache_event_ids, sizeof(hw_cache_event_ids)); 7973 memcpy(hw_cache_extra_regs, hsw_hw_cache_extra_regs, sizeof(hw_cache_extra_regs)); 7974 7975 intel_pmu_lbr_init_hsw(); 7976 7977 x86_pmu.event_constraints = intel_hsw_event_constraints; 7978 x86_pmu.pebs_constraints = intel_hsw_pebs_event_constraints; 7979 x86_pmu.extra_regs = intel_snbep_extra_regs; 7980 x86_pmu.pebs_aliases = intel_pebs_aliases_ivb; 7981 x86_pmu.pebs_prec_dist = true; 7982 /* all extra regs are per-cpu when HT is on */ 7983 x86_pmu.flags |= PMU_FL_HAS_RSP_1; 7984 x86_pmu.flags |= PMU_FL_NO_HT_SHARING; 7985 7986 x86_pmu.hw_config = hsw_hw_config; 7987 x86_pmu.get_event_constraints = hsw_get_event_constraints; 7988 x86_pmu.limit_period = hsw_limit_period; 7989 x86_pmu.lbr_double_abort = true; 7990 extra_attr = boot_cpu_has(X86_FEATURE_RTM) ? 7991 hsw_format_attr : nhm_format_attr; 7992 td_attr = hsw_events_attrs; 7993 mem_attr = hsw_mem_events_attrs; 7994 tsx_attr = hsw_tsx_events_attrs; 7995 pr_cont("Haswell events, "); 7996 name = "haswell"; 7997 break; 7998 7999 case INTEL_BROADWELL: 8000 case INTEL_BROADWELL_D: 8001 case INTEL_BROADWELL_G: 8002 case INTEL_BROADWELL_X: 8003 x86_add_quirk(intel_pebs_isolation_quirk); 8004 x86_pmu.late_ack = true; 8005 memcpy(hw_cache_event_ids, hsw_hw_cache_event_ids, sizeof(hw_cache_event_ids)); 8006 memcpy(hw_cache_extra_regs, hsw_hw_cache_extra_regs, sizeof(hw_cache_extra_regs)); 8007 8008 /* L3_MISS_LOCAL_DRAM is BIT(26) in Broadwell */ 8009 hw_cache_extra_regs[C(LL)][C(OP_READ)][C(RESULT_MISS)] = HSW_DEMAND_READ | 8010 BDW_L3_MISS|HSW_SNOOP_DRAM; 8011 hw_cache_extra_regs[C(LL)][C(OP_WRITE)][C(RESULT_MISS)] = HSW_DEMAND_WRITE|BDW_L3_MISS| 8012 HSW_SNOOP_DRAM; 8013 hw_cache_extra_regs[C(NODE)][C(OP_READ)][C(RESULT_ACCESS)] = HSW_DEMAND_READ| 8014 BDW_L3_MISS_LOCAL|HSW_SNOOP_DRAM; 8015 hw_cache_extra_regs[C(NODE)][C(OP_WRITE)][C(RESULT_ACCESS)] = HSW_DEMAND_WRITE| 8016 BDW_L3_MISS_LOCAL|HSW_SNOOP_DRAM; 8017 8018 intel_pmu_lbr_init_hsw(); 8019 8020 x86_pmu.event_constraints = intel_bdw_event_constraints; 8021 x86_pmu.pebs_constraints = intel_bdw_pebs_event_constraints; 8022 x86_pmu.extra_regs = intel_snbep_extra_regs; 8023 x86_pmu.pebs_aliases = intel_pebs_aliases_ivb; 8024 x86_pmu.pebs_prec_dist = true; 8025 /* all extra regs are per-cpu when HT is on */ 8026 x86_pmu.flags |= PMU_FL_HAS_RSP_1; 8027 x86_pmu.flags |= PMU_FL_NO_HT_SHARING; 8028 8029 x86_pmu.hw_config = hsw_hw_config; 8030 x86_pmu.get_event_constraints = hsw_get_event_constraints; 8031 x86_pmu.limit_period = bdw_limit_period; 8032 extra_attr = boot_cpu_has(X86_FEATURE_RTM) ? 8033 hsw_format_attr : nhm_format_attr; 8034 td_attr = hsw_events_attrs; 8035 mem_attr = hsw_mem_events_attrs; 8036 tsx_attr = hsw_tsx_events_attrs; 8037 pr_cont("Broadwell events, "); 8038 name = "broadwell"; 8039 break; 8040 8041 case INTEL_XEON_PHI_KNL: 8042 case INTEL_XEON_PHI_KNM: 8043 memcpy(hw_cache_event_ids, 8044 slm_hw_cache_event_ids, sizeof(hw_cache_event_ids)); 8045 memcpy(hw_cache_extra_regs, 8046 knl_hw_cache_extra_regs, sizeof(hw_cache_extra_regs)); 8047 intel_pmu_lbr_init_knl(); 8048 8049 x86_pmu.event_constraints = intel_slm_event_constraints; 8050 x86_pmu.pebs_constraints = intel_slm_pebs_event_constraints; 8051 x86_pmu.extra_regs = intel_knl_extra_regs; 8052 8053 /* all extra regs are per-cpu when HT is on */ 8054 x86_pmu.flags |= PMU_FL_HAS_RSP_1; 8055 x86_pmu.flags |= PMU_FL_NO_HT_SHARING; 8056 extra_attr = slm_format_attr; 8057 pr_cont("Knights Landing/Mill events, "); 8058 name = "knights-landing"; 8059 break; 8060 8061 case INTEL_SKYLAKE_X: 8062 pmem = true; 8063 fallthrough; 8064 case INTEL_SKYLAKE_L: 8065 case INTEL_SKYLAKE: 8066 case INTEL_KABYLAKE_L: 8067 case INTEL_KABYLAKE: 8068 case INTEL_COMETLAKE_L: 8069 case INTEL_COMETLAKE: 8070 x86_add_quirk(intel_pebs_isolation_quirk); 8071 x86_pmu.late_ack = true; 8072 memcpy(hw_cache_event_ids, skl_hw_cache_event_ids, sizeof(hw_cache_event_ids)); 8073 memcpy(hw_cache_extra_regs, skl_hw_cache_extra_regs, sizeof(hw_cache_extra_regs)); 8074 intel_pmu_lbr_init_skl(); 8075 8076 /* INT_MISC.RECOVERY_CYCLES has umask 1 in Skylake */ 8077 event_attr_td_recovery_bubbles.event_str_noht = 8078 "event=0xd,umask=0x1,cmask=1"; 8079 event_attr_td_recovery_bubbles.event_str_ht = 8080 "event=0xd,umask=0x1,cmask=1,any=1"; 8081 8082 x86_pmu.event_constraints = intel_skl_event_constraints; 8083 x86_pmu.pebs_constraints = intel_skl_pebs_event_constraints; 8084 x86_pmu.extra_regs = intel_skl_extra_regs; 8085 x86_pmu.pebs_aliases = intel_pebs_aliases_skl; 8086 x86_pmu.pebs_prec_dist = true; 8087 /* all extra regs are per-cpu when HT is on */ 8088 x86_pmu.flags |= PMU_FL_HAS_RSP_1; 8089 x86_pmu.flags |= PMU_FL_NO_HT_SHARING; 8090 8091 x86_pmu.hw_config = hsw_hw_config; 8092 x86_pmu.get_event_constraints = hsw_get_event_constraints; 8093 extra_attr = boot_cpu_has(X86_FEATURE_RTM) ? 8094 hsw_format_attr : nhm_format_attr; 8095 extra_skl_attr = skl_format_attr; 8096 td_attr = hsw_events_attrs; 8097 mem_attr = hsw_mem_events_attrs; 8098 tsx_attr = hsw_tsx_events_attrs; 8099 intel_pmu_pebs_data_source_skl(pmem); 8100 8101 /* 8102 * Processors with CPUID.RTM_ALWAYS_ABORT have TSX deprecated by default. 8103 * TSX force abort hooks are not required on these systems. Only deploy 8104 * workaround when microcode has not enabled X86_FEATURE_RTM_ALWAYS_ABORT. 8105 */ 8106 if (boot_cpu_has(X86_FEATURE_TSX_FORCE_ABORT) && 8107 !boot_cpu_has(X86_FEATURE_RTM_ALWAYS_ABORT)) { 8108 x86_pmu.flags |= PMU_FL_TFA; 8109 x86_pmu.get_event_constraints = tfa_get_event_constraints; 8110 x86_pmu.enable_all = intel_tfa_pmu_enable_all; 8111 x86_pmu.commit_scheduling = intel_tfa_commit_scheduling; 8112 } 8113 8114 pr_cont("Skylake events, "); 8115 name = "skylake"; 8116 break; 8117 8118 case INTEL_ICELAKE_X: 8119 case INTEL_ICELAKE_D: 8120 x86_pmu.pebs_ept = 1; 8121 pmem = true; 8122 fallthrough; 8123 case INTEL_ICELAKE_L: 8124 case INTEL_ICELAKE: 8125 case INTEL_TIGERLAKE_L: 8126 case INTEL_TIGERLAKE: 8127 case INTEL_ROCKETLAKE: 8128 x86_pmu.late_ack = true; 8129 memcpy(hw_cache_event_ids, skl_hw_cache_event_ids, sizeof(hw_cache_event_ids)); 8130 memcpy(hw_cache_extra_regs, skl_hw_cache_extra_regs, sizeof(hw_cache_extra_regs)); 8131 hw_cache_event_ids[C(ITLB)][C(OP_READ)][C(RESULT_ACCESS)] = -1; 8132 intel_pmu_lbr_init_skl(); 8133 8134 x86_pmu.event_constraints = intel_icl_event_constraints; 8135 x86_pmu.pebs_constraints = intel_icl_pebs_event_constraints; 8136 x86_pmu.extra_regs = intel_icl_extra_regs; 8137 x86_pmu.pebs_aliases = NULL; 8138 x86_pmu.pebs_prec_dist = true; 8139 x86_pmu.flags |= PMU_FL_HAS_RSP_1; 8140 x86_pmu.flags |= PMU_FL_NO_HT_SHARING; 8141 8142 x86_pmu.hw_config = hsw_hw_config; 8143 x86_pmu.get_event_constraints = icl_get_event_constraints; 8144 extra_attr = boot_cpu_has(X86_FEATURE_RTM) ? 8145 hsw_format_attr : nhm_format_attr; 8146 extra_skl_attr = skl_format_attr; 8147 mem_attr = icl_events_attrs; 8148 td_attr = icl_td_events_attrs; 8149 tsx_attr = icl_tsx_events_attrs; 8150 x86_pmu.rtm_abort_event = X86_CONFIG(.event=0xc9, .umask=0x04); 8151 x86_pmu.lbr_pt_coexist = true; 8152 intel_pmu_pebs_data_source_skl(pmem); 8153 x86_pmu.num_topdown_events = 4; 8154 static_call_update(intel_pmu_update_topdown_event, 8155 &icl_update_topdown_event); 8156 static_call_update(intel_pmu_set_topdown_event_period, 8157 &icl_set_topdown_event_period); 8158 pr_cont("Icelake events, "); 8159 name = "icelake"; 8160 break; 8161 8162 case INTEL_SAPPHIRERAPIDS_X: 8163 case INTEL_EMERALDRAPIDS_X: 8164 x86_pmu.flags |= PMU_FL_MEM_LOADS_AUX; 8165 x86_pmu.extra_regs = intel_glc_extra_regs; 8166 pr_cont("Sapphire Rapids events, "); 8167 name = "sapphire_rapids"; 8168 goto glc_common; 8169 8170 case INTEL_GRANITERAPIDS_X: 8171 case INTEL_GRANITERAPIDS_D: 8172 x86_pmu.extra_regs = intel_rwc_extra_regs; 8173 pr_cont("Granite Rapids events, "); 8174 name = "granite_rapids"; 8175 goto glc_common; 8176 8177 case INTEL_DIAMONDRAPIDS_X: 8178 intel_pmu_init_pnc(NULL); 8179 x86_pmu.pebs_latency_data = pnc_latency_data; 8180 8181 pr_cont("Panthercove events, "); 8182 name = "panthercove"; 8183 goto glc_base; 8184 8185 glc_common: 8186 intel_pmu_init_glc(NULL); 8187 intel_pmu_pebs_data_source_skl(true); 8188 8189 glc_base: 8190 x86_pmu.pebs_ept = 1; 8191 x86_pmu.hw_config = hsw_hw_config; 8192 x86_pmu.get_event_constraints = glc_get_event_constraints; 8193 extra_attr = boot_cpu_has(X86_FEATURE_RTM) ? 8194 hsw_format_attr : nhm_format_attr; 8195 extra_skl_attr = skl_format_attr; 8196 mem_attr = glc_events_attrs; 8197 td_attr = glc_td_events_attrs; 8198 tsx_attr = glc_tsx_events_attrs; 8199 break; 8200 8201 case INTEL_ALDERLAKE: 8202 case INTEL_ALDERLAKE_L: 8203 case INTEL_RAPTORLAKE: 8204 case INTEL_RAPTORLAKE_P: 8205 case INTEL_RAPTORLAKE_S: 8206 /* 8207 * Alder Lake has 2 types of CPU, core and atom. 8208 * 8209 * Initialize the common PerfMon capabilities here. 8210 */ 8211 intel_pmu_init_hybrid(hybrid_big_small); 8212 8213 x86_pmu.pebs_latency_data = grt_latency_data; 8214 x86_pmu.get_event_constraints = adl_get_event_constraints; 8215 x86_pmu.hw_config = adl_hw_config; 8216 x86_pmu.get_hybrid_cpu_type = adl_get_hybrid_cpu_type; 8217 8218 td_attr = adl_hybrid_events_attrs; 8219 mem_attr = adl_hybrid_mem_attrs; 8220 tsx_attr = adl_hybrid_tsx_attrs; 8221 extra_attr = boot_cpu_has(X86_FEATURE_RTM) ? 8222 adl_hybrid_extra_attr_rtm : adl_hybrid_extra_attr; 8223 8224 /* Initialize big core specific PerfMon capabilities.*/ 8225 pmu = &x86_pmu.hybrid_pmu[X86_HYBRID_PMU_CORE_IDX]; 8226 intel_pmu_init_glc(&pmu->pmu); 8227 if (cpu_feature_enabled(X86_FEATURE_HYBRID_CPU)) { 8228 pmu->cntr_mask64 <<= 2; 8229 pmu->cntr_mask64 |= 0x3; 8230 pmu->fixed_cntr_mask64 <<= 1; 8231 pmu->fixed_cntr_mask64 |= 0x1; 8232 } else { 8233 pmu->cntr_mask64 = x86_pmu.cntr_mask64; 8234 pmu->fixed_cntr_mask64 = x86_pmu.fixed_cntr_mask64; 8235 } 8236 8237 /* 8238 * Quirk: For some Alder Lake machine, when all E-cores are disabled in 8239 * a BIOS, the leaf 0xA will enumerate all counters of P-cores. However, 8240 * the X86_FEATURE_HYBRID_CPU is still set. The above codes will 8241 * mistakenly add extra counters for P-cores. Correct the number of 8242 * counters here. 8243 */ 8244 if ((x86_pmu_num_counters(&pmu->pmu) > 8) || (x86_pmu_num_counters_fixed(&pmu->pmu) > 4)) { 8245 pmu->cntr_mask64 = x86_pmu.cntr_mask64; 8246 pmu->fixed_cntr_mask64 = x86_pmu.fixed_cntr_mask64; 8247 } 8248 8249 pmu->pebs_events_mask = intel_pmu_pebs_mask(pmu->cntr_mask64); 8250 pmu->unconstrained = (struct event_constraint) 8251 __EVENT_CONSTRAINT(0, pmu->cntr_mask64, 8252 0, x86_pmu_num_counters(&pmu->pmu), 0, 0); 8253 8254 pmu->extra_regs = intel_glc_extra_regs; 8255 8256 /* Initialize Atom core specific PerfMon capabilities.*/ 8257 pmu = &x86_pmu.hybrid_pmu[X86_HYBRID_PMU_ATOM_IDX]; 8258 intel_pmu_init_grt(&pmu->pmu); 8259 8260 x86_pmu.flags |= PMU_FL_MEM_LOADS_AUX; 8261 intel_pmu_pebs_data_source_adl(); 8262 pr_cont("Alderlake Hybrid events, "); 8263 name = "alderlake_hybrid"; 8264 break; 8265 8266 case INTEL_METEORLAKE: 8267 case INTEL_METEORLAKE_L: 8268 case INTEL_ARROWLAKE_U: 8269 intel_pmu_init_hybrid(hybrid_big_small); 8270 8271 x86_pmu.pebs_latency_data = cmt_latency_data; 8272 x86_pmu.get_event_constraints = mtl_get_event_constraints; 8273 x86_pmu.hw_config = adl_hw_config; 8274 8275 td_attr = adl_hybrid_events_attrs; 8276 mem_attr = mtl_hybrid_mem_attrs; 8277 tsx_attr = adl_hybrid_tsx_attrs; 8278 extra_attr = boot_cpu_has(X86_FEATURE_RTM) ? 8279 mtl_hybrid_extra_attr_rtm : mtl_hybrid_extra_attr; 8280 8281 /* Initialize big core specific PerfMon capabilities.*/ 8282 pmu = &x86_pmu.hybrid_pmu[X86_HYBRID_PMU_CORE_IDX]; 8283 intel_pmu_init_glc(&pmu->pmu); 8284 pmu->extra_regs = intel_rwc_extra_regs; 8285 8286 /* Initialize Atom core specific PerfMon capabilities.*/ 8287 pmu = &x86_pmu.hybrid_pmu[X86_HYBRID_PMU_ATOM_IDX]; 8288 intel_pmu_init_grt(&pmu->pmu); 8289 pmu->extra_regs = intel_cmt_extra_regs; 8290 8291 intel_pmu_pebs_data_source_mtl(); 8292 pr_cont("Meteorlake Hybrid events, "); 8293 name = "meteorlake_hybrid"; 8294 break; 8295 8296 case INTEL_PANTHERLAKE_L: 8297 case INTEL_WILDCATLAKE_L: 8298 pr_cont("Pantherlake Hybrid events, "); 8299 name = "pantherlake_hybrid"; 8300 goto lnl_common; 8301 8302 case INTEL_LUNARLAKE_M: 8303 case INTEL_ARROWLAKE: 8304 pr_cont("Lunarlake Hybrid events, "); 8305 name = "lunarlake_hybrid"; 8306 8307 lnl_common: 8308 intel_pmu_init_hybrid(hybrid_big_small); 8309 8310 x86_pmu.pebs_latency_data = lnl_latency_data; 8311 x86_pmu.get_event_constraints = mtl_get_event_constraints; 8312 x86_pmu.hw_config = adl_hw_config; 8313 8314 td_attr = lnl_hybrid_events_attrs; 8315 mem_attr = mtl_hybrid_mem_attrs; 8316 tsx_attr = adl_hybrid_tsx_attrs; 8317 extra_attr = boot_cpu_has(X86_FEATURE_RTM) ? 8318 mtl_hybrid_extra_attr_rtm : mtl_hybrid_extra_attr; 8319 8320 /* Initialize big core specific PerfMon capabilities.*/ 8321 pmu = &x86_pmu.hybrid_pmu[X86_HYBRID_PMU_CORE_IDX]; 8322 intel_pmu_init_lnc(&pmu->pmu); 8323 8324 /* Initialize Atom core specific PerfMon capabilities.*/ 8325 pmu = &x86_pmu.hybrid_pmu[X86_HYBRID_PMU_ATOM_IDX]; 8326 intel_pmu_init_skt(&pmu->pmu); 8327 8328 intel_pmu_pebs_data_source_lnl(); 8329 break; 8330 8331 case INTEL_ARROWLAKE_H: 8332 intel_pmu_init_hybrid(hybrid_big_small_tiny); 8333 8334 x86_pmu.pebs_latency_data = arl_h_latency_data; 8335 x86_pmu.get_event_constraints = arl_h_get_event_constraints; 8336 x86_pmu.hw_config = arl_h_hw_config; 8337 8338 td_attr = arl_h_hybrid_events_attrs; 8339 mem_attr = arl_h_hybrid_mem_attrs; 8340 tsx_attr = adl_hybrid_tsx_attrs; 8341 extra_attr = boot_cpu_has(X86_FEATURE_RTM) ? 8342 mtl_hybrid_extra_attr_rtm : mtl_hybrid_extra_attr; 8343 8344 /* Initialize big core specific PerfMon capabilities. */ 8345 pmu = &x86_pmu.hybrid_pmu[X86_HYBRID_PMU_CORE_IDX]; 8346 intel_pmu_init_lnc(&pmu->pmu); 8347 8348 /* Initialize Atom core specific PerfMon capabilities. */ 8349 pmu = &x86_pmu.hybrid_pmu[X86_HYBRID_PMU_ATOM_IDX]; 8350 intel_pmu_init_skt(&pmu->pmu); 8351 8352 /* Initialize Lower Power Atom specific PerfMon capabilities. */ 8353 pmu = &x86_pmu.hybrid_pmu[X86_HYBRID_PMU_TINY_IDX]; 8354 intel_pmu_init_grt(&pmu->pmu); 8355 pmu->extra_regs = intel_cmt_extra_regs; 8356 8357 intel_pmu_pebs_data_source_arl_h(); 8358 pr_cont("ArrowLake-H Hybrid events, "); 8359 name = "arrowlake_h_hybrid"; 8360 break; 8361 8362 case INTEL_NOVALAKE: 8363 case INTEL_NOVALAKE_L: 8364 pr_cont("Novalake Hybrid events, "); 8365 name = "novalake_hybrid"; 8366 intel_pmu_init_hybrid(hybrid_big_small); 8367 8368 x86_pmu.pebs_latency_data = nvl_latency_data; 8369 x86_pmu.get_event_constraints = mtl_get_event_constraints; 8370 x86_pmu.hw_config = adl_hw_config; 8371 8372 td_attr = lnl_hybrid_events_attrs; 8373 mem_attr = mtl_hybrid_mem_attrs; 8374 tsx_attr = adl_hybrid_tsx_attrs; 8375 extra_attr = boot_cpu_has(X86_FEATURE_RTM) ? 8376 mtl_hybrid_extra_attr_rtm : mtl_hybrid_extra_attr; 8377 8378 /* Initialize big core specific PerfMon capabilities.*/ 8379 pmu = &x86_pmu.hybrid_pmu[X86_HYBRID_PMU_CORE_IDX]; 8380 intel_pmu_init_pnc(&pmu->pmu); 8381 8382 /* Initialize Atom core specific PerfMon capabilities.*/ 8383 pmu = &x86_pmu.hybrid_pmu[X86_HYBRID_PMU_ATOM_IDX]; 8384 intel_pmu_init_arw(&pmu->pmu); 8385 8386 intel_pmu_pebs_data_source_lnl(); 8387 break; 8388 8389 default: 8390 switch (x86_pmu.version) { 8391 case 1: 8392 x86_pmu.event_constraints = intel_v1_event_constraints; 8393 pr_cont("generic architected perfmon v1, "); 8394 name = "generic_arch_v1"; 8395 break; 8396 case 2: 8397 case 3: 8398 case 4: 8399 /* 8400 * default constraints for v2 and up 8401 */ 8402 x86_pmu.event_constraints = intel_gen_event_constraints; 8403 pr_cont("generic architected perfmon, "); 8404 name = "generic_arch_v2+"; 8405 break; 8406 default: 8407 /* 8408 * The default constraints for v5 and up can support up to 8409 * 16 fixed counters. For the fixed counters 4 and later, 8410 * the pseudo-encoding is applied. 8411 * The constraints may be cut according to the CPUID enumeration 8412 * by inserting the EVENT_CONSTRAINT_END. 8413 */ 8414 if (fls64(x86_pmu.fixed_cntr_mask64) > INTEL_PMC_MAX_FIXED) 8415 x86_pmu.fixed_cntr_mask64 &= GENMASK_ULL(INTEL_PMC_MAX_FIXED - 1, 0); 8416 intel_v5_gen_event_constraints[fls64(x86_pmu.fixed_cntr_mask64)].weight = -1; 8417 x86_pmu.event_constraints = intel_v5_gen_event_constraints; 8418 pr_cont("generic architected perfmon, "); 8419 name = "generic_arch_v5+"; 8420 break; 8421 } 8422 } 8423 8424 snprintf(pmu_name_str, sizeof(pmu_name_str), "%s", name); 8425 8426 if (!is_hybrid()) { 8427 group_events_td.attrs = td_attr; 8428 group_events_mem.attrs = mem_attr; 8429 group_events_tsx.attrs = tsx_attr; 8430 group_format_extra.attrs = extra_attr; 8431 group_format_extra_skl.attrs = extra_skl_attr; 8432 8433 x86_pmu.attr_update = attr_update; 8434 } else { 8435 hybrid_group_events_td.attrs = td_attr; 8436 hybrid_group_events_mem.attrs = mem_attr; 8437 hybrid_group_events_tsx.attrs = tsx_attr; 8438 hybrid_group_format_extra.attrs = extra_attr; 8439 8440 x86_pmu.attr_update = hybrid_attr_update; 8441 } 8442 8443 /* 8444 * The archPerfmonExt (0x23) includes an enhanced enumeration of 8445 * PMU architectural features with a per-core view. For non-hybrid, 8446 * each core has the same PMU capabilities. It's good enough to 8447 * update the x86_pmu from the booting CPU. For hybrid, the x86_pmu 8448 * is used to keep the common capabilities. Still keep the values 8449 * from the leaf 0xa. The core specific update will be done later 8450 * when a new type is online. 8451 */ 8452 if (!is_hybrid() && boot_cpu_has(X86_FEATURE_ARCH_PERFMON_EXT)) 8453 update_pmu_cap(NULL); 8454 8455 if (x86_pmu.arch_pebs) { 8456 static_call_update(intel_pmu_disable_event_ext, 8457 intel_pmu_disable_event_ext); 8458 static_call_update(intel_pmu_enable_event_ext, 8459 intel_pmu_enable_event_ext); 8460 pr_cont("Architectural PEBS, "); 8461 } 8462 8463 intel_pmu_check_counters_mask(&x86_pmu.cntr_mask64, 8464 &x86_pmu.fixed_cntr_mask64, 8465 &x86_pmu.intel_ctrl); 8466 8467 /* AnyThread may be deprecated on arch perfmon v5 or later */ 8468 if (x86_pmu.intel_cap.anythread_deprecated) 8469 x86_pmu.format_attrs = intel_arch_formats_attr; 8470 8471 intel_pmu_check_event_constraints_all(NULL); 8472 8473 /* 8474 * Access LBR MSR may cause #GP under certain circumstances. 8475 * Check all LBR MSR here. 8476 * Disable LBR access if any LBR MSRs can not be accessed. 8477 */ 8478 if (x86_pmu.lbr_tos && !check_msr(x86_pmu.lbr_tos, 0x3UL)) 8479 x86_pmu.lbr_nr = 0; 8480 for (i = 0; i < x86_pmu.lbr_nr; i++) { 8481 if (!(check_msr(x86_pmu.lbr_from + i, 0xffffUL) && 8482 check_msr(x86_pmu.lbr_to + i, 0xffffUL))) 8483 x86_pmu.lbr_nr = 0; 8484 } 8485 8486 if (x86_pmu.lbr_nr) { 8487 intel_pmu_lbr_init(); 8488 8489 pr_cont("%d-deep LBR, ", x86_pmu.lbr_nr); 8490 8491 /* only support branch_stack snapshot for perfmon >= v2 */ 8492 if (x86_pmu.disable_all == intel_pmu_disable_all) { 8493 if (boot_cpu_has(X86_FEATURE_ARCH_LBR)) { 8494 static_call_update(perf_snapshot_branch_stack, 8495 intel_pmu_snapshot_arch_branch_stack); 8496 } else { 8497 static_call_update(perf_snapshot_branch_stack, 8498 intel_pmu_snapshot_branch_stack); 8499 } 8500 } 8501 } 8502 8503 intel_pmu_check_extra_regs(x86_pmu.extra_regs); 8504 8505 /* Support full width counters using alternative MSR range */ 8506 if (x86_pmu.intel_cap.full_width_write) { 8507 x86_pmu.max_period = x86_pmu.cntval_mask >> 1; 8508 x86_pmu.perfctr = MSR_IA32_PMC0; 8509 pr_cont("full-width counters, "); 8510 } 8511 8512 /* Support V6+ MSR Aliasing */ 8513 if (x86_pmu.version >= 6) { 8514 x86_pmu.perfctr = MSR_IA32_PMC_V6_GP0_CTR; 8515 x86_pmu.eventsel = MSR_IA32_PMC_V6_GP0_CFG_A; 8516 x86_pmu.fixedctr = MSR_IA32_PMC_V6_FX0_CTR; 8517 x86_pmu.addr_offset = intel_pmu_v6_addr_offset; 8518 } 8519 8520 if (!is_hybrid() && x86_pmu.intel_cap.perf_metrics) 8521 x86_pmu.intel_ctrl |= GLOBAL_CTRL_EN_PERF_METRICS; 8522 8523 if (x86_pmu.intel_cap.pebs_timing_info) 8524 x86_pmu.flags |= PMU_FL_RETIRE_LATENCY; 8525 8526 intel_aux_output_init(); 8527 8528 return 0; 8529 } 8530 8531 /* 8532 * HT bug: phase 2 init 8533 * Called once we have valid topology information to check 8534 * whether or not HT is enabled 8535 * If HT is off, then we disable the workaround 8536 */ 8537 static __init int fixup_ht_bug(void) 8538 { 8539 int c; 8540 /* 8541 * problem not present on this CPU model, nothing to do 8542 */ 8543 if (!(x86_pmu.flags & PMU_FL_EXCL_ENABLED)) 8544 return 0; 8545 8546 if (topology_max_smt_threads() > 1) { 8547 pr_info("PMU erratum BJ122, BV98, HSD29 worked around, HT is on\n"); 8548 return 0; 8549 } 8550 8551 cpus_read_lock(); 8552 8553 hardlockup_detector_perf_stop(); 8554 8555 x86_pmu.flags &= ~(PMU_FL_EXCL_CNTRS | PMU_FL_EXCL_ENABLED); 8556 8557 x86_pmu.start_scheduling = NULL; 8558 x86_pmu.commit_scheduling = NULL; 8559 x86_pmu.stop_scheduling = NULL; 8560 8561 hardlockup_detector_perf_restart(); 8562 8563 for_each_online_cpu(c) 8564 free_excl_cntrs(&per_cpu(cpu_hw_events, c)); 8565 8566 cpus_read_unlock(); 8567 pr_info("PMU erratum BJ122, BV98, HSD29 workaround disabled, HT off\n"); 8568 return 0; 8569 } 8570 subsys_initcall(fixup_ht_bug) 8571