1 // SPDX-License-Identifier: GPL-2.0-only
3 * Intel Running Average Power Limit (RAPL) Driver
4 * Copyright (c) 2013, Intel Corporation.
6 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
8 #include <linux/kernel.h>
9 #include <linux/module.h>
10 #include <linux/list.h>
11 #include <linux/types.h>
12 #include <linux/device.h>
13 #include <linux/slab.h>
14 #include <linux/log2.h>
15 #include <linux/bitmap.h>
16 #include <linux/delay.h>
17 #include <linux/sysfs.h>
18 #include <linux/cpu.h>
19 #include <linux/powercap.h>
20 #include <linux/suspend.h>
21 #include <linux/intel_rapl.h>
23 #include <asm/iosf_mbi.h>
24 #include <asm/processor.h>
25 #include <asm/cpu_device_id.h>
26 #include <asm/intel-family.h>
29 #define MSR_PLATFORM_POWER_LIMIT 0x0000065C
31 /* bitmasks for RAPL MSRs, used by primitive access functions */
32 #define ENERGY_STATUS_MASK 0xffffffff
34 #define POWER_LIMIT1_MASK 0x7FFF
35 #define POWER_LIMIT1_ENABLE BIT(15)
36 #define POWER_LIMIT1_CLAMP BIT(16)
38 #define POWER_LIMIT2_MASK (0x7FFFULL<<32)
39 #define POWER_LIMIT2_ENABLE BIT_ULL(47)
40 #define POWER_LIMIT2_CLAMP BIT_ULL(48)
41 #define POWER_PACKAGE_LOCK BIT_ULL(63)
42 #define POWER_PP_LOCK BIT(31)
44 #define TIME_WINDOW1_MASK (0x7FULL<<17)
45 #define TIME_WINDOW2_MASK (0x7FULL<<49)
47 #define POWER_UNIT_OFFSET 0
48 #define POWER_UNIT_MASK 0x0F
50 #define ENERGY_UNIT_OFFSET 0x08
51 #define ENERGY_UNIT_MASK 0x1F00
53 #define TIME_UNIT_OFFSET 0x10
54 #define TIME_UNIT_MASK 0xF0000
56 #define POWER_INFO_MAX_MASK (0x7fffULL<<32)
57 #define POWER_INFO_MIN_MASK (0x7fffULL<<16)
58 #define POWER_INFO_MAX_TIME_WIN_MASK (0x3fULL<<48)
59 #define POWER_INFO_THERMAL_SPEC_MASK 0x7fff
61 #define PERF_STATUS_THROTTLE_TIME_MASK 0xffffffff
62 #define PP_POLICY_MASK 0x1F
64 /* Non HW constants */
65 #define RAPL_PRIMITIVE_DERIVED BIT(1) /* not from raw data */
66 #define RAPL_PRIMITIVE_DUMMY BIT(2)
68 #define TIME_WINDOW_MAX_MSEC 40000
69 #define TIME_WINDOW_MIN_MSEC 250
70 #define ENERGY_UNIT_SCALE 1000 /* scale from driver unit to powercap unit */
72 ARBITRARY_UNIT, /* no translation */
78 /* private data for RAPL MSR Interface */
79 static struct rapl_if_priv rapl_msr_priv = {
80 .reg_unit = MSR_RAPL_POWER_UNIT,
81 .regs[RAPL_DOMAIN_PACKAGE] = {
82 MSR_PKG_POWER_LIMIT, MSR_PKG_ENERGY_STATUS, MSR_PKG_PERF_STATUS, 0, MSR_PKG_POWER_INFO },
83 .regs[RAPL_DOMAIN_PP0] = {
84 MSR_PP0_POWER_LIMIT, MSR_PP0_ENERGY_STATUS, 0, MSR_PP0_POLICY, 0 },
85 .regs[RAPL_DOMAIN_PP1] = {
86 MSR_PP1_POWER_LIMIT, MSR_PP1_ENERGY_STATUS, 0, MSR_PP1_POLICY, 0 },
87 .regs[RAPL_DOMAIN_DRAM] = {
88 MSR_DRAM_POWER_LIMIT, MSR_DRAM_ENERGY_STATUS, MSR_DRAM_PERF_STATUS, 0, MSR_DRAM_POWER_INFO },
89 .regs[RAPL_DOMAIN_PLATFORM] = {
90 MSR_PLATFORM_POWER_LIMIT, MSR_PLATFORM_ENERGY_STATUS, 0, 0, 0},
93 /* per domain data, some are optional */
94 #define NR_RAW_PRIMITIVES (NR_RAPL_PRIMITIVES - 2)
96 #define DOMAIN_STATE_INACTIVE BIT(0)
97 #define DOMAIN_STATE_POWER_LIMIT_SET BIT(1)
98 #define DOMAIN_STATE_BIOS_LOCKED BIT(2)
100 static const char pl1_name[] = "long_term";
101 static const char pl2_name[] = "short_term";
103 #define power_zone_to_rapl_domain(_zone) \
104 container_of(_zone, struct rapl_domain, power_zone)
106 struct rapl_defaults {
107 u8 floor_freq_reg_addr;
108 int (*check_unit)(struct rapl_package *rp, int cpu);
109 void (*set_floor_freq)(struct rapl_domain *rd, bool mode);
110 u64 (*compute_time_window)(struct rapl_package *rp, u64 val,
112 unsigned int dram_domain_energy_unit;
114 static struct rapl_defaults *rapl_defaults;
116 /* Sideband MBI registers */
117 #define IOSF_CPU_POWER_BUDGET_CTL_BYT (0x2)
118 #define IOSF_CPU_POWER_BUDGET_CTL_TNG (0xdf)
120 #define PACKAGE_PLN_INT_SAVED BIT(0)
121 #define MAX_PRIM_NAME (32)
123 /* per domain data. used to describe individual knobs such that access function
124 * can be consolidated into one instead of many inline functions.
126 struct rapl_primitive_info {
130 enum rapl_domain_reg_id id;
135 #define PRIMITIVE_INFO_INIT(p, m, s, i, u, f) { \
144 static void rapl_init_domains(struct rapl_package *rp);
145 static int rapl_read_data_raw(struct rapl_domain *rd,
146 enum rapl_primitives prim,
147 bool xlate, u64 *data);
148 static int rapl_write_data_raw(struct rapl_domain *rd,
149 enum rapl_primitives prim,
150 unsigned long long value);
151 static u64 rapl_unit_xlate(struct rapl_domain *rd,
152 enum unit_type type, u64 value,
154 static void package_power_limit_irq_save(struct rapl_package *rp);
156 static LIST_HEAD(rapl_packages); /* guarded by CPU hotplug lock */
158 static const char * const rapl_domain_names[] = {
166 /* caller to ensure CPU hotplug lock is held */
167 static struct rapl_package *rapl_find_package_domain(int cpu, struct rapl_if_priv *priv)
169 int id = topology_logical_die_id(cpu);
170 struct rapl_package *rp;
172 list_for_each_entry(rp, &rapl_packages, plist) {
173 if (rp->id == id && rp->priv->control_type == priv->control_type)
180 static int get_energy_counter(struct powercap_zone *power_zone, u64 *energy_raw)
182 struct rapl_domain *rd;
185 /* prevent CPU hotplug, make sure the RAPL domain does not go
186 * away while reading the counter.
189 rd = power_zone_to_rapl_domain(power_zone);
191 if (!rapl_read_data_raw(rd, ENERGY_COUNTER, true, &energy_now)) {
192 *energy_raw = energy_now;
202 static int get_max_energy_counter(struct powercap_zone *pcd_dev, u64 *energy)
204 struct rapl_domain *rd = power_zone_to_rapl_domain(pcd_dev);
206 *energy = rapl_unit_xlate(rd, ENERGY_UNIT, ENERGY_STATUS_MASK, 0);
210 static int release_zone(struct powercap_zone *power_zone)
212 struct rapl_domain *rd = power_zone_to_rapl_domain(power_zone);
213 struct rapl_package *rp = rd->rp;
215 /* package zone is the last zone of a package, we can free
216 * memory here since all children has been unregistered.
218 if (rd->id == RAPL_DOMAIN_PACKAGE) {
227 static int find_nr_power_limit(struct rapl_domain *rd)
231 for (i = 0; i < NR_POWER_LIMITS; i++) {
239 static int set_domain_enable(struct powercap_zone *power_zone, bool mode)
241 struct rapl_domain *rd = power_zone_to_rapl_domain(power_zone);
243 if (rd->state & DOMAIN_STATE_BIOS_LOCKED)
247 rapl_write_data_raw(rd, PL1_ENABLE, mode);
248 if (rapl_defaults->set_floor_freq)
249 rapl_defaults->set_floor_freq(rd, mode);
255 static int get_domain_enable(struct powercap_zone *power_zone, bool *mode)
257 struct rapl_domain *rd = power_zone_to_rapl_domain(power_zone);
260 if (rd->state & DOMAIN_STATE_BIOS_LOCKED) {
265 if (rapl_read_data_raw(rd, PL1_ENABLE, true, &val)) {
275 /* per RAPL domain ops, in the order of rapl_domain_type */
276 static const struct powercap_zone_ops zone_ops[] = {
277 /* RAPL_DOMAIN_PACKAGE */
279 .get_energy_uj = get_energy_counter,
280 .get_max_energy_range_uj = get_max_energy_counter,
281 .release = release_zone,
282 .set_enable = set_domain_enable,
283 .get_enable = get_domain_enable,
285 /* RAPL_DOMAIN_PP0 */
287 .get_energy_uj = get_energy_counter,
288 .get_max_energy_range_uj = get_max_energy_counter,
289 .release = release_zone,
290 .set_enable = set_domain_enable,
291 .get_enable = get_domain_enable,
293 /* RAPL_DOMAIN_PP1 */
295 .get_energy_uj = get_energy_counter,
296 .get_max_energy_range_uj = get_max_energy_counter,
297 .release = release_zone,
298 .set_enable = set_domain_enable,
299 .get_enable = get_domain_enable,
301 /* RAPL_DOMAIN_DRAM */
303 .get_energy_uj = get_energy_counter,
304 .get_max_energy_range_uj = get_max_energy_counter,
305 .release = release_zone,
306 .set_enable = set_domain_enable,
307 .get_enable = get_domain_enable,
309 /* RAPL_DOMAIN_PLATFORM */
311 .get_energy_uj = get_energy_counter,
312 .get_max_energy_range_uj = get_max_energy_counter,
313 .release = release_zone,
314 .set_enable = set_domain_enable,
315 .get_enable = get_domain_enable,
321 * Constraint index used by powercap can be different than power limit (PL)
322 * index in that some PLs maybe missing due to non-existant MSRs. So we
323 * need to convert here by finding the valid PLs only (name populated).
325 static int contraint_to_pl(struct rapl_domain *rd, int cid)
329 for (i = 0, j = 0; i < NR_POWER_LIMITS; i++) {
330 if ((rd->rpl[i].name) && j++ == cid) {
331 pr_debug("%s: index %d\n", __func__, i);
335 pr_err("Cannot find matching power limit for constraint %d\n", cid);
340 static int set_power_limit(struct powercap_zone *power_zone, int cid,
343 struct rapl_domain *rd;
344 struct rapl_package *rp;
349 rd = power_zone_to_rapl_domain(power_zone);
350 id = contraint_to_pl(rd, cid);
358 if (rd->state & DOMAIN_STATE_BIOS_LOCKED) {
359 dev_warn(&power_zone->dev, "%s locked by BIOS, monitoring only\n",
365 switch (rd->rpl[id].prim_id) {
367 rapl_write_data_raw(rd, POWER_LIMIT1, power_limit);
370 rapl_write_data_raw(rd, POWER_LIMIT2, power_limit);
376 package_power_limit_irq_save(rp);
382 static int get_current_power_limit(struct powercap_zone *power_zone, int cid,
385 struct rapl_domain *rd;
392 rd = power_zone_to_rapl_domain(power_zone);
393 id = contraint_to_pl(rd, cid);
399 switch (rd->rpl[id].prim_id) {
410 if (rapl_read_data_raw(rd, prim, true, &val))
421 static int set_time_window(struct powercap_zone *power_zone, int cid,
424 struct rapl_domain *rd;
429 rd = power_zone_to_rapl_domain(power_zone);
430 id = contraint_to_pl(rd, cid);
436 switch (rd->rpl[id].prim_id) {
438 rapl_write_data_raw(rd, TIME_WINDOW1, window);
441 rapl_write_data_raw(rd, TIME_WINDOW2, window);
452 static int get_time_window(struct powercap_zone *power_zone, int cid, u64 *data)
454 struct rapl_domain *rd;
460 rd = power_zone_to_rapl_domain(power_zone);
461 id = contraint_to_pl(rd, cid);
467 switch (rd->rpl[id].prim_id) {
469 ret = rapl_read_data_raw(rd, TIME_WINDOW1, true, &val);
472 ret = rapl_read_data_raw(rd, TIME_WINDOW2, true, &val);
487 static const char *get_constraint_name(struct powercap_zone *power_zone, int cid)
489 struct rapl_domain *rd;
492 rd = power_zone_to_rapl_domain(power_zone);
493 id = contraint_to_pl(rd, cid);
495 return rd->rpl[id].name;
501 static int get_max_power(struct powercap_zone *power_zone, int id,
504 struct rapl_domain *rd;
510 rd = power_zone_to_rapl_domain(power_zone);
511 switch (rd->rpl[id].prim_id) {
513 prim = THERMAL_SPEC_POWER;
522 if (rapl_read_data_raw(rd, prim, true, &val))
532 static const struct powercap_zone_constraint_ops constraint_ops = {
533 .set_power_limit_uw = set_power_limit,
534 .get_power_limit_uw = get_current_power_limit,
535 .set_time_window_us = set_time_window,
536 .get_time_window_us = get_time_window,
537 .get_max_power_uw = get_max_power,
538 .get_name = get_constraint_name,
541 /* called after domain detection and package level data are set */
542 static void rapl_init_domains(struct rapl_package *rp)
545 struct rapl_domain *rd = rp->domains;
547 for (i = 0; i < RAPL_DOMAIN_MAX; i++) {
548 unsigned int mask = rp->domain_map & (1 << i);
550 rd->regs[RAPL_DOMAIN_REG_LIMIT] = rp->priv->regs[i][RAPL_DOMAIN_REG_LIMIT];
551 rd->regs[RAPL_DOMAIN_REG_STATUS] = rp->priv->regs[i][RAPL_DOMAIN_REG_STATUS];
552 rd->regs[RAPL_DOMAIN_REG_PERF] = rp->priv->regs[i][RAPL_DOMAIN_REG_PERF];
553 rd->regs[RAPL_DOMAIN_REG_POLICY] = rp->priv->regs[i][RAPL_DOMAIN_REG_POLICY];
554 rd->regs[RAPL_DOMAIN_REG_INFO] = rp->priv->regs[i][RAPL_DOMAIN_REG_INFO];
557 case BIT(RAPL_DOMAIN_PACKAGE):
558 rd->name = rapl_domain_names[RAPL_DOMAIN_PACKAGE];
559 rd->id = RAPL_DOMAIN_PACKAGE;
560 rd->rpl[0].prim_id = PL1_ENABLE;
561 rd->rpl[0].name = pl1_name;
562 rd->rpl[1].prim_id = PL2_ENABLE;
563 rd->rpl[1].name = pl2_name;
565 case BIT(RAPL_DOMAIN_PP0):
566 rd->name = rapl_domain_names[RAPL_DOMAIN_PP0];
567 rd->id = RAPL_DOMAIN_PP0;
568 rd->rpl[0].prim_id = PL1_ENABLE;
569 rd->rpl[0].name = pl1_name;
571 case BIT(RAPL_DOMAIN_PP1):
572 rd->name = rapl_domain_names[RAPL_DOMAIN_PP1];
573 rd->id = RAPL_DOMAIN_PP1;
574 rd->rpl[0].prim_id = PL1_ENABLE;
575 rd->rpl[0].name = pl1_name;
577 case BIT(RAPL_DOMAIN_DRAM):
578 rd->name = rapl_domain_names[RAPL_DOMAIN_DRAM];
579 rd->id = RAPL_DOMAIN_DRAM;
580 rd->rpl[0].prim_id = PL1_ENABLE;
581 rd->rpl[0].name = pl1_name;
582 rd->domain_energy_unit =
583 rapl_defaults->dram_domain_energy_unit;
584 if (rd->domain_energy_unit)
585 pr_info("DRAM domain energy unit %dpj\n",
586 rd->domain_energy_unit);
596 static u64 rapl_unit_xlate(struct rapl_domain *rd, enum unit_type type,
597 u64 value, int to_raw)
600 struct rapl_package *rp = rd->rp;
605 units = rp->power_unit;
608 scale = ENERGY_UNIT_SCALE;
609 /* per domain unit takes precedence */
610 if (rd->domain_energy_unit)
611 units = rd->domain_energy_unit;
613 units = rp->energy_unit;
616 return rapl_defaults->compute_time_window(rp, value, to_raw);
623 return div64_u64(value, units) * scale;
627 return div64_u64(value, scale);
630 /* in the order of enum rapl_primitives */
631 static struct rapl_primitive_info rpi[] = {
632 /* name, mask, shift, msr index, unit divisor */
633 PRIMITIVE_INFO_INIT(ENERGY_COUNTER, ENERGY_STATUS_MASK, 0,
634 RAPL_DOMAIN_REG_STATUS, ENERGY_UNIT, 0),
635 PRIMITIVE_INFO_INIT(POWER_LIMIT1, POWER_LIMIT1_MASK, 0,
636 RAPL_DOMAIN_REG_LIMIT, POWER_UNIT, 0),
637 PRIMITIVE_INFO_INIT(POWER_LIMIT2, POWER_LIMIT2_MASK, 32,
638 RAPL_DOMAIN_REG_LIMIT, POWER_UNIT, 0),
639 PRIMITIVE_INFO_INIT(FW_LOCK, POWER_PP_LOCK, 31,
640 RAPL_DOMAIN_REG_LIMIT, ARBITRARY_UNIT, 0),
641 PRIMITIVE_INFO_INIT(PL1_ENABLE, POWER_LIMIT1_ENABLE, 15,
642 RAPL_DOMAIN_REG_LIMIT, ARBITRARY_UNIT, 0),
643 PRIMITIVE_INFO_INIT(PL1_CLAMP, POWER_LIMIT1_CLAMP, 16,
644 RAPL_DOMAIN_REG_LIMIT, ARBITRARY_UNIT, 0),
645 PRIMITIVE_INFO_INIT(PL2_ENABLE, POWER_LIMIT2_ENABLE, 47,
646 RAPL_DOMAIN_REG_LIMIT, ARBITRARY_UNIT, 0),
647 PRIMITIVE_INFO_INIT(PL2_CLAMP, POWER_LIMIT2_CLAMP, 48,
648 RAPL_DOMAIN_REG_LIMIT, ARBITRARY_UNIT, 0),
649 PRIMITIVE_INFO_INIT(TIME_WINDOW1, TIME_WINDOW1_MASK, 17,
650 RAPL_DOMAIN_REG_LIMIT, TIME_UNIT, 0),
651 PRIMITIVE_INFO_INIT(TIME_WINDOW2, TIME_WINDOW2_MASK, 49,
652 RAPL_DOMAIN_REG_LIMIT, TIME_UNIT, 0),
653 PRIMITIVE_INFO_INIT(THERMAL_SPEC_POWER, POWER_INFO_THERMAL_SPEC_MASK,
654 0, RAPL_DOMAIN_REG_INFO, POWER_UNIT, 0),
655 PRIMITIVE_INFO_INIT(MAX_POWER, POWER_INFO_MAX_MASK, 32,
656 RAPL_DOMAIN_REG_INFO, POWER_UNIT, 0),
657 PRIMITIVE_INFO_INIT(MIN_POWER, POWER_INFO_MIN_MASK, 16,
658 RAPL_DOMAIN_REG_INFO, POWER_UNIT, 0),
659 PRIMITIVE_INFO_INIT(MAX_TIME_WINDOW, POWER_INFO_MAX_TIME_WIN_MASK, 48,
660 RAPL_DOMAIN_REG_INFO, TIME_UNIT, 0),
661 PRIMITIVE_INFO_INIT(THROTTLED_TIME, PERF_STATUS_THROTTLE_TIME_MASK, 0,
662 RAPL_DOMAIN_REG_PERF, TIME_UNIT, 0),
663 PRIMITIVE_INFO_INIT(PRIORITY_LEVEL, PP_POLICY_MASK, 0,
664 RAPL_DOMAIN_REG_POLICY, ARBITRARY_UNIT, 0),
666 PRIMITIVE_INFO_INIT(AVERAGE_POWER, 0, 0, 0, POWER_UNIT,
667 RAPL_PRIMITIVE_DERIVED),
671 /* Read primitive data based on its related struct rapl_primitive_info.
672 * if xlate flag is set, return translated data based on data units, i.e.
673 * time, energy, and power.
674 * RAPL MSRs are non-architectual and are laid out not consistently across
675 * domains. Here we use primitive info to allow writing consolidated access
677 * For a given primitive, it is processed by MSR mask and shift. Unit conversion
678 * is pre-assigned based on RAPL unit MSRs read at init time.
679 * 63-------------------------- 31--------------------------- 0
681 * | |<- shift ----------------|
682 * 63-------------------------- 31--------------------------- 0
684 static int rapl_read_data_raw(struct rapl_domain *rd,
685 enum rapl_primitives prim,
686 bool xlate, u64 *data)
689 struct rapl_primitive_info *rp = &rpi[prim];
690 struct reg_action ra;
693 if (!rp->name || rp->flag & RAPL_PRIMITIVE_DUMMY)
696 ra.reg = rd->regs[rp->id];
700 cpu = rd->rp->lead_cpu;
702 /* special-case package domain, which uses a different bit*/
703 if (prim == FW_LOCK && rd->id == RAPL_DOMAIN_PACKAGE) {
704 rp->mask = POWER_PACKAGE_LOCK;
707 /* non-hardware data are collected by the polling thread */
708 if (rp->flag & RAPL_PRIMITIVE_DERIVED) {
709 *data = rd->rdd.primitives[prim];
715 if (rd->rp->priv->read_raw(cpu, &ra)) {
716 pr_debug("failed to read reg 0x%x on cpu %d\n", ra.reg, cpu);
720 value = ra.value >> rp->shift;
723 *data = rapl_unit_xlate(rd, rp->unit, value, 0);
730 /* Similar use of primitive info in the read counterpart */
731 static int rapl_write_data_raw(struct rapl_domain *rd,
732 enum rapl_primitives prim,
733 unsigned long long value)
735 struct rapl_primitive_info *rp = &rpi[prim];
738 struct reg_action ra;
741 cpu = rd->rp->lead_cpu;
742 bits = rapl_unit_xlate(rd, rp->unit, value, 1);
746 memset(&ra, 0, sizeof(ra));
748 ra.reg = rd->regs[rp->id];
752 ret = rd->rp->priv->write_raw(cpu, &ra);
758 * Raw RAPL data stored in MSRs are in certain scales. We need to
759 * convert them into standard units based on the units reported in
760 * the RAPL unit MSRs. This is specific to CPUs as the method to
761 * calculate units differ on different CPUs.
762 * We convert the units to below format based on CPUs.
764 * energy unit: picoJoules : Represented in picoJoules by default
765 * power unit : microWatts : Represented in milliWatts by default
766 * time unit : microseconds: Represented in seconds by default
768 static int rapl_check_unit_core(struct rapl_package *rp, int cpu)
770 struct reg_action ra;
773 ra.reg = rp->priv->reg_unit;
775 if (rp->priv->read_raw(cpu, &ra)) {
776 pr_err("Failed to read power unit REG 0x%x on CPU %d, exit.\n",
777 rp->priv->reg_unit, cpu);
781 value = (ra.value & ENERGY_UNIT_MASK) >> ENERGY_UNIT_OFFSET;
782 rp->energy_unit = ENERGY_UNIT_SCALE * 1000000 / (1 << value);
784 value = (ra.value & POWER_UNIT_MASK) >> POWER_UNIT_OFFSET;
785 rp->power_unit = 1000000 / (1 << value);
787 value = (ra.value & TIME_UNIT_MASK) >> TIME_UNIT_OFFSET;
788 rp->time_unit = 1000000 / (1 << value);
790 pr_debug("Core CPU %s energy=%dpJ, time=%dus, power=%duW\n",
791 rp->name, rp->energy_unit, rp->time_unit, rp->power_unit);
796 static int rapl_check_unit_atom(struct rapl_package *rp, int cpu)
798 struct reg_action ra;
801 ra.reg = rp->priv->reg_unit;
803 if (rp->priv->read_raw(cpu, &ra)) {
804 pr_err("Failed to read power unit REG 0x%x on CPU %d, exit.\n",
805 rp->priv->reg_unit, cpu);
809 value = (ra.value & ENERGY_UNIT_MASK) >> ENERGY_UNIT_OFFSET;
810 rp->energy_unit = ENERGY_UNIT_SCALE * 1 << value;
812 value = (ra.value & POWER_UNIT_MASK) >> POWER_UNIT_OFFSET;
813 rp->power_unit = (1 << value) * 1000;
815 value = (ra.value & TIME_UNIT_MASK) >> TIME_UNIT_OFFSET;
816 rp->time_unit = 1000000 / (1 << value);
818 pr_debug("Atom %s energy=%dpJ, time=%dus, power=%duW\n",
819 rp->name, rp->energy_unit, rp->time_unit, rp->power_unit);
824 static void power_limit_irq_save_cpu(void *info)
827 struct rapl_package *rp = (struct rapl_package *)info;
829 /* save the state of PLN irq mask bit before disabling it */
830 rdmsr_safe(MSR_IA32_PACKAGE_THERM_INTERRUPT, &l, &h);
831 if (!(rp->power_limit_irq & PACKAGE_PLN_INT_SAVED)) {
832 rp->power_limit_irq = l & PACKAGE_THERM_INT_PLN_ENABLE;
833 rp->power_limit_irq |= PACKAGE_PLN_INT_SAVED;
835 l &= ~PACKAGE_THERM_INT_PLN_ENABLE;
836 wrmsr_safe(MSR_IA32_PACKAGE_THERM_INTERRUPT, l, h);
841 * When package power limit is set artificially low by RAPL, LVT
842 * thermal interrupt for package power limit should be ignored
843 * since we are not really exceeding the real limit. The intention
844 * is to avoid excessive interrupts while we are trying to save power.
845 * A useful feature might be routing the package_power_limit interrupt
846 * to userspace via eventfd. once we have a usecase, this is simple
847 * to do by adding an atomic notifier.
850 static void package_power_limit_irq_save(struct rapl_package *rp)
852 if (!boot_cpu_has(X86_FEATURE_PTS) || !boot_cpu_has(X86_FEATURE_PLN))
855 smp_call_function_single(rp->lead_cpu, power_limit_irq_save_cpu, rp, 1);
859 * Restore per package power limit interrupt enable state. Called from cpu
860 * hotplug code on package removal.
862 static void package_power_limit_irq_restore(struct rapl_package *rp)
866 if (!boot_cpu_has(X86_FEATURE_PTS) || !boot_cpu_has(X86_FEATURE_PLN))
869 /* irq enable state not saved, nothing to restore */
870 if (!(rp->power_limit_irq & PACKAGE_PLN_INT_SAVED))
873 rdmsr_safe(MSR_IA32_PACKAGE_THERM_INTERRUPT, &l, &h);
875 if (rp->power_limit_irq & PACKAGE_THERM_INT_PLN_ENABLE)
876 l |= PACKAGE_THERM_INT_PLN_ENABLE;
878 l &= ~PACKAGE_THERM_INT_PLN_ENABLE;
880 wrmsr_safe(MSR_IA32_PACKAGE_THERM_INTERRUPT, l, h);
883 static void set_floor_freq_default(struct rapl_domain *rd, bool mode)
885 int nr_powerlimit = find_nr_power_limit(rd);
887 /* always enable clamp such that p-state can go below OS requested
888 * range. power capping priority over guranteed frequency.
890 rapl_write_data_raw(rd, PL1_CLAMP, mode);
892 /* some domains have pl2 */
893 if (nr_powerlimit > 1) {
894 rapl_write_data_raw(rd, PL2_ENABLE, mode);
895 rapl_write_data_raw(rd, PL2_CLAMP, mode);
899 static void set_floor_freq_atom(struct rapl_domain *rd, bool enable)
901 static u32 power_ctrl_orig_val;
904 if (!rapl_defaults->floor_freq_reg_addr) {
905 pr_err("Invalid floor frequency config register\n");
909 if (!power_ctrl_orig_val)
910 iosf_mbi_read(BT_MBI_UNIT_PMC, MBI_CR_READ,
911 rapl_defaults->floor_freq_reg_addr,
912 &power_ctrl_orig_val);
913 mdata = power_ctrl_orig_val;
915 mdata &= ~(0x7f << 8);
918 iosf_mbi_write(BT_MBI_UNIT_PMC, MBI_CR_WRITE,
919 rapl_defaults->floor_freq_reg_addr, mdata);
922 static u64 rapl_compute_time_window_core(struct rapl_package *rp, u64 value,
925 u64 f, y; /* fraction and exp. used for time unit */
928 * Special processing based on 2^Y*(1+F/4), refer
929 * to Intel Software Developer's manual Vol.3B: CH 14.9.3.
932 f = (value & 0x60) >> 5;
934 value = (1 << y) * (4 + f) * rp->time_unit / 4;
936 do_div(value, rp->time_unit);
938 f = div64_u64(4 * (value - (1 << y)), 1 << y);
939 value = (y & 0x1f) | ((f & 0x3) << 5);
944 static u64 rapl_compute_time_window_atom(struct rapl_package *rp, u64 value,
948 * Atom time unit encoding is straight forward val * time_unit,
949 * where time_unit is default to 1 sec. Never 0.
952 return (value) ? value *= rp->time_unit : rp->time_unit;
954 value = div64_u64(value, rp->time_unit);
959 static const struct rapl_defaults rapl_defaults_core = {
960 .floor_freq_reg_addr = 0,
961 .check_unit = rapl_check_unit_core,
962 .set_floor_freq = set_floor_freq_default,
963 .compute_time_window = rapl_compute_time_window_core,
966 static const struct rapl_defaults rapl_defaults_hsw_server = {
967 .check_unit = rapl_check_unit_core,
968 .set_floor_freq = set_floor_freq_default,
969 .compute_time_window = rapl_compute_time_window_core,
970 .dram_domain_energy_unit = 15300,
973 static const struct rapl_defaults rapl_defaults_byt = {
974 .floor_freq_reg_addr = IOSF_CPU_POWER_BUDGET_CTL_BYT,
975 .check_unit = rapl_check_unit_atom,
976 .set_floor_freq = set_floor_freq_atom,
977 .compute_time_window = rapl_compute_time_window_atom,
980 static const struct rapl_defaults rapl_defaults_tng = {
981 .floor_freq_reg_addr = IOSF_CPU_POWER_BUDGET_CTL_TNG,
982 .check_unit = rapl_check_unit_atom,
983 .set_floor_freq = set_floor_freq_atom,
984 .compute_time_window = rapl_compute_time_window_atom,
987 static const struct rapl_defaults rapl_defaults_ann = {
988 .floor_freq_reg_addr = 0,
989 .check_unit = rapl_check_unit_atom,
990 .set_floor_freq = NULL,
991 .compute_time_window = rapl_compute_time_window_atom,
994 static const struct rapl_defaults rapl_defaults_cht = {
995 .floor_freq_reg_addr = 0,
996 .check_unit = rapl_check_unit_atom,
997 .set_floor_freq = NULL,
998 .compute_time_window = rapl_compute_time_window_atom,
1001 static const struct x86_cpu_id rapl_ids[] __initconst = {
1002 INTEL_CPU_FAM6(SANDYBRIDGE, rapl_defaults_core),
1003 INTEL_CPU_FAM6(SANDYBRIDGE_X, rapl_defaults_core),
1005 INTEL_CPU_FAM6(IVYBRIDGE, rapl_defaults_core),
1006 INTEL_CPU_FAM6(IVYBRIDGE_X, rapl_defaults_core),
1008 INTEL_CPU_FAM6(HASWELL_CORE, rapl_defaults_core),
1009 INTEL_CPU_FAM6(HASWELL_ULT, rapl_defaults_core),
1010 INTEL_CPU_FAM6(HASWELL_GT3E, rapl_defaults_core),
1011 INTEL_CPU_FAM6(HASWELL_X, rapl_defaults_hsw_server),
1013 INTEL_CPU_FAM6(BROADWELL_CORE, rapl_defaults_core),
1014 INTEL_CPU_FAM6(BROADWELL_GT3E, rapl_defaults_core),
1015 INTEL_CPU_FAM6(BROADWELL_XEON_D, rapl_defaults_core),
1016 INTEL_CPU_FAM6(BROADWELL_X, rapl_defaults_hsw_server),
1018 INTEL_CPU_FAM6(SKYLAKE_DESKTOP, rapl_defaults_core),
1019 INTEL_CPU_FAM6(SKYLAKE_MOBILE, rapl_defaults_core),
1020 INTEL_CPU_FAM6(SKYLAKE_X, rapl_defaults_hsw_server),
1021 INTEL_CPU_FAM6(KABYLAKE_MOBILE, rapl_defaults_core),
1022 INTEL_CPU_FAM6(KABYLAKE_DESKTOP, rapl_defaults_core),
1023 INTEL_CPU_FAM6(CANNONLAKE_MOBILE, rapl_defaults_core),
1024 INTEL_CPU_FAM6(ICELAKE_MOBILE, rapl_defaults_core),
1026 INTEL_CPU_FAM6(ATOM_SILVERMONT, rapl_defaults_byt),
1027 INTEL_CPU_FAM6(ATOM_AIRMONT, rapl_defaults_cht),
1028 INTEL_CPU_FAM6(ATOM_SILVERMONT_MID, rapl_defaults_tng),
1029 INTEL_CPU_FAM6(ATOM_AIRMONT_MID, rapl_defaults_ann),
1030 INTEL_CPU_FAM6(ATOM_GOLDMONT, rapl_defaults_core),
1031 INTEL_CPU_FAM6(ATOM_GOLDMONT_PLUS, rapl_defaults_core),
1032 INTEL_CPU_FAM6(ATOM_GOLDMONT_X, rapl_defaults_core),
1033 INTEL_CPU_FAM6(ATOM_TREMONT_X, rapl_defaults_core),
1035 INTEL_CPU_FAM6(XEON_PHI_KNL, rapl_defaults_hsw_server),
1036 INTEL_CPU_FAM6(XEON_PHI_KNM, rapl_defaults_hsw_server),
1039 MODULE_DEVICE_TABLE(x86cpu, rapl_ids);
1041 /* Read once for all raw primitive data for domains */
1042 static void rapl_update_domain_data(struct rapl_package *rp)
1047 for (dmn = 0; dmn < rp->nr_domains; dmn++) {
1048 pr_debug("update %s domain %s data\n", rp->name,
1049 rp->domains[dmn].name);
1050 /* exclude non-raw primitives */
1051 for (prim = 0; prim < NR_RAW_PRIMITIVES; prim++) {
1052 if (!rapl_read_data_raw(&rp->domains[dmn], prim,
1053 rpi[prim].unit, &val))
1054 rp->domains[dmn].rdd.primitives[prim] = val;
1060 static int rapl_package_register_powercap(struct rapl_package *rp)
1062 struct rapl_domain *rd;
1063 struct powercap_zone *power_zone = NULL;
1066 /* Update the domain data of the new package */
1067 rapl_update_domain_data(rp);
1069 /* first we register package domain as the parent zone*/
1070 for (rd = rp->domains; rd < rp->domains + rp->nr_domains; rd++) {
1071 if (rd->id == RAPL_DOMAIN_PACKAGE) {
1072 nr_pl = find_nr_power_limit(rd);
1073 pr_debug("register package domain %s\n", rp->name);
1074 power_zone = powercap_register_zone(&rd->power_zone,
1075 rp->priv->control_type,
1080 if (IS_ERR(power_zone)) {
1081 pr_debug("failed to register power zone %s\n",
1083 return PTR_ERR(power_zone);
1085 /* track parent zone in per package/socket data */
1086 rp->power_zone = power_zone;
1087 /* done, only one package domain per socket */
1092 pr_err("no package domain found, unknown topology!\n");
1095 /* now register domains as children of the socket/package*/
1096 for (rd = rp->domains; rd < rp->domains + rp->nr_domains; rd++) {
1097 if (rd->id == RAPL_DOMAIN_PACKAGE)
1099 /* number of power limits per domain varies */
1100 nr_pl = find_nr_power_limit(rd);
1101 power_zone = powercap_register_zone(&rd->power_zone,
1102 rp->priv->control_type, rd->name,
1104 &zone_ops[rd->id], nr_pl,
1107 if (IS_ERR(power_zone)) {
1108 pr_debug("failed to register power_zone, %s:%s\n",
1109 rp->name, rd->name);
1110 ret = PTR_ERR(power_zone);
1118 * Clean up previously initialized domains within the package if we
1119 * failed after the first domain setup.
1121 while (--rd >= rp->domains) {
1122 pr_debug("unregister %s domain %s\n", rp->name, rd->name);
1123 powercap_unregister_zone(rp->priv->control_type, &rd->power_zone);
1129 static int __init rapl_add_platform_domain(struct rapl_if_priv *priv)
1131 struct rapl_domain *rd;
1132 struct powercap_zone *power_zone;
1133 struct reg_action ra;
1136 ra.reg = priv->regs[RAPL_DOMAIN_PLATFORM][RAPL_DOMAIN_REG_STATUS];
1138 ret = priv->read_raw(0, &ra);
1139 if (ret || !ra.value)
1142 ra.reg = priv->regs[RAPL_DOMAIN_PLATFORM][RAPL_DOMAIN_REG_LIMIT];
1144 ret = priv->read_raw(0, &ra);
1145 if (ret || !ra.value)
1148 rd = kzalloc(sizeof(*rd), GFP_KERNEL);
1152 rd->name = rapl_domain_names[RAPL_DOMAIN_PLATFORM];
1153 rd->id = RAPL_DOMAIN_PLATFORM;
1154 rd->regs[RAPL_DOMAIN_REG_LIMIT] = priv->regs[RAPL_DOMAIN_PLATFORM][RAPL_DOMAIN_REG_LIMIT];
1155 rd->regs[RAPL_DOMAIN_REG_STATUS] = priv->regs[RAPL_DOMAIN_PLATFORM][RAPL_DOMAIN_REG_STATUS];
1156 rd->rpl[0].prim_id = PL1_ENABLE;
1157 rd->rpl[0].name = pl1_name;
1158 rd->rpl[1].prim_id = PL2_ENABLE;
1159 rd->rpl[1].name = pl2_name;
1160 rd->rp = rapl_find_package_domain(0, priv);
1162 power_zone = powercap_register_zone(&rd->power_zone, priv->control_type,
1164 &zone_ops[RAPL_DOMAIN_PLATFORM],
1165 2, &constraint_ops);
1167 if (IS_ERR(power_zone)) {
1169 return PTR_ERR(power_zone);
1172 priv->platform_rapl_domain = rd;
1177 static void rapl_remove_platform_domain(struct rapl_if_priv *priv)
1179 if (priv->platform_rapl_domain) {
1180 powercap_unregister_zone(priv->control_type,
1181 &priv->platform_rapl_domain->power_zone);
1182 kfree(priv->platform_rapl_domain);
1186 static int rapl_check_domain(int cpu, int domain, struct rapl_package *rp)
1188 struct reg_action ra;
1191 case RAPL_DOMAIN_PACKAGE:
1192 case RAPL_DOMAIN_PP0:
1193 case RAPL_DOMAIN_PP1:
1194 case RAPL_DOMAIN_DRAM:
1195 ra.reg = rp->priv->regs[domain][RAPL_DOMAIN_REG_STATUS];
1197 case RAPL_DOMAIN_PLATFORM:
1198 /* PSYS(PLATFORM) is not a CPU domain, so avoid printng error */
1201 pr_err("invalid domain id %d\n", domain);
1204 /* make sure domain counters are available and contains non-zero
1205 * values, otherwise skip it.
1209 if (rp->priv->read_raw(cpu, &ra) || !ra.value)
1217 * Check if power limits are available. Two cases when they are not available:
1218 * 1. Locked by BIOS, in this case we still provide read-only access so that
1219 * users can see what limit is set by the BIOS.
1220 * 2. Some CPUs make some domains monitoring only which means PLx MSRs may not
1221 * exist at all. In this case, we do not show the contraints in powercap.
1223 * Called after domains are detected and initialized.
1225 static void rapl_detect_powerlimit(struct rapl_domain *rd)
1230 /* check if the domain is locked by BIOS, ignore if MSR doesn't exist */
1231 if (!rapl_read_data_raw(rd, FW_LOCK, false, &val64)) {
1233 pr_info("RAPL %s domain %s locked by BIOS\n",
1234 rd->rp->name, rd->name);
1235 rd->state |= DOMAIN_STATE_BIOS_LOCKED;
1238 /* check if power limit MSRs exists, otherwise domain is monitoring only */
1239 for (i = 0; i < NR_POWER_LIMITS; i++) {
1240 int prim = rd->rpl[i].prim_id;
1241 if (rapl_read_data_raw(rd, prim, false, &val64))
1242 rd->rpl[i].name = NULL;
1246 /* Detect active and valid domains for the given CPU, caller must
1247 * ensure the CPU belongs to the targeted package and CPU hotlug is disabled.
1249 static int rapl_detect_domains(struct rapl_package *rp, int cpu)
1251 struct rapl_domain *rd;
1254 for (i = 0; i < RAPL_DOMAIN_MAX; i++) {
1255 /* use physical package id to read counters */
1256 if (!rapl_check_domain(cpu, i, rp)) {
1257 rp->domain_map |= 1 << i;
1258 pr_info("Found RAPL domain %s\n", rapl_domain_names[i]);
1261 rp->nr_domains = bitmap_weight(&rp->domain_map, RAPL_DOMAIN_MAX);
1262 if (!rp->nr_domains) {
1263 pr_debug("no valid rapl domains found in %s\n", rp->name);
1266 pr_debug("found %d domains on %s\n", rp->nr_domains, rp->name);
1268 rp->domains = kcalloc(rp->nr_domains + 1, sizeof(struct rapl_domain),
1273 rapl_init_domains(rp);
1275 for (rd = rp->domains; rd < rp->domains + rp->nr_domains; rd++)
1276 rapl_detect_powerlimit(rd);
1281 /* called from CPU hotplug notifier, hotplug lock held */
1282 static void rapl_remove_package(struct rapl_package *rp)
1284 struct rapl_domain *rd, *rd_package = NULL;
1286 package_power_limit_irq_restore(rp);
1288 for (rd = rp->domains; rd < rp->domains + rp->nr_domains; rd++) {
1289 rapl_write_data_raw(rd, PL1_ENABLE, 0);
1290 rapl_write_data_raw(rd, PL1_CLAMP, 0);
1291 if (find_nr_power_limit(rd) > 1) {
1292 rapl_write_data_raw(rd, PL2_ENABLE, 0);
1293 rapl_write_data_raw(rd, PL2_CLAMP, 0);
1295 if (rd->id == RAPL_DOMAIN_PACKAGE) {
1299 pr_debug("remove package, undo power limit on %s: %s\n",
1300 rp->name, rd->name);
1301 powercap_unregister_zone(rp->priv->control_type, &rd->power_zone);
1303 /* do parent zone last */
1304 powercap_unregister_zone(rp->priv->control_type, &rd_package->power_zone);
1305 list_del(&rp->plist);
1309 /* called from CPU hotplug notifier, hotplug lock held */
1310 static struct rapl_package *rapl_add_package(int cpu, struct rapl_if_priv *priv)
1312 int id = topology_logical_die_id(cpu);
1313 struct rapl_package *rp;
1314 struct cpuinfo_x86 *c = &cpu_data(cpu);
1317 rp = kzalloc(sizeof(struct rapl_package), GFP_KERNEL);
1319 return ERR_PTR(-ENOMEM);
1321 /* add the new package to the list */
1326 if (topology_max_die_per_package() > 1)
1327 snprintf(rp->name, PACKAGE_DOMAIN_NAME_LENGTH,
1328 "package-%d-die-%d", c->phys_proc_id, c->cpu_die_id);
1330 snprintf(rp->name, PACKAGE_DOMAIN_NAME_LENGTH, "package-%d",
1333 /* check if the package contains valid domains */
1334 if (rapl_detect_domains(rp, cpu) ||
1335 rapl_defaults->check_unit(rp, cpu)) {
1337 goto err_free_package;
1339 ret = rapl_package_register_powercap(rp);
1341 INIT_LIST_HEAD(&rp->plist);
1342 list_add(&rp->plist, &rapl_packages);
1349 return ERR_PTR(ret);
1352 /* Handles CPU hotplug on multi-socket systems.
1353 * If a CPU goes online as the first CPU of the physical package
1354 * we add the RAPL package to the system. Similarly, when the last
1355 * CPU of the package is removed, we remove the RAPL package and its
1356 * associated domains. Cooling devices are handled accordingly at
1359 static int rapl_cpu_online(unsigned int cpu)
1361 struct rapl_package *rp;
1363 rp = rapl_find_package_domain(cpu, &rapl_msr_priv);
1365 rp = rapl_add_package(cpu, &rapl_msr_priv);
1369 cpumask_set_cpu(cpu, &rp->cpumask);
1373 static int rapl_cpu_down_prep(unsigned int cpu)
1375 struct rapl_package *rp;
1378 rp = rapl_find_package_domain(cpu, &rapl_msr_priv);
1382 cpumask_clear_cpu(cpu, &rp->cpumask);
1383 lead_cpu = cpumask_first(&rp->cpumask);
1384 if (lead_cpu >= nr_cpu_ids)
1385 rapl_remove_package(rp);
1386 else if (rp->lead_cpu == cpu)
1387 rp->lead_cpu = lead_cpu;
1391 static void power_limit_state_save(void)
1393 struct rapl_package *rp;
1394 struct rapl_domain *rd;
1398 list_for_each_entry(rp, &rapl_packages, plist) {
1399 if (!rp->power_zone)
1401 rd = power_zone_to_rapl_domain(rp->power_zone);
1402 nr_pl = find_nr_power_limit(rd);
1403 for (i = 0; i < nr_pl; i++) {
1404 switch (rd->rpl[i].prim_id) {
1406 ret = rapl_read_data_raw(rd,
1409 &rd->rpl[i].last_power_limit);
1411 rd->rpl[i].last_power_limit = 0;
1414 ret = rapl_read_data_raw(rd,
1417 &rd->rpl[i].last_power_limit);
1419 rd->rpl[i].last_power_limit = 0;
1427 static void power_limit_state_restore(void)
1429 struct rapl_package *rp;
1430 struct rapl_domain *rd;
1434 list_for_each_entry(rp, &rapl_packages, plist) {
1435 if (!rp->power_zone)
1437 rd = power_zone_to_rapl_domain(rp->power_zone);
1438 nr_pl = find_nr_power_limit(rd);
1439 for (i = 0; i < nr_pl; i++) {
1440 switch (rd->rpl[i].prim_id) {
1442 if (rd->rpl[i].last_power_limit)
1443 rapl_write_data_raw(rd,
1445 rd->rpl[i].last_power_limit);
1448 if (rd->rpl[i].last_power_limit)
1449 rapl_write_data_raw(rd,
1451 rd->rpl[i].last_power_limit);
1459 static int rapl_pm_callback(struct notifier_block *nb,
1460 unsigned long mode, void *_unused)
1463 case PM_SUSPEND_PREPARE:
1464 power_limit_state_save();
1466 case PM_POST_SUSPEND:
1467 power_limit_state_restore();
1473 static struct notifier_block rapl_pm_notifier = {
1474 .notifier_call = rapl_pm_callback,
1477 static int rapl_msr_read_raw(int cpu, struct reg_action *ra)
1479 if (rdmsrl_safe_on_cpu(cpu, ra->reg, &ra->value)) {
1480 pr_debug("failed to read msr 0x%x on cpu %d\n", ra->reg, cpu);
1483 ra->value &= ra->mask;
1487 static void rapl_msr_update_func(void *info)
1489 struct reg_action *ra = info;
1492 ra->err = rdmsrl_safe(ra->reg, &val);
1499 ra->err = wrmsrl_safe(ra->reg, val);
1503 static int rapl_msr_write_raw(int cpu, struct reg_action *ra)
1507 ret = smp_call_function_single(cpu, rapl_msr_update_func, ra, 1);
1508 if (WARN_ON_ONCE(ret))
1514 static int __init rapl_init(void)
1516 const struct x86_cpu_id *id;
1519 id = x86_match_cpu(rapl_ids);
1521 pr_err("driver does not support CPU family %d model %d\n",
1522 boot_cpu_data.x86, boot_cpu_data.x86_model);
1527 rapl_defaults = (struct rapl_defaults *)id->driver_data;
1529 rapl_msr_priv.read_raw = rapl_msr_read_raw;
1530 rapl_msr_priv.write_raw = rapl_msr_write_raw;
1532 rapl_msr_priv.control_type = powercap_register_control_type(NULL, "intel-rapl", NULL);
1533 if (IS_ERR(rapl_msr_priv.control_type)) {
1534 pr_debug("failed to register powercap control_type.\n");
1535 return PTR_ERR(rapl_msr_priv.control_type);
1538 ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "powercap/rapl:online",
1539 rapl_cpu_online, rapl_cpu_down_prep);
1542 rapl_msr_priv.pcap_rapl_online = ret;
1544 /* Don't bail out if PSys is not supported */
1545 rapl_add_platform_domain(&rapl_msr_priv);
1547 ret = register_pm_notifier(&rapl_pm_notifier);
1554 cpuhp_remove_state(rapl_msr_priv.pcap_rapl_online);
1557 powercap_unregister_control_type(rapl_msr_priv.control_type);
1561 static void __exit rapl_exit(void)
1563 unregister_pm_notifier(&rapl_pm_notifier);
1564 cpuhp_remove_state(rapl_msr_priv.pcap_rapl_online);
1565 rapl_remove_platform_domain(&rapl_msr_priv);
1566 powercap_unregister_control_type(rapl_msr_priv.control_type);
1569 module_init(rapl_init);
1570 module_exit(rapl_exit);
1572 MODULE_DESCRIPTION("Driver for Intel RAPL (Running Average Power Limit)");
1573 MODULE_AUTHOR("Jacob Pan <jacob.jun.pan@intel.com>");
1574 MODULE_LICENSE("GPL v2");