1 // SPDX-License-Identifier: GPL-2.0-only
3 * Support Intel/AMD RAPL energy consumption counters
4 * Copyright (C) 2013 Google, Inc., Stephane Eranian
6 * Intel RAPL interface is specified in the IA-32 Manual Vol3b
7 * section 14.7.1 (September 2013)
9 * AMD RAPL interface for Fam17h is described in the public PPR:
10 * https://bugzilla.kernel.org/show_bug.cgi?id=206537
12 * RAPL provides more controls than just reporting energy consumption
13 * however here we only expose the 3 energy consumption free running
14 * counters (pp0, pkg, dram).
16 * Each of those counters increments in a power unit defined by the
17 * RAPL_POWER_UNIT MSR. On SandyBridge, this unit is 1/(2^16) Joules
20 * Counter to rapl events mappings:
22 * pp0 counter: consumption of all physical cores (power plane 0)
23 * event: rapl_energy_cores
26 * pkg counter: consumption of the whole processor package
27 * event: rapl_energy_pkg
30 * dram counter: consumption of the dram domain (servers only)
31 * event: rapl_energy_dram
34 * gpu counter: consumption of the builtin-gpu domain (client only)
35 * event: rapl_energy_gpu
38 * psys counter: consumption of the builtin-psys domain (client only)
39 * event: rapl_energy_psys
42 * We manage those counters as free running (read-only). They may be
43 * use simultaneously by other tools, such as turbostat.
45 * The events only support system-wide mode counting. There is no
46 * sampling support because it does not make sense and is not
47 * supported by the RAPL hardware.
49 * Because we want to avoid floating-point operations in the kernel,
50 * the events are all reported in fixed point arithmetic (32.32).
51 * Tools must adjust the counts to convert them to Watts using
52 * the duration of the measurement. Tools may use a function such as
53 * ldexp(raw_count, -32);
56 #define pr_fmt(fmt) "RAPL PMU: " fmt
58 #include <linux/module.h>
59 #include <linux/slab.h>
60 #include <linux/perf_event.h>
61 #include <linux/nospec.h>
62 #include <asm/cpu_device_id.h>
63 #include <asm/intel-family.h>
64 #include "perf_event.h"
67 MODULE_LICENSE("GPL");
70 * RAPL energy status counters
72 enum perf_rapl_events {
73 PERF_RAPL_PP0 = 0, /* all cores */
74 PERF_RAPL_PKG, /* entire package */
75 PERF_RAPL_RAM, /* DRAM */
76 PERF_RAPL_PP1, /* gpu */
77 PERF_RAPL_PSYS, /* psys */
80 NR_RAPL_DOMAINS = PERF_RAPL_MAX,
83 static const char *const rapl_domain_names[NR_RAPL_DOMAINS] __initconst = {
92 * event code: LSB 8 bits, passed in attr->config
93 * any other bit is reserved
95 #define RAPL_EVENT_MASK 0xFFULL
97 #define DEFINE_RAPL_FORMAT_ATTR(_var, _name, _format) \
98 static ssize_t __rapl_##_var##_show(struct kobject *kobj, \
99 struct kobj_attribute *attr, \
102 BUILD_BUG_ON(sizeof(_format) >= PAGE_SIZE); \
103 return sprintf(page, _format "\n"); \
105 static struct kobj_attribute format_attr_##_var = \
106 __ATTR(_name, 0444, __rapl_##_var##_show, NULL)
108 #define RAPL_CNTR_WIDTH 32
110 #define RAPL_EVENT_ATTR_STR(_name, v, str) \
111 static struct perf_pmu_events_attr event_attr_##v = { \
112 .attr = __ATTR(_name, 0444, perf_event_sysfs_show, NULL), \
121 struct list_head active_list;
123 ktime_t timer_interval;
124 struct hrtimer hrtimer;
130 struct rapl_pmu *pmus[];
134 struct perf_msr *rapl_msrs;
135 unsigned long events;
136 unsigned int msr_power_unit;
140 /* 1/2^hw_unit Joule */
141 static int rapl_hw_unit[NR_RAPL_DOMAINS] __read_mostly;
142 static struct rapl_pmus *rapl_pmus;
143 static cpumask_t rapl_cpu_mask;
144 static unsigned int rapl_cntr_mask;
145 static u64 rapl_timer_ms;
146 static struct perf_msr *rapl_msrs;
148 static inline struct rapl_pmu *cpu_to_rapl_pmu(unsigned int cpu)
150 unsigned int dieid = topology_logical_die_id(cpu);
153 * The unsigned check also catches the '-1' return value for non
154 * existent mappings in the topology map.
156 return dieid < rapl_pmus->maxdie ? rapl_pmus->pmus[dieid] : NULL;
159 static inline u64 rapl_read_counter(struct perf_event *event)
162 rdmsrl(event->hw.event_base, raw);
166 static inline u64 rapl_scale(u64 v, int cfg)
168 if (cfg > NR_RAPL_DOMAINS) {
169 pr_warn("Invalid domain %d, failed to scale data\n", cfg);
173 * scale delta to smallest unit (1/2^32)
174 * users must then scale back: count * 1/(1e9*2^32) to get Joules
175 * or use ldexp(count, -32).
176 * Watts = Joules/Time delta
178 return v << (32 - rapl_hw_unit[cfg - 1]);
181 static u64 rapl_event_update(struct perf_event *event)
183 struct hw_perf_event *hwc = &event->hw;
184 u64 prev_raw_count, new_raw_count;
186 int shift = RAPL_CNTR_WIDTH;
189 prev_raw_count = local64_read(&hwc->prev_count);
190 rdmsrl(event->hw.event_base, new_raw_count);
192 if (local64_cmpxchg(&hwc->prev_count, prev_raw_count,
193 new_raw_count) != prev_raw_count) {
199 * Now we have the new raw value and have updated the prev
200 * timestamp already. We can now calculate the elapsed delta
201 * (event-)time and add that to the generic event.
203 * Careful, not all hw sign-extends above the physical width
206 delta = (new_raw_count << shift) - (prev_raw_count << shift);
209 sdelta = rapl_scale(delta, event->hw.config);
211 local64_add(sdelta, &event->count);
213 return new_raw_count;
216 static void rapl_start_hrtimer(struct rapl_pmu *pmu)
218 hrtimer_start(&pmu->hrtimer, pmu->timer_interval,
219 HRTIMER_MODE_REL_PINNED);
222 static enum hrtimer_restart rapl_hrtimer_handle(struct hrtimer *hrtimer)
224 struct rapl_pmu *pmu = container_of(hrtimer, struct rapl_pmu, hrtimer);
225 struct perf_event *event;
229 return HRTIMER_NORESTART;
231 raw_spin_lock_irqsave(&pmu->lock, flags);
233 list_for_each_entry(event, &pmu->active_list, active_entry)
234 rapl_event_update(event);
236 raw_spin_unlock_irqrestore(&pmu->lock, flags);
238 hrtimer_forward_now(hrtimer, pmu->timer_interval);
240 return HRTIMER_RESTART;
243 static void rapl_hrtimer_init(struct rapl_pmu *pmu)
245 struct hrtimer *hr = &pmu->hrtimer;
247 hrtimer_init(hr, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
248 hr->function = rapl_hrtimer_handle;
251 static void __rapl_pmu_event_start(struct rapl_pmu *pmu,
252 struct perf_event *event)
254 if (WARN_ON_ONCE(!(event->hw.state & PERF_HES_STOPPED)))
259 list_add_tail(&event->active_entry, &pmu->active_list);
261 local64_set(&event->hw.prev_count, rapl_read_counter(event));
264 if (pmu->n_active == 1)
265 rapl_start_hrtimer(pmu);
268 static void rapl_pmu_event_start(struct perf_event *event, int mode)
270 struct rapl_pmu *pmu = event->pmu_private;
273 raw_spin_lock_irqsave(&pmu->lock, flags);
274 __rapl_pmu_event_start(pmu, event);
275 raw_spin_unlock_irqrestore(&pmu->lock, flags);
278 static void rapl_pmu_event_stop(struct perf_event *event, int mode)
280 struct rapl_pmu *pmu = event->pmu_private;
281 struct hw_perf_event *hwc = &event->hw;
284 raw_spin_lock_irqsave(&pmu->lock, flags);
286 /* mark event as deactivated and stopped */
287 if (!(hwc->state & PERF_HES_STOPPED)) {
288 WARN_ON_ONCE(pmu->n_active <= 0);
290 if (pmu->n_active == 0)
291 hrtimer_cancel(&pmu->hrtimer);
293 list_del(&event->active_entry);
295 WARN_ON_ONCE(hwc->state & PERF_HES_STOPPED);
296 hwc->state |= PERF_HES_STOPPED;
299 /* check if update of sw counter is necessary */
300 if ((mode & PERF_EF_UPDATE) && !(hwc->state & PERF_HES_UPTODATE)) {
302 * Drain the remaining delta count out of a event
303 * that we are disabling:
305 rapl_event_update(event);
306 hwc->state |= PERF_HES_UPTODATE;
309 raw_spin_unlock_irqrestore(&pmu->lock, flags);
312 static int rapl_pmu_event_add(struct perf_event *event, int mode)
314 struct rapl_pmu *pmu = event->pmu_private;
315 struct hw_perf_event *hwc = &event->hw;
318 raw_spin_lock_irqsave(&pmu->lock, flags);
320 hwc->state = PERF_HES_UPTODATE | PERF_HES_STOPPED;
322 if (mode & PERF_EF_START)
323 __rapl_pmu_event_start(pmu, event);
325 raw_spin_unlock_irqrestore(&pmu->lock, flags);
330 static void rapl_pmu_event_del(struct perf_event *event, int flags)
332 rapl_pmu_event_stop(event, PERF_EF_UPDATE);
335 static int rapl_pmu_event_init(struct perf_event *event)
337 u64 cfg = event->attr.config & RAPL_EVENT_MASK;
339 struct rapl_pmu *pmu;
341 /* only look at RAPL events */
342 if (event->attr.type != rapl_pmus->pmu.type)
345 /* check only supported bits are set */
346 if (event->attr.config & ~RAPL_EVENT_MASK)
352 event->event_caps |= PERF_EV_CAP_READ_ACTIVE_PKG;
354 if (!cfg || cfg >= NR_RAPL_DOMAINS + 1)
357 cfg = array_index_nospec((long)cfg, NR_RAPL_DOMAINS + 1);
360 /* check event supported */
361 if (!(rapl_cntr_mask & (1 << bit)))
364 /* unsupported modes and filters */
365 if (event->attr.sample_period) /* no sampling */
368 /* must be done before validate_group */
369 pmu = cpu_to_rapl_pmu(event->cpu);
372 event->cpu = pmu->cpu;
373 event->pmu_private = pmu;
374 event->hw.event_base = rapl_msrs[bit].msr;
375 event->hw.config = cfg;
381 static void rapl_pmu_event_read(struct perf_event *event)
383 rapl_event_update(event);
386 static ssize_t rapl_get_attr_cpumask(struct device *dev,
387 struct device_attribute *attr, char *buf)
389 return cpumap_print_to_pagebuf(true, buf, &rapl_cpu_mask);
392 static DEVICE_ATTR(cpumask, S_IRUGO, rapl_get_attr_cpumask, NULL);
394 static struct attribute *rapl_pmu_attrs[] = {
395 &dev_attr_cpumask.attr,
399 static struct attribute_group rapl_pmu_attr_group = {
400 .attrs = rapl_pmu_attrs,
403 RAPL_EVENT_ATTR_STR(energy-cores, rapl_cores, "event=0x01");
404 RAPL_EVENT_ATTR_STR(energy-pkg , rapl_pkg, "event=0x02");
405 RAPL_EVENT_ATTR_STR(energy-ram , rapl_ram, "event=0x03");
406 RAPL_EVENT_ATTR_STR(energy-gpu , rapl_gpu, "event=0x04");
407 RAPL_EVENT_ATTR_STR(energy-psys, rapl_psys, "event=0x05");
409 RAPL_EVENT_ATTR_STR(energy-cores.unit, rapl_cores_unit, "Joules");
410 RAPL_EVENT_ATTR_STR(energy-pkg.unit , rapl_pkg_unit, "Joules");
411 RAPL_EVENT_ATTR_STR(energy-ram.unit , rapl_ram_unit, "Joules");
412 RAPL_EVENT_ATTR_STR(energy-gpu.unit , rapl_gpu_unit, "Joules");
413 RAPL_EVENT_ATTR_STR(energy-psys.unit, rapl_psys_unit, "Joules");
416 * we compute in 0.23 nJ increments regardless of MSR
418 RAPL_EVENT_ATTR_STR(energy-cores.scale, rapl_cores_scale, "2.3283064365386962890625e-10");
419 RAPL_EVENT_ATTR_STR(energy-pkg.scale, rapl_pkg_scale, "2.3283064365386962890625e-10");
420 RAPL_EVENT_ATTR_STR(energy-ram.scale, rapl_ram_scale, "2.3283064365386962890625e-10");
421 RAPL_EVENT_ATTR_STR(energy-gpu.scale, rapl_gpu_scale, "2.3283064365386962890625e-10");
422 RAPL_EVENT_ATTR_STR(energy-psys.scale, rapl_psys_scale, "2.3283064365386962890625e-10");
425 * There are no default events, but we need to create
426 * "events" group (with empty attrs) before updating
427 * it with detected events.
429 static struct attribute *attrs_empty[] = {
433 static struct attribute_group rapl_pmu_events_group = {
435 .attrs = attrs_empty,
438 DEFINE_RAPL_FORMAT_ATTR(event, event, "config:0-7");
439 static struct attribute *rapl_formats_attr[] = {
440 &format_attr_event.attr,
444 static struct attribute_group rapl_pmu_format_group = {
446 .attrs = rapl_formats_attr,
449 static const struct attribute_group *rapl_attr_groups[] = {
450 &rapl_pmu_attr_group,
451 &rapl_pmu_format_group,
452 &rapl_pmu_events_group,
456 static struct attribute *rapl_events_cores[] = {
457 EVENT_PTR(rapl_cores),
458 EVENT_PTR(rapl_cores_unit),
459 EVENT_PTR(rapl_cores_scale),
464 rapl_not_visible(struct kobject *kobj, struct attribute *attr, int i)
469 static struct attribute_group rapl_events_cores_group = {
471 .attrs = rapl_events_cores,
472 .is_visible = rapl_not_visible,
475 static struct attribute *rapl_events_pkg[] = {
477 EVENT_PTR(rapl_pkg_unit),
478 EVENT_PTR(rapl_pkg_scale),
482 static struct attribute_group rapl_events_pkg_group = {
484 .attrs = rapl_events_pkg,
485 .is_visible = rapl_not_visible,
488 static struct attribute *rapl_events_ram[] = {
490 EVENT_PTR(rapl_ram_unit),
491 EVENT_PTR(rapl_ram_scale),
495 static struct attribute_group rapl_events_ram_group = {
497 .attrs = rapl_events_ram,
498 .is_visible = rapl_not_visible,
501 static struct attribute *rapl_events_gpu[] = {
503 EVENT_PTR(rapl_gpu_unit),
504 EVENT_PTR(rapl_gpu_scale),
508 static struct attribute_group rapl_events_gpu_group = {
510 .attrs = rapl_events_gpu,
511 .is_visible = rapl_not_visible,
514 static struct attribute *rapl_events_psys[] = {
515 EVENT_PTR(rapl_psys),
516 EVENT_PTR(rapl_psys_unit),
517 EVENT_PTR(rapl_psys_scale),
521 static struct attribute_group rapl_events_psys_group = {
523 .attrs = rapl_events_psys,
524 .is_visible = rapl_not_visible,
527 static bool test_msr(int idx, void *data)
529 return test_bit(idx, (unsigned long *) data);
532 static struct perf_msr intel_rapl_msrs[] = {
533 [PERF_RAPL_PP0] = { MSR_PP0_ENERGY_STATUS, &rapl_events_cores_group, test_msr },
534 [PERF_RAPL_PKG] = { MSR_PKG_ENERGY_STATUS, &rapl_events_pkg_group, test_msr },
535 [PERF_RAPL_RAM] = { MSR_DRAM_ENERGY_STATUS, &rapl_events_ram_group, test_msr },
536 [PERF_RAPL_PP1] = { MSR_PP1_ENERGY_STATUS, &rapl_events_gpu_group, test_msr },
537 [PERF_RAPL_PSYS] = { MSR_PLATFORM_ENERGY_STATUS, &rapl_events_psys_group, test_msr },
541 * Force to PERF_RAPL_MAX size due to:
542 * - perf_msr_probe(PERF_RAPL_MAX)
543 * - want to use same event codes across both architectures
545 static struct perf_msr amd_rapl_msrs[PERF_RAPL_MAX] = {
546 [PERF_RAPL_PKG] = { MSR_AMD_PKG_ENERGY_STATUS, &rapl_events_pkg_group, test_msr },
550 static int rapl_cpu_offline(unsigned int cpu)
552 struct rapl_pmu *pmu = cpu_to_rapl_pmu(cpu);
555 /* Check if exiting cpu is used for collecting rapl events */
556 if (!cpumask_test_and_clear_cpu(cpu, &rapl_cpu_mask))
560 /* Find a new cpu to collect rapl events */
561 target = cpumask_any_but(topology_die_cpumask(cpu), cpu);
563 /* Migrate rapl events to the new target */
564 if (target < nr_cpu_ids) {
565 cpumask_set_cpu(target, &rapl_cpu_mask);
567 perf_pmu_migrate_context(pmu->pmu, cpu, target);
572 static int rapl_cpu_online(unsigned int cpu)
574 struct rapl_pmu *pmu = cpu_to_rapl_pmu(cpu);
578 pmu = kzalloc_node(sizeof(*pmu), GFP_KERNEL, cpu_to_node(cpu));
582 raw_spin_lock_init(&pmu->lock);
583 INIT_LIST_HEAD(&pmu->active_list);
584 pmu->pmu = &rapl_pmus->pmu;
585 pmu->timer_interval = ms_to_ktime(rapl_timer_ms);
586 rapl_hrtimer_init(pmu);
588 rapl_pmus->pmus[topology_logical_die_id(cpu)] = pmu;
592 * Check if there is an online cpu in the package which collects rapl
595 target = cpumask_any_and(&rapl_cpu_mask, topology_die_cpumask(cpu));
596 if (target < nr_cpu_ids)
599 cpumask_set_cpu(cpu, &rapl_cpu_mask);
604 static int rapl_check_hw_unit(struct rapl_model *rm)
606 u64 msr_rapl_power_unit_bits;
609 /* protect rdmsrl() to handle virtualization */
610 if (rdmsrl_safe(rm->msr_power_unit, &msr_rapl_power_unit_bits))
612 for (i = 0; i < NR_RAPL_DOMAINS; i++)
613 rapl_hw_unit[i] = (msr_rapl_power_unit_bits >> 8) & 0x1FULL;
616 * DRAM domain on HSW server and KNL has fixed energy unit which can be
617 * different than the unit from power unit MSR. See
618 * "Intel Xeon Processor E5-1600 and E5-2600 v3 Product Families, V2
619 * of 2. Datasheet, September 2014, Reference Number: 330784-001 "
622 rapl_hw_unit[PERF_RAPL_RAM] = 16;
625 * Calculate the timer rate:
626 * Use reference of 200W for scaling the timeout to avoid counter
627 * overflows. 200W = 200 Joules/sec
628 * Divide interval by 2 to avoid lockstep (2 * 100)
629 * if hw unit is 32, then we use 2 ms 1/200/2
632 if (rapl_hw_unit[0] < 32) {
633 rapl_timer_ms = (1000 / (2 * 100));
634 rapl_timer_ms *= (1ULL << (32 - rapl_hw_unit[0] - 1));
639 static void __init rapl_advertise(void)
643 pr_info("API unit is 2^-32 Joules, %d fixed counters, %llu ms ovfl timer\n",
644 hweight32(rapl_cntr_mask), rapl_timer_ms);
646 for (i = 0; i < NR_RAPL_DOMAINS; i++) {
647 if (rapl_cntr_mask & (1 << i)) {
648 pr_info("hw unit of domain %s 2^-%d Joules\n",
649 rapl_domain_names[i], rapl_hw_unit[i]);
654 static void cleanup_rapl_pmus(void)
658 for (i = 0; i < rapl_pmus->maxdie; i++)
659 kfree(rapl_pmus->pmus[i]);
663 static const struct attribute_group *rapl_attr_update[] = {
664 &rapl_events_cores_group,
665 &rapl_events_pkg_group,
666 &rapl_events_ram_group,
667 &rapl_events_gpu_group,
668 &rapl_events_gpu_group,
672 static int __init init_rapl_pmus(void)
674 int maxdie = topology_max_packages() * topology_max_die_per_package();
677 size = sizeof(*rapl_pmus) + maxdie * sizeof(struct rapl_pmu *);
678 rapl_pmus = kzalloc(size, GFP_KERNEL);
682 rapl_pmus->maxdie = maxdie;
683 rapl_pmus->pmu.attr_groups = rapl_attr_groups;
684 rapl_pmus->pmu.attr_update = rapl_attr_update;
685 rapl_pmus->pmu.task_ctx_nr = perf_invalid_context;
686 rapl_pmus->pmu.event_init = rapl_pmu_event_init;
687 rapl_pmus->pmu.add = rapl_pmu_event_add;
688 rapl_pmus->pmu.del = rapl_pmu_event_del;
689 rapl_pmus->pmu.start = rapl_pmu_event_start;
690 rapl_pmus->pmu.stop = rapl_pmu_event_stop;
691 rapl_pmus->pmu.read = rapl_pmu_event_read;
692 rapl_pmus->pmu.module = THIS_MODULE;
693 rapl_pmus->pmu.capabilities = PERF_PMU_CAP_NO_EXCLUDE;
697 static struct rapl_model model_snb = {
698 .events = BIT(PERF_RAPL_PP0) |
701 .apply_quirk = false,
702 .msr_power_unit = MSR_RAPL_POWER_UNIT,
703 .rapl_msrs = intel_rapl_msrs,
706 static struct rapl_model model_snbep = {
707 .events = BIT(PERF_RAPL_PP0) |
710 .apply_quirk = false,
711 .msr_power_unit = MSR_RAPL_POWER_UNIT,
712 .rapl_msrs = intel_rapl_msrs,
715 static struct rapl_model model_hsw = {
716 .events = BIT(PERF_RAPL_PP0) |
720 .apply_quirk = false,
721 .msr_power_unit = MSR_RAPL_POWER_UNIT,
722 .rapl_msrs = intel_rapl_msrs,
725 static struct rapl_model model_hsx = {
726 .events = BIT(PERF_RAPL_PP0) |
730 .msr_power_unit = MSR_RAPL_POWER_UNIT,
731 .rapl_msrs = intel_rapl_msrs,
734 static struct rapl_model model_knl = {
735 .events = BIT(PERF_RAPL_PKG) |
738 .msr_power_unit = MSR_RAPL_POWER_UNIT,
739 .rapl_msrs = intel_rapl_msrs,
742 static struct rapl_model model_skl = {
743 .events = BIT(PERF_RAPL_PP0) |
748 .apply_quirk = false,
749 .msr_power_unit = MSR_RAPL_POWER_UNIT,
750 .rapl_msrs = intel_rapl_msrs,
753 static struct rapl_model model_amd_fam17h = {
754 .events = BIT(PERF_RAPL_PKG),
755 .apply_quirk = false,
756 .msr_power_unit = MSR_AMD_RAPL_POWER_UNIT,
757 .rapl_msrs = amd_rapl_msrs,
760 static const struct x86_cpu_id rapl_model_match[] __initconst = {
761 X86_MATCH_INTEL_FAM6_MODEL(SANDYBRIDGE, &model_snb),
762 X86_MATCH_INTEL_FAM6_MODEL(SANDYBRIDGE_X, &model_snbep),
763 X86_MATCH_INTEL_FAM6_MODEL(IVYBRIDGE, &model_snb),
764 X86_MATCH_INTEL_FAM6_MODEL(IVYBRIDGE_X, &model_snbep),
765 X86_MATCH_INTEL_FAM6_MODEL(HASWELL, &model_hsw),
766 X86_MATCH_INTEL_FAM6_MODEL(HASWELL_X, &model_hsx),
767 X86_MATCH_INTEL_FAM6_MODEL(HASWELL_L, &model_hsw),
768 X86_MATCH_INTEL_FAM6_MODEL(HASWELL_G, &model_hsw),
769 X86_MATCH_INTEL_FAM6_MODEL(BROADWELL, &model_hsw),
770 X86_MATCH_INTEL_FAM6_MODEL(BROADWELL_G, &model_hsw),
771 X86_MATCH_INTEL_FAM6_MODEL(BROADWELL_X, &model_hsx),
772 X86_MATCH_INTEL_FAM6_MODEL(BROADWELL_D, &model_hsx),
773 X86_MATCH_INTEL_FAM6_MODEL(XEON_PHI_KNL, &model_knl),
774 X86_MATCH_INTEL_FAM6_MODEL(XEON_PHI_KNM, &model_knl),
775 X86_MATCH_INTEL_FAM6_MODEL(SKYLAKE_L, &model_skl),
776 X86_MATCH_INTEL_FAM6_MODEL(SKYLAKE, &model_skl),
777 X86_MATCH_INTEL_FAM6_MODEL(SKYLAKE_X, &model_hsx),
778 X86_MATCH_INTEL_FAM6_MODEL(KABYLAKE_L, &model_skl),
779 X86_MATCH_INTEL_FAM6_MODEL(KABYLAKE, &model_skl),
780 X86_MATCH_INTEL_FAM6_MODEL(CANNONLAKE_L, &model_skl),
781 X86_MATCH_INTEL_FAM6_MODEL(ATOM_GOLDMONT, &model_hsw),
782 X86_MATCH_INTEL_FAM6_MODEL(ATOM_GOLDMONT_D, &model_hsw),
783 X86_MATCH_INTEL_FAM6_MODEL(ATOM_GOLDMONT_PLUS, &model_hsw),
784 X86_MATCH_INTEL_FAM6_MODEL(ICELAKE_L, &model_skl),
785 X86_MATCH_INTEL_FAM6_MODEL(ICELAKE, &model_skl),
786 X86_MATCH_INTEL_FAM6_MODEL(ICELAKE_D, &model_hsx),
787 X86_MATCH_INTEL_FAM6_MODEL(ICELAKE_X, &model_hsx),
788 X86_MATCH_INTEL_FAM6_MODEL(COMETLAKE_L, &model_skl),
789 X86_MATCH_INTEL_FAM6_MODEL(COMETLAKE, &model_skl),
790 X86_MATCH_VENDOR_FAM(AMD, 0x17, &model_amd_fam17h),
791 X86_MATCH_VENDOR_FAM(HYGON, 0x18, &model_amd_fam17h),
794 MODULE_DEVICE_TABLE(x86cpu, rapl_model_match);
796 static int __init rapl_pmu_init(void)
798 const struct x86_cpu_id *id;
799 struct rapl_model *rm;
802 id = x86_match_cpu(rapl_model_match);
806 rm = (struct rapl_model *) id->driver_data;
808 rapl_msrs = rm->rapl_msrs;
810 rapl_cntr_mask = perf_msr_probe(rapl_msrs, PERF_RAPL_MAX,
811 false, (void *) &rm->events);
813 ret = rapl_check_hw_unit(rm);
817 ret = init_rapl_pmus();
822 * Install callbacks. Core will call them for each online cpu.
824 ret = cpuhp_setup_state(CPUHP_AP_PERF_X86_RAPL_ONLINE,
825 "perf/x86/rapl:online",
826 rapl_cpu_online, rapl_cpu_offline);
830 ret = perf_pmu_register(&rapl_pmus->pmu, "power", -1);
838 cpuhp_remove_state(CPUHP_AP_PERF_X86_RAPL_ONLINE);
840 pr_warn("Initialization failed (%d), disabled\n", ret);
844 module_init(rapl_pmu_init);
846 static void __exit intel_rapl_exit(void)
848 cpuhp_remove_state_nocalls(CPUHP_AP_PERF_X86_RAPL_ONLINE);
849 perf_pmu_unregister(&rapl_pmus->pmu);
852 module_exit(intel_rapl_exit);