1 // SPDX-License-Identifier: GPL-2.0-only
3 * Intel Running Average Power Limit (RAPL) Driver
4 * Copyright (c) 2013, Intel Corporation.
6 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
8 #include <linux/kernel.h>
9 #include <linux/module.h>
10 #include <linux/list.h>
11 #include <linux/types.h>
12 #include <linux/device.h>
13 #include <linux/slab.h>
14 #include <linux/log2.h>
15 #include <linux/bitmap.h>
16 #include <linux/delay.h>
17 #include <linux/sysfs.h>
18 #include <linux/cpu.h>
19 #include <linux/powercap.h>
20 #include <linux/suspend.h>
21 #include <asm/iosf_mbi.h>
23 #include <asm/processor.h>
24 #include <asm/cpu_device_id.h>
25 #include <asm/intel-family.h>
28 #define MSR_PLATFORM_POWER_LIMIT 0x0000065C
30 /* bitmasks for RAPL MSRs, used by primitive access functions */
31 #define ENERGY_STATUS_MASK 0xffffffff
33 #define POWER_LIMIT1_MASK 0x7FFF
34 #define POWER_LIMIT1_ENABLE BIT(15)
35 #define POWER_LIMIT1_CLAMP BIT(16)
37 #define POWER_LIMIT2_MASK (0x7FFFULL<<32)
38 #define POWER_LIMIT2_ENABLE BIT_ULL(47)
39 #define POWER_LIMIT2_CLAMP BIT_ULL(48)
40 #define POWER_PACKAGE_LOCK BIT_ULL(63)
41 #define POWER_PP_LOCK BIT(31)
43 #define TIME_WINDOW1_MASK (0x7FULL<<17)
44 #define TIME_WINDOW2_MASK (0x7FULL<<49)
46 #define POWER_UNIT_OFFSET 0
47 #define POWER_UNIT_MASK 0x0F
49 #define ENERGY_UNIT_OFFSET 0x08
50 #define ENERGY_UNIT_MASK 0x1F00
52 #define TIME_UNIT_OFFSET 0x10
53 #define TIME_UNIT_MASK 0xF0000
55 #define POWER_INFO_MAX_MASK (0x7fffULL<<32)
56 #define POWER_INFO_MIN_MASK (0x7fffULL<<16)
57 #define POWER_INFO_MAX_TIME_WIN_MASK (0x3fULL<<48)
58 #define POWER_INFO_THERMAL_SPEC_MASK 0x7fff
60 #define PERF_STATUS_THROTTLE_TIME_MASK 0xffffffff
61 #define PP_POLICY_MASK 0x1F
63 /* Non HW constants */
64 #define RAPL_PRIMITIVE_DERIVED BIT(1) /* not from raw data */
65 #define RAPL_PRIMITIVE_DUMMY BIT(2)
67 #define TIME_WINDOW_MAX_MSEC 40000
68 #define TIME_WINDOW_MIN_MSEC 250
69 #define ENERGY_UNIT_SCALE 1000 /* scale from driver unit to powercap unit */
71 ARBITRARY_UNIT, /* no translation */
77 enum rapl_domain_type {
78 RAPL_DOMAIN_PACKAGE, /* entire package/socket */
79 RAPL_DOMAIN_PP0, /* core power plane */
80 RAPL_DOMAIN_PP1, /* graphics uncore */
81 RAPL_DOMAIN_DRAM,/* DRAM control_type */
82 RAPL_DOMAIN_PLATFORM, /* PSys control_type */
86 enum rapl_domain_reg_id {
87 RAPL_DOMAIN_REG_LIMIT,
88 RAPL_DOMAIN_REG_STATUS,
90 RAPL_DOMAIN_REG_POLICY,
95 /* per domain data, some are optional */
96 enum rapl_primitives {
102 PL1_ENABLE, /* power limit 1, aka long term */
103 PL1_CLAMP, /* allow frequency to go below OS request */
104 PL2_ENABLE, /* power limit 2, aka short term, instantaneous */
107 TIME_WINDOW1, /* long term */
108 TIME_WINDOW2, /* short term */
117 /* below are not raw primitive data */
122 #define NR_RAW_PRIMITIVES (NR_RAPL_PRIMITIVES - 2)
124 /* Can be expanded to include events, etc.*/
125 struct rapl_domain_data {
126 u64 primitives[NR_RAPL_PRIMITIVES];
127 unsigned long timestamp;
137 #define DOMAIN_STATE_INACTIVE BIT(0)
138 #define DOMAIN_STATE_POWER_LIMIT_SET BIT(1)
139 #define DOMAIN_STATE_BIOS_LOCKED BIT(2)
141 #define NR_POWER_LIMITS (2)
142 struct rapl_power_limit {
143 struct powercap_zone_constraint *constraint;
144 int prim_id; /* primitive ID used to enable */
145 struct rapl_domain *domain;
147 u64 last_power_limit;
150 static const char pl1_name[] = "long_term";
151 static const char pl2_name[] = "short_term";
156 enum rapl_domain_type id;
157 int regs[RAPL_DOMAIN_REG_MAX];
158 struct powercap_zone power_zone;
159 struct rapl_domain_data rdd;
160 struct rapl_power_limit rpl[NR_POWER_LIMITS];
161 u64 attr_map; /* track capabilities */
163 unsigned int domain_energy_unit;
164 struct rapl_package *rp;
166 #define power_zone_to_rapl_domain(_zone) \
167 container_of(_zone, struct rapl_domain, power_zone)
169 /* maximum rapl package domain name: package-%d-die-%d */
170 #define PACKAGE_DOMAIN_NAME_LENGTH 30
173 /* Each rapl package contains multiple domains, these are the common
174 * data across RAPL domains within a package.
176 struct rapl_package {
177 unsigned int id; /* logical die id, equals physical 1-die systems */
178 unsigned int nr_domains;
179 unsigned long domain_map; /* bit map of active domains */
180 unsigned int power_unit;
181 unsigned int energy_unit;
182 unsigned int time_unit;
183 struct rapl_domain *domains; /* array of domains, sized at runtime */
184 struct powercap_zone *power_zone; /* keep track of parent zone */
185 unsigned long power_limit_irq; /* keep track of package power limit
186 * notify interrupt enable status.
188 struct list_head plist;
189 int lead_cpu; /* one active cpu per package for access */
190 /* Track active cpus */
191 struct cpumask cpumask;
192 char name[PACKAGE_DOMAIN_NAME_LENGTH];
195 struct rapl_defaults {
196 u8 floor_freq_reg_addr;
197 int (*check_unit)(struct rapl_package *rp, int cpu);
198 void (*set_floor_freq)(struct rapl_domain *rd, bool mode);
199 u64 (*compute_time_window)(struct rapl_package *rp, u64 val,
201 unsigned int dram_domain_energy_unit;
203 static struct rapl_defaults *rapl_defaults;
205 /* Sideband MBI registers */
206 #define IOSF_CPU_POWER_BUDGET_CTL_BYT (0x2)
207 #define IOSF_CPU_POWER_BUDGET_CTL_TNG (0xdf)
209 #define PACKAGE_PLN_INT_SAVED BIT(0)
210 #define MAX_PRIM_NAME (32)
212 /* per domain data. used to describe individual knobs such that access function
213 * can be consolidated into one instead of many inline functions.
215 struct rapl_primitive_info {
219 enum rapl_domain_reg_id id;
224 #define PRIMITIVE_INFO_INIT(p, m, s, i, u, f) { \
233 static void rapl_init_domains(struct rapl_package *rp);
234 static int rapl_read_data_raw(struct rapl_domain *rd,
235 enum rapl_primitives prim,
236 bool xlate, u64 *data);
237 static int rapl_write_data_raw(struct rapl_domain *rd,
238 enum rapl_primitives prim,
239 unsigned long long value);
240 static u64 rapl_unit_xlate(struct rapl_domain *rd,
241 enum unit_type type, u64 value,
243 static void package_power_limit_irq_save(struct rapl_package *rp);
245 static LIST_HEAD(rapl_packages); /* guarded by CPU hotplug lock */
247 static const char * const rapl_domain_names[] = {
255 static struct powercap_control_type *control_type; /* PowerCap Controller */
256 static struct rapl_domain *platform_rapl_domain; /* Platform (PSys) domain */
258 /* caller to ensure CPU hotplug lock is held */
259 static struct rapl_package *rapl_find_package_domain(int cpu)
261 int id = topology_logical_die_id(cpu);
262 struct rapl_package *rp;
264 list_for_each_entry(rp, &rapl_packages, plist) {
272 static int get_energy_counter(struct powercap_zone *power_zone, u64 *energy_raw)
274 struct rapl_domain *rd;
277 /* prevent CPU hotplug, make sure the RAPL domain does not go
278 * away while reading the counter.
281 rd = power_zone_to_rapl_domain(power_zone);
283 if (!rapl_read_data_raw(rd, ENERGY_COUNTER, true, &energy_now)) {
284 *energy_raw = energy_now;
294 static int get_max_energy_counter(struct powercap_zone *pcd_dev, u64 *energy)
296 struct rapl_domain *rd = power_zone_to_rapl_domain(pcd_dev);
298 *energy = rapl_unit_xlate(rd, ENERGY_UNIT, ENERGY_STATUS_MASK, 0);
302 static int release_zone(struct powercap_zone *power_zone)
304 struct rapl_domain *rd = power_zone_to_rapl_domain(power_zone);
305 struct rapl_package *rp = rd->rp;
307 /* package zone is the last zone of a package, we can free
308 * memory here since all children has been unregistered.
310 if (rd->id == RAPL_DOMAIN_PACKAGE) {
319 static int find_nr_power_limit(struct rapl_domain *rd)
323 for (i = 0; i < NR_POWER_LIMITS; i++) {
331 static int set_domain_enable(struct powercap_zone *power_zone, bool mode)
333 struct rapl_domain *rd = power_zone_to_rapl_domain(power_zone);
335 if (rd->state & DOMAIN_STATE_BIOS_LOCKED)
339 rapl_write_data_raw(rd, PL1_ENABLE, mode);
340 if (rapl_defaults->set_floor_freq)
341 rapl_defaults->set_floor_freq(rd, mode);
347 static int get_domain_enable(struct powercap_zone *power_zone, bool *mode)
349 struct rapl_domain *rd = power_zone_to_rapl_domain(power_zone);
352 if (rd->state & DOMAIN_STATE_BIOS_LOCKED) {
357 if (rapl_read_data_raw(rd, PL1_ENABLE, true, &val)) {
367 /* per RAPL domain ops, in the order of rapl_domain_type */
368 static const struct powercap_zone_ops zone_ops[] = {
369 /* RAPL_DOMAIN_PACKAGE */
371 .get_energy_uj = get_energy_counter,
372 .get_max_energy_range_uj = get_max_energy_counter,
373 .release = release_zone,
374 .set_enable = set_domain_enable,
375 .get_enable = get_domain_enable,
377 /* RAPL_DOMAIN_PP0 */
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_PP1 */
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_DRAM */
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_PLATFORM */
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,
413 * Constraint index used by powercap can be different than power limit (PL)
414 * index in that some PLs maybe missing due to non-existant MSRs. So we
415 * need to convert here by finding the valid PLs only (name populated).
417 static int contraint_to_pl(struct rapl_domain *rd, int cid)
421 for (i = 0, j = 0; i < NR_POWER_LIMITS; i++) {
422 if ((rd->rpl[i].name) && j++ == cid) {
423 pr_debug("%s: index %d\n", __func__, i);
427 pr_err("Cannot find matching power limit for constraint %d\n", cid);
432 static int set_power_limit(struct powercap_zone *power_zone, int cid,
435 struct rapl_domain *rd;
436 struct rapl_package *rp;
441 rd = power_zone_to_rapl_domain(power_zone);
442 id = contraint_to_pl(rd, cid);
450 if (rd->state & DOMAIN_STATE_BIOS_LOCKED) {
451 dev_warn(&power_zone->dev, "%s locked by BIOS, monitoring only\n",
457 switch (rd->rpl[id].prim_id) {
459 rapl_write_data_raw(rd, POWER_LIMIT1, power_limit);
462 rapl_write_data_raw(rd, POWER_LIMIT2, power_limit);
468 package_power_limit_irq_save(rp);
474 static int get_current_power_limit(struct powercap_zone *power_zone, int cid,
477 struct rapl_domain *rd;
484 rd = power_zone_to_rapl_domain(power_zone);
485 id = contraint_to_pl(rd, cid);
491 switch (rd->rpl[id].prim_id) {
502 if (rapl_read_data_raw(rd, prim, true, &val))
513 static int set_time_window(struct powercap_zone *power_zone, int cid,
516 struct rapl_domain *rd;
521 rd = power_zone_to_rapl_domain(power_zone);
522 id = contraint_to_pl(rd, cid);
528 switch (rd->rpl[id].prim_id) {
530 rapl_write_data_raw(rd, TIME_WINDOW1, window);
533 rapl_write_data_raw(rd, TIME_WINDOW2, window);
544 static int get_time_window(struct powercap_zone *power_zone, int cid, u64 *data)
546 struct rapl_domain *rd;
552 rd = power_zone_to_rapl_domain(power_zone);
553 id = contraint_to_pl(rd, cid);
559 switch (rd->rpl[id].prim_id) {
561 ret = rapl_read_data_raw(rd, TIME_WINDOW1, true, &val);
564 ret = rapl_read_data_raw(rd, TIME_WINDOW2, true, &val);
579 static const char *get_constraint_name(struct powercap_zone *power_zone, int cid)
581 struct rapl_domain *rd;
584 rd = power_zone_to_rapl_domain(power_zone);
585 id = contraint_to_pl(rd, cid);
587 return rd->rpl[id].name;
593 static int get_max_power(struct powercap_zone *power_zone, int id,
596 struct rapl_domain *rd;
602 rd = power_zone_to_rapl_domain(power_zone);
603 switch (rd->rpl[id].prim_id) {
605 prim = THERMAL_SPEC_POWER;
614 if (rapl_read_data_raw(rd, prim, true, &val))
624 static const struct powercap_zone_constraint_ops constraint_ops = {
625 .set_power_limit_uw = set_power_limit,
626 .get_power_limit_uw = get_current_power_limit,
627 .set_time_window_us = set_time_window,
628 .get_time_window_us = get_time_window,
629 .get_max_power_uw = get_max_power,
630 .get_name = get_constraint_name,
633 /* called after domain detection and package level data are set */
634 static void rapl_init_domains(struct rapl_package *rp)
637 struct rapl_domain *rd = rp->domains;
639 for (i = 0; i < RAPL_DOMAIN_MAX; i++) {
640 unsigned int mask = rp->domain_map & (1 << i);
642 case BIT(RAPL_DOMAIN_PACKAGE):
643 rd->name = rapl_domain_names[RAPL_DOMAIN_PACKAGE];
644 rd->id = RAPL_DOMAIN_PACKAGE;
645 rd->regs[RAPL_DOMAIN_REG_LIMIT] = MSR_PKG_POWER_LIMIT;
646 rd->regs[RAPL_DOMAIN_REG_STATUS] = MSR_PKG_ENERGY_STATUS;
647 rd->regs[RAPL_DOMAIN_REG_PERF] = MSR_PKG_PERF_STATUS;
648 rd->regs[RAPL_DOMAIN_REG_POLICY] = 0;
649 rd->regs[RAPL_DOMAIN_REG_INFO] = MSR_PKG_POWER_INFO;
650 rd->rpl[0].prim_id = PL1_ENABLE;
651 rd->rpl[0].name = pl1_name;
652 rd->rpl[1].prim_id = PL2_ENABLE;
653 rd->rpl[1].name = pl2_name;
655 case BIT(RAPL_DOMAIN_PP0):
656 rd->name = rapl_domain_names[RAPL_DOMAIN_PP0];
657 rd->id = RAPL_DOMAIN_PP0;
658 rd->regs[RAPL_DOMAIN_REG_LIMIT] = MSR_PP0_POWER_LIMIT;
659 rd->regs[RAPL_DOMAIN_REG_STATUS] = MSR_PP0_ENERGY_STATUS;
660 rd->regs[RAPL_DOMAIN_REG_PERF] = 0;
661 rd->regs[RAPL_DOMAIN_REG_POLICY] = MSR_PP0_POLICY;
662 rd->regs[RAPL_DOMAIN_REG_INFO] = 0;
663 rd->rpl[0].prim_id = PL1_ENABLE;
664 rd->rpl[0].name = pl1_name;
666 case BIT(RAPL_DOMAIN_PP1):
667 rd->name = rapl_domain_names[RAPL_DOMAIN_PP1];
668 rd->id = RAPL_DOMAIN_PP1;
669 rd->regs[RAPL_DOMAIN_REG_LIMIT] = MSR_PP1_POWER_LIMIT;
670 rd->regs[RAPL_DOMAIN_REG_STATUS] = MSR_PP1_ENERGY_STATUS;
671 rd->regs[RAPL_DOMAIN_REG_PERF] = 0;
672 rd->regs[RAPL_DOMAIN_REG_POLICY] = MSR_PP1_POLICY;
673 rd->regs[RAPL_DOMAIN_REG_INFO] = 0;
674 rd->rpl[0].prim_id = PL1_ENABLE;
675 rd->rpl[0].name = pl1_name;
677 case BIT(RAPL_DOMAIN_DRAM):
678 rd->name = rapl_domain_names[RAPL_DOMAIN_DRAM];
679 rd->id = RAPL_DOMAIN_DRAM;
680 rd->regs[RAPL_DOMAIN_REG_LIMIT] = MSR_DRAM_POWER_LIMIT;
681 rd->regs[RAPL_DOMAIN_REG_STATUS] = MSR_DRAM_ENERGY_STATUS;
682 rd->regs[RAPL_DOMAIN_REG_PERF] = MSR_DRAM_PERF_STATUS;
683 rd->regs[RAPL_DOMAIN_REG_POLICY] = 0;
684 rd->regs[RAPL_DOMAIN_REG_INFO] = MSR_DRAM_POWER_INFO;
685 rd->rpl[0].prim_id = PL1_ENABLE;
686 rd->rpl[0].name = pl1_name;
687 rd->domain_energy_unit =
688 rapl_defaults->dram_domain_energy_unit;
689 if (rd->domain_energy_unit)
690 pr_info("DRAM domain energy unit %dpj\n",
691 rd->domain_energy_unit);
701 static u64 rapl_unit_xlate(struct rapl_domain *rd, enum unit_type type,
702 u64 value, int to_raw)
705 struct rapl_package *rp = rd->rp;
710 units = rp->power_unit;
713 scale = ENERGY_UNIT_SCALE;
714 /* per domain unit takes precedence */
715 if (rd->domain_energy_unit)
716 units = rd->domain_energy_unit;
718 units = rp->energy_unit;
721 return rapl_defaults->compute_time_window(rp, value, to_raw);
728 return div64_u64(value, units) * scale;
732 return div64_u64(value, scale);
735 /* in the order of enum rapl_primitives */
736 static struct rapl_primitive_info rpi[] = {
737 /* name, mask, shift, msr index, unit divisor */
738 PRIMITIVE_INFO_INIT(ENERGY_COUNTER, ENERGY_STATUS_MASK, 0,
739 RAPL_DOMAIN_REG_STATUS, ENERGY_UNIT, 0),
740 PRIMITIVE_INFO_INIT(POWER_LIMIT1, POWER_LIMIT1_MASK, 0,
741 RAPL_DOMAIN_REG_LIMIT, POWER_UNIT, 0),
742 PRIMITIVE_INFO_INIT(POWER_LIMIT2, POWER_LIMIT2_MASK, 32,
743 RAPL_DOMAIN_REG_LIMIT, POWER_UNIT, 0),
744 PRIMITIVE_INFO_INIT(FW_LOCK, POWER_PP_LOCK, 31,
745 RAPL_DOMAIN_REG_LIMIT, ARBITRARY_UNIT, 0),
746 PRIMITIVE_INFO_INIT(PL1_ENABLE, POWER_LIMIT1_ENABLE, 15,
747 RAPL_DOMAIN_REG_LIMIT, ARBITRARY_UNIT, 0),
748 PRIMITIVE_INFO_INIT(PL1_CLAMP, POWER_LIMIT1_CLAMP, 16,
749 RAPL_DOMAIN_REG_LIMIT, ARBITRARY_UNIT, 0),
750 PRIMITIVE_INFO_INIT(PL2_ENABLE, POWER_LIMIT2_ENABLE, 47,
751 RAPL_DOMAIN_REG_LIMIT, ARBITRARY_UNIT, 0),
752 PRIMITIVE_INFO_INIT(PL2_CLAMP, POWER_LIMIT2_CLAMP, 48,
753 RAPL_DOMAIN_REG_LIMIT, ARBITRARY_UNIT, 0),
754 PRIMITIVE_INFO_INIT(TIME_WINDOW1, TIME_WINDOW1_MASK, 17,
755 RAPL_DOMAIN_REG_LIMIT, TIME_UNIT, 0),
756 PRIMITIVE_INFO_INIT(TIME_WINDOW2, TIME_WINDOW2_MASK, 49,
757 RAPL_DOMAIN_REG_LIMIT, TIME_UNIT, 0),
758 PRIMITIVE_INFO_INIT(THERMAL_SPEC_POWER, POWER_INFO_THERMAL_SPEC_MASK,
759 0, RAPL_DOMAIN_REG_INFO, POWER_UNIT, 0),
760 PRIMITIVE_INFO_INIT(MAX_POWER, POWER_INFO_MAX_MASK, 32,
761 RAPL_DOMAIN_REG_INFO, POWER_UNIT, 0),
762 PRIMITIVE_INFO_INIT(MIN_POWER, POWER_INFO_MIN_MASK, 16,
763 RAPL_DOMAIN_REG_INFO, POWER_UNIT, 0),
764 PRIMITIVE_INFO_INIT(MAX_TIME_WINDOW, POWER_INFO_MAX_TIME_WIN_MASK, 48,
765 RAPL_DOMAIN_REG_INFO, TIME_UNIT, 0),
766 PRIMITIVE_INFO_INIT(THROTTLED_TIME, PERF_STATUS_THROTTLE_TIME_MASK, 0,
767 RAPL_DOMAIN_REG_PERF, TIME_UNIT, 0),
768 PRIMITIVE_INFO_INIT(PRIORITY_LEVEL, PP_POLICY_MASK, 0,
769 RAPL_DOMAIN_REG_POLICY, ARBITRARY_UNIT, 0),
771 PRIMITIVE_INFO_INIT(AVERAGE_POWER, 0, 0, 0, POWER_UNIT,
772 RAPL_PRIMITIVE_DERIVED),
776 /* Read primitive data based on its related struct rapl_primitive_info.
777 * if xlate flag is set, return translated data based on data units, i.e.
778 * time, energy, and power.
779 * RAPL MSRs are non-architectual and are laid out not consistently across
780 * domains. Here we use primitive info to allow writing consolidated access
782 * For a given primitive, it is processed by MSR mask and shift. Unit conversion
783 * is pre-assigned based on RAPL unit MSRs read at init time.
784 * 63-------------------------- 31--------------------------- 0
786 * | |<- shift ----------------|
787 * 63-------------------------- 31--------------------------- 0
789 static int rapl_read_data_raw(struct rapl_domain *rd,
790 enum rapl_primitives prim,
791 bool xlate, u64 *data)
795 struct rapl_primitive_info *rp = &rpi[prim];
798 if (!rp->name || rp->flag & RAPL_PRIMITIVE_DUMMY)
801 msr = rd->regs[rp->id];
805 cpu = rd->rp->lead_cpu;
807 /* special-case package domain, which uses a different bit*/
808 if (prim == FW_LOCK && rd->id == RAPL_DOMAIN_PACKAGE) {
809 rp->mask = POWER_PACKAGE_LOCK;
812 /* non-hardware data are collected by the polling thread */
813 if (rp->flag & RAPL_PRIMITIVE_DERIVED) {
814 *data = rd->rdd.primitives[prim];
818 if (rdmsrl_safe_on_cpu(cpu, msr, &value)) {
819 pr_debug("failed to read msr 0x%x on cpu %d\n", msr, cpu);
823 final = value & rp->mask;
824 final = final >> rp->shift;
826 *data = rapl_unit_xlate(rd, rp->unit, final, 0);
834 static int msrl_update_safe(u32 msr_no, u64 clear_mask, u64 set_mask)
839 err = rdmsrl_safe(msr_no, &val);
846 err = wrmsrl_safe(msr_no, val);
852 static void msrl_update_func(void *info)
854 struct msrl_action *ma = info;
856 ma->err = msrl_update_safe(ma->msr_no, ma->clear_mask, ma->set_mask);
859 /* Similar use of primitive info in the read counterpart */
860 static int rapl_write_data_raw(struct rapl_domain *rd,
861 enum rapl_primitives prim,
862 unsigned long long value)
864 struct rapl_primitive_info *rp = &rpi[prim];
867 struct msrl_action ma;
870 cpu = rd->rp->lead_cpu;
871 bits = rapl_unit_xlate(rd, rp->unit, value, 1);
875 memset(&ma, 0, sizeof(ma));
877 ma.msr_no = rd->regs[rp->id];
878 ma.clear_mask = rp->mask;
881 ret = smp_call_function_single(cpu, msrl_update_func, &ma, 1);
891 * Raw RAPL data stored in MSRs are in certain scales. We need to
892 * convert them into standard units based on the units reported in
893 * the RAPL unit MSRs. This is specific to CPUs as the method to
894 * calculate units differ on different CPUs.
895 * We convert the units to below format based on CPUs.
897 * energy unit: picoJoules : Represented in picoJoules by default
898 * power unit : microWatts : Represented in milliWatts by default
899 * time unit : microseconds: Represented in seconds by default
901 static int rapl_check_unit_core(struct rapl_package *rp, int cpu)
906 if (rdmsrl_safe_on_cpu(cpu, MSR_RAPL_POWER_UNIT, &msr_val)) {
907 pr_err("Failed to read power unit MSR 0x%x on CPU %d, exit.\n",
908 MSR_RAPL_POWER_UNIT, cpu);
912 value = (msr_val & ENERGY_UNIT_MASK) >> ENERGY_UNIT_OFFSET;
913 rp->energy_unit = ENERGY_UNIT_SCALE * 1000000 / (1 << value);
915 value = (msr_val & POWER_UNIT_MASK) >> POWER_UNIT_OFFSET;
916 rp->power_unit = 1000000 / (1 << value);
918 value = (msr_val & TIME_UNIT_MASK) >> TIME_UNIT_OFFSET;
919 rp->time_unit = 1000000 / (1 << value);
921 pr_debug("Core CPU %s energy=%dpJ, time=%dus, power=%duW\n",
922 rp->name, rp->energy_unit, rp->time_unit, rp->power_unit);
927 static int rapl_check_unit_atom(struct rapl_package *rp, int cpu)
932 if (rdmsrl_safe_on_cpu(cpu, MSR_RAPL_POWER_UNIT, &msr_val)) {
933 pr_err("Failed to read power unit MSR 0x%x on CPU %d, exit.\n",
934 MSR_RAPL_POWER_UNIT, cpu);
937 value = (msr_val & ENERGY_UNIT_MASK) >> ENERGY_UNIT_OFFSET;
938 rp->energy_unit = ENERGY_UNIT_SCALE * 1 << value;
940 value = (msr_val & POWER_UNIT_MASK) >> POWER_UNIT_OFFSET;
941 rp->power_unit = (1 << value) * 1000;
943 value = (msr_val & TIME_UNIT_MASK) >> TIME_UNIT_OFFSET;
944 rp->time_unit = 1000000 / (1 << value);
946 pr_debug("Atom %s energy=%dpJ, time=%dus, power=%duW\n",
947 rp->name, rp->energy_unit, rp->time_unit, rp->power_unit);
952 static void power_limit_irq_save_cpu(void *info)
955 struct rapl_package *rp = (struct rapl_package *)info;
957 /* save the state of PLN irq mask bit before disabling it */
958 rdmsr_safe(MSR_IA32_PACKAGE_THERM_INTERRUPT, &l, &h);
959 if (!(rp->power_limit_irq & PACKAGE_PLN_INT_SAVED)) {
960 rp->power_limit_irq = l & PACKAGE_THERM_INT_PLN_ENABLE;
961 rp->power_limit_irq |= PACKAGE_PLN_INT_SAVED;
963 l &= ~PACKAGE_THERM_INT_PLN_ENABLE;
964 wrmsr_safe(MSR_IA32_PACKAGE_THERM_INTERRUPT, l, h);
969 * When package power limit is set artificially low by RAPL, LVT
970 * thermal interrupt for package power limit should be ignored
971 * since we are not really exceeding the real limit. The intention
972 * is to avoid excessive interrupts while we are trying to save power.
973 * A useful feature might be routing the package_power_limit interrupt
974 * to userspace via eventfd. once we have a usecase, this is simple
975 * to do by adding an atomic notifier.
978 static void package_power_limit_irq_save(struct rapl_package *rp)
980 if (!boot_cpu_has(X86_FEATURE_PTS) || !boot_cpu_has(X86_FEATURE_PLN))
983 smp_call_function_single(rp->lead_cpu, power_limit_irq_save_cpu, rp, 1);
987 * Restore per package power limit interrupt enable state. Called from cpu
988 * hotplug code on package removal.
990 static void package_power_limit_irq_restore(struct rapl_package *rp)
994 if (!boot_cpu_has(X86_FEATURE_PTS) || !boot_cpu_has(X86_FEATURE_PLN))
997 /* irq enable state not saved, nothing to restore */
998 if (!(rp->power_limit_irq & PACKAGE_PLN_INT_SAVED))
1001 rdmsr_safe(MSR_IA32_PACKAGE_THERM_INTERRUPT, &l, &h);
1003 if (rp->power_limit_irq & PACKAGE_THERM_INT_PLN_ENABLE)
1004 l |= PACKAGE_THERM_INT_PLN_ENABLE;
1006 l &= ~PACKAGE_THERM_INT_PLN_ENABLE;
1008 wrmsr_safe(MSR_IA32_PACKAGE_THERM_INTERRUPT, l, h);
1011 static void set_floor_freq_default(struct rapl_domain *rd, bool mode)
1013 int nr_powerlimit = find_nr_power_limit(rd);
1015 /* always enable clamp such that p-state can go below OS requested
1016 * range. power capping priority over guranteed frequency.
1018 rapl_write_data_raw(rd, PL1_CLAMP, mode);
1020 /* some domains have pl2 */
1021 if (nr_powerlimit > 1) {
1022 rapl_write_data_raw(rd, PL2_ENABLE, mode);
1023 rapl_write_data_raw(rd, PL2_CLAMP, mode);
1027 static void set_floor_freq_atom(struct rapl_domain *rd, bool enable)
1029 static u32 power_ctrl_orig_val;
1032 if (!rapl_defaults->floor_freq_reg_addr) {
1033 pr_err("Invalid floor frequency config register\n");
1037 if (!power_ctrl_orig_val)
1038 iosf_mbi_read(BT_MBI_UNIT_PMC, MBI_CR_READ,
1039 rapl_defaults->floor_freq_reg_addr,
1040 &power_ctrl_orig_val);
1041 mdata = power_ctrl_orig_val;
1043 mdata &= ~(0x7f << 8);
1046 iosf_mbi_write(BT_MBI_UNIT_PMC, MBI_CR_WRITE,
1047 rapl_defaults->floor_freq_reg_addr, mdata);
1050 static u64 rapl_compute_time_window_core(struct rapl_package *rp, u64 value,
1053 u64 f, y; /* fraction and exp. used for time unit */
1056 * Special processing based on 2^Y*(1+F/4), refer
1057 * to Intel Software Developer's manual Vol.3B: CH 14.9.3.
1060 f = (value & 0x60) >> 5;
1062 value = (1 << y) * (4 + f) * rp->time_unit / 4;
1064 do_div(value, rp->time_unit);
1066 f = div64_u64(4 * (value - (1 << y)), 1 << y);
1067 value = (y & 0x1f) | ((f & 0x3) << 5);
1072 static u64 rapl_compute_time_window_atom(struct rapl_package *rp, u64 value,
1076 * Atom time unit encoding is straight forward val * time_unit,
1077 * where time_unit is default to 1 sec. Never 0.
1080 return (value) ? value *= rp->time_unit : rp->time_unit;
1082 value = div64_u64(value, rp->time_unit);
1087 static const struct rapl_defaults rapl_defaults_core = {
1088 .floor_freq_reg_addr = 0,
1089 .check_unit = rapl_check_unit_core,
1090 .set_floor_freq = set_floor_freq_default,
1091 .compute_time_window = rapl_compute_time_window_core,
1094 static const struct rapl_defaults rapl_defaults_hsw_server = {
1095 .check_unit = rapl_check_unit_core,
1096 .set_floor_freq = set_floor_freq_default,
1097 .compute_time_window = rapl_compute_time_window_core,
1098 .dram_domain_energy_unit = 15300,
1101 static const struct rapl_defaults rapl_defaults_byt = {
1102 .floor_freq_reg_addr = IOSF_CPU_POWER_BUDGET_CTL_BYT,
1103 .check_unit = rapl_check_unit_atom,
1104 .set_floor_freq = set_floor_freq_atom,
1105 .compute_time_window = rapl_compute_time_window_atom,
1108 static const struct rapl_defaults rapl_defaults_tng = {
1109 .floor_freq_reg_addr = IOSF_CPU_POWER_BUDGET_CTL_TNG,
1110 .check_unit = rapl_check_unit_atom,
1111 .set_floor_freq = set_floor_freq_atom,
1112 .compute_time_window = rapl_compute_time_window_atom,
1115 static const struct rapl_defaults rapl_defaults_ann = {
1116 .floor_freq_reg_addr = 0,
1117 .check_unit = rapl_check_unit_atom,
1118 .set_floor_freq = NULL,
1119 .compute_time_window = rapl_compute_time_window_atom,
1122 static const struct rapl_defaults rapl_defaults_cht = {
1123 .floor_freq_reg_addr = 0,
1124 .check_unit = rapl_check_unit_atom,
1125 .set_floor_freq = NULL,
1126 .compute_time_window = rapl_compute_time_window_atom,
1129 static const struct x86_cpu_id rapl_ids[] __initconst = {
1130 INTEL_CPU_FAM6(SANDYBRIDGE, rapl_defaults_core),
1131 INTEL_CPU_FAM6(SANDYBRIDGE_X, rapl_defaults_core),
1133 INTEL_CPU_FAM6(IVYBRIDGE, rapl_defaults_core),
1134 INTEL_CPU_FAM6(IVYBRIDGE_X, rapl_defaults_core),
1136 INTEL_CPU_FAM6(HASWELL_CORE, rapl_defaults_core),
1137 INTEL_CPU_FAM6(HASWELL_ULT, rapl_defaults_core),
1138 INTEL_CPU_FAM6(HASWELL_GT3E, rapl_defaults_core),
1139 INTEL_CPU_FAM6(HASWELL_X, rapl_defaults_hsw_server),
1141 INTEL_CPU_FAM6(BROADWELL_CORE, rapl_defaults_core),
1142 INTEL_CPU_FAM6(BROADWELL_GT3E, rapl_defaults_core),
1143 INTEL_CPU_FAM6(BROADWELL_XEON_D, rapl_defaults_core),
1144 INTEL_CPU_FAM6(BROADWELL_X, rapl_defaults_hsw_server),
1146 INTEL_CPU_FAM6(SKYLAKE_DESKTOP, rapl_defaults_core),
1147 INTEL_CPU_FAM6(SKYLAKE_MOBILE, rapl_defaults_core),
1148 INTEL_CPU_FAM6(SKYLAKE_X, rapl_defaults_hsw_server),
1149 INTEL_CPU_FAM6(KABYLAKE_MOBILE, rapl_defaults_core),
1150 INTEL_CPU_FAM6(KABYLAKE_DESKTOP, rapl_defaults_core),
1151 INTEL_CPU_FAM6(CANNONLAKE_MOBILE, rapl_defaults_core),
1152 INTEL_CPU_FAM6(ICELAKE_MOBILE, rapl_defaults_core),
1154 INTEL_CPU_FAM6(ATOM_SILVERMONT, rapl_defaults_byt),
1155 INTEL_CPU_FAM6(ATOM_AIRMONT, rapl_defaults_cht),
1156 INTEL_CPU_FAM6(ATOM_SILVERMONT_MID, rapl_defaults_tng),
1157 INTEL_CPU_FAM6(ATOM_AIRMONT_MID, rapl_defaults_ann),
1158 INTEL_CPU_FAM6(ATOM_GOLDMONT, rapl_defaults_core),
1159 INTEL_CPU_FAM6(ATOM_GOLDMONT_PLUS, rapl_defaults_core),
1160 INTEL_CPU_FAM6(ATOM_GOLDMONT_X, rapl_defaults_core),
1161 INTEL_CPU_FAM6(ATOM_TREMONT_X, rapl_defaults_core),
1163 INTEL_CPU_FAM6(XEON_PHI_KNL, rapl_defaults_hsw_server),
1164 INTEL_CPU_FAM6(XEON_PHI_KNM, rapl_defaults_hsw_server),
1167 MODULE_DEVICE_TABLE(x86cpu, rapl_ids);
1169 /* Read once for all raw primitive data for domains */
1170 static void rapl_update_domain_data(struct rapl_package *rp)
1175 for (dmn = 0; dmn < rp->nr_domains; dmn++) {
1176 pr_debug("update %s domain %s data\n", rp->name,
1177 rp->domains[dmn].name);
1178 /* exclude non-raw primitives */
1179 for (prim = 0; prim < NR_RAW_PRIMITIVES; prim++) {
1180 if (!rapl_read_data_raw(&rp->domains[dmn], prim,
1181 rpi[prim].unit, &val))
1182 rp->domains[dmn].rdd.primitives[prim] = val;
1188 static void rapl_unregister_powercap(void)
1190 if (platform_rapl_domain) {
1191 powercap_unregister_zone(control_type,
1192 &platform_rapl_domain->power_zone);
1193 kfree(platform_rapl_domain);
1195 powercap_unregister_control_type(control_type);
1198 static int rapl_package_register_powercap(struct rapl_package *rp)
1200 struct rapl_domain *rd;
1201 struct powercap_zone *power_zone = NULL;
1204 /* Update the domain data of the new package */
1205 rapl_update_domain_data(rp);
1207 /* first we register package domain as the parent zone*/
1208 for (rd = rp->domains; rd < rp->domains + rp->nr_domains; rd++) {
1209 if (rd->id == RAPL_DOMAIN_PACKAGE) {
1210 nr_pl = find_nr_power_limit(rd);
1211 pr_debug("register package domain %s\n", rp->name);
1212 power_zone = powercap_register_zone(&rd->power_zone,
1218 if (IS_ERR(power_zone)) {
1219 pr_debug("failed to register power zone %s\n",
1221 return PTR_ERR(power_zone);
1223 /* track parent zone in per package/socket data */
1224 rp->power_zone = power_zone;
1225 /* done, only one package domain per socket */
1230 pr_err("no package domain found, unknown topology!\n");
1233 /* now register domains as children of the socket/package*/
1234 for (rd = rp->domains; rd < rp->domains + rp->nr_domains; rd++) {
1235 if (rd->id == RAPL_DOMAIN_PACKAGE)
1237 /* number of power limits per domain varies */
1238 nr_pl = find_nr_power_limit(rd);
1239 power_zone = powercap_register_zone(&rd->power_zone,
1240 control_type, rd->name,
1242 &zone_ops[rd->id], nr_pl,
1245 if (IS_ERR(power_zone)) {
1246 pr_debug("failed to register power_zone, %s:%s\n",
1247 rp->name, rd->name);
1248 ret = PTR_ERR(power_zone);
1256 * Clean up previously initialized domains within the package if we
1257 * failed after the first domain setup.
1259 while (--rd >= rp->domains) {
1260 pr_debug("unregister %s domain %s\n", rp->name, rd->name);
1261 powercap_unregister_zone(control_type, &rd->power_zone);
1267 static int __init rapl_register_psys(void)
1269 struct rapl_domain *rd;
1270 struct powercap_zone *power_zone;
1273 if (rdmsrl_safe_on_cpu(0, MSR_PLATFORM_ENERGY_STATUS, &val) || !val)
1276 if (rdmsrl_safe_on_cpu(0, MSR_PLATFORM_POWER_LIMIT, &val) || !val)
1279 rd = kzalloc(sizeof(*rd), GFP_KERNEL);
1283 rd->name = rapl_domain_names[RAPL_DOMAIN_PLATFORM];
1284 rd->id = RAPL_DOMAIN_PLATFORM;
1285 rd->regs[RAPL_DOMAIN_REG_LIMIT] = MSR_PLATFORM_POWER_LIMIT;
1286 rd->regs[RAPL_DOMAIN_REG_STATUS] = MSR_PLATFORM_ENERGY_STATUS;
1287 rd->rpl[0].prim_id = PL1_ENABLE;
1288 rd->rpl[0].name = pl1_name;
1289 rd->rpl[1].prim_id = PL2_ENABLE;
1290 rd->rpl[1].name = pl2_name;
1291 rd->rp = rapl_find_package_domain(0);
1293 power_zone = powercap_register_zone(&rd->power_zone, control_type,
1295 &zone_ops[RAPL_DOMAIN_PLATFORM],
1296 2, &constraint_ops);
1298 if (IS_ERR(power_zone)) {
1300 return PTR_ERR(power_zone);
1303 platform_rapl_domain = rd;
1308 static int __init rapl_register_powercap(void)
1310 control_type = powercap_register_control_type(NULL, "intel-rapl", NULL);
1311 if (IS_ERR(control_type)) {
1312 pr_debug("failed to register powercap control_type.\n");
1313 return PTR_ERR(control_type);
1318 static int rapl_check_domain(int cpu, int domain)
1324 case RAPL_DOMAIN_PACKAGE:
1325 msr = MSR_PKG_ENERGY_STATUS;
1327 case RAPL_DOMAIN_PP0:
1328 msr = MSR_PP0_ENERGY_STATUS;
1330 case RAPL_DOMAIN_PP1:
1331 msr = MSR_PP1_ENERGY_STATUS;
1333 case RAPL_DOMAIN_DRAM:
1334 msr = MSR_DRAM_ENERGY_STATUS;
1336 case RAPL_DOMAIN_PLATFORM:
1337 /* PSYS(PLATFORM) is not a CPU domain, so avoid printng error */
1340 pr_err("invalid domain id %d\n", domain);
1343 /* make sure domain counters are available and contains non-zero
1344 * values, otherwise skip it.
1346 if (rdmsrl_safe_on_cpu(cpu, msr, &val) || !val)
1354 * Check if power limits are available. Two cases when they are not available:
1355 * 1. Locked by BIOS, in this case we still provide read-only access so that
1356 * users can see what limit is set by the BIOS.
1357 * 2. Some CPUs make some domains monitoring only which means PLx MSRs may not
1358 * exist at all. In this case, we do not show the contraints in powercap.
1360 * Called after domains are detected and initialized.
1362 static void rapl_detect_powerlimit(struct rapl_domain *rd)
1367 /* check if the domain is locked by BIOS, ignore if MSR doesn't exist */
1368 if (!rapl_read_data_raw(rd, FW_LOCK, false, &val64)) {
1370 pr_info("RAPL %s domain %s locked by BIOS\n",
1371 rd->rp->name, rd->name);
1372 rd->state |= DOMAIN_STATE_BIOS_LOCKED;
1375 /* check if power limit MSRs exists, otherwise domain is monitoring only */
1376 for (i = 0; i < NR_POWER_LIMITS; i++) {
1377 int prim = rd->rpl[i].prim_id;
1378 if (rapl_read_data_raw(rd, prim, false, &val64))
1379 rd->rpl[i].name = NULL;
1383 /* Detect active and valid domains for the given CPU, caller must
1384 * ensure the CPU belongs to the targeted package and CPU hotlug is disabled.
1386 static int rapl_detect_domains(struct rapl_package *rp, int cpu)
1388 struct rapl_domain *rd;
1391 for (i = 0; i < RAPL_DOMAIN_MAX; i++) {
1392 /* use physical package id to read counters */
1393 if (!rapl_check_domain(cpu, i)) {
1394 rp->domain_map |= 1 << i;
1395 pr_info("Found RAPL domain %s\n", rapl_domain_names[i]);
1398 rp->nr_domains = bitmap_weight(&rp->domain_map, RAPL_DOMAIN_MAX);
1399 if (!rp->nr_domains) {
1400 pr_debug("no valid rapl domains found in %s\n", rp->name);
1403 pr_debug("found %d domains on %s\n", rp->nr_domains, rp->name);
1405 rp->domains = kcalloc(rp->nr_domains + 1, sizeof(struct rapl_domain),
1410 rapl_init_domains(rp);
1412 for (rd = rp->domains; rd < rp->domains + rp->nr_domains; rd++)
1413 rapl_detect_powerlimit(rd);
1418 /* called from CPU hotplug notifier, hotplug lock held */
1419 static void rapl_remove_package(struct rapl_package *rp)
1421 struct rapl_domain *rd, *rd_package = NULL;
1423 package_power_limit_irq_restore(rp);
1425 for (rd = rp->domains; rd < rp->domains + rp->nr_domains; rd++) {
1426 rapl_write_data_raw(rd, PL1_ENABLE, 0);
1427 rapl_write_data_raw(rd, PL1_CLAMP, 0);
1428 if (find_nr_power_limit(rd) > 1) {
1429 rapl_write_data_raw(rd, PL2_ENABLE, 0);
1430 rapl_write_data_raw(rd, PL2_CLAMP, 0);
1432 if (rd->id == RAPL_DOMAIN_PACKAGE) {
1436 pr_debug("remove package, undo power limit on %s: %s\n",
1437 rp->name, rd->name);
1438 powercap_unregister_zone(control_type, &rd->power_zone);
1440 /* do parent zone last */
1441 powercap_unregister_zone(control_type, &rd_package->power_zone);
1442 list_del(&rp->plist);
1446 /* called from CPU hotplug notifier, hotplug lock held */
1447 static struct rapl_package *rapl_add_package(int cpu)
1449 int id = topology_logical_die_id(cpu);
1450 struct rapl_package *rp;
1451 struct cpuinfo_x86 *c = &cpu_data(cpu);
1454 rp = kzalloc(sizeof(struct rapl_package), GFP_KERNEL);
1456 return ERR_PTR(-ENOMEM);
1458 /* add the new package to the list */
1462 if (topology_max_die_per_package() > 1)
1463 snprintf(rp->name, PACKAGE_DOMAIN_NAME_LENGTH,
1464 "package-%d-die-%d", c->phys_proc_id, c->cpu_die_id);
1466 snprintf(rp->name, PACKAGE_DOMAIN_NAME_LENGTH, "package-%d",
1469 /* check if the package contains valid domains */
1470 if (rapl_detect_domains(rp, cpu) ||
1471 rapl_defaults->check_unit(rp, cpu)) {
1473 goto err_free_package;
1475 ret = rapl_package_register_powercap(rp);
1477 INIT_LIST_HEAD(&rp->plist);
1478 list_add(&rp->plist, &rapl_packages);
1485 return ERR_PTR(ret);
1488 /* Handles CPU hotplug on multi-socket systems.
1489 * If a CPU goes online as the first CPU of the physical package
1490 * we add the RAPL package to the system. Similarly, when the last
1491 * CPU of the package is removed, we remove the RAPL package and its
1492 * associated domains. Cooling devices are handled accordingly at
1495 static int rapl_cpu_online(unsigned int cpu)
1497 struct rapl_package *rp;
1499 rp = rapl_find_package_domain(cpu);
1501 rp = rapl_add_package(cpu);
1505 cpumask_set_cpu(cpu, &rp->cpumask);
1509 static int rapl_cpu_down_prep(unsigned int cpu)
1511 struct rapl_package *rp;
1514 rp = rapl_find_package_domain(cpu);
1518 cpumask_clear_cpu(cpu, &rp->cpumask);
1519 lead_cpu = cpumask_first(&rp->cpumask);
1520 if (lead_cpu >= nr_cpu_ids)
1521 rapl_remove_package(rp);
1522 else if (rp->lead_cpu == cpu)
1523 rp->lead_cpu = lead_cpu;
1527 static enum cpuhp_state pcap_rapl_online;
1529 static void power_limit_state_save(void)
1531 struct rapl_package *rp;
1532 struct rapl_domain *rd;
1536 list_for_each_entry(rp, &rapl_packages, plist) {
1537 if (!rp->power_zone)
1539 rd = power_zone_to_rapl_domain(rp->power_zone);
1540 nr_pl = find_nr_power_limit(rd);
1541 for (i = 0; i < nr_pl; i++) {
1542 switch (rd->rpl[i].prim_id) {
1544 ret = rapl_read_data_raw(rd,
1547 &rd->rpl[i].last_power_limit);
1549 rd->rpl[i].last_power_limit = 0;
1552 ret = rapl_read_data_raw(rd,
1555 &rd->rpl[i].last_power_limit);
1557 rd->rpl[i].last_power_limit = 0;
1565 static void power_limit_state_restore(void)
1567 struct rapl_package *rp;
1568 struct rapl_domain *rd;
1572 list_for_each_entry(rp, &rapl_packages, plist) {
1573 if (!rp->power_zone)
1575 rd = power_zone_to_rapl_domain(rp->power_zone);
1576 nr_pl = find_nr_power_limit(rd);
1577 for (i = 0; i < nr_pl; i++) {
1578 switch (rd->rpl[i].prim_id) {
1580 if (rd->rpl[i].last_power_limit)
1581 rapl_write_data_raw(rd,
1583 rd->rpl[i].last_power_limit);
1586 if (rd->rpl[i].last_power_limit)
1587 rapl_write_data_raw(rd,
1589 rd->rpl[i].last_power_limit);
1597 static int rapl_pm_callback(struct notifier_block *nb,
1598 unsigned long mode, void *_unused)
1601 case PM_SUSPEND_PREPARE:
1602 power_limit_state_save();
1604 case PM_POST_SUSPEND:
1605 power_limit_state_restore();
1611 static struct notifier_block rapl_pm_notifier = {
1612 .notifier_call = rapl_pm_callback,
1615 static int __init rapl_init(void)
1617 const struct x86_cpu_id *id;
1620 id = x86_match_cpu(rapl_ids);
1622 pr_err("driver does not support CPU family %d model %d\n",
1623 boot_cpu_data.x86, boot_cpu_data.x86_model);
1628 rapl_defaults = (struct rapl_defaults *)id->driver_data;
1630 ret = rapl_register_powercap();
1634 ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "powercap/rapl:online",
1635 rapl_cpu_online, rapl_cpu_down_prep);
1638 pcap_rapl_online = ret;
1640 /* Don't bail out if PSys is not supported */
1641 rapl_register_psys();
1643 ret = register_pm_notifier(&rapl_pm_notifier);
1650 cpuhp_remove_state(pcap_rapl_online);
1653 rapl_unregister_powercap();
1657 static void __exit rapl_exit(void)
1659 unregister_pm_notifier(&rapl_pm_notifier);
1660 cpuhp_remove_state(pcap_rapl_online);
1661 rapl_unregister_powercap();
1664 module_init(rapl_init);
1665 module_exit(rapl_exit);
1667 MODULE_DESCRIPTION("Driver for Intel RAPL (Running Average Power Limit)");
1668 MODULE_AUTHOR("Jacob Pan <jacob.jun.pan@intel.com>");
1669 MODULE_LICENSE("GPL v2");