1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun #include <linux/perf_event.h>
3*4882a593Smuzhiyun #include <linux/sysfs.h>
4*4882a593Smuzhiyun #include <linux/nospec.h>
5*4882a593Smuzhiyun #include <asm/intel-family.h>
6*4882a593Smuzhiyun #include "probe.h"
7*4882a593Smuzhiyun
8*4882a593Smuzhiyun enum perf_msr_id {
9*4882a593Smuzhiyun PERF_MSR_TSC = 0,
10*4882a593Smuzhiyun PERF_MSR_APERF = 1,
11*4882a593Smuzhiyun PERF_MSR_MPERF = 2,
12*4882a593Smuzhiyun PERF_MSR_PPERF = 3,
13*4882a593Smuzhiyun PERF_MSR_SMI = 4,
14*4882a593Smuzhiyun PERF_MSR_PTSC = 5,
15*4882a593Smuzhiyun PERF_MSR_IRPERF = 6,
16*4882a593Smuzhiyun PERF_MSR_THERM = 7,
17*4882a593Smuzhiyun PERF_MSR_EVENT_MAX,
18*4882a593Smuzhiyun };
19*4882a593Smuzhiyun
test_aperfmperf(int idx,void * data)20*4882a593Smuzhiyun static bool test_aperfmperf(int idx, void *data)
21*4882a593Smuzhiyun {
22*4882a593Smuzhiyun return boot_cpu_has(X86_FEATURE_APERFMPERF);
23*4882a593Smuzhiyun }
24*4882a593Smuzhiyun
test_ptsc(int idx,void * data)25*4882a593Smuzhiyun static bool test_ptsc(int idx, void *data)
26*4882a593Smuzhiyun {
27*4882a593Smuzhiyun return boot_cpu_has(X86_FEATURE_PTSC);
28*4882a593Smuzhiyun }
29*4882a593Smuzhiyun
test_irperf(int idx,void * data)30*4882a593Smuzhiyun static bool test_irperf(int idx, void *data)
31*4882a593Smuzhiyun {
32*4882a593Smuzhiyun return boot_cpu_has(X86_FEATURE_IRPERF);
33*4882a593Smuzhiyun }
34*4882a593Smuzhiyun
test_therm_status(int idx,void * data)35*4882a593Smuzhiyun static bool test_therm_status(int idx, void *data)
36*4882a593Smuzhiyun {
37*4882a593Smuzhiyun return boot_cpu_has(X86_FEATURE_DTHERM);
38*4882a593Smuzhiyun }
39*4882a593Smuzhiyun
test_intel(int idx,void * data)40*4882a593Smuzhiyun static bool test_intel(int idx, void *data)
41*4882a593Smuzhiyun {
42*4882a593Smuzhiyun if (boot_cpu_data.x86_vendor != X86_VENDOR_INTEL ||
43*4882a593Smuzhiyun boot_cpu_data.x86 != 6)
44*4882a593Smuzhiyun return false;
45*4882a593Smuzhiyun
46*4882a593Smuzhiyun switch (boot_cpu_data.x86_model) {
47*4882a593Smuzhiyun case INTEL_FAM6_NEHALEM:
48*4882a593Smuzhiyun case INTEL_FAM6_NEHALEM_G:
49*4882a593Smuzhiyun case INTEL_FAM6_NEHALEM_EP:
50*4882a593Smuzhiyun case INTEL_FAM6_NEHALEM_EX:
51*4882a593Smuzhiyun
52*4882a593Smuzhiyun case INTEL_FAM6_WESTMERE:
53*4882a593Smuzhiyun case INTEL_FAM6_WESTMERE_EP:
54*4882a593Smuzhiyun case INTEL_FAM6_WESTMERE_EX:
55*4882a593Smuzhiyun
56*4882a593Smuzhiyun case INTEL_FAM6_SANDYBRIDGE:
57*4882a593Smuzhiyun case INTEL_FAM6_SANDYBRIDGE_X:
58*4882a593Smuzhiyun
59*4882a593Smuzhiyun case INTEL_FAM6_IVYBRIDGE:
60*4882a593Smuzhiyun case INTEL_FAM6_IVYBRIDGE_X:
61*4882a593Smuzhiyun
62*4882a593Smuzhiyun case INTEL_FAM6_HASWELL:
63*4882a593Smuzhiyun case INTEL_FAM6_HASWELL_X:
64*4882a593Smuzhiyun case INTEL_FAM6_HASWELL_L:
65*4882a593Smuzhiyun case INTEL_FAM6_HASWELL_G:
66*4882a593Smuzhiyun
67*4882a593Smuzhiyun case INTEL_FAM6_BROADWELL:
68*4882a593Smuzhiyun case INTEL_FAM6_BROADWELL_D:
69*4882a593Smuzhiyun case INTEL_FAM6_BROADWELL_G:
70*4882a593Smuzhiyun case INTEL_FAM6_BROADWELL_X:
71*4882a593Smuzhiyun case INTEL_FAM6_SAPPHIRERAPIDS_X:
72*4882a593Smuzhiyun
73*4882a593Smuzhiyun case INTEL_FAM6_ATOM_SILVERMONT:
74*4882a593Smuzhiyun case INTEL_FAM6_ATOM_SILVERMONT_D:
75*4882a593Smuzhiyun case INTEL_FAM6_ATOM_AIRMONT:
76*4882a593Smuzhiyun
77*4882a593Smuzhiyun case INTEL_FAM6_ATOM_GOLDMONT:
78*4882a593Smuzhiyun case INTEL_FAM6_ATOM_GOLDMONT_D:
79*4882a593Smuzhiyun case INTEL_FAM6_ATOM_GOLDMONT_PLUS:
80*4882a593Smuzhiyun case INTEL_FAM6_ATOM_TREMONT_D:
81*4882a593Smuzhiyun case INTEL_FAM6_ATOM_TREMONT:
82*4882a593Smuzhiyun case INTEL_FAM6_ATOM_TREMONT_L:
83*4882a593Smuzhiyun
84*4882a593Smuzhiyun case INTEL_FAM6_XEON_PHI_KNL:
85*4882a593Smuzhiyun case INTEL_FAM6_XEON_PHI_KNM:
86*4882a593Smuzhiyun if (idx == PERF_MSR_SMI)
87*4882a593Smuzhiyun return true;
88*4882a593Smuzhiyun break;
89*4882a593Smuzhiyun
90*4882a593Smuzhiyun case INTEL_FAM6_SKYLAKE_L:
91*4882a593Smuzhiyun case INTEL_FAM6_SKYLAKE:
92*4882a593Smuzhiyun case INTEL_FAM6_SKYLAKE_X:
93*4882a593Smuzhiyun case INTEL_FAM6_KABYLAKE_L:
94*4882a593Smuzhiyun case INTEL_FAM6_KABYLAKE:
95*4882a593Smuzhiyun case INTEL_FAM6_COMETLAKE_L:
96*4882a593Smuzhiyun case INTEL_FAM6_COMETLAKE:
97*4882a593Smuzhiyun case INTEL_FAM6_ICELAKE_L:
98*4882a593Smuzhiyun case INTEL_FAM6_ICELAKE:
99*4882a593Smuzhiyun case INTEL_FAM6_ICELAKE_X:
100*4882a593Smuzhiyun case INTEL_FAM6_ICELAKE_D:
101*4882a593Smuzhiyun case INTEL_FAM6_TIGERLAKE_L:
102*4882a593Smuzhiyun case INTEL_FAM6_TIGERLAKE:
103*4882a593Smuzhiyun if (idx == PERF_MSR_SMI || idx == PERF_MSR_PPERF)
104*4882a593Smuzhiyun return true;
105*4882a593Smuzhiyun break;
106*4882a593Smuzhiyun }
107*4882a593Smuzhiyun
108*4882a593Smuzhiyun return false;
109*4882a593Smuzhiyun }
110*4882a593Smuzhiyun
111*4882a593Smuzhiyun PMU_EVENT_ATTR_STRING(tsc, attr_tsc, "event=0x00" );
112*4882a593Smuzhiyun PMU_EVENT_ATTR_STRING(aperf, attr_aperf, "event=0x01" );
113*4882a593Smuzhiyun PMU_EVENT_ATTR_STRING(mperf, attr_mperf, "event=0x02" );
114*4882a593Smuzhiyun PMU_EVENT_ATTR_STRING(pperf, attr_pperf, "event=0x03" );
115*4882a593Smuzhiyun PMU_EVENT_ATTR_STRING(smi, attr_smi, "event=0x04" );
116*4882a593Smuzhiyun PMU_EVENT_ATTR_STRING(ptsc, attr_ptsc, "event=0x05" );
117*4882a593Smuzhiyun PMU_EVENT_ATTR_STRING(irperf, attr_irperf, "event=0x06" );
118*4882a593Smuzhiyun PMU_EVENT_ATTR_STRING(cpu_thermal_margin, attr_therm, "event=0x07" );
119*4882a593Smuzhiyun PMU_EVENT_ATTR_STRING(cpu_thermal_margin.snapshot, attr_therm_snap, "1" );
120*4882a593Smuzhiyun PMU_EVENT_ATTR_STRING(cpu_thermal_margin.unit, attr_therm_unit, "C" );
121*4882a593Smuzhiyun
122*4882a593Smuzhiyun static unsigned long msr_mask;
123*4882a593Smuzhiyun
124*4882a593Smuzhiyun PMU_EVENT_GROUP(events, aperf);
125*4882a593Smuzhiyun PMU_EVENT_GROUP(events, mperf);
126*4882a593Smuzhiyun PMU_EVENT_GROUP(events, pperf);
127*4882a593Smuzhiyun PMU_EVENT_GROUP(events, smi);
128*4882a593Smuzhiyun PMU_EVENT_GROUP(events, ptsc);
129*4882a593Smuzhiyun PMU_EVENT_GROUP(events, irperf);
130*4882a593Smuzhiyun
131*4882a593Smuzhiyun static struct attribute *attrs_therm[] = {
132*4882a593Smuzhiyun &attr_therm.attr.attr,
133*4882a593Smuzhiyun &attr_therm_snap.attr.attr,
134*4882a593Smuzhiyun &attr_therm_unit.attr.attr,
135*4882a593Smuzhiyun NULL,
136*4882a593Smuzhiyun };
137*4882a593Smuzhiyun
138*4882a593Smuzhiyun static struct attribute_group group_therm = {
139*4882a593Smuzhiyun .name = "events",
140*4882a593Smuzhiyun .attrs = attrs_therm,
141*4882a593Smuzhiyun };
142*4882a593Smuzhiyun
143*4882a593Smuzhiyun static struct perf_msr msr[] = {
144*4882a593Smuzhiyun [PERF_MSR_TSC] = { .no_check = true, },
145*4882a593Smuzhiyun [PERF_MSR_APERF] = { MSR_IA32_APERF, &group_aperf, test_aperfmperf, },
146*4882a593Smuzhiyun [PERF_MSR_MPERF] = { MSR_IA32_MPERF, &group_mperf, test_aperfmperf, },
147*4882a593Smuzhiyun [PERF_MSR_PPERF] = { MSR_PPERF, &group_pperf, test_intel, },
148*4882a593Smuzhiyun [PERF_MSR_SMI] = { MSR_SMI_COUNT, &group_smi, test_intel, },
149*4882a593Smuzhiyun [PERF_MSR_PTSC] = { MSR_F15H_PTSC, &group_ptsc, test_ptsc, },
150*4882a593Smuzhiyun [PERF_MSR_IRPERF] = { MSR_F17H_IRPERF, &group_irperf, test_irperf, },
151*4882a593Smuzhiyun [PERF_MSR_THERM] = { MSR_IA32_THERM_STATUS, &group_therm, test_therm_status, },
152*4882a593Smuzhiyun };
153*4882a593Smuzhiyun
154*4882a593Smuzhiyun static struct attribute *events_attrs[] = {
155*4882a593Smuzhiyun &attr_tsc.attr.attr,
156*4882a593Smuzhiyun NULL,
157*4882a593Smuzhiyun };
158*4882a593Smuzhiyun
159*4882a593Smuzhiyun static struct attribute_group events_attr_group = {
160*4882a593Smuzhiyun .name = "events",
161*4882a593Smuzhiyun .attrs = events_attrs,
162*4882a593Smuzhiyun };
163*4882a593Smuzhiyun
164*4882a593Smuzhiyun PMU_FORMAT_ATTR(event, "config:0-63");
165*4882a593Smuzhiyun static struct attribute *format_attrs[] = {
166*4882a593Smuzhiyun &format_attr_event.attr,
167*4882a593Smuzhiyun NULL,
168*4882a593Smuzhiyun };
169*4882a593Smuzhiyun static struct attribute_group format_attr_group = {
170*4882a593Smuzhiyun .name = "format",
171*4882a593Smuzhiyun .attrs = format_attrs,
172*4882a593Smuzhiyun };
173*4882a593Smuzhiyun
174*4882a593Smuzhiyun static const struct attribute_group *attr_groups[] = {
175*4882a593Smuzhiyun &events_attr_group,
176*4882a593Smuzhiyun &format_attr_group,
177*4882a593Smuzhiyun NULL,
178*4882a593Smuzhiyun };
179*4882a593Smuzhiyun
180*4882a593Smuzhiyun static const struct attribute_group *attr_update[] = {
181*4882a593Smuzhiyun &group_aperf,
182*4882a593Smuzhiyun &group_mperf,
183*4882a593Smuzhiyun &group_pperf,
184*4882a593Smuzhiyun &group_smi,
185*4882a593Smuzhiyun &group_ptsc,
186*4882a593Smuzhiyun &group_irperf,
187*4882a593Smuzhiyun &group_therm,
188*4882a593Smuzhiyun NULL,
189*4882a593Smuzhiyun };
190*4882a593Smuzhiyun
msr_event_init(struct perf_event * event)191*4882a593Smuzhiyun static int msr_event_init(struct perf_event *event)
192*4882a593Smuzhiyun {
193*4882a593Smuzhiyun u64 cfg = event->attr.config;
194*4882a593Smuzhiyun
195*4882a593Smuzhiyun if (event->attr.type != event->pmu->type)
196*4882a593Smuzhiyun return -ENOENT;
197*4882a593Smuzhiyun
198*4882a593Smuzhiyun /* unsupported modes and filters */
199*4882a593Smuzhiyun if (event->attr.sample_period) /* no sampling */
200*4882a593Smuzhiyun return -EINVAL;
201*4882a593Smuzhiyun
202*4882a593Smuzhiyun if (cfg >= PERF_MSR_EVENT_MAX)
203*4882a593Smuzhiyun return -EINVAL;
204*4882a593Smuzhiyun
205*4882a593Smuzhiyun cfg = array_index_nospec((unsigned long)cfg, PERF_MSR_EVENT_MAX);
206*4882a593Smuzhiyun
207*4882a593Smuzhiyun if (!(msr_mask & (1 << cfg)))
208*4882a593Smuzhiyun return -EINVAL;
209*4882a593Smuzhiyun
210*4882a593Smuzhiyun event->hw.idx = -1;
211*4882a593Smuzhiyun event->hw.event_base = msr[cfg].msr;
212*4882a593Smuzhiyun event->hw.config = cfg;
213*4882a593Smuzhiyun
214*4882a593Smuzhiyun return 0;
215*4882a593Smuzhiyun }
216*4882a593Smuzhiyun
msr_read_counter(struct perf_event * event)217*4882a593Smuzhiyun static inline u64 msr_read_counter(struct perf_event *event)
218*4882a593Smuzhiyun {
219*4882a593Smuzhiyun u64 now;
220*4882a593Smuzhiyun
221*4882a593Smuzhiyun if (event->hw.event_base)
222*4882a593Smuzhiyun rdmsrl(event->hw.event_base, now);
223*4882a593Smuzhiyun else
224*4882a593Smuzhiyun now = rdtsc_ordered();
225*4882a593Smuzhiyun
226*4882a593Smuzhiyun return now;
227*4882a593Smuzhiyun }
228*4882a593Smuzhiyun
msr_event_update(struct perf_event * event)229*4882a593Smuzhiyun static void msr_event_update(struct perf_event *event)
230*4882a593Smuzhiyun {
231*4882a593Smuzhiyun u64 prev, now;
232*4882a593Smuzhiyun s64 delta;
233*4882a593Smuzhiyun
234*4882a593Smuzhiyun /* Careful, an NMI might modify the previous event value: */
235*4882a593Smuzhiyun again:
236*4882a593Smuzhiyun prev = local64_read(&event->hw.prev_count);
237*4882a593Smuzhiyun now = msr_read_counter(event);
238*4882a593Smuzhiyun
239*4882a593Smuzhiyun if (local64_cmpxchg(&event->hw.prev_count, prev, now) != prev)
240*4882a593Smuzhiyun goto again;
241*4882a593Smuzhiyun
242*4882a593Smuzhiyun delta = now - prev;
243*4882a593Smuzhiyun if (unlikely(event->hw.event_base == MSR_SMI_COUNT)) {
244*4882a593Smuzhiyun delta = sign_extend64(delta, 31);
245*4882a593Smuzhiyun local64_add(delta, &event->count);
246*4882a593Smuzhiyun } else if (unlikely(event->hw.event_base == MSR_IA32_THERM_STATUS)) {
247*4882a593Smuzhiyun /* If valid, extract digital readout, otherwise set to -1: */
248*4882a593Smuzhiyun now = now & (1ULL << 31) ? (now >> 16) & 0x3f : -1;
249*4882a593Smuzhiyun local64_set(&event->count, now);
250*4882a593Smuzhiyun } else {
251*4882a593Smuzhiyun local64_add(delta, &event->count);
252*4882a593Smuzhiyun }
253*4882a593Smuzhiyun }
254*4882a593Smuzhiyun
msr_event_start(struct perf_event * event,int flags)255*4882a593Smuzhiyun static void msr_event_start(struct perf_event *event, int flags)
256*4882a593Smuzhiyun {
257*4882a593Smuzhiyun u64 now = msr_read_counter(event);
258*4882a593Smuzhiyun
259*4882a593Smuzhiyun local64_set(&event->hw.prev_count, now);
260*4882a593Smuzhiyun }
261*4882a593Smuzhiyun
msr_event_stop(struct perf_event * event,int flags)262*4882a593Smuzhiyun static void msr_event_stop(struct perf_event *event, int flags)
263*4882a593Smuzhiyun {
264*4882a593Smuzhiyun msr_event_update(event);
265*4882a593Smuzhiyun }
266*4882a593Smuzhiyun
msr_event_del(struct perf_event * event,int flags)267*4882a593Smuzhiyun static void msr_event_del(struct perf_event *event, int flags)
268*4882a593Smuzhiyun {
269*4882a593Smuzhiyun msr_event_stop(event, PERF_EF_UPDATE);
270*4882a593Smuzhiyun }
271*4882a593Smuzhiyun
msr_event_add(struct perf_event * event,int flags)272*4882a593Smuzhiyun static int msr_event_add(struct perf_event *event, int flags)
273*4882a593Smuzhiyun {
274*4882a593Smuzhiyun if (flags & PERF_EF_START)
275*4882a593Smuzhiyun msr_event_start(event, flags);
276*4882a593Smuzhiyun
277*4882a593Smuzhiyun return 0;
278*4882a593Smuzhiyun }
279*4882a593Smuzhiyun
280*4882a593Smuzhiyun static struct pmu pmu_msr = {
281*4882a593Smuzhiyun .task_ctx_nr = perf_sw_context,
282*4882a593Smuzhiyun .attr_groups = attr_groups,
283*4882a593Smuzhiyun .event_init = msr_event_init,
284*4882a593Smuzhiyun .add = msr_event_add,
285*4882a593Smuzhiyun .del = msr_event_del,
286*4882a593Smuzhiyun .start = msr_event_start,
287*4882a593Smuzhiyun .stop = msr_event_stop,
288*4882a593Smuzhiyun .read = msr_event_update,
289*4882a593Smuzhiyun .capabilities = PERF_PMU_CAP_NO_INTERRUPT | PERF_PMU_CAP_NO_EXCLUDE,
290*4882a593Smuzhiyun .attr_update = attr_update,
291*4882a593Smuzhiyun };
292*4882a593Smuzhiyun
msr_init(void)293*4882a593Smuzhiyun static int __init msr_init(void)
294*4882a593Smuzhiyun {
295*4882a593Smuzhiyun if (!boot_cpu_has(X86_FEATURE_TSC)) {
296*4882a593Smuzhiyun pr_cont("no MSR PMU driver.\n");
297*4882a593Smuzhiyun return 0;
298*4882a593Smuzhiyun }
299*4882a593Smuzhiyun
300*4882a593Smuzhiyun msr_mask = perf_msr_probe(msr, PERF_MSR_EVENT_MAX, true, NULL);
301*4882a593Smuzhiyun
302*4882a593Smuzhiyun perf_pmu_register(&pmu_msr, "msr", -1);
303*4882a593Smuzhiyun
304*4882a593Smuzhiyun return 0;
305*4882a593Smuzhiyun }
306*4882a593Smuzhiyun device_initcall(msr_init);
307