1 // SPDX-License-Identifier: GPL-2.0-only
3 * Common code for Intel Running Average Power Limit (RAPL) support.
4 * Copyright (c) 2019, 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>
22 #include <linux/processor.h>
23 #include <linux/platform_device.h>
25 #include <asm/iosf_mbi.h>
26 #include <asm/cpu_device_id.h>
27 #include <asm/intel-family.h>
29 /* bitmasks for RAPL MSRs, used by primitive access functions */
30 #define ENERGY_STATUS_MASK 0xffffffff
32 #define POWER_LIMIT1_MASK 0x7FFF
33 #define POWER_LIMIT1_ENABLE BIT(15)
34 #define POWER_LIMIT1_CLAMP BIT(16)
36 #define POWER_LIMIT2_MASK (0x7FFFULL<<32)
37 #define POWER_LIMIT2_ENABLE BIT_ULL(47)
38 #define POWER_LIMIT2_CLAMP BIT_ULL(48)
39 #define POWER_HIGH_LOCK BIT_ULL(63)
40 #define POWER_LOW_LOCK BIT(31)
42 #define POWER_LIMIT4_MASK 0x1FFF
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
65 * SPR has different layout for Psys Domain PowerLimit registers.
66 * There are 17 bits of PL1 and PL2 instead of 15 bits.
67 * The Enable bits and TimeWindow bits are also shifted as a result.
69 #define PSYS_POWER_LIMIT1_MASK 0x1FFFF
70 #define PSYS_POWER_LIMIT1_ENABLE BIT(17)
72 #define PSYS_POWER_LIMIT2_MASK (0x1FFFFULL<<32)
73 #define PSYS_POWER_LIMIT2_ENABLE BIT_ULL(49)
75 #define PSYS_TIME_WINDOW1_MASK (0x7FULL<<19)
76 #define PSYS_TIME_WINDOW2_MASK (0x7FULL<<51)
78 /* Non HW constants */
79 #define RAPL_PRIMITIVE_DERIVED BIT(1) /* not from raw data */
80 #define RAPL_PRIMITIVE_DUMMY BIT(2)
82 #define TIME_WINDOW_MAX_MSEC 40000
83 #define TIME_WINDOW_MIN_MSEC 250
84 #define ENERGY_UNIT_SCALE 1000 /* scale from driver unit to powercap unit */
86 ARBITRARY_UNIT, /* no translation */
92 /* per domain data, some are optional */
93 #define NR_RAW_PRIMITIVES (NR_RAPL_PRIMITIVES - 2)
95 #define DOMAIN_STATE_INACTIVE BIT(0)
96 #define DOMAIN_STATE_POWER_LIMIT_SET BIT(1)
97 #define DOMAIN_STATE_BIOS_LOCKED BIT(2)
99 static const char pl1_name[] = "long_term";
100 static const char pl2_name[] = "short_term";
101 static const char pl4_name[] = "peak_power";
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;
113 unsigned int psys_domain_energy_unit;
116 static struct rapl_defaults *rapl_defaults;
118 static struct rapl_defaults *get_defaults(struct rapl_package *rp)
120 return rp->priv->defaults;
123 /* Sideband MBI registers */
124 #define IOSF_CPU_POWER_BUDGET_CTL_BYT (0x2)
125 #define IOSF_CPU_POWER_BUDGET_CTL_TNG (0xdf)
127 #define PACKAGE_PLN_INT_SAVED BIT(0)
128 #define MAX_PRIM_NAME (32)
130 /* per domain data. used to describe individual knobs such that access function
131 * can be consolidated into one instead of many inline functions.
133 struct rapl_primitive_info {
137 enum rapl_domain_reg_id id;
142 #define PRIMITIVE_INFO_INIT(p, m, s, i, u, f) { \
151 static void rapl_init_domains(struct rapl_package *rp);
152 static int rapl_read_data_raw(struct rapl_domain *rd,
153 enum rapl_primitives prim,
154 bool xlate, u64 *data);
155 static int rapl_write_data_raw(struct rapl_domain *rd,
156 enum rapl_primitives prim,
157 unsigned long long value);
158 static u64 rapl_unit_xlate(struct rapl_domain *rd,
159 enum unit_type type, u64 value, int to_raw);
160 static void package_power_limit_irq_save(struct rapl_package *rp);
162 static LIST_HEAD(rapl_packages); /* guarded by CPU hotplug lock */
164 static const char *const rapl_domain_names[] = {
172 static int get_energy_counter(struct powercap_zone *power_zone,
175 struct rapl_domain *rd;
178 /* prevent CPU hotplug, make sure the RAPL domain does not go
179 * away while reading the counter.
182 rd = power_zone_to_rapl_domain(power_zone);
184 if (!rapl_read_data_raw(rd, ENERGY_COUNTER, true, &energy_now)) {
185 *energy_raw = energy_now;
195 static int get_max_energy_counter(struct powercap_zone *pcd_dev, u64 *energy)
197 struct rapl_domain *rd = power_zone_to_rapl_domain(pcd_dev);
199 *energy = rapl_unit_xlate(rd, ENERGY_UNIT, ENERGY_STATUS_MASK, 0);
203 static int release_zone(struct powercap_zone *power_zone)
205 struct rapl_domain *rd = power_zone_to_rapl_domain(power_zone);
206 struct rapl_package *rp = rd->rp;
208 /* package zone is the last zone of a package, we can free
209 * memory here since all children has been unregistered.
211 if (rd->id == RAPL_DOMAIN_PACKAGE) {
220 static int find_nr_power_limit(struct rapl_domain *rd)
224 for (i = 0; i < NR_POWER_LIMITS; i++) {
232 static int set_domain_enable(struct powercap_zone *power_zone, bool mode)
234 struct rapl_domain *rd = power_zone_to_rapl_domain(power_zone);
235 struct rapl_defaults *defaults = get_defaults(rd->rp);
237 if (rd->state & DOMAIN_STATE_BIOS_LOCKED)
241 rapl_write_data_raw(rd, PL1_ENABLE, mode);
242 if (defaults->set_floor_freq)
243 defaults->set_floor_freq(rd, mode);
249 static int get_domain_enable(struct powercap_zone *power_zone, bool *mode)
251 struct rapl_domain *rd = power_zone_to_rapl_domain(power_zone);
254 if (rd->state & DOMAIN_STATE_BIOS_LOCKED) {
259 if (rapl_read_data_raw(rd, PL1_ENABLE, true, &val)) {
269 /* per RAPL domain ops, in the order of rapl_domain_type */
270 static const struct powercap_zone_ops zone_ops[] = {
271 /* RAPL_DOMAIN_PACKAGE */
273 .get_energy_uj = get_energy_counter,
274 .get_max_energy_range_uj = get_max_energy_counter,
275 .release = release_zone,
276 .set_enable = set_domain_enable,
277 .get_enable = get_domain_enable,
279 /* RAPL_DOMAIN_PP0 */
281 .get_energy_uj = get_energy_counter,
282 .get_max_energy_range_uj = get_max_energy_counter,
283 .release = release_zone,
284 .set_enable = set_domain_enable,
285 .get_enable = get_domain_enable,
287 /* RAPL_DOMAIN_PP1 */
289 .get_energy_uj = get_energy_counter,
290 .get_max_energy_range_uj = get_max_energy_counter,
291 .release = release_zone,
292 .set_enable = set_domain_enable,
293 .get_enable = get_domain_enable,
295 /* RAPL_DOMAIN_DRAM */
297 .get_energy_uj = get_energy_counter,
298 .get_max_energy_range_uj = get_max_energy_counter,
299 .release = release_zone,
300 .set_enable = set_domain_enable,
301 .get_enable = get_domain_enable,
303 /* RAPL_DOMAIN_PLATFORM */
305 .get_energy_uj = get_energy_counter,
306 .get_max_energy_range_uj = get_max_energy_counter,
307 .release = release_zone,
308 .set_enable = set_domain_enable,
309 .get_enable = get_domain_enable,
314 * Constraint index used by powercap can be different than power limit (PL)
315 * index in that some PLs maybe missing due to non-existent MSRs. So we
316 * need to convert here by finding the valid PLs only (name populated).
318 static int contraint_to_pl(struct rapl_domain *rd, int cid)
322 for (i = 0, j = 0; i < NR_POWER_LIMITS; i++) {
323 if ((rd->rpl[i].name) && j++ == cid) {
324 pr_debug("%s: index %d\n", __func__, i);
328 pr_err("Cannot find matching power limit for constraint %d\n", cid);
333 static int set_power_limit(struct powercap_zone *power_zone, int cid,
336 struct rapl_domain *rd;
337 struct rapl_package *rp;
342 rd = power_zone_to_rapl_domain(power_zone);
343 id = contraint_to_pl(rd, cid);
351 if (rd->state & DOMAIN_STATE_BIOS_LOCKED) {
352 dev_warn(&power_zone->dev,
353 "%s locked by BIOS, monitoring only\n", rd->name);
358 switch (rd->rpl[id].prim_id) {
360 rapl_write_data_raw(rd, POWER_LIMIT1, power_limit);
363 rapl_write_data_raw(rd, POWER_LIMIT2, power_limit);
366 rapl_write_data_raw(rd, POWER_LIMIT4, power_limit);
372 package_power_limit_irq_save(rp);
378 static int get_current_power_limit(struct powercap_zone *power_zone, int cid,
381 struct rapl_domain *rd;
388 rd = power_zone_to_rapl_domain(power_zone);
389 id = contraint_to_pl(rd, cid);
395 switch (rd->rpl[id].prim_id) {
409 if (rapl_read_data_raw(rd, prim, true, &val))
420 static int set_time_window(struct powercap_zone *power_zone, int cid,
423 struct rapl_domain *rd;
428 rd = power_zone_to_rapl_domain(power_zone);
429 id = contraint_to_pl(rd, cid);
435 switch (rd->rpl[id].prim_id) {
437 rapl_write_data_raw(rd, TIME_WINDOW1, window);
440 rapl_write_data_raw(rd, TIME_WINDOW2, window);
451 static int get_time_window(struct powercap_zone *power_zone, int cid,
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);
476 * Time window parameter is not applicable for PL4 entry
477 * so assigining '0' as default value.
494 static const char *get_constraint_name(struct powercap_zone *power_zone,
497 struct rapl_domain *rd;
500 rd = power_zone_to_rapl_domain(power_zone);
501 id = contraint_to_pl(rd, cid);
503 return rd->rpl[id].name;
508 static int get_max_power(struct powercap_zone *power_zone, int id, u64 *data)
510 struct rapl_domain *rd;
516 rd = power_zone_to_rapl_domain(power_zone);
517 switch (rd->rpl[id].prim_id) {
519 prim = THERMAL_SPEC_POWER;
531 if (rapl_read_data_raw(rd, prim, true, &val))
536 /* As a generalization rule, PL4 would be around two times PL2. */
537 if (rd->rpl[id].prim_id == PL4_ENABLE)
545 static const struct powercap_zone_constraint_ops constraint_ops = {
546 .set_power_limit_uw = set_power_limit,
547 .get_power_limit_uw = get_current_power_limit,
548 .set_time_window_us = set_time_window,
549 .get_time_window_us = get_time_window,
550 .get_max_power_uw = get_max_power,
551 .get_name = get_constraint_name,
554 /* called after domain detection and package level data are set */
555 static void rapl_init_domains(struct rapl_package *rp)
557 enum rapl_domain_type i;
558 enum rapl_domain_reg_id j;
559 struct rapl_domain *rd = rp->domains;
560 struct rapl_defaults *defaults = get_defaults(rp);
562 for (i = 0; i < RAPL_DOMAIN_MAX; i++) {
563 unsigned int mask = rp->domain_map & (1 << i);
570 if (i == RAPL_DOMAIN_PLATFORM && rp->id > 0) {
571 snprintf(rd->name, RAPL_DOMAIN_NAME_LENGTH, "psys-%d",
572 topology_physical_package_id(rp->lead_cpu));
574 snprintf(rd->name, RAPL_DOMAIN_NAME_LENGTH, "%s",
575 rapl_domain_names[i]);
578 rd->rpl[0].prim_id = PL1_ENABLE;
579 rd->rpl[0].name = pl1_name;
582 * The PL2 power domain is applicable for limits two
585 if (rp->priv->limits[i] >= 2) {
586 rd->rpl[1].prim_id = PL2_ENABLE;
587 rd->rpl[1].name = pl2_name;
590 /* Enable PL4 domain if the total power limits are three */
591 if (rp->priv->limits[i] == 3) {
592 rd->rpl[2].prim_id = PL4_ENABLE;
593 rd->rpl[2].name = pl4_name;
596 for (j = 0; j < RAPL_DOMAIN_REG_MAX; j++)
597 rd->regs[j] = rp->priv->regs[i][j];
600 case RAPL_DOMAIN_DRAM:
601 rd->domain_energy_unit =
602 defaults->dram_domain_energy_unit;
603 if (rd->domain_energy_unit)
604 pr_info("DRAM domain energy unit %dpj\n",
605 rd->domain_energy_unit);
607 case RAPL_DOMAIN_PLATFORM:
608 rd->domain_energy_unit =
609 defaults->psys_domain_energy_unit;
610 if (rd->domain_energy_unit)
611 pr_info("Platform domain energy unit %dpj\n",
612 rd->domain_energy_unit);
621 static u64 rapl_unit_xlate(struct rapl_domain *rd, enum unit_type type,
622 u64 value, int to_raw)
625 struct rapl_package *rp = rd->rp;
626 struct rapl_defaults *defaults = get_defaults(rp);
631 units = rp->power_unit;
634 scale = ENERGY_UNIT_SCALE;
635 /* per domain unit takes precedence */
636 if (rd->domain_energy_unit)
637 units = rd->domain_energy_unit;
639 units = rp->energy_unit;
642 return defaults->compute_time_window(rp, value, to_raw);
649 return div64_u64(value, units) * scale;
653 return div64_u64(value, scale);
656 /* in the order of enum rapl_primitives */
657 static struct rapl_primitive_info rpi[] = {
658 /* name, mask, shift, msr index, unit divisor */
659 PRIMITIVE_INFO_INIT(ENERGY_COUNTER, ENERGY_STATUS_MASK, 0,
660 RAPL_DOMAIN_REG_STATUS, ENERGY_UNIT, 0),
661 PRIMITIVE_INFO_INIT(POWER_LIMIT1, POWER_LIMIT1_MASK, 0,
662 RAPL_DOMAIN_REG_LIMIT, POWER_UNIT, 0),
663 PRIMITIVE_INFO_INIT(POWER_LIMIT2, POWER_LIMIT2_MASK, 32,
664 RAPL_DOMAIN_REG_LIMIT, POWER_UNIT, 0),
665 PRIMITIVE_INFO_INIT(POWER_LIMIT4, POWER_LIMIT4_MASK, 0,
666 RAPL_DOMAIN_REG_PL4, POWER_UNIT, 0),
667 PRIMITIVE_INFO_INIT(FW_LOCK, POWER_LOW_LOCK, 31,
668 RAPL_DOMAIN_REG_LIMIT, ARBITRARY_UNIT, 0),
669 PRIMITIVE_INFO_INIT(PL1_ENABLE, POWER_LIMIT1_ENABLE, 15,
670 RAPL_DOMAIN_REG_LIMIT, ARBITRARY_UNIT, 0),
671 PRIMITIVE_INFO_INIT(PL1_CLAMP, POWER_LIMIT1_CLAMP, 16,
672 RAPL_DOMAIN_REG_LIMIT, ARBITRARY_UNIT, 0),
673 PRIMITIVE_INFO_INIT(PL2_ENABLE, POWER_LIMIT2_ENABLE, 47,
674 RAPL_DOMAIN_REG_LIMIT, ARBITRARY_UNIT, 0),
675 PRIMITIVE_INFO_INIT(PL2_CLAMP, POWER_LIMIT2_CLAMP, 48,
676 RAPL_DOMAIN_REG_LIMIT, ARBITRARY_UNIT, 0),
677 PRIMITIVE_INFO_INIT(PL4_ENABLE, POWER_LIMIT4_MASK, 0,
678 RAPL_DOMAIN_REG_PL4, ARBITRARY_UNIT, 0),
679 PRIMITIVE_INFO_INIT(TIME_WINDOW1, TIME_WINDOW1_MASK, 17,
680 RAPL_DOMAIN_REG_LIMIT, TIME_UNIT, 0),
681 PRIMITIVE_INFO_INIT(TIME_WINDOW2, TIME_WINDOW2_MASK, 49,
682 RAPL_DOMAIN_REG_LIMIT, TIME_UNIT, 0),
683 PRIMITIVE_INFO_INIT(THERMAL_SPEC_POWER, POWER_INFO_THERMAL_SPEC_MASK,
684 0, RAPL_DOMAIN_REG_INFO, POWER_UNIT, 0),
685 PRIMITIVE_INFO_INIT(MAX_POWER, POWER_INFO_MAX_MASK, 32,
686 RAPL_DOMAIN_REG_INFO, POWER_UNIT, 0),
687 PRIMITIVE_INFO_INIT(MIN_POWER, POWER_INFO_MIN_MASK, 16,
688 RAPL_DOMAIN_REG_INFO, POWER_UNIT, 0),
689 PRIMITIVE_INFO_INIT(MAX_TIME_WINDOW, POWER_INFO_MAX_TIME_WIN_MASK, 48,
690 RAPL_DOMAIN_REG_INFO, TIME_UNIT, 0),
691 PRIMITIVE_INFO_INIT(THROTTLED_TIME, PERF_STATUS_THROTTLE_TIME_MASK, 0,
692 RAPL_DOMAIN_REG_PERF, TIME_UNIT, 0),
693 PRIMITIVE_INFO_INIT(PRIORITY_LEVEL, PP_POLICY_MASK, 0,
694 RAPL_DOMAIN_REG_POLICY, ARBITRARY_UNIT, 0),
695 PRIMITIVE_INFO_INIT(PSYS_POWER_LIMIT1, PSYS_POWER_LIMIT1_MASK, 0,
696 RAPL_DOMAIN_REG_LIMIT, POWER_UNIT, 0),
697 PRIMITIVE_INFO_INIT(PSYS_POWER_LIMIT2, PSYS_POWER_LIMIT2_MASK, 32,
698 RAPL_DOMAIN_REG_LIMIT, POWER_UNIT, 0),
699 PRIMITIVE_INFO_INIT(PSYS_PL1_ENABLE, PSYS_POWER_LIMIT1_ENABLE, 17,
700 RAPL_DOMAIN_REG_LIMIT, ARBITRARY_UNIT, 0),
701 PRIMITIVE_INFO_INIT(PSYS_PL2_ENABLE, PSYS_POWER_LIMIT2_ENABLE, 49,
702 RAPL_DOMAIN_REG_LIMIT, ARBITRARY_UNIT, 0),
703 PRIMITIVE_INFO_INIT(PSYS_TIME_WINDOW1, PSYS_TIME_WINDOW1_MASK, 19,
704 RAPL_DOMAIN_REG_LIMIT, TIME_UNIT, 0),
705 PRIMITIVE_INFO_INIT(PSYS_TIME_WINDOW2, PSYS_TIME_WINDOW2_MASK, 51,
706 RAPL_DOMAIN_REG_LIMIT, TIME_UNIT, 0),
708 PRIMITIVE_INFO_INIT(AVERAGE_POWER, 0, 0, 0, POWER_UNIT,
709 RAPL_PRIMITIVE_DERIVED),
713 static int rapl_config(struct rapl_package *rp)
715 rp->priv->defaults = (void *)rapl_defaults;
719 static enum rapl_primitives
720 prim_fixups(struct rapl_domain *rd, enum rapl_primitives prim)
722 struct rapl_defaults *defaults = get_defaults(rd->rp);
724 if (!defaults->spr_psys_bits)
727 if (rd->id != RAPL_DOMAIN_PLATFORM)
732 return PSYS_POWER_LIMIT1;
734 return PSYS_POWER_LIMIT2;
736 return PSYS_PL1_ENABLE;
738 return PSYS_PL2_ENABLE;
740 return PSYS_TIME_WINDOW1;
742 return PSYS_TIME_WINDOW2;
748 /* Read primitive data based on its related struct rapl_primitive_info.
749 * if xlate flag is set, return translated data based on data units, i.e.
750 * time, energy, and power.
751 * RAPL MSRs are non-architectual and are laid out not consistently across
752 * domains. Here we use primitive info to allow writing consolidated access
754 * For a given primitive, it is processed by MSR mask and shift. Unit conversion
755 * is pre-assigned based on RAPL unit MSRs read at init time.
756 * 63-------------------------- 31--------------------------- 0
758 * | |<- shift ----------------|
759 * 63-------------------------- 31--------------------------- 0
761 static int rapl_read_data_raw(struct rapl_domain *rd,
762 enum rapl_primitives prim, bool xlate, u64 *data)
765 enum rapl_primitives prim_fixed = prim_fixups(rd, prim);
766 struct rapl_primitive_info *rp = &rpi[prim_fixed];
767 struct reg_action ra;
770 if (!rp->name || rp->flag & RAPL_PRIMITIVE_DUMMY)
773 ra.reg = rd->regs[rp->id];
777 cpu = rd->rp->lead_cpu;
779 /* domain with 2 limits has different bit */
780 if (prim == FW_LOCK && rd->rp->priv->limits[rd->id] == 2) {
781 rp->mask = POWER_HIGH_LOCK;
784 /* non-hardware data are collected by the polling thread */
785 if (rp->flag & RAPL_PRIMITIVE_DERIVED) {
786 *data = rd->rdd.primitives[prim];
792 if (rd->rp->priv->read_raw(cpu, &ra)) {
793 pr_debug("failed to read reg 0x%llx on cpu %d\n", ra.reg, cpu);
797 value = ra.value >> rp->shift;
800 *data = rapl_unit_xlate(rd, rp->unit, value, 0);
807 /* Similar use of primitive info in the read counterpart */
808 static int rapl_write_data_raw(struct rapl_domain *rd,
809 enum rapl_primitives prim,
810 unsigned long long value)
812 enum rapl_primitives prim_fixed = prim_fixups(rd, prim);
813 struct rapl_primitive_info *rp = &rpi[prim_fixed];
816 struct reg_action ra;
819 cpu = rd->rp->lead_cpu;
820 bits = rapl_unit_xlate(rd, rp->unit, value, 1);
824 memset(&ra, 0, sizeof(ra));
826 ra.reg = rd->regs[rp->id];
830 ret = rd->rp->priv->write_raw(cpu, &ra);
836 * Raw RAPL data stored in MSRs are in certain scales. We need to
837 * convert them into standard units based on the units reported in
838 * the RAPL unit MSRs. This is specific to CPUs as the method to
839 * calculate units differ on different CPUs.
840 * We convert the units to below format based on CPUs.
842 * energy unit: picoJoules : Represented in picoJoules by default
843 * power unit : microWatts : Represented in milliWatts by default
844 * time unit : microseconds: Represented in seconds by default
846 static int rapl_check_unit_core(struct rapl_package *rp, int cpu)
848 struct reg_action ra;
851 ra.reg = rp->priv->reg_unit;
853 if (rp->priv->read_raw(cpu, &ra)) {
854 pr_err("Failed to read power unit REG 0x%llx on CPU %d, exit.\n",
855 rp->priv->reg_unit, cpu);
859 value = (ra.value & ENERGY_UNIT_MASK) >> ENERGY_UNIT_OFFSET;
860 rp->energy_unit = ENERGY_UNIT_SCALE * 1000000 / (1 << value);
862 value = (ra.value & POWER_UNIT_MASK) >> POWER_UNIT_OFFSET;
863 rp->power_unit = 1000000 / (1 << value);
865 value = (ra.value & TIME_UNIT_MASK) >> TIME_UNIT_OFFSET;
866 rp->time_unit = 1000000 / (1 << value);
868 pr_debug("Core CPU %s energy=%dpJ, time=%dus, power=%duW\n",
869 rp->name, rp->energy_unit, rp->time_unit, rp->power_unit);
874 static int rapl_check_unit_atom(struct rapl_package *rp, int cpu)
876 struct reg_action ra;
879 ra.reg = rp->priv->reg_unit;
881 if (rp->priv->read_raw(cpu, &ra)) {
882 pr_err("Failed to read power unit REG 0x%llx on CPU %d, exit.\n",
883 rp->priv->reg_unit, cpu);
887 value = (ra.value & ENERGY_UNIT_MASK) >> ENERGY_UNIT_OFFSET;
888 rp->energy_unit = ENERGY_UNIT_SCALE * 1 << value;
890 value = (ra.value & POWER_UNIT_MASK) >> POWER_UNIT_OFFSET;
891 rp->power_unit = (1 << value) * 1000;
893 value = (ra.value & TIME_UNIT_MASK) >> TIME_UNIT_OFFSET;
894 rp->time_unit = 1000000 / (1 << value);
896 pr_debug("Atom %s energy=%dpJ, time=%dus, power=%duW\n",
897 rp->name, rp->energy_unit, rp->time_unit, rp->power_unit);
902 static void power_limit_irq_save_cpu(void *info)
905 struct rapl_package *rp = (struct rapl_package *)info;
907 /* save the state of PLN irq mask bit before disabling it */
908 rdmsr_safe(MSR_IA32_PACKAGE_THERM_INTERRUPT, &l, &h);
909 if (!(rp->power_limit_irq & PACKAGE_PLN_INT_SAVED)) {
910 rp->power_limit_irq = l & PACKAGE_THERM_INT_PLN_ENABLE;
911 rp->power_limit_irq |= PACKAGE_PLN_INT_SAVED;
913 l &= ~PACKAGE_THERM_INT_PLN_ENABLE;
914 wrmsr_safe(MSR_IA32_PACKAGE_THERM_INTERRUPT, l, h);
918 * When package power limit is set artificially low by RAPL, LVT
919 * thermal interrupt for package power limit should be ignored
920 * since we are not really exceeding the real limit. The intention
921 * is to avoid excessive interrupts while we are trying to save power.
922 * A useful feature might be routing the package_power_limit interrupt
923 * to userspace via eventfd. once we have a usecase, this is simple
924 * to do by adding an atomic notifier.
927 static void package_power_limit_irq_save(struct rapl_package *rp)
929 if (!boot_cpu_has(X86_FEATURE_PTS) || !boot_cpu_has(X86_FEATURE_PLN))
932 smp_call_function_single(rp->lead_cpu, power_limit_irq_save_cpu, rp, 1);
936 * Restore per package power limit interrupt enable state. Called from cpu
937 * hotplug code on package removal.
939 static void package_power_limit_irq_restore(struct rapl_package *rp)
943 if (!boot_cpu_has(X86_FEATURE_PTS) || !boot_cpu_has(X86_FEATURE_PLN))
946 /* irq enable state not saved, nothing to restore */
947 if (!(rp->power_limit_irq & PACKAGE_PLN_INT_SAVED))
950 rdmsr_safe(MSR_IA32_PACKAGE_THERM_INTERRUPT, &l, &h);
952 if (rp->power_limit_irq & PACKAGE_THERM_INT_PLN_ENABLE)
953 l |= PACKAGE_THERM_INT_PLN_ENABLE;
955 l &= ~PACKAGE_THERM_INT_PLN_ENABLE;
957 wrmsr_safe(MSR_IA32_PACKAGE_THERM_INTERRUPT, l, h);
960 static void set_floor_freq_default(struct rapl_domain *rd, bool mode)
962 int nr_powerlimit = find_nr_power_limit(rd);
964 /* always enable clamp such that p-state can go below OS requested
965 * range. power capping priority over guranteed frequency.
967 rapl_write_data_raw(rd, PL1_CLAMP, mode);
969 /* some domains have pl2 */
970 if (nr_powerlimit > 1) {
971 rapl_write_data_raw(rd, PL2_ENABLE, mode);
972 rapl_write_data_raw(rd, PL2_CLAMP, mode);
976 static void set_floor_freq_atom(struct rapl_domain *rd, bool enable)
978 static u32 power_ctrl_orig_val;
979 struct rapl_defaults *defaults = get_defaults(rd->rp);
982 if (!defaults->floor_freq_reg_addr) {
983 pr_err("Invalid floor frequency config register\n");
987 if (!power_ctrl_orig_val)
988 iosf_mbi_read(BT_MBI_UNIT_PMC, MBI_CR_READ,
989 defaults->floor_freq_reg_addr,
990 &power_ctrl_orig_val);
991 mdata = power_ctrl_orig_val;
993 mdata &= ~(0x7f << 8);
996 iosf_mbi_write(BT_MBI_UNIT_PMC, MBI_CR_WRITE,
997 defaults->floor_freq_reg_addr, mdata);
1000 static u64 rapl_compute_time_window_core(struct rapl_package *rp, u64 value,
1003 u64 f, y; /* fraction and exp. used for time unit */
1006 * Special processing based on 2^Y*(1+F/4), refer
1007 * to Intel Software Developer's manual Vol.3B: CH 14.9.3.
1010 f = (value & 0x60) >> 5;
1012 value = (1 << y) * (4 + f) * rp->time_unit / 4;
1014 if (value < rp->time_unit)
1017 do_div(value, rp->time_unit);
1021 * The target hardware field is 7 bits wide, so return all ones
1022 * if the exponent is too large.
1027 f = div64_u64(4 * (value - (1ULL << y)), 1ULL << y);
1028 value = (y & 0x1f) | ((f & 0x3) << 5);
1033 static u64 rapl_compute_time_window_atom(struct rapl_package *rp, u64 value,
1037 * Atom time unit encoding is straight forward val * time_unit,
1038 * where time_unit is default to 1 sec. Never 0.
1041 return (value) ? value * rp->time_unit : rp->time_unit;
1043 value = div64_u64(value, rp->time_unit);
1048 static const struct rapl_defaults rapl_defaults_core = {
1049 .floor_freq_reg_addr = 0,
1050 .check_unit = rapl_check_unit_core,
1051 .set_floor_freq = set_floor_freq_default,
1052 .compute_time_window = rapl_compute_time_window_core,
1055 static const struct rapl_defaults rapl_defaults_hsw_server = {
1056 .check_unit = rapl_check_unit_core,
1057 .set_floor_freq = set_floor_freq_default,
1058 .compute_time_window = rapl_compute_time_window_core,
1059 .dram_domain_energy_unit = 15300,
1062 static const struct rapl_defaults rapl_defaults_spr_server = {
1063 .check_unit = rapl_check_unit_core,
1064 .set_floor_freq = set_floor_freq_default,
1065 .compute_time_window = rapl_compute_time_window_core,
1066 .psys_domain_energy_unit = 1000000000,
1067 .spr_psys_bits = true,
1070 static const struct rapl_defaults rapl_defaults_byt = {
1071 .floor_freq_reg_addr = IOSF_CPU_POWER_BUDGET_CTL_BYT,
1072 .check_unit = rapl_check_unit_atom,
1073 .set_floor_freq = set_floor_freq_atom,
1074 .compute_time_window = rapl_compute_time_window_atom,
1077 static const struct rapl_defaults rapl_defaults_tng = {
1078 .floor_freq_reg_addr = IOSF_CPU_POWER_BUDGET_CTL_TNG,
1079 .check_unit = rapl_check_unit_atom,
1080 .set_floor_freq = set_floor_freq_atom,
1081 .compute_time_window = rapl_compute_time_window_atom,
1084 static const struct rapl_defaults rapl_defaults_ann = {
1085 .floor_freq_reg_addr = 0,
1086 .check_unit = rapl_check_unit_atom,
1087 .set_floor_freq = NULL,
1088 .compute_time_window = rapl_compute_time_window_atom,
1091 static const struct rapl_defaults rapl_defaults_cht = {
1092 .floor_freq_reg_addr = 0,
1093 .check_unit = rapl_check_unit_atom,
1094 .set_floor_freq = NULL,
1095 .compute_time_window = rapl_compute_time_window_atom,
1098 static const struct rapl_defaults rapl_defaults_amd = {
1099 .check_unit = rapl_check_unit_core,
1102 static const struct x86_cpu_id rapl_ids[] __initconst = {
1103 X86_MATCH_INTEL_FAM6_MODEL(SANDYBRIDGE, &rapl_defaults_core),
1104 X86_MATCH_INTEL_FAM6_MODEL(SANDYBRIDGE_X, &rapl_defaults_core),
1106 X86_MATCH_INTEL_FAM6_MODEL(IVYBRIDGE, &rapl_defaults_core),
1107 X86_MATCH_INTEL_FAM6_MODEL(IVYBRIDGE_X, &rapl_defaults_core),
1109 X86_MATCH_INTEL_FAM6_MODEL(HASWELL, &rapl_defaults_core),
1110 X86_MATCH_INTEL_FAM6_MODEL(HASWELL_L, &rapl_defaults_core),
1111 X86_MATCH_INTEL_FAM6_MODEL(HASWELL_G, &rapl_defaults_core),
1112 X86_MATCH_INTEL_FAM6_MODEL(HASWELL_X, &rapl_defaults_hsw_server),
1114 X86_MATCH_INTEL_FAM6_MODEL(BROADWELL, &rapl_defaults_core),
1115 X86_MATCH_INTEL_FAM6_MODEL(BROADWELL_G, &rapl_defaults_core),
1116 X86_MATCH_INTEL_FAM6_MODEL(BROADWELL_D, &rapl_defaults_core),
1117 X86_MATCH_INTEL_FAM6_MODEL(BROADWELL_X, &rapl_defaults_hsw_server),
1119 X86_MATCH_INTEL_FAM6_MODEL(SKYLAKE, &rapl_defaults_core),
1120 X86_MATCH_INTEL_FAM6_MODEL(SKYLAKE_L, &rapl_defaults_core),
1121 X86_MATCH_INTEL_FAM6_MODEL(SKYLAKE_X, &rapl_defaults_hsw_server),
1122 X86_MATCH_INTEL_FAM6_MODEL(KABYLAKE_L, &rapl_defaults_core),
1123 X86_MATCH_INTEL_FAM6_MODEL(KABYLAKE, &rapl_defaults_core),
1124 X86_MATCH_INTEL_FAM6_MODEL(CANNONLAKE_L, &rapl_defaults_core),
1125 X86_MATCH_INTEL_FAM6_MODEL(ICELAKE_L, &rapl_defaults_core),
1126 X86_MATCH_INTEL_FAM6_MODEL(ICELAKE, &rapl_defaults_core),
1127 X86_MATCH_INTEL_FAM6_MODEL(ICELAKE_NNPI, &rapl_defaults_core),
1128 X86_MATCH_INTEL_FAM6_MODEL(ICELAKE_X, &rapl_defaults_hsw_server),
1129 X86_MATCH_INTEL_FAM6_MODEL(ICELAKE_D, &rapl_defaults_hsw_server),
1130 X86_MATCH_INTEL_FAM6_MODEL(COMETLAKE_L, &rapl_defaults_core),
1131 X86_MATCH_INTEL_FAM6_MODEL(COMETLAKE, &rapl_defaults_core),
1132 X86_MATCH_INTEL_FAM6_MODEL(TIGERLAKE_L, &rapl_defaults_core),
1133 X86_MATCH_INTEL_FAM6_MODEL(TIGERLAKE, &rapl_defaults_core),
1134 X86_MATCH_INTEL_FAM6_MODEL(ROCKETLAKE, &rapl_defaults_core),
1135 X86_MATCH_INTEL_FAM6_MODEL(ALDERLAKE, &rapl_defaults_core),
1136 X86_MATCH_INTEL_FAM6_MODEL(ALDERLAKE_L, &rapl_defaults_core),
1137 X86_MATCH_INTEL_FAM6_MODEL(ALDERLAKE_N, &rapl_defaults_core),
1138 X86_MATCH_INTEL_FAM6_MODEL(RAPTORLAKE, &rapl_defaults_core),
1139 X86_MATCH_INTEL_FAM6_MODEL(RAPTORLAKE_P, &rapl_defaults_core),
1140 X86_MATCH_INTEL_FAM6_MODEL(RAPTORLAKE_S, &rapl_defaults_core),
1141 X86_MATCH_INTEL_FAM6_MODEL(METEORLAKE, &rapl_defaults_core),
1142 X86_MATCH_INTEL_FAM6_MODEL(METEORLAKE_L, &rapl_defaults_core),
1143 X86_MATCH_INTEL_FAM6_MODEL(SAPPHIRERAPIDS_X, &rapl_defaults_spr_server),
1144 X86_MATCH_INTEL_FAM6_MODEL(EMERALDRAPIDS_X, &rapl_defaults_spr_server),
1145 X86_MATCH_INTEL_FAM6_MODEL(LAKEFIELD, &rapl_defaults_core),
1147 X86_MATCH_INTEL_FAM6_MODEL(ATOM_SILVERMONT, &rapl_defaults_byt),
1148 X86_MATCH_INTEL_FAM6_MODEL(ATOM_AIRMONT, &rapl_defaults_cht),
1149 X86_MATCH_INTEL_FAM6_MODEL(ATOM_SILVERMONT_MID, &rapl_defaults_tng),
1150 X86_MATCH_INTEL_FAM6_MODEL(ATOM_AIRMONT_MID, &rapl_defaults_ann),
1151 X86_MATCH_INTEL_FAM6_MODEL(ATOM_GOLDMONT, &rapl_defaults_core),
1152 X86_MATCH_INTEL_FAM6_MODEL(ATOM_GOLDMONT_PLUS, &rapl_defaults_core),
1153 X86_MATCH_INTEL_FAM6_MODEL(ATOM_GOLDMONT_D, &rapl_defaults_core),
1154 X86_MATCH_INTEL_FAM6_MODEL(ATOM_TREMONT, &rapl_defaults_core),
1155 X86_MATCH_INTEL_FAM6_MODEL(ATOM_TREMONT_D, &rapl_defaults_core),
1156 X86_MATCH_INTEL_FAM6_MODEL(ATOM_TREMONT_L, &rapl_defaults_core),
1158 X86_MATCH_INTEL_FAM6_MODEL(XEON_PHI_KNL, &rapl_defaults_hsw_server),
1159 X86_MATCH_INTEL_FAM6_MODEL(XEON_PHI_KNM, &rapl_defaults_hsw_server),
1161 X86_MATCH_VENDOR_FAM(AMD, 0x17, &rapl_defaults_amd),
1162 X86_MATCH_VENDOR_FAM(AMD, 0x19, &rapl_defaults_amd),
1163 X86_MATCH_VENDOR_FAM(HYGON, 0x18, &rapl_defaults_amd),
1166 MODULE_DEVICE_TABLE(x86cpu, rapl_ids);
1168 /* Read once for all raw primitive data for domains */
1169 static void rapl_update_domain_data(struct rapl_package *rp)
1174 for (dmn = 0; dmn < rp->nr_domains; dmn++) {
1175 pr_debug("update %s domain %s data\n", rp->name,
1176 rp->domains[dmn].name);
1177 /* exclude non-raw primitives */
1178 for (prim = 0; prim < NR_RAW_PRIMITIVES; prim++) {
1179 if (!rapl_read_data_raw(&rp->domains[dmn], prim,
1180 rpi[prim].unit, &val))
1181 rp->domains[dmn].rdd.primitives[prim] = val;
1187 static int rapl_package_register_powercap(struct rapl_package *rp)
1189 struct rapl_domain *rd;
1190 struct powercap_zone *power_zone = NULL;
1193 /* Update the domain data of the new package */
1194 rapl_update_domain_data(rp);
1196 /* first we register package domain as the parent zone */
1197 for (rd = rp->domains; rd < rp->domains + rp->nr_domains; rd++) {
1198 if (rd->id == RAPL_DOMAIN_PACKAGE) {
1199 nr_pl = find_nr_power_limit(rd);
1200 pr_debug("register package domain %s\n", rp->name);
1201 power_zone = powercap_register_zone(&rd->power_zone,
1202 rp->priv->control_type, rp->name,
1203 NULL, &zone_ops[rd->id], nr_pl,
1205 if (IS_ERR(power_zone)) {
1206 pr_debug("failed to register power zone %s\n",
1208 return PTR_ERR(power_zone);
1210 /* track parent zone in per package/socket data */
1211 rp->power_zone = power_zone;
1212 /* done, only one package domain per socket */
1217 pr_err("no package domain found, unknown topology!\n");
1220 /* now register domains as children of the socket/package */
1221 for (rd = rp->domains; rd < rp->domains + rp->nr_domains; rd++) {
1222 struct powercap_zone *parent = rp->power_zone;
1224 if (rd->id == RAPL_DOMAIN_PACKAGE)
1226 if (rd->id == RAPL_DOMAIN_PLATFORM)
1228 /* number of power limits per domain varies */
1229 nr_pl = find_nr_power_limit(rd);
1230 power_zone = powercap_register_zone(&rd->power_zone,
1231 rp->priv->control_type,
1233 &zone_ops[rd->id], nr_pl,
1236 if (IS_ERR(power_zone)) {
1237 pr_debug("failed to register power_zone, %s:%s\n",
1238 rp->name, rd->name);
1239 ret = PTR_ERR(power_zone);
1247 * Clean up previously initialized domains within the package if we
1248 * failed after the first domain setup.
1250 while (--rd >= rp->domains) {
1251 pr_debug("unregister %s domain %s\n", rp->name, rd->name);
1252 powercap_unregister_zone(rp->priv->control_type,
1259 static int rapl_check_domain(int cpu, int domain, struct rapl_package *rp)
1261 struct reg_action ra;
1264 case RAPL_DOMAIN_PACKAGE:
1265 case RAPL_DOMAIN_PP0:
1266 case RAPL_DOMAIN_PP1:
1267 case RAPL_DOMAIN_DRAM:
1268 case RAPL_DOMAIN_PLATFORM:
1269 ra.reg = rp->priv->regs[domain][RAPL_DOMAIN_REG_STATUS];
1272 pr_err("invalid domain id %d\n", domain);
1275 /* make sure domain counters are available and contains non-zero
1276 * values, otherwise skip it.
1279 ra.mask = ENERGY_STATUS_MASK;
1280 if (rp->priv->read_raw(cpu, &ra) || !ra.value)
1287 * Check if power limits are available. Two cases when they are not available:
1288 * 1. Locked by BIOS, in this case we still provide read-only access so that
1289 * users can see what limit is set by the BIOS.
1290 * 2. Some CPUs make some domains monitoring only which means PLx MSRs may not
1291 * exist at all. In this case, we do not show the constraints in powercap.
1293 * Called after domains are detected and initialized.
1295 static void rapl_detect_powerlimit(struct rapl_domain *rd)
1300 /* check if the domain is locked by BIOS, ignore if MSR doesn't exist */
1301 if (!rapl_read_data_raw(rd, FW_LOCK, false, &val64)) {
1303 pr_info("RAPL %s domain %s locked by BIOS\n",
1304 rd->rp->name, rd->name);
1305 rd->state |= DOMAIN_STATE_BIOS_LOCKED;
1308 /* check if power limit MSR exists, otherwise domain is monitoring only */
1309 for (i = 0; i < NR_POWER_LIMITS; i++) {
1310 int prim = rd->rpl[i].prim_id;
1312 if (rapl_read_data_raw(rd, prim, false, &val64))
1313 rd->rpl[i].name = NULL;
1317 /* Detect active and valid domains for the given CPU, caller must
1318 * ensure the CPU belongs to the targeted package and CPU hotlug is disabled.
1320 static int rapl_detect_domains(struct rapl_package *rp, int cpu)
1322 struct rapl_domain *rd;
1325 for (i = 0; i < RAPL_DOMAIN_MAX; i++) {
1326 /* use physical package id to read counters */
1327 if (!rapl_check_domain(cpu, i, rp)) {
1328 rp->domain_map |= 1 << i;
1329 pr_info("Found RAPL domain %s\n", rapl_domain_names[i]);
1332 rp->nr_domains = bitmap_weight(&rp->domain_map, RAPL_DOMAIN_MAX);
1333 if (!rp->nr_domains) {
1334 pr_debug("no valid rapl domains found in %s\n", rp->name);
1337 pr_debug("found %d domains on %s\n", rp->nr_domains, rp->name);
1339 rp->domains = kcalloc(rp->nr_domains + 1, sizeof(struct rapl_domain),
1344 rapl_init_domains(rp);
1346 for (rd = rp->domains; rd < rp->domains + rp->nr_domains; rd++)
1347 rapl_detect_powerlimit(rd);
1352 /* called from CPU hotplug notifier, hotplug lock held */
1353 void rapl_remove_package(struct rapl_package *rp)
1355 struct rapl_domain *rd, *rd_package = NULL;
1357 package_power_limit_irq_restore(rp);
1359 for (rd = rp->domains; rd < rp->domains + rp->nr_domains; rd++) {
1360 rapl_write_data_raw(rd, PL1_ENABLE, 0);
1361 rapl_write_data_raw(rd, PL1_CLAMP, 0);
1362 if (find_nr_power_limit(rd) > 1) {
1363 rapl_write_data_raw(rd, PL2_ENABLE, 0);
1364 rapl_write_data_raw(rd, PL2_CLAMP, 0);
1365 rapl_write_data_raw(rd, PL4_ENABLE, 0);
1367 if (rd->id == RAPL_DOMAIN_PACKAGE) {
1371 pr_debug("remove package, undo power limit on %s: %s\n",
1372 rp->name, rd->name);
1373 powercap_unregister_zone(rp->priv->control_type,
1376 /* do parent zone last */
1377 powercap_unregister_zone(rp->priv->control_type,
1378 &rd_package->power_zone);
1379 list_del(&rp->plist);
1382 EXPORT_SYMBOL_GPL(rapl_remove_package);
1384 /* caller to ensure CPU hotplug lock is held */
1385 struct rapl_package *rapl_find_package_domain(int cpu, struct rapl_if_priv *priv)
1387 int id = topology_logical_die_id(cpu);
1388 struct rapl_package *rp;
1390 list_for_each_entry(rp, &rapl_packages, plist) {
1392 && rp->priv->control_type == priv->control_type)
1398 EXPORT_SYMBOL_GPL(rapl_find_package_domain);
1400 /* called from CPU hotplug notifier, hotplug lock held */
1401 struct rapl_package *rapl_add_package(int cpu, struct rapl_if_priv *priv)
1403 int id = topology_logical_die_id(cpu);
1404 struct rapl_package *rp;
1405 struct rapl_defaults *defaults;
1408 rp = kzalloc(sizeof(struct rapl_package), GFP_KERNEL);
1410 return ERR_PTR(-ENOMEM);
1412 /* add the new package to the list */
1417 ret = rapl_config(rp);
1419 goto err_free_package;
1421 if (topology_max_die_per_package() > 1)
1422 snprintf(rp->name, PACKAGE_DOMAIN_NAME_LENGTH,
1423 "package-%d-die-%d",
1424 topology_physical_package_id(cpu), topology_die_id(cpu));
1426 snprintf(rp->name, PACKAGE_DOMAIN_NAME_LENGTH, "package-%d",
1427 topology_physical_package_id(cpu));
1429 defaults = get_defaults(rp);
1430 /* check if the package contains valid domains */
1431 if (rapl_detect_domains(rp, cpu) || defaults->check_unit(rp, cpu)) {
1433 goto err_free_package;
1435 ret = rapl_package_register_powercap(rp);
1437 INIT_LIST_HEAD(&rp->plist);
1438 list_add(&rp->plist, &rapl_packages);
1445 return ERR_PTR(ret);
1447 EXPORT_SYMBOL_GPL(rapl_add_package);
1449 static void power_limit_state_save(void)
1451 struct rapl_package *rp;
1452 struct rapl_domain *rd;
1456 list_for_each_entry(rp, &rapl_packages, plist) {
1457 if (!rp->power_zone)
1459 rd = power_zone_to_rapl_domain(rp->power_zone);
1460 nr_pl = find_nr_power_limit(rd);
1461 for (i = 0; i < nr_pl; i++) {
1462 switch (rd->rpl[i].prim_id) {
1464 ret = rapl_read_data_raw(rd,
1466 &rd->rpl[i].last_power_limit);
1468 rd->rpl[i].last_power_limit = 0;
1471 ret = rapl_read_data_raw(rd,
1473 &rd->rpl[i].last_power_limit);
1475 rd->rpl[i].last_power_limit = 0;
1478 ret = rapl_read_data_raw(rd,
1480 &rd->rpl[i].last_power_limit);
1482 rd->rpl[i].last_power_limit = 0;
1490 static void power_limit_state_restore(void)
1492 struct rapl_package *rp;
1493 struct rapl_domain *rd;
1497 list_for_each_entry(rp, &rapl_packages, plist) {
1498 if (!rp->power_zone)
1500 rd = power_zone_to_rapl_domain(rp->power_zone);
1501 nr_pl = find_nr_power_limit(rd);
1502 for (i = 0; i < nr_pl; i++) {
1503 switch (rd->rpl[i].prim_id) {
1505 if (rd->rpl[i].last_power_limit)
1506 rapl_write_data_raw(rd, POWER_LIMIT1,
1507 rd->rpl[i].last_power_limit);
1510 if (rd->rpl[i].last_power_limit)
1511 rapl_write_data_raw(rd, POWER_LIMIT2,
1512 rd->rpl[i].last_power_limit);
1515 if (rd->rpl[i].last_power_limit)
1516 rapl_write_data_raw(rd, POWER_LIMIT4,
1517 rd->rpl[i].last_power_limit);
1525 static int rapl_pm_callback(struct notifier_block *nb,
1526 unsigned long mode, void *_unused)
1529 case PM_SUSPEND_PREPARE:
1530 power_limit_state_save();
1532 case PM_POST_SUSPEND:
1533 power_limit_state_restore();
1539 static struct notifier_block rapl_pm_notifier = {
1540 .notifier_call = rapl_pm_callback,
1543 static struct platform_device *rapl_msr_platdev;
1545 static int __init rapl_init(void)
1547 const struct x86_cpu_id *id;
1550 id = x86_match_cpu(rapl_ids);
1552 rapl_defaults = (struct rapl_defaults *)id->driver_data;
1554 rapl_msr_platdev = platform_device_alloc("intel_rapl_msr", 0);
1555 if (!rapl_msr_platdev)
1558 ret = platform_device_add(rapl_msr_platdev);
1560 platform_device_put(rapl_msr_platdev);
1565 ret = register_pm_notifier(&rapl_pm_notifier);
1566 if (ret && rapl_msr_platdev) {
1567 platform_device_del(rapl_msr_platdev);
1568 platform_device_put(rapl_msr_platdev);
1574 static void __exit rapl_exit(void)
1576 platform_device_unregister(rapl_msr_platdev);
1577 unregister_pm_notifier(&rapl_pm_notifier);
1580 fs_initcall(rapl_init);
1581 module_exit(rapl_exit);
1583 MODULE_DESCRIPTION("Intel Runtime Average Power Limit (RAPL) common code");
1584 MODULE_AUTHOR("Jacob Pan <jacob.jun.pan@intel.com>");
1585 MODULE_LICENSE("GPL v2");