1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/perf_event.h>
3 #include <linux/sysfs.h>
4 #include <linux/nospec.h>
5 #include <asm/intel-family.h>
20 static bool test_aperfmperf(int idx, void *data)
22 return boot_cpu_has(X86_FEATURE_APERFMPERF);
25 static bool test_ptsc(int idx, void *data)
27 return boot_cpu_has(X86_FEATURE_PTSC);
30 static bool test_irperf(int idx, void *data)
32 return boot_cpu_has(X86_FEATURE_IRPERF);
35 static bool test_therm_status(int idx, void *data)
37 return boot_cpu_has(X86_FEATURE_DTHERM);
40 static bool test_intel(int idx, void *data)
42 if (boot_cpu_data.x86_vendor != X86_VENDOR_INTEL ||
43 boot_cpu_data.x86 != 6)
46 switch (boot_cpu_data.x86_model) {
47 case INTEL_FAM6_NEHALEM:
48 case INTEL_FAM6_NEHALEM_G:
49 case INTEL_FAM6_NEHALEM_EP:
50 case INTEL_FAM6_NEHALEM_EX:
52 case INTEL_FAM6_WESTMERE:
53 case INTEL_FAM6_WESTMERE_EP:
54 case INTEL_FAM6_WESTMERE_EX:
56 case INTEL_FAM6_SANDYBRIDGE:
57 case INTEL_FAM6_SANDYBRIDGE_X:
59 case INTEL_FAM6_IVYBRIDGE:
60 case INTEL_FAM6_IVYBRIDGE_X:
62 case INTEL_FAM6_HASWELL_CORE:
63 case INTEL_FAM6_HASWELL_X:
64 case INTEL_FAM6_HASWELL_ULT:
65 case INTEL_FAM6_HASWELL_GT3E:
67 case INTEL_FAM6_BROADWELL_CORE:
68 case INTEL_FAM6_BROADWELL_XEON_D:
69 case INTEL_FAM6_BROADWELL_GT3E:
70 case INTEL_FAM6_BROADWELL_X:
72 case INTEL_FAM6_ATOM_SILVERMONT:
73 case INTEL_FAM6_ATOM_SILVERMONT_X:
74 case INTEL_FAM6_ATOM_AIRMONT:
76 case INTEL_FAM6_ATOM_GOLDMONT:
77 case INTEL_FAM6_ATOM_GOLDMONT_X:
79 case INTEL_FAM6_ATOM_GOLDMONT_PLUS:
81 case INTEL_FAM6_XEON_PHI_KNL:
82 case INTEL_FAM6_XEON_PHI_KNM:
83 if (idx == PERF_MSR_SMI)
87 case INTEL_FAM6_SKYLAKE_MOBILE:
88 case INTEL_FAM6_SKYLAKE_DESKTOP:
89 case INTEL_FAM6_SKYLAKE_X:
90 case INTEL_FAM6_KABYLAKE_MOBILE:
91 case INTEL_FAM6_KABYLAKE_DESKTOP:
92 case INTEL_FAM6_ICELAKE_MOBILE:
93 if (idx == PERF_MSR_SMI || idx == PERF_MSR_PPERF)
101 PMU_EVENT_ATTR_STRING(tsc, attr_tsc, "event=0x00" );
102 PMU_EVENT_ATTR_STRING(aperf, attr_aperf, "event=0x01" );
103 PMU_EVENT_ATTR_STRING(mperf, attr_mperf, "event=0x02" );
104 PMU_EVENT_ATTR_STRING(pperf, attr_pperf, "event=0x03" );
105 PMU_EVENT_ATTR_STRING(smi, attr_smi, "event=0x04" );
106 PMU_EVENT_ATTR_STRING(ptsc, attr_ptsc, "event=0x05" );
107 PMU_EVENT_ATTR_STRING(irperf, attr_irperf, "event=0x06" );
108 PMU_EVENT_ATTR_STRING(cpu_thermal_margin, attr_therm, "event=0x07" );
109 PMU_EVENT_ATTR_STRING(cpu_thermal_margin.snapshot, attr_therm_snap, "1" );
110 PMU_EVENT_ATTR_STRING(cpu_thermal_margin.unit, attr_therm_unit, "C" );
112 static unsigned long msr_mask;
114 PMU_EVENT_GROUP(events, aperf);
115 PMU_EVENT_GROUP(events, mperf);
116 PMU_EVENT_GROUP(events, pperf);
117 PMU_EVENT_GROUP(events, smi);
118 PMU_EVENT_GROUP(events, ptsc);
119 PMU_EVENT_GROUP(events, irperf);
121 static struct attribute *attrs_therm[] = {
122 &attr_therm.attr.attr,
123 &attr_therm_snap.attr.attr,
124 &attr_therm_unit.attr.attr,
128 static struct attribute_group group_therm = {
130 .attrs = attrs_therm,
133 static struct perf_msr msr[] = {
134 [PERF_MSR_TSC] = { .no_check = true, },
135 [PERF_MSR_APERF] = { MSR_IA32_APERF, &group_aperf, test_aperfmperf, },
136 [PERF_MSR_MPERF] = { MSR_IA32_MPERF, &group_mperf, test_aperfmperf, },
137 [PERF_MSR_PPERF] = { MSR_PPERF, &group_pperf, test_intel, },
138 [PERF_MSR_SMI] = { MSR_SMI_COUNT, &group_smi, test_intel, },
139 [PERF_MSR_PTSC] = { MSR_F15H_PTSC, &group_ptsc, test_ptsc, },
140 [PERF_MSR_IRPERF] = { MSR_F17H_IRPERF, &group_irperf, test_irperf, },
141 [PERF_MSR_THERM] = { MSR_IA32_THERM_STATUS, &group_therm, test_therm_status, },
144 static struct attribute *events_attrs[] = {
149 static struct attribute_group events_attr_group = {
151 .attrs = events_attrs,
154 PMU_FORMAT_ATTR(event, "config:0-63");
155 static struct attribute *format_attrs[] = {
156 &format_attr_event.attr,
159 static struct attribute_group format_attr_group = {
161 .attrs = format_attrs,
164 static const struct attribute_group *attr_groups[] = {
170 const struct attribute_group *attr_update[] = {
181 static int msr_event_init(struct perf_event *event)
183 u64 cfg = event->attr.config;
185 if (event->attr.type != event->pmu->type)
188 /* unsupported modes and filters */
189 if (event->attr.sample_period) /* no sampling */
192 if (cfg >= PERF_MSR_EVENT_MAX)
195 cfg = array_index_nospec((unsigned long)cfg, PERF_MSR_EVENT_MAX);
197 if (!(msr_mask & (1 << cfg)))
201 event->hw.event_base = msr[cfg].msr;
202 event->hw.config = cfg;
207 static inline u64 msr_read_counter(struct perf_event *event)
211 if (event->hw.event_base)
212 rdmsrl(event->hw.event_base, now);
214 now = rdtsc_ordered();
219 static void msr_event_update(struct perf_event *event)
224 /* Careful, an NMI might modify the previous event value: */
226 prev = local64_read(&event->hw.prev_count);
227 now = msr_read_counter(event);
229 if (local64_cmpxchg(&event->hw.prev_count, prev, now) != prev)
233 if (unlikely(event->hw.event_base == MSR_SMI_COUNT)) {
234 delta = sign_extend64(delta, 31);
235 local64_add(delta, &event->count);
236 } else if (unlikely(event->hw.event_base == MSR_IA32_THERM_STATUS)) {
237 /* If valid, extract digital readout, otherwise set to -1: */
238 now = now & (1ULL << 31) ? (now >> 16) & 0x3f : -1;
239 local64_set(&event->count, now);
241 local64_add(delta, &event->count);
245 static void msr_event_start(struct perf_event *event, int flags)
247 u64 now = msr_read_counter(event);
249 local64_set(&event->hw.prev_count, now);
252 static void msr_event_stop(struct perf_event *event, int flags)
254 msr_event_update(event);
257 static void msr_event_del(struct perf_event *event, int flags)
259 msr_event_stop(event, PERF_EF_UPDATE);
262 static int msr_event_add(struct perf_event *event, int flags)
264 if (flags & PERF_EF_START)
265 msr_event_start(event, flags);
270 static struct pmu pmu_msr = {
271 .task_ctx_nr = perf_sw_context,
272 .attr_groups = attr_groups,
273 .event_init = msr_event_init,
274 .add = msr_event_add,
275 .del = msr_event_del,
276 .start = msr_event_start,
277 .stop = msr_event_stop,
278 .read = msr_event_update,
279 .capabilities = PERF_PMU_CAP_NO_INTERRUPT | PERF_PMU_CAP_NO_EXCLUDE,
280 .attr_update = attr_update,
283 static int __init msr_init(void)
285 if (!boot_cpu_has(X86_FEATURE_TSC)) {
286 pr_cont("no MSR PMU driver.\n");
290 msr_mask = perf_msr_probe(msr, PERF_MSR_EVENT_MAX, true, NULL);
292 perf_pmu_register(&pmu_msr, "msr", -1);
296 device_initcall(msr_init);