]> git.proxmox.com Git - mirror_ubuntu-focal-kernel.git/blame - arch/x86/events/intel/cstate.c
x86/perf/intel/cstate: Make cstate hotplug handling actually work
[mirror_ubuntu-focal-kernel.git] / arch / x86 / events / intel / cstate.c
CommitLineData
7ce1346a
KL
1/*
2 * perf_event_intel_cstate.c: support cstate residency counters
3 *
4 * Copyright (C) 2015, Intel Corp.
5 * Author: Kan Liang (kan.liang@intel.com)
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
16 *
17 */
18
19/*
20 * This file export cstate related free running (read-only) counters
21 * for perf. These counters may be use simultaneously by other tools,
22 * such as turbostat. However, it still make sense to implement them
23 * in perf. Because we can conveniently collect them together with
24 * other events, and allow to use them from tools without special MSR
25 * access code.
26 *
27 * The events only support system-wide mode counting. There is no
28 * sampling support because it is not supported by the hardware.
29 *
30 * According to counters' scope and category, two PMUs are registered
31 * with the perf_event core subsystem.
32 * - 'cstate_core': The counter is available for each physical core.
33 * The counters include CORE_C*_RESIDENCY.
34 * - 'cstate_pkg': The counter is available for each physical package.
35 * The counters include PKG_C*_RESIDENCY.
36 *
37 * All of these counters are specified in the IntelĀ® 64 and IA-32
38 * Architectures Software Developer.s Manual Vol3b.
39 *
40 * Model specific counters:
41 * MSR_CORE_C1_RES: CORE C1 Residency Counter
42 * perf code: 0x00
43 * Available model: SLM,AMT
44 * Scope: Core (each processor core has a MSR)
45 * MSR_CORE_C3_RESIDENCY: CORE C3 Residency Counter
46 * perf code: 0x01
47 * Available model: NHM,WSM,SNB,IVB,HSW,BDW,SKL
48 * Scope: Core
49 * MSR_CORE_C6_RESIDENCY: CORE C6 Residency Counter
50 * perf code: 0x02
51 * Available model: SLM,AMT,NHM,WSM,SNB,IVB,HSW,BDW,SKL
52 * Scope: Core
53 * MSR_CORE_C7_RESIDENCY: CORE C7 Residency Counter
54 * perf code: 0x03
55 * Available model: SNB,IVB,HSW,BDW,SKL
56 * Scope: Core
57 * MSR_PKG_C2_RESIDENCY: Package C2 Residency Counter.
58 * perf code: 0x00
59 * Available model: SNB,IVB,HSW,BDW,SKL
60 * Scope: Package (physical package)
61 * MSR_PKG_C3_RESIDENCY: Package C3 Residency Counter.
62 * perf code: 0x01
63 * Available model: NHM,WSM,SNB,IVB,HSW,BDW,SKL
64 * Scope: Package (physical package)
65 * MSR_PKG_C6_RESIDENCY: Package C6 Residency Counter.
66 * perf code: 0x02
67 * Available model: SLM,AMT,NHM,WSM,SNB,IVB,HSW,BDW,SKL
68 * Scope: Package (physical package)
69 * MSR_PKG_C7_RESIDENCY: Package C7 Residency Counter.
70 * perf code: 0x03
71 * Available model: NHM,WSM,SNB,IVB,HSW,BDW,SKL
72 * Scope: Package (physical package)
73 * MSR_PKG_C8_RESIDENCY: Package C8 Residency Counter.
74 * perf code: 0x04
75 * Available model: HSW ULT only
76 * Scope: Package (physical package)
77 * MSR_PKG_C9_RESIDENCY: Package C9 Residency Counter.
78 * perf code: 0x05
79 * Available model: HSW ULT only
80 * Scope: Package (physical package)
81 * MSR_PKG_C10_RESIDENCY: Package C10 Residency Counter.
82 * perf code: 0x06
83 * Available model: HSW ULT only
84 * Scope: Package (physical package)
85 *
86 */
87
88#include <linux/module.h>
89#include <linux/slab.h>
90#include <linux/perf_event.h>
91#include <asm/cpu_device_id.h>
27f6d22b 92#include "../perf_event.h"
7ce1346a
KL
93
94#define DEFINE_CSTATE_FORMAT_ATTR(_var, _name, _format) \
95static ssize_t __cstate_##_var##_show(struct kobject *kobj, \
96 struct kobj_attribute *attr, \
97 char *page) \
98{ \
99 BUILD_BUG_ON(sizeof(_format) >= PAGE_SIZE); \
100 return sprintf(page, _format "\n"); \
101} \
102static struct kobj_attribute format_attr_##_var = \
103 __ATTR(_name, 0444, __cstate_##_var##_show, NULL)
104
105static ssize_t cstate_get_attr_cpumask(struct device *dev,
106 struct device_attribute *attr,
107 char *buf);
108
109struct perf_cstate_msr {
110 u64 msr;
111 struct perf_pmu_events_attr *attr;
112 bool (*test)(int idx);
113};
114
115
116/* cstate_core PMU */
117
118static struct pmu cstate_core_pmu;
119static bool has_cstate_core;
120
121enum perf_cstate_core_id {
122 /*
123 * cstate_core events
124 */
125 PERF_CSTATE_CORE_C1_RES = 0,
126 PERF_CSTATE_CORE_C3_RES,
127 PERF_CSTATE_CORE_C6_RES,
128 PERF_CSTATE_CORE_C7_RES,
129
130 PERF_CSTATE_CORE_EVENT_MAX,
131};
132
133bool test_core(int idx)
134{
135 if (boot_cpu_data.x86_vendor != X86_VENDOR_INTEL ||
136 boot_cpu_data.x86 != 6)
137 return false;
138
139 switch (boot_cpu_data.x86_model) {
140 case 30: /* 45nm Nehalem */
141 case 26: /* 45nm Nehalem-EP */
142 case 46: /* 45nm Nehalem-EX */
143
144 case 37: /* 32nm Westmere */
145 case 44: /* 32nm Westmere-EP */
146 case 47: /* 32nm Westmere-EX */
147 if (idx == PERF_CSTATE_CORE_C3_RES ||
148 idx == PERF_CSTATE_CORE_C6_RES)
149 return true;
150 break;
151 case 42: /* 32nm SandyBridge */
152 case 45: /* 32nm SandyBridge-E/EN/EP */
153
154 case 58: /* 22nm IvyBridge */
155 case 62: /* 22nm IvyBridge-EP/EX */
156
157 case 60: /* 22nm Haswell Core */
158 case 63: /* 22nm Haswell Server */
159 case 69: /* 22nm Haswell ULT */
160 case 70: /* 22nm Haswell + GT3e (Intel Iris Pro graphics) */
161
162 case 61: /* 14nm Broadwell Core-M */
163 case 86: /* 14nm Broadwell Xeon D */
164 case 71: /* 14nm Broadwell + GT3e (Intel Iris Pro graphics) */
165 case 79: /* 14nm Broadwell Server */
166
167 case 78: /* 14nm Skylake Mobile */
168 case 94: /* 14nm Skylake Desktop */
169 if (idx == PERF_CSTATE_CORE_C3_RES ||
170 idx == PERF_CSTATE_CORE_C6_RES ||
171 idx == PERF_CSTATE_CORE_C7_RES)
172 return true;
173 break;
174 case 55: /* 22nm Atom "Silvermont" */
175 case 77: /* 22nm Atom "Silvermont Avoton/Rangely" */
176 case 76: /* 14nm Atom "Airmont" */
177 if (idx == PERF_CSTATE_CORE_C1_RES ||
178 idx == PERF_CSTATE_CORE_C6_RES)
179 return true;
180 break;
181 }
182
183 return false;
184}
185
186PMU_EVENT_ATTR_STRING(c1-residency, evattr_cstate_core_c1, "event=0x00");
187PMU_EVENT_ATTR_STRING(c3-residency, evattr_cstate_core_c3, "event=0x01");
188PMU_EVENT_ATTR_STRING(c6-residency, evattr_cstate_core_c6, "event=0x02");
189PMU_EVENT_ATTR_STRING(c7-residency, evattr_cstate_core_c7, "event=0x03");
190
191static struct perf_cstate_msr core_msr[] = {
192 [PERF_CSTATE_CORE_C1_RES] = { MSR_CORE_C1_RES, &evattr_cstate_core_c1, test_core, },
193 [PERF_CSTATE_CORE_C3_RES] = { MSR_CORE_C3_RESIDENCY, &evattr_cstate_core_c3, test_core, },
194 [PERF_CSTATE_CORE_C6_RES] = { MSR_CORE_C6_RESIDENCY, &evattr_cstate_core_c6, test_core, },
195 [PERF_CSTATE_CORE_C7_RES] = { MSR_CORE_C7_RESIDENCY, &evattr_cstate_core_c7, test_core, },
196};
197
198static struct attribute *core_events_attrs[PERF_CSTATE_CORE_EVENT_MAX + 1] = {
199 NULL,
200};
201
202static struct attribute_group core_events_attr_group = {
203 .name = "events",
204 .attrs = core_events_attrs,
205};
206
207DEFINE_CSTATE_FORMAT_ATTR(core_event, event, "config:0-63");
208static struct attribute *core_format_attrs[] = {
209 &format_attr_core_event.attr,
210 NULL,
211};
212
213static struct attribute_group core_format_attr_group = {
214 .name = "format",
215 .attrs = core_format_attrs,
216};
217
218static cpumask_t cstate_core_cpu_mask;
219static DEVICE_ATTR(cpumask, S_IRUGO, cstate_get_attr_cpumask, NULL);
220
221static struct attribute *cstate_cpumask_attrs[] = {
222 &dev_attr_cpumask.attr,
223 NULL,
224};
225
226static struct attribute_group cpumask_attr_group = {
227 .attrs = cstate_cpumask_attrs,
228};
229
230static const struct attribute_group *core_attr_groups[] = {
231 &core_events_attr_group,
232 &core_format_attr_group,
233 &cpumask_attr_group,
234 NULL,
235};
236
237/* cstate_core PMU end */
238
239
240/* cstate_pkg PMU */
241
242static struct pmu cstate_pkg_pmu;
243static bool has_cstate_pkg;
244
245enum perf_cstate_pkg_id {
246 /*
247 * cstate_pkg events
248 */
249 PERF_CSTATE_PKG_C2_RES = 0,
250 PERF_CSTATE_PKG_C3_RES,
251 PERF_CSTATE_PKG_C6_RES,
252 PERF_CSTATE_PKG_C7_RES,
253 PERF_CSTATE_PKG_C8_RES,
254 PERF_CSTATE_PKG_C9_RES,
255 PERF_CSTATE_PKG_C10_RES,
256
257 PERF_CSTATE_PKG_EVENT_MAX,
258};
259
260bool test_pkg(int idx)
261{
262 if (boot_cpu_data.x86_vendor != X86_VENDOR_INTEL ||
263 boot_cpu_data.x86 != 6)
264 return false;
265
266 switch (boot_cpu_data.x86_model) {
267 case 30: /* 45nm Nehalem */
268 case 26: /* 45nm Nehalem-EP */
269 case 46: /* 45nm Nehalem-EX */
270
271 case 37: /* 32nm Westmere */
272 case 44: /* 32nm Westmere-EP */
273 case 47: /* 32nm Westmere-EX */
274 if (idx == PERF_CSTATE_CORE_C3_RES ||
275 idx == PERF_CSTATE_CORE_C6_RES ||
276 idx == PERF_CSTATE_CORE_C7_RES)
277 return true;
278 break;
279 case 42: /* 32nm SandyBridge */
280 case 45: /* 32nm SandyBridge-E/EN/EP */
281
282 case 58: /* 22nm IvyBridge */
283 case 62: /* 22nm IvyBridge-EP/EX */
284
285 case 60: /* 22nm Haswell Core */
286 case 63: /* 22nm Haswell Server */
287 case 70: /* 22nm Haswell + GT3e (Intel Iris Pro graphics) */
288
289 case 61: /* 14nm Broadwell Core-M */
290 case 86: /* 14nm Broadwell Xeon D */
291 case 71: /* 14nm Broadwell + GT3e (Intel Iris Pro graphics) */
292 case 79: /* 14nm Broadwell Server */
293
294 case 78: /* 14nm Skylake Mobile */
295 case 94: /* 14nm Skylake Desktop */
296 if (idx == PERF_CSTATE_PKG_C2_RES ||
297 idx == PERF_CSTATE_PKG_C3_RES ||
298 idx == PERF_CSTATE_PKG_C6_RES ||
299 idx == PERF_CSTATE_PKG_C7_RES)
300 return true;
301 break;
302 case 55: /* 22nm Atom "Silvermont" */
303 case 77: /* 22nm Atom "Silvermont Avoton/Rangely" */
304 case 76: /* 14nm Atom "Airmont" */
305 if (idx == PERF_CSTATE_CORE_C6_RES)
306 return true;
307 break;
308 case 69: /* 22nm Haswell ULT */
309 if (idx == PERF_CSTATE_PKG_C2_RES ||
310 idx == PERF_CSTATE_PKG_C3_RES ||
311 idx == PERF_CSTATE_PKG_C6_RES ||
312 idx == PERF_CSTATE_PKG_C7_RES ||
313 idx == PERF_CSTATE_PKG_C8_RES ||
314 idx == PERF_CSTATE_PKG_C9_RES ||
315 idx == PERF_CSTATE_PKG_C10_RES)
316 return true;
317 break;
318 }
319
320 return false;
321}
322
323PMU_EVENT_ATTR_STRING(c2-residency, evattr_cstate_pkg_c2, "event=0x00");
324PMU_EVENT_ATTR_STRING(c3-residency, evattr_cstate_pkg_c3, "event=0x01");
325PMU_EVENT_ATTR_STRING(c6-residency, evattr_cstate_pkg_c6, "event=0x02");
326PMU_EVENT_ATTR_STRING(c7-residency, evattr_cstate_pkg_c7, "event=0x03");
327PMU_EVENT_ATTR_STRING(c8-residency, evattr_cstate_pkg_c8, "event=0x04");
328PMU_EVENT_ATTR_STRING(c9-residency, evattr_cstate_pkg_c9, "event=0x05");
329PMU_EVENT_ATTR_STRING(c10-residency, evattr_cstate_pkg_c10, "event=0x06");
330
331static struct perf_cstate_msr pkg_msr[] = {
332 [PERF_CSTATE_PKG_C2_RES] = { MSR_PKG_C2_RESIDENCY, &evattr_cstate_pkg_c2, test_pkg, },
333 [PERF_CSTATE_PKG_C3_RES] = { MSR_PKG_C3_RESIDENCY, &evattr_cstate_pkg_c3, test_pkg, },
334 [PERF_CSTATE_PKG_C6_RES] = { MSR_PKG_C6_RESIDENCY, &evattr_cstate_pkg_c6, test_pkg, },
335 [PERF_CSTATE_PKG_C7_RES] = { MSR_PKG_C7_RESIDENCY, &evattr_cstate_pkg_c7, test_pkg, },
336 [PERF_CSTATE_PKG_C8_RES] = { MSR_PKG_C8_RESIDENCY, &evattr_cstate_pkg_c8, test_pkg, },
337 [PERF_CSTATE_PKG_C9_RES] = { MSR_PKG_C9_RESIDENCY, &evattr_cstate_pkg_c9, test_pkg, },
338 [PERF_CSTATE_PKG_C10_RES] = { MSR_PKG_C10_RESIDENCY, &evattr_cstate_pkg_c10, test_pkg, },
339};
340
341static struct attribute *pkg_events_attrs[PERF_CSTATE_PKG_EVENT_MAX + 1] = {
342 NULL,
343};
344
345static struct attribute_group pkg_events_attr_group = {
346 .name = "events",
347 .attrs = pkg_events_attrs,
348};
349
350DEFINE_CSTATE_FORMAT_ATTR(pkg_event, event, "config:0-63");
351static struct attribute *pkg_format_attrs[] = {
352 &format_attr_pkg_event.attr,
353 NULL,
354};
355static struct attribute_group pkg_format_attr_group = {
356 .name = "format",
357 .attrs = pkg_format_attrs,
358};
359
360static cpumask_t cstate_pkg_cpu_mask;
361
362static const struct attribute_group *pkg_attr_groups[] = {
363 &pkg_events_attr_group,
364 &pkg_format_attr_group,
365 &cpumask_attr_group,
366 NULL,
367};
368
369/* cstate_pkg PMU end*/
370
371static ssize_t cstate_get_attr_cpumask(struct device *dev,
372 struct device_attribute *attr,
373 char *buf)
374{
375 struct pmu *pmu = dev_get_drvdata(dev);
376
377 if (pmu == &cstate_core_pmu)
378 return cpumap_print_to_pagebuf(true, buf, &cstate_core_cpu_mask);
379 else if (pmu == &cstate_pkg_pmu)
380 return cpumap_print_to_pagebuf(true, buf, &cstate_pkg_cpu_mask);
381 else
382 return 0;
383}
384
385static int cstate_pmu_event_init(struct perf_event *event)
386{
387 u64 cfg = event->attr.config;
49de0493 388 int cpu;
7ce1346a
KL
389
390 if (event->attr.type != event->pmu->type)
391 return -ENOENT;
392
393 /* unsupported modes and filters */
394 if (event->attr.exclude_user ||
395 event->attr.exclude_kernel ||
396 event->attr.exclude_hv ||
397 event->attr.exclude_idle ||
398 event->attr.exclude_host ||
399 event->attr.exclude_guest ||
400 event->attr.sample_period) /* no sampling */
401 return -EINVAL;
402
49de0493
TG
403 if (event->cpu < 0)
404 return -EINVAL;
405
7ce1346a
KL
406 if (event->pmu == &cstate_core_pmu) {
407 if (cfg >= PERF_CSTATE_CORE_EVENT_MAX)
408 return -EINVAL;
409 if (!core_msr[cfg].attr)
410 return -EINVAL;
411 event->hw.event_base = core_msr[cfg].msr;
49de0493
TG
412 cpu = cpumask_any_and(&cstate_core_cpu_mask,
413 topology_sibling_cpumask(event->cpu));
7ce1346a
KL
414 } else if (event->pmu == &cstate_pkg_pmu) {
415 if (cfg >= PERF_CSTATE_PKG_EVENT_MAX)
416 return -EINVAL;
417 if (!pkg_msr[cfg].attr)
418 return -EINVAL;
419 event->hw.event_base = pkg_msr[cfg].msr;
49de0493
TG
420 cpu = cpumask_any_and(&cstate_pkg_cpu_mask,
421 topology_core_cpumask(event->cpu));
422 } else {
7ce1346a 423 return -ENOENT;
49de0493
TG
424 }
425
426 if (cpu >= nr_cpu_ids)
427 return -ENODEV;
7ce1346a 428
49de0493 429 event->cpu = cpu;
7ce1346a
KL
430 event->hw.config = cfg;
431 event->hw.idx = -1;
49de0493 432 return 0;
7ce1346a
KL
433}
434
435static inline u64 cstate_pmu_read_counter(struct perf_event *event)
436{
437 u64 val;
438
439 rdmsrl(event->hw.event_base, val);
440 return val;
441}
442
443static void cstate_pmu_event_update(struct perf_event *event)
444{
445 struct hw_perf_event *hwc = &event->hw;
446 u64 prev_raw_count, new_raw_count;
447
448again:
449 prev_raw_count = local64_read(&hwc->prev_count);
450 new_raw_count = cstate_pmu_read_counter(event);
451
452 if (local64_cmpxchg(&hwc->prev_count, prev_raw_count,
453 new_raw_count) != prev_raw_count)
454 goto again;
455
456 local64_add(new_raw_count - prev_raw_count, &event->count);
457}
458
459static void cstate_pmu_event_start(struct perf_event *event, int mode)
460{
461 local64_set(&event->hw.prev_count, cstate_pmu_read_counter(event));
462}
463
464static void cstate_pmu_event_stop(struct perf_event *event, int mode)
465{
466 cstate_pmu_event_update(event);
467}
468
469static void cstate_pmu_event_del(struct perf_event *event, int mode)
470{
471 cstate_pmu_event_stop(event, PERF_EF_UPDATE);
472}
473
474static int cstate_pmu_event_add(struct perf_event *event, int mode)
475{
476 if (mode & PERF_EF_START)
477 cstate_pmu_event_start(event, mode);
478
479 return 0;
480}
481
49de0493
TG
482/*
483 * Check if exiting cpu is the designated reader. If so migrate the
484 * events when there is a valid target available
485 */
7ce1346a
KL
486static void cstate_cpu_exit(int cpu)
487{
49de0493
TG
488 unsigned int target;
489
490 if (has_cstate_core &&
491 cpumask_test_and_clear_cpu(cpu, &cstate_core_cpu_mask)) {
492
493 target = cpumask_any_but(topology_sibling_cpumask(cpu), cpu);
494 /* Migrate events if there is a valid target */
495 if (target < nr_cpu_ids) {
7ce1346a 496 cpumask_set_cpu(target, &cstate_core_cpu_mask);
7ce1346a 497 perf_pmu_migrate_context(&cstate_core_pmu, cpu, target);
49de0493 498 }
7ce1346a
KL
499 }
500
49de0493
TG
501 if (has_cstate_pkg &&
502 cpumask_test_and_clear_cpu(cpu, &cstate_pkg_cpu_mask)) {
503
504 target = cpumask_any_but(topology_core_cpumask(cpu), cpu);
505 /* Migrate events if there is a valid target */
506 if (target < nr_cpu_ids) {
7ce1346a 507 cpumask_set_cpu(target, &cstate_pkg_cpu_mask);
7ce1346a 508 perf_pmu_migrate_context(&cstate_pkg_pmu, cpu, target);
49de0493 509 }
7ce1346a
KL
510 }
511}
512
513static void cstate_cpu_init(int cpu)
514{
49de0493 515 unsigned int target;
7ce1346a 516
49de0493
TG
517 /*
518 * If this is the first online thread of that core, set it in
519 * the core cpu mask as the designated reader.
520 */
521 target = cpumask_any_and(&cstate_core_cpu_mask,
522 topology_sibling_cpumask(cpu));
523
524 if (has_cstate_core && target >= nr_cpu_ids)
525 cpumask_set_cpu(cpu, &cstate_core_cpu_mask);
526
527 /*
528 * If this is the first online thread of that package, set it
529 * in the package cpu mask as the designated reader.
530 */
531 target = cpumask_any_and(&cstate_pkg_cpu_mask,
532 topology_core_cpumask(cpu));
533 if (has_cstate_pkg && target >= nr_cpu_ids)
534 cpumask_set_cpu(cpu, &cstate_pkg_cpu_mask);
7ce1346a
KL
535}
536
537static int cstate_cpu_notifier(struct notifier_block *self,
49de0493 538 unsigned long action, void *hcpu)
7ce1346a
KL
539{
540 unsigned int cpu = (long)hcpu;
541
542 switch (action & ~CPU_TASKS_FROZEN) {
7ce1346a
KL
543 case CPU_STARTING:
544 cstate_cpu_init(cpu);
545 break;
7ce1346a
KL
546 case CPU_DOWN_PREPARE:
547 cstate_cpu_exit(cpu);
548 break;
549 default:
550 break;
551 }
7ce1346a
KL
552 return NOTIFY_OK;
553}
554
555/*
556 * Probe the cstate events and insert the available one into sysfs attrs
557 * Return false if there is no available events.
558 */
559static bool cstate_probe_msr(struct perf_cstate_msr *msr,
560 struct attribute **events_attrs,
561 int max_event_nr)
562{
563 int i, j = 0;
564 u64 val;
565
566 /* Probe the cstate events. */
567 for (i = 0; i < max_event_nr; i++) {
568 if (!msr[i].test(i) || rdmsrl_safe(msr[i].msr, &val))
569 msr[i].attr = NULL;
570 }
571
572 /* List remaining events in the sysfs attrs. */
573 for (i = 0; i < max_event_nr; i++) {
574 if (msr[i].attr)
575 events_attrs[j++] = &msr[i].attr->attr.attr;
576 }
577 events_attrs[j] = NULL;
578
579 return (j > 0) ? true : false;
580}
581
582static int __init cstate_init(void)
583{
584 /* SLM has different MSR for PKG C6 */
585 switch (boot_cpu_data.x86_model) {
586 case 55:
587 case 76:
588 case 77:
589 pkg_msr[PERF_CSTATE_PKG_C6_RES].msr = MSR_PKG_C7_RESIDENCY;
590 }
591
592 if (cstate_probe_msr(core_msr, core_events_attrs, PERF_CSTATE_CORE_EVENT_MAX))
593 has_cstate_core = true;
594
595 if (cstate_probe_msr(pkg_msr, pkg_events_attrs, PERF_CSTATE_PKG_EVENT_MAX))
596 has_cstate_pkg = true;
597
598 return (has_cstate_core || has_cstate_pkg) ? 0 : -ENODEV;
599}
600
601static void __init cstate_cpumask_init(void)
602{
603 int cpu;
604
605 cpu_notifier_register_begin();
606
607 for_each_online_cpu(cpu)
608 cstate_cpu_init(cpu);
609
610 __perf_cpu_notifier(cstate_cpu_notifier);
611
612 cpu_notifier_register_done();
613}
614
615static struct pmu cstate_core_pmu = {
616 .attr_groups = core_attr_groups,
617 .name = "cstate_core",
618 .task_ctx_nr = perf_invalid_context,
619 .event_init = cstate_pmu_event_init,
620 .add = cstate_pmu_event_add, /* must have */
621 .del = cstate_pmu_event_del, /* must have */
622 .start = cstate_pmu_event_start,
623 .stop = cstate_pmu_event_stop,
624 .read = cstate_pmu_event_update,
625 .capabilities = PERF_PMU_CAP_NO_INTERRUPT,
626};
627
628static struct pmu cstate_pkg_pmu = {
629 .attr_groups = pkg_attr_groups,
630 .name = "cstate_pkg",
631 .task_ctx_nr = perf_invalid_context,
632 .event_init = cstate_pmu_event_init,
633 .add = cstate_pmu_event_add, /* must have */
634 .del = cstate_pmu_event_del, /* must have */
635 .start = cstate_pmu_event_start,
636 .stop = cstate_pmu_event_stop,
637 .read = cstate_pmu_event_update,
638 .capabilities = PERF_PMU_CAP_NO_INTERRUPT,
639};
640
641static void __init cstate_pmus_register(void)
642{
643 int err;
644
645 if (has_cstate_core) {
646 err = perf_pmu_register(&cstate_core_pmu, cstate_core_pmu.name, -1);
647 if (WARN_ON(err))
648 pr_info("Failed to register PMU %s error %d\n",
649 cstate_core_pmu.name, err);
650 }
651
652 if (has_cstate_pkg) {
653 err = perf_pmu_register(&cstate_pkg_pmu, cstate_pkg_pmu.name, -1);
654 if (WARN_ON(err))
655 pr_info("Failed to register PMU %s error %d\n",
656 cstate_pkg_pmu.name, err);
657 }
658}
659
660static int __init cstate_pmu_init(void)
661{
662 int err;
663
664 if (cpu_has_hypervisor)
665 return -ENODEV;
666
667 err = cstate_init();
668 if (err)
669 return err;
670
671 cstate_cpumask_init();
672
673 cstate_pmus_register();
674
675 return 0;
676}
677
678device_initcall(cstate_pmu_init);