2 * Intel Running Average Power Limit (RAPL) Driver
3 * Copyright (c) 2013, Intel Corporation.
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
9 * This program is distributed in the hope it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * You should have received a copy of the GNU General Public License along with
15 * this program; if not, write to the Free Software Foundation, Inc.
18 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
20 #include <linux/kernel.h>
21 #include <linux/module.h>
22 #include <linux/list.h>
23 #include <linux/types.h>
24 #include <linux/device.h>
25 #include <linux/slab.h>
26 #include <linux/log2.h>
27 #include <linux/bitmap.h>
28 #include <linux/delay.h>
29 #include <linux/sysfs.h>
30 #include <linux/cpu.h>
31 #include <linux/powercap.h>
32 #include <linux/suspend.h>
33 #include <asm/iosf_mbi.h>
35 #include <asm/processor.h>
36 #include <asm/cpu_device_id.h>
37 #include <asm/intel-family.h>
40 #define MSR_PLATFORM_POWER_LIMIT 0x0000065C
42 /* bitmasks for RAPL MSRs, used by primitive access functions */
43 #define ENERGY_STATUS_MASK 0xffffffff
45 #define POWER_LIMIT1_MASK 0x7FFF
46 #define POWER_LIMIT1_ENABLE BIT(15)
47 #define POWER_LIMIT1_CLAMP BIT(16)
49 #define POWER_LIMIT2_MASK (0x7FFFULL<<32)
50 #define POWER_LIMIT2_ENABLE BIT_ULL(47)
51 #define POWER_LIMIT2_CLAMP BIT_ULL(48)
52 #define POWER_PACKAGE_LOCK BIT_ULL(63)
53 #define POWER_PP_LOCK BIT(31)
55 #define TIME_WINDOW1_MASK (0x7FULL<<17)
56 #define TIME_WINDOW2_MASK (0x7FULL<<49)
58 #define POWER_UNIT_OFFSET 0
59 #define POWER_UNIT_MASK 0x0F
61 #define ENERGY_UNIT_OFFSET 0x08
62 #define ENERGY_UNIT_MASK 0x1F00
64 #define TIME_UNIT_OFFSET 0x10
65 #define TIME_UNIT_MASK 0xF0000
67 #define POWER_INFO_MAX_MASK (0x7fffULL<<32)
68 #define POWER_INFO_MIN_MASK (0x7fffULL<<16)
69 #define POWER_INFO_MAX_TIME_WIN_MASK (0x3fULL<<48)
70 #define POWER_INFO_THERMAL_SPEC_MASK 0x7fff
72 #define PERF_STATUS_THROTTLE_TIME_MASK 0xffffffff
73 #define PP_POLICY_MASK 0x1F
75 /* Non HW constants */
76 #define RAPL_PRIMITIVE_DERIVED BIT(1) /* not from raw data */
77 #define RAPL_PRIMITIVE_DUMMY BIT(2)
79 #define TIME_WINDOW_MAX_MSEC 40000
80 #define TIME_WINDOW_MIN_MSEC 250
81 #define ENERGY_UNIT_SCALE 1000 /* scale from driver unit to powercap unit */
83 ARBITRARY_UNIT, /* no translation */
89 enum rapl_domain_type {
90 RAPL_DOMAIN_PACKAGE, /* entire package/socket */
91 RAPL_DOMAIN_PP0, /* core power plane */
92 RAPL_DOMAIN_PP1, /* graphics uncore */
93 RAPL_DOMAIN_DRAM,/* DRAM control_type */
94 RAPL_DOMAIN_PLATFORM, /* PSys control_type */
98 enum rapl_domain_msr_id {
99 RAPL_DOMAIN_MSR_LIMIT,
100 RAPL_DOMAIN_MSR_STATUS,
101 RAPL_DOMAIN_MSR_PERF,
102 RAPL_DOMAIN_MSR_POLICY,
103 RAPL_DOMAIN_MSR_INFO,
107 /* per domain data, some are optional */
108 enum rapl_primitives {
114 PL1_ENABLE, /* power limit 1, aka long term */
115 PL1_CLAMP, /* allow frequency to go below OS request */
116 PL2_ENABLE, /* power limit 2, aka short term, instantaneous */
119 TIME_WINDOW1, /* long term */
120 TIME_WINDOW2, /* short term */
129 /* below are not raw primitive data */
134 #define NR_RAW_PRIMITIVES (NR_RAPL_PRIMITIVES - 2)
136 /* Can be expanded to include events, etc.*/
137 struct rapl_domain_data {
138 u64 primitives[NR_RAPL_PRIMITIVES];
139 unsigned long timestamp;
149 #define DOMAIN_STATE_INACTIVE BIT(0)
150 #define DOMAIN_STATE_POWER_LIMIT_SET BIT(1)
151 #define DOMAIN_STATE_BIOS_LOCKED BIT(2)
153 #define NR_POWER_LIMITS (2)
154 struct rapl_power_limit {
155 struct powercap_zone_constraint *constraint;
156 int prim_id; /* primitive ID used to enable */
157 struct rapl_domain *domain;
159 u64 last_power_limit;
162 static const char pl1_name[] = "long_term";
163 static const char pl2_name[] = "short_term";
168 enum rapl_domain_type id;
169 int msrs[RAPL_DOMAIN_MSR_MAX];
170 struct powercap_zone power_zone;
171 struct rapl_domain_data rdd;
172 struct rapl_power_limit rpl[NR_POWER_LIMITS];
173 u64 attr_map; /* track capabilities */
175 unsigned int domain_energy_unit;
176 struct rapl_package *rp;
178 #define power_zone_to_rapl_domain(_zone) \
179 container_of(_zone, struct rapl_domain, power_zone)
182 /* Each physical package contains multiple domains, these are the common
183 * data across RAPL domains within a package.
185 struct rapl_package {
186 unsigned int id; /* physical package/socket id */
187 unsigned int nr_domains;
188 unsigned long domain_map; /* bit map of active domains */
189 unsigned int power_unit;
190 unsigned int energy_unit;
191 unsigned int time_unit;
192 struct rapl_domain *domains; /* array of domains, sized at runtime */
193 struct powercap_zone *power_zone; /* keep track of parent zone */
194 unsigned long power_limit_irq; /* keep track of package power limit
195 * notify interrupt enable status.
197 struct list_head plist;
198 int lead_cpu; /* one active cpu per package for access */
199 /* Track active cpus */
200 struct cpumask cpumask;
203 struct rapl_defaults {
204 u8 floor_freq_reg_addr;
205 int (*check_unit)(struct rapl_package *rp, int cpu);
206 void (*set_floor_freq)(struct rapl_domain *rd, bool mode);
207 u64 (*compute_time_window)(struct rapl_package *rp, u64 val,
209 unsigned int dram_domain_energy_unit;
211 static struct rapl_defaults *rapl_defaults;
213 /* Sideband MBI registers */
214 #define IOSF_CPU_POWER_BUDGET_CTL_BYT (0x2)
215 #define IOSF_CPU_POWER_BUDGET_CTL_TNG (0xdf)
217 #define PACKAGE_PLN_INT_SAVED BIT(0)
218 #define MAX_PRIM_NAME (32)
220 /* per domain data. used to describe individual knobs such that access function
221 * can be consolidated into one instead of many inline functions.
223 struct rapl_primitive_info {
227 enum rapl_domain_msr_id id;
232 #define PRIMITIVE_INFO_INIT(p, m, s, i, u, f) { \
241 static void rapl_init_domains(struct rapl_package *rp);
242 static int rapl_read_data_raw(struct rapl_domain *rd,
243 enum rapl_primitives prim,
244 bool xlate, u64 *data);
245 static int rapl_write_data_raw(struct rapl_domain *rd,
246 enum rapl_primitives prim,
247 unsigned long long value);
248 static u64 rapl_unit_xlate(struct rapl_domain *rd,
249 enum unit_type type, u64 value,
251 static void package_power_limit_irq_save(struct rapl_package *rp);
253 static LIST_HEAD(rapl_packages); /* guarded by CPU hotplug lock */
255 static const char * const rapl_domain_names[] = {
263 static struct powercap_control_type *control_type; /* PowerCap Controller */
264 static struct rapl_domain *platform_rapl_domain; /* Platform (PSys) domain */
266 /* caller to ensure CPU hotplug lock is held */
267 static struct rapl_package *rapl_find_package_domain(int cpu)
269 int id = topology_logical_die_id(cpu);
270 struct rapl_package *rp;
272 list_for_each_entry(rp, &rapl_packages, plist) {
280 static int get_energy_counter(struct powercap_zone *power_zone, u64 *energy_raw)
282 struct rapl_domain *rd;
285 /* prevent CPU hotplug, make sure the RAPL domain does not go
286 * away while reading the counter.
289 rd = power_zone_to_rapl_domain(power_zone);
291 if (!rapl_read_data_raw(rd, ENERGY_COUNTER, true, &energy_now)) {
292 *energy_raw = energy_now;
302 static int get_max_energy_counter(struct powercap_zone *pcd_dev, u64 *energy)
304 struct rapl_domain *rd = power_zone_to_rapl_domain(pcd_dev);
306 *energy = rapl_unit_xlate(rd, ENERGY_UNIT, ENERGY_STATUS_MASK, 0);
310 static int release_zone(struct powercap_zone *power_zone)
312 struct rapl_domain *rd = power_zone_to_rapl_domain(power_zone);
313 struct rapl_package *rp = rd->rp;
315 /* package zone is the last zone of a package, we can free
316 * memory here since all children has been unregistered.
318 if (rd->id == RAPL_DOMAIN_PACKAGE) {
327 static int find_nr_power_limit(struct rapl_domain *rd)
331 for (i = 0; i < NR_POWER_LIMITS; i++) {
339 static int set_domain_enable(struct powercap_zone *power_zone, bool mode)
341 struct rapl_domain *rd = power_zone_to_rapl_domain(power_zone);
343 if (rd->state & DOMAIN_STATE_BIOS_LOCKED)
347 rapl_write_data_raw(rd, PL1_ENABLE, mode);
348 if (rapl_defaults->set_floor_freq)
349 rapl_defaults->set_floor_freq(rd, mode);
355 static int get_domain_enable(struct powercap_zone *power_zone, bool *mode)
357 struct rapl_domain *rd = power_zone_to_rapl_domain(power_zone);
360 if (rd->state & DOMAIN_STATE_BIOS_LOCKED) {
365 if (rapl_read_data_raw(rd, PL1_ENABLE, true, &val)) {
375 /* per RAPL domain ops, in the order of rapl_domain_type */
376 static const struct powercap_zone_ops zone_ops[] = {
377 /* RAPL_DOMAIN_PACKAGE */
379 .get_energy_uj = get_energy_counter,
380 .get_max_energy_range_uj = get_max_energy_counter,
381 .release = release_zone,
382 .set_enable = set_domain_enable,
383 .get_enable = get_domain_enable,
385 /* RAPL_DOMAIN_PP0 */
387 .get_energy_uj = get_energy_counter,
388 .get_max_energy_range_uj = get_max_energy_counter,
389 .release = release_zone,
390 .set_enable = set_domain_enable,
391 .get_enable = get_domain_enable,
393 /* RAPL_DOMAIN_PP1 */
395 .get_energy_uj = get_energy_counter,
396 .get_max_energy_range_uj = get_max_energy_counter,
397 .release = release_zone,
398 .set_enable = set_domain_enable,
399 .get_enable = get_domain_enable,
401 /* RAPL_DOMAIN_DRAM */
403 .get_energy_uj = get_energy_counter,
404 .get_max_energy_range_uj = get_max_energy_counter,
405 .release = release_zone,
406 .set_enable = set_domain_enable,
407 .get_enable = get_domain_enable,
409 /* RAPL_DOMAIN_PLATFORM */
411 .get_energy_uj = get_energy_counter,
412 .get_max_energy_range_uj = get_max_energy_counter,
413 .release = release_zone,
414 .set_enable = set_domain_enable,
415 .get_enable = get_domain_enable,
421 * Constraint index used by powercap can be different than power limit (PL)
422 * index in that some PLs maybe missing due to non-existant MSRs. So we
423 * need to convert here by finding the valid PLs only (name populated).
425 static int contraint_to_pl(struct rapl_domain *rd, int cid)
429 for (i = 0, j = 0; i < NR_POWER_LIMITS; i++) {
430 if ((rd->rpl[i].name) && j++ == cid) {
431 pr_debug("%s: index %d\n", __func__, i);
435 pr_err("Cannot find matching power limit for constraint %d\n", cid);
440 static int set_power_limit(struct powercap_zone *power_zone, int cid,
443 struct rapl_domain *rd;
444 struct rapl_package *rp;
449 rd = power_zone_to_rapl_domain(power_zone);
450 id = contraint_to_pl(rd, cid);
458 if (rd->state & DOMAIN_STATE_BIOS_LOCKED) {
459 dev_warn(&power_zone->dev, "%s locked by BIOS, monitoring only\n",
465 switch (rd->rpl[id].prim_id) {
467 rapl_write_data_raw(rd, POWER_LIMIT1, power_limit);
470 rapl_write_data_raw(rd, POWER_LIMIT2, power_limit);
476 package_power_limit_irq_save(rp);
482 static int get_current_power_limit(struct powercap_zone *power_zone, int cid,
485 struct rapl_domain *rd;
492 rd = power_zone_to_rapl_domain(power_zone);
493 id = contraint_to_pl(rd, cid);
499 switch (rd->rpl[id].prim_id) {
510 if (rapl_read_data_raw(rd, prim, true, &val))
521 static int set_time_window(struct powercap_zone *power_zone, int cid,
524 struct rapl_domain *rd;
529 rd = power_zone_to_rapl_domain(power_zone);
530 id = contraint_to_pl(rd, cid);
536 switch (rd->rpl[id].prim_id) {
538 rapl_write_data_raw(rd, TIME_WINDOW1, window);
541 rapl_write_data_raw(rd, TIME_WINDOW2, window);
552 static int get_time_window(struct powercap_zone *power_zone, int cid, u64 *data)
554 struct rapl_domain *rd;
560 rd = power_zone_to_rapl_domain(power_zone);
561 id = contraint_to_pl(rd, cid);
567 switch (rd->rpl[id].prim_id) {
569 ret = rapl_read_data_raw(rd, TIME_WINDOW1, true, &val);
572 ret = rapl_read_data_raw(rd, TIME_WINDOW2, true, &val);
587 static const char *get_constraint_name(struct powercap_zone *power_zone, int cid)
589 struct rapl_domain *rd;
592 rd = power_zone_to_rapl_domain(power_zone);
593 id = contraint_to_pl(rd, cid);
595 return rd->rpl[id].name;
601 static int get_max_power(struct powercap_zone *power_zone, int id,
604 struct rapl_domain *rd;
610 rd = power_zone_to_rapl_domain(power_zone);
611 switch (rd->rpl[id].prim_id) {
613 prim = THERMAL_SPEC_POWER;
622 if (rapl_read_data_raw(rd, prim, true, &val))
632 static const struct powercap_zone_constraint_ops constraint_ops = {
633 .set_power_limit_uw = set_power_limit,
634 .get_power_limit_uw = get_current_power_limit,
635 .set_time_window_us = set_time_window,
636 .get_time_window_us = get_time_window,
637 .get_max_power_uw = get_max_power,
638 .get_name = get_constraint_name,
641 /* called after domain detection and package level data are set */
642 static void rapl_init_domains(struct rapl_package *rp)
645 struct rapl_domain *rd = rp->domains;
647 for (i = 0; i < RAPL_DOMAIN_MAX; i++) {
648 unsigned int mask = rp->domain_map & (1 << i);
650 case BIT(RAPL_DOMAIN_PACKAGE):
651 rd->name = rapl_domain_names[RAPL_DOMAIN_PACKAGE];
652 rd->id = RAPL_DOMAIN_PACKAGE;
653 rd->msrs[0] = MSR_PKG_POWER_LIMIT;
654 rd->msrs[1] = MSR_PKG_ENERGY_STATUS;
655 rd->msrs[2] = MSR_PKG_PERF_STATUS;
657 rd->msrs[4] = MSR_PKG_POWER_INFO;
658 rd->rpl[0].prim_id = PL1_ENABLE;
659 rd->rpl[0].name = pl1_name;
660 rd->rpl[1].prim_id = PL2_ENABLE;
661 rd->rpl[1].name = pl2_name;
663 case BIT(RAPL_DOMAIN_PP0):
664 rd->name = rapl_domain_names[RAPL_DOMAIN_PP0];
665 rd->id = RAPL_DOMAIN_PP0;
666 rd->msrs[0] = MSR_PP0_POWER_LIMIT;
667 rd->msrs[1] = MSR_PP0_ENERGY_STATUS;
669 rd->msrs[3] = MSR_PP0_POLICY;
671 rd->rpl[0].prim_id = PL1_ENABLE;
672 rd->rpl[0].name = pl1_name;
674 case BIT(RAPL_DOMAIN_PP1):
675 rd->name = rapl_domain_names[RAPL_DOMAIN_PP1];
676 rd->id = RAPL_DOMAIN_PP1;
677 rd->msrs[0] = MSR_PP1_POWER_LIMIT;
678 rd->msrs[1] = MSR_PP1_ENERGY_STATUS;
680 rd->msrs[3] = MSR_PP1_POLICY;
682 rd->rpl[0].prim_id = PL1_ENABLE;
683 rd->rpl[0].name = pl1_name;
685 case BIT(RAPL_DOMAIN_DRAM):
686 rd->name = rapl_domain_names[RAPL_DOMAIN_DRAM];
687 rd->id = RAPL_DOMAIN_DRAM;
688 rd->msrs[0] = MSR_DRAM_POWER_LIMIT;
689 rd->msrs[1] = MSR_DRAM_ENERGY_STATUS;
690 rd->msrs[2] = MSR_DRAM_PERF_STATUS;
692 rd->msrs[4] = MSR_DRAM_POWER_INFO;
693 rd->rpl[0].prim_id = PL1_ENABLE;
694 rd->rpl[0].name = pl1_name;
695 rd->domain_energy_unit =
696 rapl_defaults->dram_domain_energy_unit;
697 if (rd->domain_energy_unit)
698 pr_info("DRAM domain energy unit %dpj\n",
699 rd->domain_energy_unit);
709 static u64 rapl_unit_xlate(struct rapl_domain *rd, enum unit_type type,
710 u64 value, int to_raw)
713 struct rapl_package *rp = rd->rp;
718 units = rp->power_unit;
721 scale = ENERGY_UNIT_SCALE;
722 /* per domain unit takes precedence */
723 if (rd->domain_energy_unit)
724 units = rd->domain_energy_unit;
726 units = rp->energy_unit;
729 return rapl_defaults->compute_time_window(rp, value, to_raw);
736 return div64_u64(value, units) * scale;
740 return div64_u64(value, scale);
743 /* in the order of enum rapl_primitives */
744 static struct rapl_primitive_info rpi[] = {
745 /* name, mask, shift, msr index, unit divisor */
746 PRIMITIVE_INFO_INIT(ENERGY_COUNTER, ENERGY_STATUS_MASK, 0,
747 RAPL_DOMAIN_MSR_STATUS, ENERGY_UNIT, 0),
748 PRIMITIVE_INFO_INIT(POWER_LIMIT1, POWER_LIMIT1_MASK, 0,
749 RAPL_DOMAIN_MSR_LIMIT, POWER_UNIT, 0),
750 PRIMITIVE_INFO_INIT(POWER_LIMIT2, POWER_LIMIT2_MASK, 32,
751 RAPL_DOMAIN_MSR_LIMIT, POWER_UNIT, 0),
752 PRIMITIVE_INFO_INIT(FW_LOCK, POWER_PP_LOCK, 31,
753 RAPL_DOMAIN_MSR_LIMIT, ARBITRARY_UNIT, 0),
754 PRIMITIVE_INFO_INIT(PL1_ENABLE, POWER_LIMIT1_ENABLE, 15,
755 RAPL_DOMAIN_MSR_LIMIT, ARBITRARY_UNIT, 0),
756 PRIMITIVE_INFO_INIT(PL1_CLAMP, POWER_LIMIT1_CLAMP, 16,
757 RAPL_DOMAIN_MSR_LIMIT, ARBITRARY_UNIT, 0),
758 PRIMITIVE_INFO_INIT(PL2_ENABLE, POWER_LIMIT2_ENABLE, 47,
759 RAPL_DOMAIN_MSR_LIMIT, ARBITRARY_UNIT, 0),
760 PRIMITIVE_INFO_INIT(PL2_CLAMP, POWER_LIMIT2_CLAMP, 48,
761 RAPL_DOMAIN_MSR_LIMIT, ARBITRARY_UNIT, 0),
762 PRIMITIVE_INFO_INIT(TIME_WINDOW1, TIME_WINDOW1_MASK, 17,
763 RAPL_DOMAIN_MSR_LIMIT, TIME_UNIT, 0),
764 PRIMITIVE_INFO_INIT(TIME_WINDOW2, TIME_WINDOW2_MASK, 49,
765 RAPL_DOMAIN_MSR_LIMIT, TIME_UNIT, 0),
766 PRIMITIVE_INFO_INIT(THERMAL_SPEC_POWER, POWER_INFO_THERMAL_SPEC_MASK,
767 0, RAPL_DOMAIN_MSR_INFO, POWER_UNIT, 0),
768 PRIMITIVE_INFO_INIT(MAX_POWER, POWER_INFO_MAX_MASK, 32,
769 RAPL_DOMAIN_MSR_INFO, POWER_UNIT, 0),
770 PRIMITIVE_INFO_INIT(MIN_POWER, POWER_INFO_MIN_MASK, 16,
771 RAPL_DOMAIN_MSR_INFO, POWER_UNIT, 0),
772 PRIMITIVE_INFO_INIT(MAX_TIME_WINDOW, POWER_INFO_MAX_TIME_WIN_MASK, 48,
773 RAPL_DOMAIN_MSR_INFO, TIME_UNIT, 0),
774 PRIMITIVE_INFO_INIT(THROTTLED_TIME, PERF_STATUS_THROTTLE_TIME_MASK, 0,
775 RAPL_DOMAIN_MSR_PERF, TIME_UNIT, 0),
776 PRIMITIVE_INFO_INIT(PRIORITY_LEVEL, PP_POLICY_MASK, 0,
777 RAPL_DOMAIN_MSR_POLICY, ARBITRARY_UNIT, 0),
779 PRIMITIVE_INFO_INIT(AVERAGE_POWER, 0, 0, 0, POWER_UNIT,
780 RAPL_PRIMITIVE_DERIVED),
784 /* Read primitive data based on its related struct rapl_primitive_info.
785 * if xlate flag is set, return translated data based on data units, i.e.
786 * time, energy, and power.
787 * RAPL MSRs are non-architectual and are laid out not consistently across
788 * domains. Here we use primitive info to allow writing consolidated access
790 * For a given primitive, it is processed by MSR mask and shift. Unit conversion
791 * is pre-assigned based on RAPL unit MSRs read at init time.
792 * 63-------------------------- 31--------------------------- 0
794 * | |<- shift ----------------|
795 * 63-------------------------- 31--------------------------- 0
797 static int rapl_read_data_raw(struct rapl_domain *rd,
798 enum rapl_primitives prim,
799 bool xlate, u64 *data)
803 struct rapl_primitive_info *rp = &rpi[prim];
806 if (!rp->name || rp->flag & RAPL_PRIMITIVE_DUMMY)
809 msr = rd->msrs[rp->id];
813 cpu = rd->rp->lead_cpu;
815 /* special-case package domain, which uses a different bit*/
816 if (prim == FW_LOCK && rd->id == RAPL_DOMAIN_PACKAGE) {
817 rp->mask = POWER_PACKAGE_LOCK;
820 /* non-hardware data are collected by the polling thread */
821 if (rp->flag & RAPL_PRIMITIVE_DERIVED) {
822 *data = rd->rdd.primitives[prim];
826 if (rdmsrl_safe_on_cpu(cpu, msr, &value)) {
827 pr_debug("failed to read msr 0x%x on cpu %d\n", msr, cpu);
831 final = value & rp->mask;
832 final = final >> rp->shift;
834 *data = rapl_unit_xlate(rd, rp->unit, final, 0);
842 static int msrl_update_safe(u32 msr_no, u64 clear_mask, u64 set_mask)
847 err = rdmsrl_safe(msr_no, &val);
854 err = wrmsrl_safe(msr_no, val);
860 static void msrl_update_func(void *info)
862 struct msrl_action *ma = info;
864 ma->err = msrl_update_safe(ma->msr_no, ma->clear_mask, ma->set_mask);
867 /* Similar use of primitive info in the read counterpart */
868 static int rapl_write_data_raw(struct rapl_domain *rd,
869 enum rapl_primitives prim,
870 unsigned long long value)
872 struct rapl_primitive_info *rp = &rpi[prim];
875 struct msrl_action ma;
878 cpu = rd->rp->lead_cpu;
879 bits = rapl_unit_xlate(rd, rp->unit, value, 1);
883 memset(&ma, 0, sizeof(ma));
885 ma.msr_no = rd->msrs[rp->id];
886 ma.clear_mask = rp->mask;
889 ret = smp_call_function_single(cpu, msrl_update_func, &ma, 1);
899 * Raw RAPL data stored in MSRs are in certain scales. We need to
900 * convert them into standard units based on the units reported in
901 * the RAPL unit MSRs. This is specific to CPUs as the method to
902 * calculate units differ on different CPUs.
903 * We convert the units to below format based on CPUs.
905 * energy unit: picoJoules : Represented in picoJoules by default
906 * power unit : microWatts : Represented in milliWatts by default
907 * time unit : microseconds: Represented in seconds by default
909 static int rapl_check_unit_core(struct rapl_package *rp, int cpu)
914 if (rdmsrl_safe_on_cpu(cpu, MSR_RAPL_POWER_UNIT, &msr_val)) {
915 pr_err("Failed to read power unit MSR 0x%x on CPU %d, exit.\n",
916 MSR_RAPL_POWER_UNIT, cpu);
920 value = (msr_val & ENERGY_UNIT_MASK) >> ENERGY_UNIT_OFFSET;
921 rp->energy_unit = ENERGY_UNIT_SCALE * 1000000 / (1 << value);
923 value = (msr_val & POWER_UNIT_MASK) >> POWER_UNIT_OFFSET;
924 rp->power_unit = 1000000 / (1 << value);
926 value = (msr_val & TIME_UNIT_MASK) >> TIME_UNIT_OFFSET;
927 rp->time_unit = 1000000 / (1 << value);
929 pr_debug("Core CPU package %d energy=%dpJ, time=%dus, power=%duW\n",
930 rp->id, rp->energy_unit, rp->time_unit, rp->power_unit);
935 static int rapl_check_unit_atom(struct rapl_package *rp, int cpu)
940 if (rdmsrl_safe_on_cpu(cpu, MSR_RAPL_POWER_UNIT, &msr_val)) {
941 pr_err("Failed to read power unit MSR 0x%x on CPU %d, exit.\n",
942 MSR_RAPL_POWER_UNIT, cpu);
945 value = (msr_val & ENERGY_UNIT_MASK) >> ENERGY_UNIT_OFFSET;
946 rp->energy_unit = ENERGY_UNIT_SCALE * 1 << value;
948 value = (msr_val & POWER_UNIT_MASK) >> POWER_UNIT_OFFSET;
949 rp->power_unit = (1 << value) * 1000;
951 value = (msr_val & TIME_UNIT_MASK) >> TIME_UNIT_OFFSET;
952 rp->time_unit = 1000000 / (1 << value);
954 pr_debug("Atom package %d energy=%dpJ, time=%dus, power=%duW\n",
955 rp->id, rp->energy_unit, rp->time_unit, rp->power_unit);
960 static void power_limit_irq_save_cpu(void *info)
963 struct rapl_package *rp = (struct rapl_package *)info;
965 /* save the state of PLN irq mask bit before disabling it */
966 rdmsr_safe(MSR_IA32_PACKAGE_THERM_INTERRUPT, &l, &h);
967 if (!(rp->power_limit_irq & PACKAGE_PLN_INT_SAVED)) {
968 rp->power_limit_irq = l & PACKAGE_THERM_INT_PLN_ENABLE;
969 rp->power_limit_irq |= PACKAGE_PLN_INT_SAVED;
971 l &= ~PACKAGE_THERM_INT_PLN_ENABLE;
972 wrmsr_safe(MSR_IA32_PACKAGE_THERM_INTERRUPT, l, h);
977 * When package power limit is set artificially low by RAPL, LVT
978 * thermal interrupt for package power limit should be ignored
979 * since we are not really exceeding the real limit. The intention
980 * is to avoid excessive interrupts while we are trying to save power.
981 * A useful feature might be routing the package_power_limit interrupt
982 * to userspace via eventfd. once we have a usecase, this is simple
983 * to do by adding an atomic notifier.
986 static void package_power_limit_irq_save(struct rapl_package *rp)
988 if (!boot_cpu_has(X86_FEATURE_PTS) || !boot_cpu_has(X86_FEATURE_PLN))
991 smp_call_function_single(rp->lead_cpu, power_limit_irq_save_cpu, rp, 1);
995 * Restore per package power limit interrupt enable state. Called from cpu
996 * hotplug code on package removal.
998 static void package_power_limit_irq_restore(struct rapl_package *rp)
1002 if (!boot_cpu_has(X86_FEATURE_PTS) || !boot_cpu_has(X86_FEATURE_PLN))
1005 /* irq enable state not saved, nothing to restore */
1006 if (!(rp->power_limit_irq & PACKAGE_PLN_INT_SAVED))
1009 rdmsr_safe(MSR_IA32_PACKAGE_THERM_INTERRUPT, &l, &h);
1011 if (rp->power_limit_irq & PACKAGE_THERM_INT_PLN_ENABLE)
1012 l |= PACKAGE_THERM_INT_PLN_ENABLE;
1014 l &= ~PACKAGE_THERM_INT_PLN_ENABLE;
1016 wrmsr_safe(MSR_IA32_PACKAGE_THERM_INTERRUPT, l, h);
1019 static void set_floor_freq_default(struct rapl_domain *rd, bool mode)
1021 int nr_powerlimit = find_nr_power_limit(rd);
1023 /* always enable clamp such that p-state can go below OS requested
1024 * range. power capping priority over guranteed frequency.
1026 rapl_write_data_raw(rd, PL1_CLAMP, mode);
1028 /* some domains have pl2 */
1029 if (nr_powerlimit > 1) {
1030 rapl_write_data_raw(rd, PL2_ENABLE, mode);
1031 rapl_write_data_raw(rd, PL2_CLAMP, mode);
1035 static void set_floor_freq_atom(struct rapl_domain *rd, bool enable)
1037 static u32 power_ctrl_orig_val;
1040 if (!rapl_defaults->floor_freq_reg_addr) {
1041 pr_err("Invalid floor frequency config register\n");
1045 if (!power_ctrl_orig_val)
1046 iosf_mbi_read(BT_MBI_UNIT_PMC, MBI_CR_READ,
1047 rapl_defaults->floor_freq_reg_addr,
1048 &power_ctrl_orig_val);
1049 mdata = power_ctrl_orig_val;
1051 mdata &= ~(0x7f << 8);
1054 iosf_mbi_write(BT_MBI_UNIT_PMC, MBI_CR_WRITE,
1055 rapl_defaults->floor_freq_reg_addr, mdata);
1058 static u64 rapl_compute_time_window_core(struct rapl_package *rp, u64 value,
1061 u64 f, y; /* fraction and exp. used for time unit */
1064 * Special processing based on 2^Y*(1+F/4), refer
1065 * to Intel Software Developer's manual Vol.3B: CH 14.9.3.
1068 f = (value & 0x60) >> 5;
1070 value = (1 << y) * (4 + f) * rp->time_unit / 4;
1072 do_div(value, rp->time_unit);
1074 f = div64_u64(4 * (value - (1 << y)), 1 << y);
1075 value = (y & 0x1f) | ((f & 0x3) << 5);
1080 static u64 rapl_compute_time_window_atom(struct rapl_package *rp, u64 value,
1084 * Atom time unit encoding is straight forward val * time_unit,
1085 * where time_unit is default to 1 sec. Never 0.
1088 return (value) ? value *= rp->time_unit : rp->time_unit;
1090 value = div64_u64(value, rp->time_unit);
1095 static const struct rapl_defaults rapl_defaults_core = {
1096 .floor_freq_reg_addr = 0,
1097 .check_unit = rapl_check_unit_core,
1098 .set_floor_freq = set_floor_freq_default,
1099 .compute_time_window = rapl_compute_time_window_core,
1102 static const struct rapl_defaults rapl_defaults_hsw_server = {
1103 .check_unit = rapl_check_unit_core,
1104 .set_floor_freq = set_floor_freq_default,
1105 .compute_time_window = rapl_compute_time_window_core,
1106 .dram_domain_energy_unit = 15300,
1109 static const struct rapl_defaults rapl_defaults_byt = {
1110 .floor_freq_reg_addr = IOSF_CPU_POWER_BUDGET_CTL_BYT,
1111 .check_unit = rapl_check_unit_atom,
1112 .set_floor_freq = set_floor_freq_atom,
1113 .compute_time_window = rapl_compute_time_window_atom,
1116 static const struct rapl_defaults rapl_defaults_tng = {
1117 .floor_freq_reg_addr = IOSF_CPU_POWER_BUDGET_CTL_TNG,
1118 .check_unit = rapl_check_unit_atom,
1119 .set_floor_freq = set_floor_freq_atom,
1120 .compute_time_window = rapl_compute_time_window_atom,
1123 static const struct rapl_defaults rapl_defaults_ann = {
1124 .floor_freq_reg_addr = 0,
1125 .check_unit = rapl_check_unit_atom,
1126 .set_floor_freq = NULL,
1127 .compute_time_window = rapl_compute_time_window_atom,
1130 static const struct rapl_defaults rapl_defaults_cht = {
1131 .floor_freq_reg_addr = 0,
1132 .check_unit = rapl_check_unit_atom,
1133 .set_floor_freq = NULL,
1134 .compute_time_window = rapl_compute_time_window_atom,
1137 static const struct x86_cpu_id rapl_ids[] __initconst = {
1138 INTEL_CPU_FAM6(SANDYBRIDGE, rapl_defaults_core),
1139 INTEL_CPU_FAM6(SANDYBRIDGE_X, rapl_defaults_core),
1141 INTEL_CPU_FAM6(IVYBRIDGE, rapl_defaults_core),
1142 INTEL_CPU_FAM6(IVYBRIDGE_X, rapl_defaults_core),
1144 INTEL_CPU_FAM6(HASWELL_CORE, rapl_defaults_core),
1145 INTEL_CPU_FAM6(HASWELL_ULT, rapl_defaults_core),
1146 INTEL_CPU_FAM6(HASWELL_GT3E, rapl_defaults_core),
1147 INTEL_CPU_FAM6(HASWELL_X, rapl_defaults_hsw_server),
1149 INTEL_CPU_FAM6(BROADWELL_CORE, rapl_defaults_core),
1150 INTEL_CPU_FAM6(BROADWELL_GT3E, rapl_defaults_core),
1151 INTEL_CPU_FAM6(BROADWELL_XEON_D, rapl_defaults_core),
1152 INTEL_CPU_FAM6(BROADWELL_X, rapl_defaults_hsw_server),
1154 INTEL_CPU_FAM6(SKYLAKE_DESKTOP, rapl_defaults_core),
1155 INTEL_CPU_FAM6(SKYLAKE_MOBILE, rapl_defaults_core),
1156 INTEL_CPU_FAM6(SKYLAKE_X, rapl_defaults_hsw_server),
1157 INTEL_CPU_FAM6(KABYLAKE_MOBILE, rapl_defaults_core),
1158 INTEL_CPU_FAM6(KABYLAKE_DESKTOP, rapl_defaults_core),
1159 INTEL_CPU_FAM6(CANNONLAKE_MOBILE, rapl_defaults_core),
1160 INTEL_CPU_FAM6(ICELAKE_MOBILE, rapl_defaults_core),
1162 INTEL_CPU_FAM6(ATOM_SILVERMONT, rapl_defaults_byt),
1163 INTEL_CPU_FAM6(ATOM_AIRMONT, rapl_defaults_cht),
1164 INTEL_CPU_FAM6(ATOM_SILVERMONT_MID, rapl_defaults_tng),
1165 INTEL_CPU_FAM6(ATOM_AIRMONT_MID, rapl_defaults_ann),
1166 INTEL_CPU_FAM6(ATOM_GOLDMONT, rapl_defaults_core),
1167 INTEL_CPU_FAM6(ATOM_GOLDMONT_PLUS, rapl_defaults_core),
1168 INTEL_CPU_FAM6(ATOM_GOLDMONT_X, rapl_defaults_core),
1169 INTEL_CPU_FAM6(ATOM_TREMONT_X, rapl_defaults_core),
1171 INTEL_CPU_FAM6(XEON_PHI_KNL, rapl_defaults_hsw_server),
1172 INTEL_CPU_FAM6(XEON_PHI_KNM, rapl_defaults_hsw_server),
1175 MODULE_DEVICE_TABLE(x86cpu, rapl_ids);
1177 /* Read once for all raw primitive data for domains */
1178 static void rapl_update_domain_data(struct rapl_package *rp)
1183 for (dmn = 0; dmn < rp->nr_domains; dmn++) {
1184 pr_debug("update package %d domain %s data\n", rp->id,
1185 rp->domains[dmn].name);
1186 /* exclude non-raw primitives */
1187 for (prim = 0; prim < NR_RAW_PRIMITIVES; prim++) {
1188 if (!rapl_read_data_raw(&rp->domains[dmn], prim,
1189 rpi[prim].unit, &val))
1190 rp->domains[dmn].rdd.primitives[prim] = val;
1196 static void rapl_unregister_powercap(void)
1198 if (platform_rapl_domain) {
1199 powercap_unregister_zone(control_type,
1200 &platform_rapl_domain->power_zone);
1201 kfree(platform_rapl_domain);
1203 powercap_unregister_control_type(control_type);
1206 static int rapl_package_register_powercap(struct rapl_package *rp)
1208 struct rapl_domain *rd;
1209 char dev_name[17]; /* max domain name = 7 + 1 + 8 for int + 1 for null*/
1210 struct powercap_zone *power_zone = NULL;
1213 /* Update the domain data of the new package */
1214 rapl_update_domain_data(rp);
1216 /* first we register package domain as the parent zone*/
1217 for (rd = rp->domains; rd < rp->domains + rp->nr_domains; rd++) {
1218 if (rd->id == RAPL_DOMAIN_PACKAGE) {
1219 nr_pl = find_nr_power_limit(rd);
1220 pr_debug("register socket %d package domain %s\n",
1222 memset(dev_name, 0, sizeof(dev_name));
1223 snprintf(dev_name, sizeof(dev_name), "%s-%d",
1225 power_zone = powercap_register_zone(&rd->power_zone,
1231 if (IS_ERR(power_zone)) {
1232 pr_debug("failed to register package, %d\n",
1234 return PTR_ERR(power_zone);
1236 /* track parent zone in per package/socket data */
1237 rp->power_zone = power_zone;
1238 /* done, only one package domain per socket */
1243 pr_err("no package domain found, unknown topology!\n");
1246 /* now register domains as children of the socket/package*/
1247 for (rd = rp->domains; rd < rp->domains + rp->nr_domains; rd++) {
1248 if (rd->id == RAPL_DOMAIN_PACKAGE)
1250 /* number of power limits per domain varies */
1251 nr_pl = find_nr_power_limit(rd);
1252 power_zone = powercap_register_zone(&rd->power_zone,
1253 control_type, rd->name,
1255 &zone_ops[rd->id], nr_pl,
1258 if (IS_ERR(power_zone)) {
1259 pr_debug("failed to register power_zone, %d:%s:%s\n",
1260 rp->id, rd->name, dev_name);
1261 ret = PTR_ERR(power_zone);
1269 * Clean up previously initialized domains within the package if we
1270 * failed after the first domain setup.
1272 while (--rd >= rp->domains) {
1273 pr_debug("unregister package %d domain %s\n", rp->id, rd->name);
1274 powercap_unregister_zone(control_type, &rd->power_zone);
1280 static int __init rapl_register_psys(void)
1282 struct rapl_domain *rd;
1283 struct powercap_zone *power_zone;
1286 if (rdmsrl_safe_on_cpu(0, MSR_PLATFORM_ENERGY_STATUS, &val) || !val)
1289 if (rdmsrl_safe_on_cpu(0, MSR_PLATFORM_POWER_LIMIT, &val) || !val)
1292 rd = kzalloc(sizeof(*rd), GFP_KERNEL);
1296 rd->name = rapl_domain_names[RAPL_DOMAIN_PLATFORM];
1297 rd->id = RAPL_DOMAIN_PLATFORM;
1298 rd->msrs[0] = MSR_PLATFORM_POWER_LIMIT;
1299 rd->msrs[1] = MSR_PLATFORM_ENERGY_STATUS;
1300 rd->rpl[0].prim_id = PL1_ENABLE;
1301 rd->rpl[0].name = pl1_name;
1302 rd->rpl[1].prim_id = PL2_ENABLE;
1303 rd->rpl[1].name = pl2_name;
1304 rd->rp = rapl_find_package_domain(0);
1306 power_zone = powercap_register_zone(&rd->power_zone, control_type,
1308 &zone_ops[RAPL_DOMAIN_PLATFORM],
1309 2, &constraint_ops);
1311 if (IS_ERR(power_zone)) {
1313 return PTR_ERR(power_zone);
1316 platform_rapl_domain = rd;
1321 static int __init rapl_register_powercap(void)
1323 control_type = powercap_register_control_type(NULL, "intel-rapl", NULL);
1324 if (IS_ERR(control_type)) {
1325 pr_debug("failed to register powercap control_type.\n");
1326 return PTR_ERR(control_type);
1331 static int rapl_check_domain(int cpu, int domain)
1337 case RAPL_DOMAIN_PACKAGE:
1338 msr = MSR_PKG_ENERGY_STATUS;
1340 case RAPL_DOMAIN_PP0:
1341 msr = MSR_PP0_ENERGY_STATUS;
1343 case RAPL_DOMAIN_PP1:
1344 msr = MSR_PP1_ENERGY_STATUS;
1346 case RAPL_DOMAIN_DRAM:
1347 msr = MSR_DRAM_ENERGY_STATUS;
1349 case RAPL_DOMAIN_PLATFORM:
1350 /* PSYS(PLATFORM) is not a CPU domain, so avoid printng error */
1353 pr_err("invalid domain id %d\n", domain);
1356 /* make sure domain counters are available and contains non-zero
1357 * values, otherwise skip it.
1359 if (rdmsrl_safe_on_cpu(cpu, msr, &val) || !val)
1367 * Check if power limits are available. Two cases when they are not available:
1368 * 1. Locked by BIOS, in this case we still provide read-only access so that
1369 * users can see what limit is set by the BIOS.
1370 * 2. Some CPUs make some domains monitoring only which means PLx MSRs may not
1371 * exist at all. In this case, we do not show the contraints in powercap.
1373 * Called after domains are detected and initialized.
1375 static void rapl_detect_powerlimit(struct rapl_domain *rd)
1380 /* check if the domain is locked by BIOS, ignore if MSR doesn't exist */
1381 if (!rapl_read_data_raw(rd, FW_LOCK, false, &val64)) {
1383 pr_info("RAPL package %d domain %s locked by BIOS\n",
1384 rd->rp->id, rd->name);
1385 rd->state |= DOMAIN_STATE_BIOS_LOCKED;
1388 /* check if power limit MSRs exists, otherwise domain is monitoring only */
1389 for (i = 0; i < NR_POWER_LIMITS; i++) {
1390 int prim = rd->rpl[i].prim_id;
1391 if (rapl_read_data_raw(rd, prim, false, &val64))
1392 rd->rpl[i].name = NULL;
1396 /* Detect active and valid domains for the given CPU, caller must
1397 * ensure the CPU belongs to the targeted package and CPU hotlug is disabled.
1399 static int rapl_detect_domains(struct rapl_package *rp, int cpu)
1401 struct rapl_domain *rd;
1404 for (i = 0; i < RAPL_DOMAIN_MAX; i++) {
1405 /* use physical package id to read counters */
1406 if (!rapl_check_domain(cpu, i)) {
1407 rp->domain_map |= 1 << i;
1408 pr_info("Found RAPL domain %s\n", rapl_domain_names[i]);
1411 rp->nr_domains = bitmap_weight(&rp->domain_map, RAPL_DOMAIN_MAX);
1412 if (!rp->nr_domains) {
1413 pr_debug("no valid rapl domains found in package %d\n", rp->id);
1416 pr_debug("found %d domains on package %d\n", rp->nr_domains, rp->id);
1418 rp->domains = kcalloc(rp->nr_domains + 1, sizeof(struct rapl_domain),
1423 rapl_init_domains(rp);
1425 for (rd = rp->domains; rd < rp->domains + rp->nr_domains; rd++)
1426 rapl_detect_powerlimit(rd);
1431 /* called from CPU hotplug notifier, hotplug lock held */
1432 static void rapl_remove_package(struct rapl_package *rp)
1434 struct rapl_domain *rd, *rd_package = NULL;
1436 package_power_limit_irq_restore(rp);
1438 for (rd = rp->domains; rd < rp->domains + rp->nr_domains; rd++) {
1439 rapl_write_data_raw(rd, PL1_ENABLE, 0);
1440 rapl_write_data_raw(rd, PL1_CLAMP, 0);
1441 if (find_nr_power_limit(rd) > 1) {
1442 rapl_write_data_raw(rd, PL2_ENABLE, 0);
1443 rapl_write_data_raw(rd, PL2_CLAMP, 0);
1445 if (rd->id == RAPL_DOMAIN_PACKAGE) {
1449 pr_debug("remove package, undo power limit on %d: %s\n",
1451 powercap_unregister_zone(control_type, &rd->power_zone);
1453 /* do parent zone last */
1454 powercap_unregister_zone(control_type, &rd_package->power_zone);
1455 list_del(&rp->plist);
1459 /* called from CPU hotplug notifier, hotplug lock held */
1460 static struct rapl_package *rapl_add_package(int cpu)
1462 int id = topology_logical_die_id(cpu);
1463 struct rapl_package *rp;
1466 rp = kzalloc(sizeof(struct rapl_package), GFP_KERNEL);
1468 return ERR_PTR(-ENOMEM);
1470 /* add the new package to the list */
1474 /* check if the package contains valid domains */
1475 if (rapl_detect_domains(rp, cpu) ||
1476 rapl_defaults->check_unit(rp, cpu)) {
1478 goto err_free_package;
1480 ret = rapl_package_register_powercap(rp);
1482 INIT_LIST_HEAD(&rp->plist);
1483 list_add(&rp->plist, &rapl_packages);
1490 return ERR_PTR(ret);
1493 /* Handles CPU hotplug on multi-socket systems.
1494 * If a CPU goes online as the first CPU of the physical package
1495 * we add the RAPL package to the system. Similarly, when the last
1496 * CPU of the package is removed, we remove the RAPL package and its
1497 * associated domains. Cooling devices are handled accordingly at
1500 static int rapl_cpu_online(unsigned int cpu)
1502 struct rapl_package *rp;
1504 rp = rapl_find_package_domain(cpu);
1506 rp = rapl_add_package(cpu);
1510 cpumask_set_cpu(cpu, &rp->cpumask);
1514 static int rapl_cpu_down_prep(unsigned int cpu)
1516 struct rapl_package *rp;
1519 rp = rapl_find_package_domain(cpu);
1523 cpumask_clear_cpu(cpu, &rp->cpumask);
1524 lead_cpu = cpumask_first(&rp->cpumask);
1525 if (lead_cpu >= nr_cpu_ids)
1526 rapl_remove_package(rp);
1527 else if (rp->lead_cpu == cpu)
1528 rp->lead_cpu = lead_cpu;
1532 static enum cpuhp_state pcap_rapl_online;
1534 static void power_limit_state_save(void)
1536 struct rapl_package *rp;
1537 struct rapl_domain *rd;
1541 list_for_each_entry(rp, &rapl_packages, plist) {
1542 if (!rp->power_zone)
1544 rd = power_zone_to_rapl_domain(rp->power_zone);
1545 nr_pl = find_nr_power_limit(rd);
1546 for (i = 0; i < nr_pl; i++) {
1547 switch (rd->rpl[i].prim_id) {
1549 ret = rapl_read_data_raw(rd,
1552 &rd->rpl[i].last_power_limit);
1554 rd->rpl[i].last_power_limit = 0;
1557 ret = rapl_read_data_raw(rd,
1560 &rd->rpl[i].last_power_limit);
1562 rd->rpl[i].last_power_limit = 0;
1570 static void power_limit_state_restore(void)
1572 struct rapl_package *rp;
1573 struct rapl_domain *rd;
1577 list_for_each_entry(rp, &rapl_packages, plist) {
1578 if (!rp->power_zone)
1580 rd = power_zone_to_rapl_domain(rp->power_zone);
1581 nr_pl = find_nr_power_limit(rd);
1582 for (i = 0; i < nr_pl; i++) {
1583 switch (rd->rpl[i].prim_id) {
1585 if (rd->rpl[i].last_power_limit)
1586 rapl_write_data_raw(rd,
1588 rd->rpl[i].last_power_limit);
1591 if (rd->rpl[i].last_power_limit)
1592 rapl_write_data_raw(rd,
1594 rd->rpl[i].last_power_limit);
1602 static int rapl_pm_callback(struct notifier_block *nb,
1603 unsigned long mode, void *_unused)
1606 case PM_SUSPEND_PREPARE:
1607 power_limit_state_save();
1609 case PM_POST_SUSPEND:
1610 power_limit_state_restore();
1616 static struct notifier_block rapl_pm_notifier = {
1617 .notifier_call = rapl_pm_callback,
1620 static int __init rapl_init(void)
1622 const struct x86_cpu_id *id;
1625 id = x86_match_cpu(rapl_ids);
1627 pr_err("driver does not support CPU family %d model %d\n",
1628 boot_cpu_data.x86, boot_cpu_data.x86_model);
1633 rapl_defaults = (struct rapl_defaults *)id->driver_data;
1635 ret = rapl_register_powercap();
1639 ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "powercap/rapl:online",
1640 rapl_cpu_online, rapl_cpu_down_prep);
1643 pcap_rapl_online = ret;
1645 /* Don't bail out if PSys is not supported */
1646 rapl_register_psys();
1648 ret = register_pm_notifier(&rapl_pm_notifier);
1655 cpuhp_remove_state(pcap_rapl_online);
1658 rapl_unregister_powercap();
1662 static void __exit rapl_exit(void)
1664 unregister_pm_notifier(&rapl_pm_notifier);
1665 cpuhp_remove_state(pcap_rapl_online);
1666 rapl_unregister_powercap();
1669 module_init(rapl_init);
1670 module_exit(rapl_exit);
1672 MODULE_DESCRIPTION("Driver for Intel RAPL (Running Average Power Limit)");
1673 MODULE_AUTHOR("Jacob Pan <jacob.jun.pan@intel.com>");
1674 MODULE_LICENSE("GPL v2");