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)
181 /* maximum rapl package domain name: package-%d-die-%d */
182 #define PACKAGE_DOMAIN_NAME_LENGTH 30
185 /* Each rapl package contains multiple domains, these are the common
186 * data across RAPL domains within a package.
188 struct rapl_package {
189 unsigned int id; /* logical die id, equals physical 1-die systems */
190 unsigned int nr_domains;
191 unsigned long domain_map; /* bit map of active domains */
192 unsigned int power_unit;
193 unsigned int energy_unit;
194 unsigned int time_unit;
195 struct rapl_domain *domains; /* array of domains, sized at runtime */
196 struct powercap_zone *power_zone; /* keep track of parent zone */
197 unsigned long power_limit_irq; /* keep track of package power limit
198 * notify interrupt enable status.
200 struct list_head plist;
201 int lead_cpu; /* one active cpu per package for access */
202 /* Track active cpus */
203 struct cpumask cpumask;
204 char name[PACKAGE_DOMAIN_NAME_LENGTH];
207 struct rapl_defaults {
208 u8 floor_freq_reg_addr;
209 int (*check_unit)(struct rapl_package *rp, int cpu);
210 void (*set_floor_freq)(struct rapl_domain *rd, bool mode);
211 u64 (*compute_time_window)(struct rapl_package *rp, u64 val,
213 unsigned int dram_domain_energy_unit;
215 static struct rapl_defaults *rapl_defaults;
217 /* Sideband MBI registers */
218 #define IOSF_CPU_POWER_BUDGET_CTL_BYT (0x2)
219 #define IOSF_CPU_POWER_BUDGET_CTL_TNG (0xdf)
221 #define PACKAGE_PLN_INT_SAVED BIT(0)
222 #define MAX_PRIM_NAME (32)
224 /* per domain data. used to describe individual knobs such that access function
225 * can be consolidated into one instead of many inline functions.
227 struct rapl_primitive_info {
231 enum rapl_domain_msr_id id;
236 #define PRIMITIVE_INFO_INIT(p, m, s, i, u, f) { \
245 static void rapl_init_domains(struct rapl_package *rp);
246 static int rapl_read_data_raw(struct rapl_domain *rd,
247 enum rapl_primitives prim,
248 bool xlate, u64 *data);
249 static int rapl_write_data_raw(struct rapl_domain *rd,
250 enum rapl_primitives prim,
251 unsigned long long value);
252 static u64 rapl_unit_xlate(struct rapl_domain *rd,
253 enum unit_type type, u64 value,
255 static void package_power_limit_irq_save(struct rapl_package *rp);
257 static LIST_HEAD(rapl_packages); /* guarded by CPU hotplug lock */
259 static const char * const rapl_domain_names[] = {
267 static struct powercap_control_type *control_type; /* PowerCap Controller */
268 static struct rapl_domain *platform_rapl_domain; /* Platform (PSys) domain */
270 /* caller to ensure CPU hotplug lock is held */
271 static struct rapl_package *rapl_find_package_domain(int cpu)
273 int id = topology_logical_die_id(cpu);
274 struct rapl_package *rp;
276 list_for_each_entry(rp, &rapl_packages, plist) {
284 static int get_energy_counter(struct powercap_zone *power_zone, u64 *energy_raw)
286 struct rapl_domain *rd;
289 /* prevent CPU hotplug, make sure the RAPL domain does not go
290 * away while reading the counter.
293 rd = power_zone_to_rapl_domain(power_zone);
295 if (!rapl_read_data_raw(rd, ENERGY_COUNTER, true, &energy_now)) {
296 *energy_raw = energy_now;
306 static int get_max_energy_counter(struct powercap_zone *pcd_dev, u64 *energy)
308 struct rapl_domain *rd = power_zone_to_rapl_domain(pcd_dev);
310 *energy = rapl_unit_xlate(rd, ENERGY_UNIT, ENERGY_STATUS_MASK, 0);
314 static int release_zone(struct powercap_zone *power_zone)
316 struct rapl_domain *rd = power_zone_to_rapl_domain(power_zone);
317 struct rapl_package *rp = rd->rp;
319 /* package zone is the last zone of a package, we can free
320 * memory here since all children has been unregistered.
322 if (rd->id == RAPL_DOMAIN_PACKAGE) {
331 static int find_nr_power_limit(struct rapl_domain *rd)
335 for (i = 0; i < NR_POWER_LIMITS; i++) {
343 static int set_domain_enable(struct powercap_zone *power_zone, bool mode)
345 struct rapl_domain *rd = power_zone_to_rapl_domain(power_zone);
347 if (rd->state & DOMAIN_STATE_BIOS_LOCKED)
351 rapl_write_data_raw(rd, PL1_ENABLE, mode);
352 if (rapl_defaults->set_floor_freq)
353 rapl_defaults->set_floor_freq(rd, mode);
359 static int get_domain_enable(struct powercap_zone *power_zone, bool *mode)
361 struct rapl_domain *rd = power_zone_to_rapl_domain(power_zone);
364 if (rd->state & DOMAIN_STATE_BIOS_LOCKED) {
369 if (rapl_read_data_raw(rd, PL1_ENABLE, true, &val)) {
379 /* per RAPL domain ops, in the order of rapl_domain_type */
380 static const struct powercap_zone_ops zone_ops[] = {
381 /* RAPL_DOMAIN_PACKAGE */
383 .get_energy_uj = get_energy_counter,
384 .get_max_energy_range_uj = get_max_energy_counter,
385 .release = release_zone,
386 .set_enable = set_domain_enable,
387 .get_enable = get_domain_enable,
389 /* RAPL_DOMAIN_PP0 */
391 .get_energy_uj = get_energy_counter,
392 .get_max_energy_range_uj = get_max_energy_counter,
393 .release = release_zone,
394 .set_enable = set_domain_enable,
395 .get_enable = get_domain_enable,
397 /* RAPL_DOMAIN_PP1 */
399 .get_energy_uj = get_energy_counter,
400 .get_max_energy_range_uj = get_max_energy_counter,
401 .release = release_zone,
402 .set_enable = set_domain_enable,
403 .get_enable = get_domain_enable,
405 /* RAPL_DOMAIN_DRAM */
407 .get_energy_uj = get_energy_counter,
408 .get_max_energy_range_uj = get_max_energy_counter,
409 .release = release_zone,
410 .set_enable = set_domain_enable,
411 .get_enable = get_domain_enable,
413 /* RAPL_DOMAIN_PLATFORM */
415 .get_energy_uj = get_energy_counter,
416 .get_max_energy_range_uj = get_max_energy_counter,
417 .release = release_zone,
418 .set_enable = set_domain_enable,
419 .get_enable = get_domain_enable,
425 * Constraint index used by powercap can be different than power limit (PL)
426 * index in that some PLs maybe missing due to non-existant MSRs. So we
427 * need to convert here by finding the valid PLs only (name populated).
429 static int contraint_to_pl(struct rapl_domain *rd, int cid)
433 for (i = 0, j = 0; i < NR_POWER_LIMITS; i++) {
434 if ((rd->rpl[i].name) && j++ == cid) {
435 pr_debug("%s: index %d\n", __func__, i);
439 pr_err("Cannot find matching power limit for constraint %d\n", cid);
444 static int set_power_limit(struct powercap_zone *power_zone, int cid,
447 struct rapl_domain *rd;
448 struct rapl_package *rp;
453 rd = power_zone_to_rapl_domain(power_zone);
454 id = contraint_to_pl(rd, cid);
462 if (rd->state & DOMAIN_STATE_BIOS_LOCKED) {
463 dev_warn(&power_zone->dev, "%s locked by BIOS, monitoring only\n",
469 switch (rd->rpl[id].prim_id) {
471 rapl_write_data_raw(rd, POWER_LIMIT1, power_limit);
474 rapl_write_data_raw(rd, POWER_LIMIT2, power_limit);
480 package_power_limit_irq_save(rp);
486 static int get_current_power_limit(struct powercap_zone *power_zone, int cid,
489 struct rapl_domain *rd;
496 rd = power_zone_to_rapl_domain(power_zone);
497 id = contraint_to_pl(rd, cid);
503 switch (rd->rpl[id].prim_id) {
514 if (rapl_read_data_raw(rd, prim, true, &val))
525 static int set_time_window(struct powercap_zone *power_zone, int cid,
528 struct rapl_domain *rd;
533 rd = power_zone_to_rapl_domain(power_zone);
534 id = contraint_to_pl(rd, cid);
540 switch (rd->rpl[id].prim_id) {
542 rapl_write_data_raw(rd, TIME_WINDOW1, window);
545 rapl_write_data_raw(rd, TIME_WINDOW2, window);
556 static int get_time_window(struct powercap_zone *power_zone, int cid, u64 *data)
558 struct rapl_domain *rd;
564 rd = power_zone_to_rapl_domain(power_zone);
565 id = contraint_to_pl(rd, cid);
571 switch (rd->rpl[id].prim_id) {
573 ret = rapl_read_data_raw(rd, TIME_WINDOW1, true, &val);
576 ret = rapl_read_data_raw(rd, TIME_WINDOW2, true, &val);
591 static const char *get_constraint_name(struct powercap_zone *power_zone, int cid)
593 struct rapl_domain *rd;
596 rd = power_zone_to_rapl_domain(power_zone);
597 id = contraint_to_pl(rd, cid);
599 return rd->rpl[id].name;
605 static int get_max_power(struct powercap_zone *power_zone, int id,
608 struct rapl_domain *rd;
614 rd = power_zone_to_rapl_domain(power_zone);
615 switch (rd->rpl[id].prim_id) {
617 prim = THERMAL_SPEC_POWER;
626 if (rapl_read_data_raw(rd, prim, true, &val))
636 static const struct powercap_zone_constraint_ops constraint_ops = {
637 .set_power_limit_uw = set_power_limit,
638 .get_power_limit_uw = get_current_power_limit,
639 .set_time_window_us = set_time_window,
640 .get_time_window_us = get_time_window,
641 .get_max_power_uw = get_max_power,
642 .get_name = get_constraint_name,
645 /* called after domain detection and package level data are set */
646 static void rapl_init_domains(struct rapl_package *rp)
649 struct rapl_domain *rd = rp->domains;
651 for (i = 0; i < RAPL_DOMAIN_MAX; i++) {
652 unsigned int mask = rp->domain_map & (1 << i);
654 case BIT(RAPL_DOMAIN_PACKAGE):
655 rd->name = rapl_domain_names[RAPL_DOMAIN_PACKAGE];
656 rd->id = RAPL_DOMAIN_PACKAGE;
657 rd->msrs[0] = MSR_PKG_POWER_LIMIT;
658 rd->msrs[1] = MSR_PKG_ENERGY_STATUS;
659 rd->msrs[2] = MSR_PKG_PERF_STATUS;
661 rd->msrs[4] = MSR_PKG_POWER_INFO;
662 rd->rpl[0].prim_id = PL1_ENABLE;
663 rd->rpl[0].name = pl1_name;
664 rd->rpl[1].prim_id = PL2_ENABLE;
665 rd->rpl[1].name = pl2_name;
667 case BIT(RAPL_DOMAIN_PP0):
668 rd->name = rapl_domain_names[RAPL_DOMAIN_PP0];
669 rd->id = RAPL_DOMAIN_PP0;
670 rd->msrs[0] = MSR_PP0_POWER_LIMIT;
671 rd->msrs[1] = MSR_PP0_ENERGY_STATUS;
673 rd->msrs[3] = MSR_PP0_POLICY;
675 rd->rpl[0].prim_id = PL1_ENABLE;
676 rd->rpl[0].name = pl1_name;
678 case BIT(RAPL_DOMAIN_PP1):
679 rd->name = rapl_domain_names[RAPL_DOMAIN_PP1];
680 rd->id = RAPL_DOMAIN_PP1;
681 rd->msrs[0] = MSR_PP1_POWER_LIMIT;
682 rd->msrs[1] = MSR_PP1_ENERGY_STATUS;
684 rd->msrs[3] = MSR_PP1_POLICY;
686 rd->rpl[0].prim_id = PL1_ENABLE;
687 rd->rpl[0].name = pl1_name;
689 case BIT(RAPL_DOMAIN_DRAM):
690 rd->name = rapl_domain_names[RAPL_DOMAIN_DRAM];
691 rd->id = RAPL_DOMAIN_DRAM;
692 rd->msrs[0] = MSR_DRAM_POWER_LIMIT;
693 rd->msrs[1] = MSR_DRAM_ENERGY_STATUS;
694 rd->msrs[2] = MSR_DRAM_PERF_STATUS;
696 rd->msrs[4] = MSR_DRAM_POWER_INFO;
697 rd->rpl[0].prim_id = PL1_ENABLE;
698 rd->rpl[0].name = pl1_name;
699 rd->domain_energy_unit =
700 rapl_defaults->dram_domain_energy_unit;
701 if (rd->domain_energy_unit)
702 pr_info("DRAM domain energy unit %dpj\n",
703 rd->domain_energy_unit);
713 static u64 rapl_unit_xlate(struct rapl_domain *rd, enum unit_type type,
714 u64 value, int to_raw)
717 struct rapl_package *rp = rd->rp;
722 units = rp->power_unit;
725 scale = ENERGY_UNIT_SCALE;
726 /* per domain unit takes precedence */
727 if (rd->domain_energy_unit)
728 units = rd->domain_energy_unit;
730 units = rp->energy_unit;
733 return rapl_defaults->compute_time_window(rp, value, to_raw);
740 return div64_u64(value, units) * scale;
744 return div64_u64(value, scale);
747 /* in the order of enum rapl_primitives */
748 static struct rapl_primitive_info rpi[] = {
749 /* name, mask, shift, msr index, unit divisor */
750 PRIMITIVE_INFO_INIT(ENERGY_COUNTER, ENERGY_STATUS_MASK, 0,
751 RAPL_DOMAIN_MSR_STATUS, ENERGY_UNIT, 0),
752 PRIMITIVE_INFO_INIT(POWER_LIMIT1, POWER_LIMIT1_MASK, 0,
753 RAPL_DOMAIN_MSR_LIMIT, POWER_UNIT, 0),
754 PRIMITIVE_INFO_INIT(POWER_LIMIT2, POWER_LIMIT2_MASK, 32,
755 RAPL_DOMAIN_MSR_LIMIT, POWER_UNIT, 0),
756 PRIMITIVE_INFO_INIT(FW_LOCK, POWER_PP_LOCK, 31,
757 RAPL_DOMAIN_MSR_LIMIT, ARBITRARY_UNIT, 0),
758 PRIMITIVE_INFO_INIT(PL1_ENABLE, POWER_LIMIT1_ENABLE, 15,
759 RAPL_DOMAIN_MSR_LIMIT, ARBITRARY_UNIT, 0),
760 PRIMITIVE_INFO_INIT(PL1_CLAMP, POWER_LIMIT1_CLAMP, 16,
761 RAPL_DOMAIN_MSR_LIMIT, ARBITRARY_UNIT, 0),
762 PRIMITIVE_INFO_INIT(PL2_ENABLE, POWER_LIMIT2_ENABLE, 47,
763 RAPL_DOMAIN_MSR_LIMIT, ARBITRARY_UNIT, 0),
764 PRIMITIVE_INFO_INIT(PL2_CLAMP, POWER_LIMIT2_CLAMP, 48,
765 RAPL_DOMAIN_MSR_LIMIT, ARBITRARY_UNIT, 0),
766 PRIMITIVE_INFO_INIT(TIME_WINDOW1, TIME_WINDOW1_MASK, 17,
767 RAPL_DOMAIN_MSR_LIMIT, TIME_UNIT, 0),
768 PRIMITIVE_INFO_INIT(TIME_WINDOW2, TIME_WINDOW2_MASK, 49,
769 RAPL_DOMAIN_MSR_LIMIT, TIME_UNIT, 0),
770 PRIMITIVE_INFO_INIT(THERMAL_SPEC_POWER, POWER_INFO_THERMAL_SPEC_MASK,
771 0, RAPL_DOMAIN_MSR_INFO, POWER_UNIT, 0),
772 PRIMITIVE_INFO_INIT(MAX_POWER, POWER_INFO_MAX_MASK, 32,
773 RAPL_DOMAIN_MSR_INFO, POWER_UNIT, 0),
774 PRIMITIVE_INFO_INIT(MIN_POWER, POWER_INFO_MIN_MASK, 16,
775 RAPL_DOMAIN_MSR_INFO, POWER_UNIT, 0),
776 PRIMITIVE_INFO_INIT(MAX_TIME_WINDOW, POWER_INFO_MAX_TIME_WIN_MASK, 48,
777 RAPL_DOMAIN_MSR_INFO, TIME_UNIT, 0),
778 PRIMITIVE_INFO_INIT(THROTTLED_TIME, PERF_STATUS_THROTTLE_TIME_MASK, 0,
779 RAPL_DOMAIN_MSR_PERF, TIME_UNIT, 0),
780 PRIMITIVE_INFO_INIT(PRIORITY_LEVEL, PP_POLICY_MASK, 0,
781 RAPL_DOMAIN_MSR_POLICY, ARBITRARY_UNIT, 0),
783 PRIMITIVE_INFO_INIT(AVERAGE_POWER, 0, 0, 0, POWER_UNIT,
784 RAPL_PRIMITIVE_DERIVED),
788 /* Read primitive data based on its related struct rapl_primitive_info.
789 * if xlate flag is set, return translated data based on data units, i.e.
790 * time, energy, and power.
791 * RAPL MSRs are non-architectual and are laid out not consistently across
792 * domains. Here we use primitive info to allow writing consolidated access
794 * For a given primitive, it is processed by MSR mask and shift. Unit conversion
795 * is pre-assigned based on RAPL unit MSRs read at init time.
796 * 63-------------------------- 31--------------------------- 0
798 * | |<- shift ----------------|
799 * 63-------------------------- 31--------------------------- 0
801 static int rapl_read_data_raw(struct rapl_domain *rd,
802 enum rapl_primitives prim,
803 bool xlate, u64 *data)
807 struct rapl_primitive_info *rp = &rpi[prim];
810 if (!rp->name || rp->flag & RAPL_PRIMITIVE_DUMMY)
813 msr = rd->msrs[rp->id];
817 cpu = rd->rp->lead_cpu;
819 /* special-case package domain, which uses a different bit*/
820 if (prim == FW_LOCK && rd->id == RAPL_DOMAIN_PACKAGE) {
821 rp->mask = POWER_PACKAGE_LOCK;
824 /* non-hardware data are collected by the polling thread */
825 if (rp->flag & RAPL_PRIMITIVE_DERIVED) {
826 *data = rd->rdd.primitives[prim];
830 if (rdmsrl_safe_on_cpu(cpu, msr, &value)) {
831 pr_debug("failed to read msr 0x%x on cpu %d\n", msr, cpu);
835 final = value & rp->mask;
836 final = final >> rp->shift;
838 *data = rapl_unit_xlate(rd, rp->unit, final, 0);
846 static int msrl_update_safe(u32 msr_no, u64 clear_mask, u64 set_mask)
851 err = rdmsrl_safe(msr_no, &val);
858 err = wrmsrl_safe(msr_no, val);
864 static void msrl_update_func(void *info)
866 struct msrl_action *ma = info;
868 ma->err = msrl_update_safe(ma->msr_no, ma->clear_mask, ma->set_mask);
871 /* Similar use of primitive info in the read counterpart */
872 static int rapl_write_data_raw(struct rapl_domain *rd,
873 enum rapl_primitives prim,
874 unsigned long long value)
876 struct rapl_primitive_info *rp = &rpi[prim];
879 struct msrl_action ma;
882 cpu = rd->rp->lead_cpu;
883 bits = rapl_unit_xlate(rd, rp->unit, value, 1);
887 memset(&ma, 0, sizeof(ma));
889 ma.msr_no = rd->msrs[rp->id];
890 ma.clear_mask = rp->mask;
893 ret = smp_call_function_single(cpu, msrl_update_func, &ma, 1);
903 * Raw RAPL data stored in MSRs are in certain scales. We need to
904 * convert them into standard units based on the units reported in
905 * the RAPL unit MSRs. This is specific to CPUs as the method to
906 * calculate units differ on different CPUs.
907 * We convert the units to below format based on CPUs.
909 * energy unit: picoJoules : Represented in picoJoules by default
910 * power unit : microWatts : Represented in milliWatts by default
911 * time unit : microseconds: Represented in seconds by default
913 static int rapl_check_unit_core(struct rapl_package *rp, int cpu)
918 if (rdmsrl_safe_on_cpu(cpu, MSR_RAPL_POWER_UNIT, &msr_val)) {
919 pr_err("Failed to read power unit MSR 0x%x on CPU %d, exit.\n",
920 MSR_RAPL_POWER_UNIT, cpu);
924 value = (msr_val & ENERGY_UNIT_MASK) >> ENERGY_UNIT_OFFSET;
925 rp->energy_unit = ENERGY_UNIT_SCALE * 1000000 / (1 << value);
927 value = (msr_val & POWER_UNIT_MASK) >> POWER_UNIT_OFFSET;
928 rp->power_unit = 1000000 / (1 << value);
930 value = (msr_val & TIME_UNIT_MASK) >> TIME_UNIT_OFFSET;
931 rp->time_unit = 1000000 / (1 << value);
933 pr_debug("Core CPU %s energy=%dpJ, time=%dus, power=%duW\n",
934 rp->name, rp->energy_unit, rp->time_unit, rp->power_unit);
939 static int rapl_check_unit_atom(struct rapl_package *rp, int cpu)
944 if (rdmsrl_safe_on_cpu(cpu, MSR_RAPL_POWER_UNIT, &msr_val)) {
945 pr_err("Failed to read power unit MSR 0x%x on CPU %d, exit.\n",
946 MSR_RAPL_POWER_UNIT, cpu);
949 value = (msr_val & ENERGY_UNIT_MASK) >> ENERGY_UNIT_OFFSET;
950 rp->energy_unit = ENERGY_UNIT_SCALE * 1 << value;
952 value = (msr_val & POWER_UNIT_MASK) >> POWER_UNIT_OFFSET;
953 rp->power_unit = (1 << value) * 1000;
955 value = (msr_val & TIME_UNIT_MASK) >> TIME_UNIT_OFFSET;
956 rp->time_unit = 1000000 / (1 << value);
958 pr_debug("Atom %s energy=%dpJ, time=%dus, power=%duW\n",
959 rp->name, rp->energy_unit, rp->time_unit, rp->power_unit);
964 static void power_limit_irq_save_cpu(void *info)
967 struct rapl_package *rp = (struct rapl_package *)info;
969 /* save the state of PLN irq mask bit before disabling it */
970 rdmsr_safe(MSR_IA32_PACKAGE_THERM_INTERRUPT, &l, &h);
971 if (!(rp->power_limit_irq & PACKAGE_PLN_INT_SAVED)) {
972 rp->power_limit_irq = l & PACKAGE_THERM_INT_PLN_ENABLE;
973 rp->power_limit_irq |= PACKAGE_PLN_INT_SAVED;
975 l &= ~PACKAGE_THERM_INT_PLN_ENABLE;
976 wrmsr_safe(MSR_IA32_PACKAGE_THERM_INTERRUPT, l, h);
981 * When package power limit is set artificially low by RAPL, LVT
982 * thermal interrupt for package power limit should be ignored
983 * since we are not really exceeding the real limit. The intention
984 * is to avoid excessive interrupts while we are trying to save power.
985 * A useful feature might be routing the package_power_limit interrupt
986 * to userspace via eventfd. once we have a usecase, this is simple
987 * to do by adding an atomic notifier.
990 static void package_power_limit_irq_save(struct rapl_package *rp)
992 if (!boot_cpu_has(X86_FEATURE_PTS) || !boot_cpu_has(X86_FEATURE_PLN))
995 smp_call_function_single(rp->lead_cpu, power_limit_irq_save_cpu, rp, 1);
999 * Restore per package power limit interrupt enable state. Called from cpu
1000 * hotplug code on package removal.
1002 static void package_power_limit_irq_restore(struct rapl_package *rp)
1006 if (!boot_cpu_has(X86_FEATURE_PTS) || !boot_cpu_has(X86_FEATURE_PLN))
1009 /* irq enable state not saved, nothing to restore */
1010 if (!(rp->power_limit_irq & PACKAGE_PLN_INT_SAVED))
1013 rdmsr_safe(MSR_IA32_PACKAGE_THERM_INTERRUPT, &l, &h);
1015 if (rp->power_limit_irq & PACKAGE_THERM_INT_PLN_ENABLE)
1016 l |= PACKAGE_THERM_INT_PLN_ENABLE;
1018 l &= ~PACKAGE_THERM_INT_PLN_ENABLE;
1020 wrmsr_safe(MSR_IA32_PACKAGE_THERM_INTERRUPT, l, h);
1023 static void set_floor_freq_default(struct rapl_domain *rd, bool mode)
1025 int nr_powerlimit = find_nr_power_limit(rd);
1027 /* always enable clamp such that p-state can go below OS requested
1028 * range. power capping priority over guranteed frequency.
1030 rapl_write_data_raw(rd, PL1_CLAMP, mode);
1032 /* some domains have pl2 */
1033 if (nr_powerlimit > 1) {
1034 rapl_write_data_raw(rd, PL2_ENABLE, mode);
1035 rapl_write_data_raw(rd, PL2_CLAMP, mode);
1039 static void set_floor_freq_atom(struct rapl_domain *rd, bool enable)
1041 static u32 power_ctrl_orig_val;
1044 if (!rapl_defaults->floor_freq_reg_addr) {
1045 pr_err("Invalid floor frequency config register\n");
1049 if (!power_ctrl_orig_val)
1050 iosf_mbi_read(BT_MBI_UNIT_PMC, MBI_CR_READ,
1051 rapl_defaults->floor_freq_reg_addr,
1052 &power_ctrl_orig_val);
1053 mdata = power_ctrl_orig_val;
1055 mdata &= ~(0x7f << 8);
1058 iosf_mbi_write(BT_MBI_UNIT_PMC, MBI_CR_WRITE,
1059 rapl_defaults->floor_freq_reg_addr, mdata);
1062 static u64 rapl_compute_time_window_core(struct rapl_package *rp, u64 value,
1065 u64 f, y; /* fraction and exp. used for time unit */
1068 * Special processing based on 2^Y*(1+F/4), refer
1069 * to Intel Software Developer's manual Vol.3B: CH 14.9.3.
1072 f = (value & 0x60) >> 5;
1074 value = (1 << y) * (4 + f) * rp->time_unit / 4;
1076 do_div(value, rp->time_unit);
1078 f = div64_u64(4 * (value - (1 << y)), 1 << y);
1079 value = (y & 0x1f) | ((f & 0x3) << 5);
1084 static u64 rapl_compute_time_window_atom(struct rapl_package *rp, u64 value,
1088 * Atom time unit encoding is straight forward val * time_unit,
1089 * where time_unit is default to 1 sec. Never 0.
1092 return (value) ? value *= rp->time_unit : rp->time_unit;
1094 value = div64_u64(value, rp->time_unit);
1099 static const struct rapl_defaults rapl_defaults_core = {
1100 .floor_freq_reg_addr = 0,
1101 .check_unit = rapl_check_unit_core,
1102 .set_floor_freq = set_floor_freq_default,
1103 .compute_time_window = rapl_compute_time_window_core,
1106 static const struct rapl_defaults rapl_defaults_hsw_server = {
1107 .check_unit = rapl_check_unit_core,
1108 .set_floor_freq = set_floor_freq_default,
1109 .compute_time_window = rapl_compute_time_window_core,
1110 .dram_domain_energy_unit = 15300,
1113 static const struct rapl_defaults rapl_defaults_byt = {
1114 .floor_freq_reg_addr = IOSF_CPU_POWER_BUDGET_CTL_BYT,
1115 .check_unit = rapl_check_unit_atom,
1116 .set_floor_freq = set_floor_freq_atom,
1117 .compute_time_window = rapl_compute_time_window_atom,
1120 static const struct rapl_defaults rapl_defaults_tng = {
1121 .floor_freq_reg_addr = IOSF_CPU_POWER_BUDGET_CTL_TNG,
1122 .check_unit = rapl_check_unit_atom,
1123 .set_floor_freq = set_floor_freq_atom,
1124 .compute_time_window = rapl_compute_time_window_atom,
1127 static const struct rapl_defaults rapl_defaults_ann = {
1128 .floor_freq_reg_addr = 0,
1129 .check_unit = rapl_check_unit_atom,
1130 .set_floor_freq = NULL,
1131 .compute_time_window = rapl_compute_time_window_atom,
1134 static const struct rapl_defaults rapl_defaults_cht = {
1135 .floor_freq_reg_addr = 0,
1136 .check_unit = rapl_check_unit_atom,
1137 .set_floor_freq = NULL,
1138 .compute_time_window = rapl_compute_time_window_atom,
1141 static const struct x86_cpu_id rapl_ids[] __initconst = {
1142 INTEL_CPU_FAM6(SANDYBRIDGE, rapl_defaults_core),
1143 INTEL_CPU_FAM6(SANDYBRIDGE_X, rapl_defaults_core),
1145 INTEL_CPU_FAM6(IVYBRIDGE, rapl_defaults_core),
1146 INTEL_CPU_FAM6(IVYBRIDGE_X, rapl_defaults_core),
1148 INTEL_CPU_FAM6(HASWELL_CORE, rapl_defaults_core),
1149 INTEL_CPU_FAM6(HASWELL_ULT, rapl_defaults_core),
1150 INTEL_CPU_FAM6(HASWELL_GT3E, rapl_defaults_core),
1151 INTEL_CPU_FAM6(HASWELL_X, rapl_defaults_hsw_server),
1153 INTEL_CPU_FAM6(BROADWELL_CORE, rapl_defaults_core),
1154 INTEL_CPU_FAM6(BROADWELL_GT3E, rapl_defaults_core),
1155 INTEL_CPU_FAM6(BROADWELL_XEON_D, rapl_defaults_core),
1156 INTEL_CPU_FAM6(BROADWELL_X, rapl_defaults_hsw_server),
1158 INTEL_CPU_FAM6(SKYLAKE_DESKTOP, rapl_defaults_core),
1159 INTEL_CPU_FAM6(SKYLAKE_MOBILE, rapl_defaults_core),
1160 INTEL_CPU_FAM6(SKYLAKE_X, rapl_defaults_hsw_server),
1161 INTEL_CPU_FAM6(KABYLAKE_MOBILE, rapl_defaults_core),
1162 INTEL_CPU_FAM6(KABYLAKE_DESKTOP, rapl_defaults_core),
1163 INTEL_CPU_FAM6(CANNONLAKE_MOBILE, rapl_defaults_core),
1164 INTEL_CPU_FAM6(ICELAKE_MOBILE, rapl_defaults_core),
1166 INTEL_CPU_FAM6(ATOM_SILVERMONT, rapl_defaults_byt),
1167 INTEL_CPU_FAM6(ATOM_AIRMONT, rapl_defaults_cht),
1168 INTEL_CPU_FAM6(ATOM_SILVERMONT_MID, rapl_defaults_tng),
1169 INTEL_CPU_FAM6(ATOM_AIRMONT_MID, rapl_defaults_ann),
1170 INTEL_CPU_FAM6(ATOM_GOLDMONT, rapl_defaults_core),
1171 INTEL_CPU_FAM6(ATOM_GOLDMONT_PLUS, rapl_defaults_core),
1172 INTEL_CPU_FAM6(ATOM_GOLDMONT_X, rapl_defaults_core),
1173 INTEL_CPU_FAM6(ATOM_TREMONT_X, rapl_defaults_core),
1175 INTEL_CPU_FAM6(XEON_PHI_KNL, rapl_defaults_hsw_server),
1176 INTEL_CPU_FAM6(XEON_PHI_KNM, rapl_defaults_hsw_server),
1179 MODULE_DEVICE_TABLE(x86cpu, rapl_ids);
1181 /* Read once for all raw primitive data for domains */
1182 static void rapl_update_domain_data(struct rapl_package *rp)
1187 for (dmn = 0; dmn < rp->nr_domains; dmn++) {
1188 pr_debug("update %s domain %s data\n", rp->name,
1189 rp->domains[dmn].name);
1190 /* exclude non-raw primitives */
1191 for (prim = 0; prim < NR_RAW_PRIMITIVES; prim++) {
1192 if (!rapl_read_data_raw(&rp->domains[dmn], prim,
1193 rpi[prim].unit, &val))
1194 rp->domains[dmn].rdd.primitives[prim] = val;
1200 static void rapl_unregister_powercap(void)
1202 if (platform_rapl_domain) {
1203 powercap_unregister_zone(control_type,
1204 &platform_rapl_domain->power_zone);
1205 kfree(platform_rapl_domain);
1207 powercap_unregister_control_type(control_type);
1210 static int rapl_package_register_powercap(struct rapl_package *rp)
1212 struct rapl_domain *rd;
1213 struct powercap_zone *power_zone = NULL;
1216 /* Update the domain data of the new package */
1217 rapl_update_domain_data(rp);
1219 /* first we register package domain as the parent zone*/
1220 for (rd = rp->domains; rd < rp->domains + rp->nr_domains; rd++) {
1221 if (rd->id == RAPL_DOMAIN_PACKAGE) {
1222 nr_pl = find_nr_power_limit(rd);
1223 pr_debug("register package domain %s\n", rp->name);
1224 power_zone = powercap_register_zone(&rd->power_zone,
1230 if (IS_ERR(power_zone)) {
1231 pr_debug("failed to register power zone %s\n",
1233 return PTR_ERR(power_zone);
1235 /* track parent zone in per package/socket data */
1236 rp->power_zone = power_zone;
1237 /* done, only one package domain per socket */
1242 pr_err("no package domain found, unknown topology!\n");
1245 /* now register domains as children of the socket/package*/
1246 for (rd = rp->domains; rd < rp->domains + rp->nr_domains; rd++) {
1247 if (rd->id == RAPL_DOMAIN_PACKAGE)
1249 /* number of power limits per domain varies */
1250 nr_pl = find_nr_power_limit(rd);
1251 power_zone = powercap_register_zone(&rd->power_zone,
1252 control_type, rd->name,
1254 &zone_ops[rd->id], nr_pl,
1257 if (IS_ERR(power_zone)) {
1258 pr_debug("failed to register power_zone, %s:%s\n",
1259 rp->name, rd->name);
1260 ret = PTR_ERR(power_zone);
1268 * Clean up previously initialized domains within the package if we
1269 * failed after the first domain setup.
1271 while (--rd >= rp->domains) {
1272 pr_debug("unregister %s domain %s\n", rp->name, rd->name);
1273 powercap_unregister_zone(control_type, &rd->power_zone);
1279 static int __init rapl_register_psys(void)
1281 struct rapl_domain *rd;
1282 struct powercap_zone *power_zone;
1285 if (rdmsrl_safe_on_cpu(0, MSR_PLATFORM_ENERGY_STATUS, &val) || !val)
1288 if (rdmsrl_safe_on_cpu(0, MSR_PLATFORM_POWER_LIMIT, &val) || !val)
1291 rd = kzalloc(sizeof(*rd), GFP_KERNEL);
1295 rd->name = rapl_domain_names[RAPL_DOMAIN_PLATFORM];
1296 rd->id = RAPL_DOMAIN_PLATFORM;
1297 rd->msrs[0] = MSR_PLATFORM_POWER_LIMIT;
1298 rd->msrs[1] = MSR_PLATFORM_ENERGY_STATUS;
1299 rd->rpl[0].prim_id = PL1_ENABLE;
1300 rd->rpl[0].name = pl1_name;
1301 rd->rpl[1].prim_id = PL2_ENABLE;
1302 rd->rpl[1].name = pl2_name;
1303 rd->rp = rapl_find_package_domain(0);
1305 power_zone = powercap_register_zone(&rd->power_zone, control_type,
1307 &zone_ops[RAPL_DOMAIN_PLATFORM],
1308 2, &constraint_ops);
1310 if (IS_ERR(power_zone)) {
1312 return PTR_ERR(power_zone);
1315 platform_rapl_domain = rd;
1320 static int __init rapl_register_powercap(void)
1322 control_type = powercap_register_control_type(NULL, "intel-rapl", NULL);
1323 if (IS_ERR(control_type)) {
1324 pr_debug("failed to register powercap control_type.\n");
1325 return PTR_ERR(control_type);
1330 static int rapl_check_domain(int cpu, int domain)
1336 case RAPL_DOMAIN_PACKAGE:
1337 msr = MSR_PKG_ENERGY_STATUS;
1339 case RAPL_DOMAIN_PP0:
1340 msr = MSR_PP0_ENERGY_STATUS;
1342 case RAPL_DOMAIN_PP1:
1343 msr = MSR_PP1_ENERGY_STATUS;
1345 case RAPL_DOMAIN_DRAM:
1346 msr = MSR_DRAM_ENERGY_STATUS;
1348 case RAPL_DOMAIN_PLATFORM:
1349 /* PSYS(PLATFORM) is not a CPU domain, so avoid printng error */
1352 pr_err("invalid domain id %d\n", domain);
1355 /* make sure domain counters are available and contains non-zero
1356 * values, otherwise skip it.
1358 if (rdmsrl_safe_on_cpu(cpu, msr, &val) || !val)
1366 * Check if power limits are available. Two cases when they are not available:
1367 * 1. Locked by BIOS, in this case we still provide read-only access so that
1368 * users can see what limit is set by the BIOS.
1369 * 2. Some CPUs make some domains monitoring only which means PLx MSRs may not
1370 * exist at all. In this case, we do not show the contraints in powercap.
1372 * Called after domains are detected and initialized.
1374 static void rapl_detect_powerlimit(struct rapl_domain *rd)
1379 /* check if the domain is locked by BIOS, ignore if MSR doesn't exist */
1380 if (!rapl_read_data_raw(rd, FW_LOCK, false, &val64)) {
1382 pr_info("RAPL %s domain %s locked by BIOS\n",
1383 rd->rp->name, rd->name);
1384 rd->state |= DOMAIN_STATE_BIOS_LOCKED;
1387 /* check if power limit MSRs exists, otherwise domain is monitoring only */
1388 for (i = 0; i < NR_POWER_LIMITS; i++) {
1389 int prim = rd->rpl[i].prim_id;
1390 if (rapl_read_data_raw(rd, prim, false, &val64))
1391 rd->rpl[i].name = NULL;
1395 /* Detect active and valid domains for the given CPU, caller must
1396 * ensure the CPU belongs to the targeted package and CPU hotlug is disabled.
1398 static int rapl_detect_domains(struct rapl_package *rp, int cpu)
1400 struct rapl_domain *rd;
1403 for (i = 0; i < RAPL_DOMAIN_MAX; i++) {
1404 /* use physical package id to read counters */
1405 if (!rapl_check_domain(cpu, i)) {
1406 rp->domain_map |= 1 << i;
1407 pr_info("Found RAPL domain %s\n", rapl_domain_names[i]);
1410 rp->nr_domains = bitmap_weight(&rp->domain_map, RAPL_DOMAIN_MAX);
1411 if (!rp->nr_domains) {
1412 pr_debug("no valid rapl domains found in %s\n", rp->name);
1415 pr_debug("found %d domains on %s\n", rp->nr_domains, rp->name);
1417 rp->domains = kcalloc(rp->nr_domains + 1, sizeof(struct rapl_domain),
1422 rapl_init_domains(rp);
1424 for (rd = rp->domains; rd < rp->domains + rp->nr_domains; rd++)
1425 rapl_detect_powerlimit(rd);
1430 /* called from CPU hotplug notifier, hotplug lock held */
1431 static void rapl_remove_package(struct rapl_package *rp)
1433 struct rapl_domain *rd, *rd_package = NULL;
1435 package_power_limit_irq_restore(rp);
1437 for (rd = rp->domains; rd < rp->domains + rp->nr_domains; rd++) {
1438 rapl_write_data_raw(rd, PL1_ENABLE, 0);
1439 rapl_write_data_raw(rd, PL1_CLAMP, 0);
1440 if (find_nr_power_limit(rd) > 1) {
1441 rapl_write_data_raw(rd, PL2_ENABLE, 0);
1442 rapl_write_data_raw(rd, PL2_CLAMP, 0);
1444 if (rd->id == RAPL_DOMAIN_PACKAGE) {
1448 pr_debug("remove package, undo power limit on %s: %s\n",
1449 rp->name, rd->name);
1450 powercap_unregister_zone(control_type, &rd->power_zone);
1452 /* do parent zone last */
1453 powercap_unregister_zone(control_type, &rd_package->power_zone);
1454 list_del(&rp->plist);
1458 /* called from CPU hotplug notifier, hotplug lock held */
1459 static struct rapl_package *rapl_add_package(int cpu)
1461 int id = topology_logical_die_id(cpu);
1462 struct rapl_package *rp;
1463 struct cpuinfo_x86 *c = &cpu_data(cpu);
1466 rp = kzalloc(sizeof(struct rapl_package), GFP_KERNEL);
1468 return ERR_PTR(-ENOMEM);
1470 /* add the new package to the list */
1474 if (topology_max_die_per_package() > 1)
1475 snprintf(rp->name, PACKAGE_DOMAIN_NAME_LENGTH,
1476 "package-%d-die-%d", c->phys_proc_id, c->cpu_die_id);
1478 snprintf(rp->name, PACKAGE_DOMAIN_NAME_LENGTH, "package-%d",
1481 /* check if the package contains valid domains */
1482 if (rapl_detect_domains(rp, cpu) ||
1483 rapl_defaults->check_unit(rp, cpu)) {
1485 goto err_free_package;
1487 ret = rapl_package_register_powercap(rp);
1489 INIT_LIST_HEAD(&rp->plist);
1490 list_add(&rp->plist, &rapl_packages);
1497 return ERR_PTR(ret);
1500 /* Handles CPU hotplug on multi-socket systems.
1501 * If a CPU goes online as the first CPU of the physical package
1502 * we add the RAPL package to the system. Similarly, when the last
1503 * CPU of the package is removed, we remove the RAPL package and its
1504 * associated domains. Cooling devices are handled accordingly at
1507 static int rapl_cpu_online(unsigned int cpu)
1509 struct rapl_package *rp;
1511 rp = rapl_find_package_domain(cpu);
1513 rp = rapl_add_package(cpu);
1517 cpumask_set_cpu(cpu, &rp->cpumask);
1521 static int rapl_cpu_down_prep(unsigned int cpu)
1523 struct rapl_package *rp;
1526 rp = rapl_find_package_domain(cpu);
1530 cpumask_clear_cpu(cpu, &rp->cpumask);
1531 lead_cpu = cpumask_first(&rp->cpumask);
1532 if (lead_cpu >= nr_cpu_ids)
1533 rapl_remove_package(rp);
1534 else if (rp->lead_cpu == cpu)
1535 rp->lead_cpu = lead_cpu;
1539 static enum cpuhp_state pcap_rapl_online;
1541 static void power_limit_state_save(void)
1543 struct rapl_package *rp;
1544 struct rapl_domain *rd;
1548 list_for_each_entry(rp, &rapl_packages, plist) {
1549 if (!rp->power_zone)
1551 rd = power_zone_to_rapl_domain(rp->power_zone);
1552 nr_pl = find_nr_power_limit(rd);
1553 for (i = 0; i < nr_pl; i++) {
1554 switch (rd->rpl[i].prim_id) {
1556 ret = rapl_read_data_raw(rd,
1559 &rd->rpl[i].last_power_limit);
1561 rd->rpl[i].last_power_limit = 0;
1564 ret = rapl_read_data_raw(rd,
1567 &rd->rpl[i].last_power_limit);
1569 rd->rpl[i].last_power_limit = 0;
1577 static void power_limit_state_restore(void)
1579 struct rapl_package *rp;
1580 struct rapl_domain *rd;
1584 list_for_each_entry(rp, &rapl_packages, plist) {
1585 if (!rp->power_zone)
1587 rd = power_zone_to_rapl_domain(rp->power_zone);
1588 nr_pl = find_nr_power_limit(rd);
1589 for (i = 0; i < nr_pl; i++) {
1590 switch (rd->rpl[i].prim_id) {
1592 if (rd->rpl[i].last_power_limit)
1593 rapl_write_data_raw(rd,
1595 rd->rpl[i].last_power_limit);
1598 if (rd->rpl[i].last_power_limit)
1599 rapl_write_data_raw(rd,
1601 rd->rpl[i].last_power_limit);
1609 static int rapl_pm_callback(struct notifier_block *nb,
1610 unsigned long mode, void *_unused)
1613 case PM_SUSPEND_PREPARE:
1614 power_limit_state_save();
1616 case PM_POST_SUSPEND:
1617 power_limit_state_restore();
1623 static struct notifier_block rapl_pm_notifier = {
1624 .notifier_call = rapl_pm_callback,
1627 static int __init rapl_init(void)
1629 const struct x86_cpu_id *id;
1632 id = x86_match_cpu(rapl_ids);
1634 pr_err("driver does not support CPU family %d model %d\n",
1635 boot_cpu_data.x86, boot_cpu_data.x86_model);
1640 rapl_defaults = (struct rapl_defaults *)id->driver_data;
1642 ret = rapl_register_powercap();
1646 ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "powercap/rapl:online",
1647 rapl_cpu_online, rapl_cpu_down_prep);
1650 pcap_rapl_online = ret;
1652 /* Don't bail out if PSys is not supported */
1653 rapl_register_psys();
1655 ret = register_pm_notifier(&rapl_pm_notifier);
1662 cpuhp_remove_state(pcap_rapl_online);
1665 rapl_unregister_powercap();
1669 static void __exit rapl_exit(void)
1671 unregister_pm_notifier(&rapl_pm_notifier);
1672 cpuhp_remove_state(pcap_rapl_online);
1673 rapl_unregister_powercap();
1676 module_init(rapl_init);
1677 module_exit(rapl_exit);
1679 MODULE_DESCRIPTION("Driver for Intel RAPL (Running Average Power Limit)");
1680 MODULE_AUTHOR("Jacob Pan <jacob.jun.pan@intel.com>");
1681 MODULE_LICENSE("GPL v2");