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
*find_package_by_id(int id
)
269 struct rapl_package
*rp
;
271 list_for_each_entry(rp
, &rapl_packages
, plist
) {
279 static int get_energy_counter(struct powercap_zone
*power_zone
, u64
*energy_raw
)
281 struct rapl_domain
*rd
;
284 /* prevent CPU hotplug, make sure the RAPL domain does not go
285 * away while reading the counter.
288 rd
= power_zone_to_rapl_domain(power_zone
);
290 if (!rapl_read_data_raw(rd
, ENERGY_COUNTER
, true, &energy_now
)) {
291 *energy_raw
= energy_now
;
301 static int get_max_energy_counter(struct powercap_zone
*pcd_dev
, u64
*energy
)
303 struct rapl_domain
*rd
= power_zone_to_rapl_domain(pcd_dev
);
305 *energy
= rapl_unit_xlate(rd
, ENERGY_UNIT
, ENERGY_STATUS_MASK
, 0);
309 static int release_zone(struct powercap_zone
*power_zone
)
311 struct rapl_domain
*rd
= power_zone_to_rapl_domain(power_zone
);
312 struct rapl_package
*rp
= rd
->rp
;
314 /* package zone is the last zone of a package, we can free
315 * memory here since all children has been unregistered.
317 if (rd
->id
== RAPL_DOMAIN_PACKAGE
) {
326 static int find_nr_power_limit(struct rapl_domain
*rd
)
330 for (i
= 0; i
< NR_POWER_LIMITS
; i
++) {
338 static int set_domain_enable(struct powercap_zone
*power_zone
, bool mode
)
340 struct rapl_domain
*rd
= power_zone_to_rapl_domain(power_zone
);
342 if (rd
->state
& DOMAIN_STATE_BIOS_LOCKED
)
346 rapl_write_data_raw(rd
, PL1_ENABLE
, mode
);
347 if (rapl_defaults
->set_floor_freq
)
348 rapl_defaults
->set_floor_freq(rd
, mode
);
354 static int get_domain_enable(struct powercap_zone
*power_zone
, bool *mode
)
356 struct rapl_domain
*rd
= power_zone_to_rapl_domain(power_zone
);
359 if (rd
->state
& DOMAIN_STATE_BIOS_LOCKED
) {
364 if (rapl_read_data_raw(rd
, PL1_ENABLE
, true, &val
)) {
374 /* per RAPL domain ops, in the order of rapl_domain_type */
375 static const struct powercap_zone_ops zone_ops
[] = {
376 /* RAPL_DOMAIN_PACKAGE */
378 .get_energy_uj
= get_energy_counter
,
379 .get_max_energy_range_uj
= get_max_energy_counter
,
380 .release
= release_zone
,
381 .set_enable
= set_domain_enable
,
382 .get_enable
= get_domain_enable
,
384 /* RAPL_DOMAIN_PP0 */
386 .get_energy_uj
= get_energy_counter
,
387 .get_max_energy_range_uj
= get_max_energy_counter
,
388 .release
= release_zone
,
389 .set_enable
= set_domain_enable
,
390 .get_enable
= get_domain_enable
,
392 /* RAPL_DOMAIN_PP1 */
394 .get_energy_uj
= get_energy_counter
,
395 .get_max_energy_range_uj
= get_max_energy_counter
,
396 .release
= release_zone
,
397 .set_enable
= set_domain_enable
,
398 .get_enable
= get_domain_enable
,
400 /* RAPL_DOMAIN_DRAM */
402 .get_energy_uj
= get_energy_counter
,
403 .get_max_energy_range_uj
= get_max_energy_counter
,
404 .release
= release_zone
,
405 .set_enable
= set_domain_enable
,
406 .get_enable
= get_domain_enable
,
408 /* RAPL_DOMAIN_PLATFORM */
410 .get_energy_uj
= get_energy_counter
,
411 .get_max_energy_range_uj
= get_max_energy_counter
,
412 .release
= release_zone
,
413 .set_enable
= set_domain_enable
,
414 .get_enable
= get_domain_enable
,
420 * Constraint index used by powercap can be different than power limit (PL)
421 * index in that some PLs maybe missing due to non-existant MSRs. So we
422 * need to convert here by finding the valid PLs only (name populated).
424 static int contraint_to_pl(struct rapl_domain
*rd
, int cid
)
428 for (i
= 0, j
= 0; i
< NR_POWER_LIMITS
; i
++) {
429 if ((rd
->rpl
[i
].name
) && j
++ == cid
) {
430 pr_debug("%s: index %d\n", __func__
, i
);
434 pr_err("Cannot find matching power limit for constraint %d\n", cid
);
439 static int set_power_limit(struct powercap_zone
*power_zone
, int cid
,
442 struct rapl_domain
*rd
;
443 struct rapl_package
*rp
;
448 rd
= power_zone_to_rapl_domain(power_zone
);
449 id
= contraint_to_pl(rd
, cid
);
457 if (rd
->state
& DOMAIN_STATE_BIOS_LOCKED
) {
458 dev_warn(&power_zone
->dev
, "%s locked by BIOS, monitoring only\n",
464 switch (rd
->rpl
[id
].prim_id
) {
466 rapl_write_data_raw(rd
, POWER_LIMIT1
, power_limit
);
469 rapl_write_data_raw(rd
, POWER_LIMIT2
, power_limit
);
475 package_power_limit_irq_save(rp
);
481 static int get_current_power_limit(struct powercap_zone
*power_zone
, int cid
,
484 struct rapl_domain
*rd
;
491 rd
= power_zone_to_rapl_domain(power_zone
);
492 id
= contraint_to_pl(rd
, cid
);
498 switch (rd
->rpl
[id
].prim_id
) {
509 if (rapl_read_data_raw(rd
, prim
, true, &val
))
520 static int set_time_window(struct powercap_zone
*power_zone
, int cid
,
523 struct rapl_domain
*rd
;
528 rd
= power_zone_to_rapl_domain(power_zone
);
529 id
= contraint_to_pl(rd
, cid
);
535 switch (rd
->rpl
[id
].prim_id
) {
537 rapl_write_data_raw(rd
, TIME_WINDOW1
, window
);
540 rapl_write_data_raw(rd
, TIME_WINDOW2
, window
);
551 static int get_time_window(struct powercap_zone
*power_zone
, int cid
, u64
*data
)
553 struct rapl_domain
*rd
;
559 rd
= power_zone_to_rapl_domain(power_zone
);
560 id
= contraint_to_pl(rd
, cid
);
566 switch (rd
->rpl
[id
].prim_id
) {
568 ret
= rapl_read_data_raw(rd
, TIME_WINDOW1
, true, &val
);
571 ret
= rapl_read_data_raw(rd
, TIME_WINDOW2
, true, &val
);
586 static const char *get_constraint_name(struct powercap_zone
*power_zone
, int cid
)
588 struct rapl_domain
*rd
;
591 rd
= power_zone_to_rapl_domain(power_zone
);
592 id
= contraint_to_pl(rd
, cid
);
594 return rd
->rpl
[id
].name
;
600 static int get_max_power(struct powercap_zone
*power_zone
, int id
,
603 struct rapl_domain
*rd
;
609 rd
= power_zone_to_rapl_domain(power_zone
);
610 switch (rd
->rpl
[id
].prim_id
) {
612 prim
= THERMAL_SPEC_POWER
;
621 if (rapl_read_data_raw(rd
, prim
, true, &val
))
631 static const struct powercap_zone_constraint_ops constraint_ops
= {
632 .set_power_limit_uw
= set_power_limit
,
633 .get_power_limit_uw
= get_current_power_limit
,
634 .set_time_window_us
= set_time_window
,
635 .get_time_window_us
= get_time_window
,
636 .get_max_power_uw
= get_max_power
,
637 .get_name
= get_constraint_name
,
640 /* called after domain detection and package level data are set */
641 static void rapl_init_domains(struct rapl_package
*rp
)
644 struct rapl_domain
*rd
= rp
->domains
;
646 for (i
= 0; i
< RAPL_DOMAIN_MAX
; i
++) {
647 unsigned int mask
= rp
->domain_map
& (1 << i
);
649 case BIT(RAPL_DOMAIN_PACKAGE
):
650 rd
->name
= rapl_domain_names
[RAPL_DOMAIN_PACKAGE
];
651 rd
->id
= RAPL_DOMAIN_PACKAGE
;
652 rd
->msrs
[0] = MSR_PKG_POWER_LIMIT
;
653 rd
->msrs
[1] = MSR_PKG_ENERGY_STATUS
;
654 rd
->msrs
[2] = MSR_PKG_PERF_STATUS
;
656 rd
->msrs
[4] = MSR_PKG_POWER_INFO
;
657 rd
->rpl
[0].prim_id
= PL1_ENABLE
;
658 rd
->rpl
[0].name
= pl1_name
;
659 rd
->rpl
[1].prim_id
= PL2_ENABLE
;
660 rd
->rpl
[1].name
= pl2_name
;
662 case BIT(RAPL_DOMAIN_PP0
):
663 rd
->name
= rapl_domain_names
[RAPL_DOMAIN_PP0
];
664 rd
->id
= RAPL_DOMAIN_PP0
;
665 rd
->msrs
[0] = MSR_PP0_POWER_LIMIT
;
666 rd
->msrs
[1] = MSR_PP0_ENERGY_STATUS
;
668 rd
->msrs
[3] = MSR_PP0_POLICY
;
670 rd
->rpl
[0].prim_id
= PL1_ENABLE
;
671 rd
->rpl
[0].name
= pl1_name
;
673 case BIT(RAPL_DOMAIN_PP1
):
674 rd
->name
= rapl_domain_names
[RAPL_DOMAIN_PP1
];
675 rd
->id
= RAPL_DOMAIN_PP1
;
676 rd
->msrs
[0] = MSR_PP1_POWER_LIMIT
;
677 rd
->msrs
[1] = MSR_PP1_ENERGY_STATUS
;
679 rd
->msrs
[3] = MSR_PP1_POLICY
;
681 rd
->rpl
[0].prim_id
= PL1_ENABLE
;
682 rd
->rpl
[0].name
= pl1_name
;
684 case BIT(RAPL_DOMAIN_DRAM
):
685 rd
->name
= rapl_domain_names
[RAPL_DOMAIN_DRAM
];
686 rd
->id
= RAPL_DOMAIN_DRAM
;
687 rd
->msrs
[0] = MSR_DRAM_POWER_LIMIT
;
688 rd
->msrs
[1] = MSR_DRAM_ENERGY_STATUS
;
689 rd
->msrs
[2] = MSR_DRAM_PERF_STATUS
;
691 rd
->msrs
[4] = MSR_DRAM_POWER_INFO
;
692 rd
->rpl
[0].prim_id
= PL1_ENABLE
;
693 rd
->rpl
[0].name
= pl1_name
;
694 rd
->domain_energy_unit
=
695 rapl_defaults
->dram_domain_energy_unit
;
696 if (rd
->domain_energy_unit
)
697 pr_info("DRAM domain energy unit %dpj\n",
698 rd
->domain_energy_unit
);
708 static u64
rapl_unit_xlate(struct rapl_domain
*rd
, enum unit_type type
,
709 u64 value
, int to_raw
)
712 struct rapl_package
*rp
= rd
->rp
;
717 units
= rp
->power_unit
;
720 scale
= ENERGY_UNIT_SCALE
;
721 /* per domain unit takes precedence */
722 if (rd
->domain_energy_unit
)
723 units
= rd
->domain_energy_unit
;
725 units
= rp
->energy_unit
;
728 return rapl_defaults
->compute_time_window(rp
, value
, to_raw
);
735 return div64_u64(value
, units
) * scale
;
739 return div64_u64(value
, scale
);
742 /* in the order of enum rapl_primitives */
743 static struct rapl_primitive_info rpi
[] = {
744 /* name, mask, shift, msr index, unit divisor */
745 PRIMITIVE_INFO_INIT(ENERGY_COUNTER
, ENERGY_STATUS_MASK
, 0,
746 RAPL_DOMAIN_MSR_STATUS
, ENERGY_UNIT
, 0),
747 PRIMITIVE_INFO_INIT(POWER_LIMIT1
, POWER_LIMIT1_MASK
, 0,
748 RAPL_DOMAIN_MSR_LIMIT
, POWER_UNIT
, 0),
749 PRIMITIVE_INFO_INIT(POWER_LIMIT2
, POWER_LIMIT2_MASK
, 32,
750 RAPL_DOMAIN_MSR_LIMIT
, POWER_UNIT
, 0),
751 PRIMITIVE_INFO_INIT(FW_LOCK
, POWER_PP_LOCK
, 31,
752 RAPL_DOMAIN_MSR_LIMIT
, ARBITRARY_UNIT
, 0),
753 PRIMITIVE_INFO_INIT(PL1_ENABLE
, POWER_LIMIT1_ENABLE
, 15,
754 RAPL_DOMAIN_MSR_LIMIT
, ARBITRARY_UNIT
, 0),
755 PRIMITIVE_INFO_INIT(PL1_CLAMP
, POWER_LIMIT1_CLAMP
, 16,
756 RAPL_DOMAIN_MSR_LIMIT
, ARBITRARY_UNIT
, 0),
757 PRIMITIVE_INFO_INIT(PL2_ENABLE
, POWER_LIMIT2_ENABLE
, 47,
758 RAPL_DOMAIN_MSR_LIMIT
, ARBITRARY_UNIT
, 0),
759 PRIMITIVE_INFO_INIT(PL2_CLAMP
, POWER_LIMIT2_CLAMP
, 48,
760 RAPL_DOMAIN_MSR_LIMIT
, ARBITRARY_UNIT
, 0),
761 PRIMITIVE_INFO_INIT(TIME_WINDOW1
, TIME_WINDOW1_MASK
, 17,
762 RAPL_DOMAIN_MSR_LIMIT
, TIME_UNIT
, 0),
763 PRIMITIVE_INFO_INIT(TIME_WINDOW2
, TIME_WINDOW2_MASK
, 49,
764 RAPL_DOMAIN_MSR_LIMIT
, TIME_UNIT
, 0),
765 PRIMITIVE_INFO_INIT(THERMAL_SPEC_POWER
, POWER_INFO_THERMAL_SPEC_MASK
,
766 0, RAPL_DOMAIN_MSR_INFO
, POWER_UNIT
, 0),
767 PRIMITIVE_INFO_INIT(MAX_POWER
, POWER_INFO_MAX_MASK
, 32,
768 RAPL_DOMAIN_MSR_INFO
, POWER_UNIT
, 0),
769 PRIMITIVE_INFO_INIT(MIN_POWER
, POWER_INFO_MIN_MASK
, 16,
770 RAPL_DOMAIN_MSR_INFO
, POWER_UNIT
, 0),
771 PRIMITIVE_INFO_INIT(MAX_TIME_WINDOW
, POWER_INFO_MAX_TIME_WIN_MASK
, 48,
772 RAPL_DOMAIN_MSR_INFO
, TIME_UNIT
, 0),
773 PRIMITIVE_INFO_INIT(THROTTLED_TIME
, PERF_STATUS_THROTTLE_TIME_MASK
, 0,
774 RAPL_DOMAIN_MSR_PERF
, TIME_UNIT
, 0),
775 PRIMITIVE_INFO_INIT(PRIORITY_LEVEL
, PP_POLICY_MASK
, 0,
776 RAPL_DOMAIN_MSR_POLICY
, ARBITRARY_UNIT
, 0),
778 PRIMITIVE_INFO_INIT(AVERAGE_POWER
, 0, 0, 0, POWER_UNIT
,
779 RAPL_PRIMITIVE_DERIVED
),
783 /* Read primitive data based on its related struct rapl_primitive_info.
784 * if xlate flag is set, return translated data based on data units, i.e.
785 * time, energy, and power.
786 * RAPL MSRs are non-architectual and are laid out not consistently across
787 * domains. Here we use primitive info to allow writing consolidated access
789 * For a given primitive, it is processed by MSR mask and shift. Unit conversion
790 * is pre-assigned based on RAPL unit MSRs read at init time.
791 * 63-------------------------- 31--------------------------- 0
793 * | |<- shift ----------------|
794 * 63-------------------------- 31--------------------------- 0
796 static int rapl_read_data_raw(struct rapl_domain
*rd
,
797 enum rapl_primitives prim
,
798 bool xlate
, u64
*data
)
802 struct rapl_primitive_info
*rp
= &rpi
[prim
];
805 if (!rp
->name
|| rp
->flag
& RAPL_PRIMITIVE_DUMMY
)
808 msr
= rd
->msrs
[rp
->id
];
812 cpu
= rd
->rp
->lead_cpu
;
814 /* special-case package domain, which uses a different bit*/
815 if (prim
== FW_LOCK
&& rd
->id
== RAPL_DOMAIN_PACKAGE
) {
816 rp
->mask
= POWER_PACKAGE_LOCK
;
819 /* non-hardware data are collected by the polling thread */
820 if (rp
->flag
& RAPL_PRIMITIVE_DERIVED
) {
821 *data
= rd
->rdd
.primitives
[prim
];
825 if (rdmsrl_safe_on_cpu(cpu
, msr
, &value
)) {
826 pr_debug("failed to read msr 0x%x on cpu %d\n", msr
, cpu
);
830 final
= value
& rp
->mask
;
831 final
= final
>> rp
->shift
;
833 *data
= rapl_unit_xlate(rd
, rp
->unit
, final
, 0);
841 static int msrl_update_safe(u32 msr_no
, u64 clear_mask
, u64 set_mask
)
846 err
= rdmsrl_safe(msr_no
, &val
);
853 err
= wrmsrl_safe(msr_no
, val
);
859 static void msrl_update_func(void *info
)
861 struct msrl_action
*ma
= info
;
863 ma
->err
= msrl_update_safe(ma
->msr_no
, ma
->clear_mask
, ma
->set_mask
);
866 /* Similar use of primitive info in the read counterpart */
867 static int rapl_write_data_raw(struct rapl_domain
*rd
,
868 enum rapl_primitives prim
,
869 unsigned long long value
)
871 struct rapl_primitive_info
*rp
= &rpi
[prim
];
874 struct msrl_action ma
;
877 cpu
= rd
->rp
->lead_cpu
;
878 bits
= rapl_unit_xlate(rd
, rp
->unit
, value
, 1);
882 memset(&ma
, 0, sizeof(ma
));
884 ma
.msr_no
= rd
->msrs
[rp
->id
];
885 ma
.clear_mask
= rp
->mask
;
888 ret
= smp_call_function_single(cpu
, msrl_update_func
, &ma
, 1);
898 * Raw RAPL data stored in MSRs are in certain scales. We need to
899 * convert them into standard units based on the units reported in
900 * the RAPL unit MSRs. This is specific to CPUs as the method to
901 * calculate units differ on different CPUs.
902 * We convert the units to below format based on CPUs.
904 * energy unit: picoJoules : Represented in picoJoules by default
905 * power unit : microWatts : Represented in milliWatts by default
906 * time unit : microseconds: Represented in seconds by default
908 static int rapl_check_unit_core(struct rapl_package
*rp
, int cpu
)
913 if (rdmsrl_safe_on_cpu(cpu
, MSR_RAPL_POWER_UNIT
, &msr_val
)) {
914 pr_err("Failed to read power unit MSR 0x%x on CPU %d, exit.\n",
915 MSR_RAPL_POWER_UNIT
, cpu
);
919 value
= (msr_val
& ENERGY_UNIT_MASK
) >> ENERGY_UNIT_OFFSET
;
920 rp
->energy_unit
= ENERGY_UNIT_SCALE
* 1000000 / (1 << value
);
922 value
= (msr_val
& POWER_UNIT_MASK
) >> POWER_UNIT_OFFSET
;
923 rp
->power_unit
= 1000000 / (1 << value
);
925 value
= (msr_val
& TIME_UNIT_MASK
) >> TIME_UNIT_OFFSET
;
926 rp
->time_unit
= 1000000 / (1 << value
);
928 pr_debug("Core CPU package %d energy=%dpJ, time=%dus, power=%duW\n",
929 rp
->id
, rp
->energy_unit
, rp
->time_unit
, rp
->power_unit
);
934 static int rapl_check_unit_atom(struct rapl_package
*rp
, int cpu
)
939 if (rdmsrl_safe_on_cpu(cpu
, MSR_RAPL_POWER_UNIT
, &msr_val
)) {
940 pr_err("Failed to read power unit MSR 0x%x on CPU %d, exit.\n",
941 MSR_RAPL_POWER_UNIT
, cpu
);
944 value
= (msr_val
& ENERGY_UNIT_MASK
) >> ENERGY_UNIT_OFFSET
;
945 rp
->energy_unit
= ENERGY_UNIT_SCALE
* 1 << value
;
947 value
= (msr_val
& POWER_UNIT_MASK
) >> POWER_UNIT_OFFSET
;
948 rp
->power_unit
= (1 << value
) * 1000;
950 value
= (msr_val
& TIME_UNIT_MASK
) >> TIME_UNIT_OFFSET
;
951 rp
->time_unit
= 1000000 / (1 << value
);
953 pr_debug("Atom package %d energy=%dpJ, time=%dus, power=%duW\n",
954 rp
->id
, rp
->energy_unit
, rp
->time_unit
, rp
->power_unit
);
959 static void power_limit_irq_save_cpu(void *info
)
962 struct rapl_package
*rp
= (struct rapl_package
*)info
;
964 /* save the state of PLN irq mask bit before disabling it */
965 rdmsr_safe(MSR_IA32_PACKAGE_THERM_INTERRUPT
, &l
, &h
);
966 if (!(rp
->power_limit_irq
& PACKAGE_PLN_INT_SAVED
)) {
967 rp
->power_limit_irq
= l
& PACKAGE_THERM_INT_PLN_ENABLE
;
968 rp
->power_limit_irq
|= PACKAGE_PLN_INT_SAVED
;
970 l
&= ~PACKAGE_THERM_INT_PLN_ENABLE
;
971 wrmsr_safe(MSR_IA32_PACKAGE_THERM_INTERRUPT
, l
, h
);
976 * When package power limit is set artificially low by RAPL, LVT
977 * thermal interrupt for package power limit should be ignored
978 * since we are not really exceeding the real limit. The intention
979 * is to avoid excessive interrupts while we are trying to save power.
980 * A useful feature might be routing the package_power_limit interrupt
981 * to userspace via eventfd. once we have a usecase, this is simple
982 * to do by adding an atomic notifier.
985 static void package_power_limit_irq_save(struct rapl_package
*rp
)
987 if (!boot_cpu_has(X86_FEATURE_PTS
) || !boot_cpu_has(X86_FEATURE_PLN
))
990 smp_call_function_single(rp
->lead_cpu
, power_limit_irq_save_cpu
, rp
, 1);
994 * Restore per package power limit interrupt enable state. Called from cpu
995 * hotplug code on package removal.
997 static void package_power_limit_irq_restore(struct rapl_package
*rp
)
1001 if (!boot_cpu_has(X86_FEATURE_PTS
) || !boot_cpu_has(X86_FEATURE_PLN
))
1004 /* irq enable state not saved, nothing to restore */
1005 if (!(rp
->power_limit_irq
& PACKAGE_PLN_INT_SAVED
))
1008 rdmsr_safe(MSR_IA32_PACKAGE_THERM_INTERRUPT
, &l
, &h
);
1010 if (rp
->power_limit_irq
& PACKAGE_THERM_INT_PLN_ENABLE
)
1011 l
|= PACKAGE_THERM_INT_PLN_ENABLE
;
1013 l
&= ~PACKAGE_THERM_INT_PLN_ENABLE
;
1015 wrmsr_safe(MSR_IA32_PACKAGE_THERM_INTERRUPT
, l
, h
);
1018 static void set_floor_freq_default(struct rapl_domain
*rd
, bool mode
)
1020 int nr_powerlimit
= find_nr_power_limit(rd
);
1022 /* always enable clamp such that p-state can go below OS requested
1023 * range. power capping priority over guranteed frequency.
1025 rapl_write_data_raw(rd
, PL1_CLAMP
, mode
);
1027 /* some domains have pl2 */
1028 if (nr_powerlimit
> 1) {
1029 rapl_write_data_raw(rd
, PL2_ENABLE
, mode
);
1030 rapl_write_data_raw(rd
, PL2_CLAMP
, mode
);
1034 static void set_floor_freq_atom(struct rapl_domain
*rd
, bool enable
)
1036 static u32 power_ctrl_orig_val
;
1039 if (!rapl_defaults
->floor_freq_reg_addr
) {
1040 pr_err("Invalid floor frequency config register\n");
1044 if (!power_ctrl_orig_val
)
1045 iosf_mbi_read(BT_MBI_UNIT_PMC
, MBI_CR_READ
,
1046 rapl_defaults
->floor_freq_reg_addr
,
1047 &power_ctrl_orig_val
);
1048 mdata
= power_ctrl_orig_val
;
1050 mdata
&= ~(0x7f << 8);
1053 iosf_mbi_write(BT_MBI_UNIT_PMC
, MBI_CR_WRITE
,
1054 rapl_defaults
->floor_freq_reg_addr
, mdata
);
1057 static u64
rapl_compute_time_window_core(struct rapl_package
*rp
, u64 value
,
1060 u64 f
, y
; /* fraction and exp. used for time unit */
1063 * Special processing based on 2^Y*(1+F/4), refer
1064 * to Intel Software Developer's manual Vol.3B: CH 14.9.3.
1067 f
= (value
& 0x60) >> 5;
1069 value
= (1 << y
) * (4 + f
) * rp
->time_unit
/ 4;
1071 do_div(value
, rp
->time_unit
);
1073 f
= div64_u64(4 * (value
- (1 << y
)), 1 << y
);
1074 value
= (y
& 0x1f) | ((f
& 0x3) << 5);
1079 static u64
rapl_compute_time_window_atom(struct rapl_package
*rp
, u64 value
,
1083 * Atom time unit encoding is straight forward val * time_unit,
1084 * where time_unit is default to 1 sec. Never 0.
1087 return (value
) ? value
*= rp
->time_unit
: rp
->time_unit
;
1089 value
= div64_u64(value
, rp
->time_unit
);
1094 static const struct rapl_defaults rapl_defaults_core
= {
1095 .floor_freq_reg_addr
= 0,
1096 .check_unit
= rapl_check_unit_core
,
1097 .set_floor_freq
= set_floor_freq_default
,
1098 .compute_time_window
= rapl_compute_time_window_core
,
1101 static const struct rapl_defaults rapl_defaults_hsw_server
= {
1102 .check_unit
= rapl_check_unit_core
,
1103 .set_floor_freq
= set_floor_freq_default
,
1104 .compute_time_window
= rapl_compute_time_window_core
,
1105 .dram_domain_energy_unit
= 15300,
1108 static const struct rapl_defaults rapl_defaults_byt
= {
1109 .floor_freq_reg_addr
= IOSF_CPU_POWER_BUDGET_CTL_BYT
,
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_tng
= {
1116 .floor_freq_reg_addr
= IOSF_CPU_POWER_BUDGET_CTL_TNG
,
1117 .check_unit
= rapl_check_unit_atom
,
1118 .set_floor_freq
= set_floor_freq_atom
,
1119 .compute_time_window
= rapl_compute_time_window_atom
,
1122 static const struct rapl_defaults rapl_defaults_ann
= {
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 rapl_defaults rapl_defaults_cht
= {
1130 .floor_freq_reg_addr
= 0,
1131 .check_unit
= rapl_check_unit_atom
,
1132 .set_floor_freq
= NULL
,
1133 .compute_time_window
= rapl_compute_time_window_atom
,
1136 #define RAPL_CPU(_model, _ops) { \
1137 .vendor = X86_VENDOR_INTEL, \
1140 .driver_data = (kernel_ulong_t)&_ops, \
1143 static const struct x86_cpu_id rapl_ids
[] __initconst
= {
1144 RAPL_CPU(INTEL_FAM6_SANDYBRIDGE
, rapl_defaults_core
),
1145 RAPL_CPU(INTEL_FAM6_SANDYBRIDGE_X
, rapl_defaults_core
),
1147 RAPL_CPU(INTEL_FAM6_IVYBRIDGE
, rapl_defaults_core
),
1148 RAPL_CPU(INTEL_FAM6_IVYBRIDGE_X
, rapl_defaults_core
),
1150 RAPL_CPU(INTEL_FAM6_HASWELL_CORE
, rapl_defaults_core
),
1151 RAPL_CPU(INTEL_FAM6_HASWELL_ULT
, rapl_defaults_core
),
1152 RAPL_CPU(INTEL_FAM6_HASWELL_GT3E
, rapl_defaults_core
),
1153 RAPL_CPU(INTEL_FAM6_HASWELL_X
, rapl_defaults_hsw_server
),
1155 RAPL_CPU(INTEL_FAM6_BROADWELL_CORE
, rapl_defaults_core
),
1156 RAPL_CPU(INTEL_FAM6_BROADWELL_GT3E
, rapl_defaults_core
),
1157 RAPL_CPU(INTEL_FAM6_BROADWELL_XEON_D
, rapl_defaults_core
),
1158 RAPL_CPU(INTEL_FAM6_BROADWELL_X
, rapl_defaults_hsw_server
),
1160 RAPL_CPU(INTEL_FAM6_SKYLAKE_DESKTOP
, rapl_defaults_core
),
1161 RAPL_CPU(INTEL_FAM6_SKYLAKE_MOBILE
, rapl_defaults_core
),
1162 RAPL_CPU(INTEL_FAM6_SKYLAKE_X
, rapl_defaults_hsw_server
),
1163 RAPL_CPU(INTEL_FAM6_KABYLAKE_MOBILE
, rapl_defaults_core
),
1164 RAPL_CPU(INTEL_FAM6_KABYLAKE_DESKTOP
, rapl_defaults_core
),
1165 RAPL_CPU(INTEL_FAM6_CANNONLAKE_MOBILE
, rapl_defaults_core
),
1167 RAPL_CPU(INTEL_FAM6_ATOM_SILVERMONT1
, rapl_defaults_byt
),
1168 RAPL_CPU(INTEL_FAM6_ATOM_AIRMONT
, rapl_defaults_cht
),
1169 RAPL_CPU(INTEL_FAM6_ATOM_MERRIFIELD
, rapl_defaults_tng
),
1170 RAPL_CPU(INTEL_FAM6_ATOM_MOOREFIELD
, rapl_defaults_ann
),
1171 RAPL_CPU(INTEL_FAM6_ATOM_GOLDMONT
, rapl_defaults_core
),
1172 RAPL_CPU(INTEL_FAM6_ATOM_GEMINI_LAKE
, rapl_defaults_core
),
1173 RAPL_CPU(INTEL_FAM6_ATOM_DENVERTON
, rapl_defaults_core
),
1175 RAPL_CPU(INTEL_FAM6_XEON_PHI_KNL
, rapl_defaults_hsw_server
),
1176 RAPL_CPU(INTEL_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 package %d domain %s data\n", rp
->id
,
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 char dev_name
[17]; /* max domain name = 7 + 1 + 8 for int + 1 for null*/
1214 struct powercap_zone
*power_zone
= NULL
;
1217 /* Update the domain data of the new package */
1218 rapl_update_domain_data(rp
);
1220 /* first we register package domain as the parent zone*/
1221 for (rd
= rp
->domains
; rd
< rp
->domains
+ rp
->nr_domains
; rd
++) {
1222 if (rd
->id
== RAPL_DOMAIN_PACKAGE
) {
1223 nr_pl
= find_nr_power_limit(rd
);
1224 pr_debug("register socket %d package domain %s\n",
1226 memset(dev_name
, 0, sizeof(dev_name
));
1227 snprintf(dev_name
, sizeof(dev_name
), "%s-%d",
1229 power_zone
= powercap_register_zone(&rd
->power_zone
,
1235 if (IS_ERR(power_zone
)) {
1236 pr_debug("failed to register package, %d\n",
1238 return PTR_ERR(power_zone
);
1240 /* track parent zone in per package/socket data */
1241 rp
->power_zone
= power_zone
;
1242 /* done, only one package domain per socket */
1247 pr_err("no package domain found, unknown topology!\n");
1250 /* now register domains as children of the socket/package*/
1251 for (rd
= rp
->domains
; rd
< rp
->domains
+ rp
->nr_domains
; rd
++) {
1252 if (rd
->id
== RAPL_DOMAIN_PACKAGE
)
1254 /* number of power limits per domain varies */
1255 nr_pl
= find_nr_power_limit(rd
);
1256 power_zone
= powercap_register_zone(&rd
->power_zone
,
1257 control_type
, rd
->name
,
1259 &zone_ops
[rd
->id
], nr_pl
,
1262 if (IS_ERR(power_zone
)) {
1263 pr_debug("failed to register power_zone, %d:%s:%s\n",
1264 rp
->id
, rd
->name
, dev_name
);
1265 ret
= PTR_ERR(power_zone
);
1273 * Clean up previously initialized domains within the package if we
1274 * failed after the first domain setup.
1276 while (--rd
>= rp
->domains
) {
1277 pr_debug("unregister package %d domain %s\n", rp
->id
, rd
->name
);
1278 powercap_unregister_zone(control_type
, &rd
->power_zone
);
1284 static int __init
rapl_register_psys(void)
1286 struct rapl_domain
*rd
;
1287 struct powercap_zone
*power_zone
;
1290 if (rdmsrl_safe_on_cpu(0, MSR_PLATFORM_ENERGY_STATUS
, &val
) || !val
)
1293 if (rdmsrl_safe_on_cpu(0, MSR_PLATFORM_POWER_LIMIT
, &val
) || !val
)
1296 rd
= kzalloc(sizeof(*rd
), GFP_KERNEL
);
1300 rd
->name
= rapl_domain_names
[RAPL_DOMAIN_PLATFORM
];
1301 rd
->id
= RAPL_DOMAIN_PLATFORM
;
1302 rd
->msrs
[0] = MSR_PLATFORM_POWER_LIMIT
;
1303 rd
->msrs
[1] = MSR_PLATFORM_ENERGY_STATUS
;
1304 rd
->rpl
[0].prim_id
= PL1_ENABLE
;
1305 rd
->rpl
[0].name
= pl1_name
;
1306 rd
->rpl
[1].prim_id
= PL2_ENABLE
;
1307 rd
->rpl
[1].name
= pl2_name
;
1308 rd
->rp
= find_package_by_id(0);
1310 power_zone
= powercap_register_zone(&rd
->power_zone
, control_type
,
1312 &zone_ops
[RAPL_DOMAIN_PLATFORM
],
1313 2, &constraint_ops
);
1315 if (IS_ERR(power_zone
)) {
1317 return PTR_ERR(power_zone
);
1320 platform_rapl_domain
= rd
;
1325 static int __init
rapl_register_powercap(void)
1327 control_type
= powercap_register_control_type(NULL
, "intel-rapl", NULL
);
1328 if (IS_ERR(control_type
)) {
1329 pr_debug("failed to register powercap control_type.\n");
1330 return PTR_ERR(control_type
);
1335 static int rapl_check_domain(int cpu
, int domain
)
1341 case RAPL_DOMAIN_PACKAGE
:
1342 msr
= MSR_PKG_ENERGY_STATUS
;
1344 case RAPL_DOMAIN_PP0
:
1345 msr
= MSR_PP0_ENERGY_STATUS
;
1347 case RAPL_DOMAIN_PP1
:
1348 msr
= MSR_PP1_ENERGY_STATUS
;
1350 case RAPL_DOMAIN_DRAM
:
1351 msr
= MSR_DRAM_ENERGY_STATUS
;
1353 case RAPL_DOMAIN_PLATFORM
:
1354 /* PSYS(PLATFORM) is not a CPU domain, so avoid printng error */
1357 pr_err("invalid domain id %d\n", domain
);
1360 /* make sure domain counters are available and contains non-zero
1361 * values, otherwise skip it.
1363 if (rdmsrl_safe_on_cpu(cpu
, msr
, &val
) || !val
)
1371 * Check if power limits are available. Two cases when they are not available:
1372 * 1. Locked by BIOS, in this case we still provide read-only access so that
1373 * users can see what limit is set by the BIOS.
1374 * 2. Some CPUs make some domains monitoring only which means PLx MSRs may not
1375 * exist at all. In this case, we do not show the contraints in powercap.
1377 * Called after domains are detected and initialized.
1379 static void rapl_detect_powerlimit(struct rapl_domain
*rd
)
1384 /* check if the domain is locked by BIOS, ignore if MSR doesn't exist */
1385 if (!rapl_read_data_raw(rd
, FW_LOCK
, false, &val64
)) {
1387 pr_info("RAPL package %d domain %s locked by BIOS\n",
1388 rd
->rp
->id
, rd
->name
);
1389 rd
->state
|= DOMAIN_STATE_BIOS_LOCKED
;
1392 /* check if power limit MSRs exists, otherwise domain is monitoring only */
1393 for (i
= 0; i
< NR_POWER_LIMITS
; i
++) {
1394 int prim
= rd
->rpl
[i
].prim_id
;
1395 if (rapl_read_data_raw(rd
, prim
, false, &val64
))
1396 rd
->rpl
[i
].name
= NULL
;
1400 /* Detect active and valid domains for the given CPU, caller must
1401 * ensure the CPU belongs to the targeted package and CPU hotlug is disabled.
1403 static int rapl_detect_domains(struct rapl_package
*rp
, int cpu
)
1405 struct rapl_domain
*rd
;
1408 for (i
= 0; i
< RAPL_DOMAIN_MAX
; i
++) {
1409 /* use physical package id to read counters */
1410 if (!rapl_check_domain(cpu
, i
)) {
1411 rp
->domain_map
|= 1 << i
;
1412 pr_info("Found RAPL domain %s\n", rapl_domain_names
[i
]);
1415 rp
->nr_domains
= bitmap_weight(&rp
->domain_map
, RAPL_DOMAIN_MAX
);
1416 if (!rp
->nr_domains
) {
1417 pr_debug("no valid rapl domains found in package %d\n", rp
->id
);
1420 pr_debug("found %d domains on package %d\n", rp
->nr_domains
, rp
->id
);
1422 rp
->domains
= kcalloc(rp
->nr_domains
+ 1, sizeof(struct rapl_domain
),
1427 rapl_init_domains(rp
);
1429 for (rd
= rp
->domains
; rd
< rp
->domains
+ rp
->nr_domains
; rd
++)
1430 rapl_detect_powerlimit(rd
);
1435 /* called from CPU hotplug notifier, hotplug lock held */
1436 static void rapl_remove_package(struct rapl_package
*rp
)
1438 struct rapl_domain
*rd
, *rd_package
= NULL
;
1440 package_power_limit_irq_restore(rp
);
1442 for (rd
= rp
->domains
; rd
< rp
->domains
+ rp
->nr_domains
; rd
++) {
1443 rapl_write_data_raw(rd
, PL1_ENABLE
, 0);
1444 rapl_write_data_raw(rd
, PL1_CLAMP
, 0);
1445 if (find_nr_power_limit(rd
) > 1) {
1446 rapl_write_data_raw(rd
, PL2_ENABLE
, 0);
1447 rapl_write_data_raw(rd
, PL2_CLAMP
, 0);
1449 if (rd
->id
== RAPL_DOMAIN_PACKAGE
) {
1453 pr_debug("remove package, undo power limit on %d: %s\n",
1455 powercap_unregister_zone(control_type
, &rd
->power_zone
);
1457 /* do parent zone last */
1458 powercap_unregister_zone(control_type
, &rd_package
->power_zone
);
1459 list_del(&rp
->plist
);
1463 /* called from CPU hotplug notifier, hotplug lock held */
1464 static struct rapl_package
*rapl_add_package(int cpu
, int pkgid
)
1466 struct rapl_package
*rp
;
1469 rp
= kzalloc(sizeof(struct rapl_package
), GFP_KERNEL
);
1471 return ERR_PTR(-ENOMEM
);
1473 /* add the new package to the list */
1477 /* check if the package contains valid domains */
1478 if (rapl_detect_domains(rp
, cpu
) ||
1479 rapl_defaults
->check_unit(rp
, cpu
)) {
1481 goto err_free_package
;
1483 ret
= rapl_package_register_powercap(rp
);
1485 INIT_LIST_HEAD(&rp
->plist
);
1486 list_add(&rp
->plist
, &rapl_packages
);
1493 return ERR_PTR(ret
);
1496 /* Handles CPU hotplug on multi-socket systems.
1497 * If a CPU goes online as the first CPU of the physical package
1498 * we add the RAPL package to the system. Similarly, when the last
1499 * CPU of the package is removed, we remove the RAPL package and its
1500 * associated domains. Cooling devices are handled accordingly at
1503 static int rapl_cpu_online(unsigned int cpu
)
1505 int pkgid
= topology_physical_package_id(cpu
);
1506 struct rapl_package
*rp
;
1508 rp
= find_package_by_id(pkgid
);
1510 rp
= rapl_add_package(cpu
, pkgid
);
1514 cpumask_set_cpu(cpu
, &rp
->cpumask
);
1518 static int rapl_cpu_down_prep(unsigned int cpu
)
1520 int pkgid
= topology_physical_package_id(cpu
);
1521 struct rapl_package
*rp
;
1524 rp
= find_package_by_id(pkgid
);
1528 cpumask_clear_cpu(cpu
, &rp
->cpumask
);
1529 lead_cpu
= cpumask_first(&rp
->cpumask
);
1530 if (lead_cpu
>= nr_cpu_ids
)
1531 rapl_remove_package(rp
);
1532 else if (rp
->lead_cpu
== cpu
)
1533 rp
->lead_cpu
= lead_cpu
;
1537 static enum cpuhp_state pcap_rapl_online
;
1539 static void power_limit_state_save(void)
1541 struct rapl_package
*rp
;
1542 struct rapl_domain
*rd
;
1546 list_for_each_entry(rp
, &rapl_packages
, plist
) {
1547 if (!rp
->power_zone
)
1549 rd
= power_zone_to_rapl_domain(rp
->power_zone
);
1550 nr_pl
= find_nr_power_limit(rd
);
1551 for (i
= 0; i
< nr_pl
; i
++) {
1552 switch (rd
->rpl
[i
].prim_id
) {
1554 ret
= rapl_read_data_raw(rd
,
1557 &rd
->rpl
[i
].last_power_limit
);
1559 rd
->rpl
[i
].last_power_limit
= 0;
1562 ret
= rapl_read_data_raw(rd
,
1565 &rd
->rpl
[i
].last_power_limit
);
1567 rd
->rpl
[i
].last_power_limit
= 0;
1575 static void power_limit_state_restore(void)
1577 struct rapl_package
*rp
;
1578 struct rapl_domain
*rd
;
1582 list_for_each_entry(rp
, &rapl_packages
, plist
) {
1583 if (!rp
->power_zone
)
1585 rd
= power_zone_to_rapl_domain(rp
->power_zone
);
1586 nr_pl
= find_nr_power_limit(rd
);
1587 for (i
= 0; i
< nr_pl
; i
++) {
1588 switch (rd
->rpl
[i
].prim_id
) {
1590 if (rd
->rpl
[i
].last_power_limit
)
1591 rapl_write_data_raw(rd
,
1593 rd
->rpl
[i
].last_power_limit
);
1596 if (rd
->rpl
[i
].last_power_limit
)
1597 rapl_write_data_raw(rd
,
1599 rd
->rpl
[i
].last_power_limit
);
1607 static int rapl_pm_callback(struct notifier_block
*nb
,
1608 unsigned long mode
, void *_unused
)
1611 case PM_SUSPEND_PREPARE
:
1612 power_limit_state_save();
1614 case PM_POST_SUSPEND
:
1615 power_limit_state_restore();
1621 static struct notifier_block rapl_pm_notifier
= {
1622 .notifier_call
= rapl_pm_callback
,
1625 static int __init
rapl_init(void)
1627 const struct x86_cpu_id
*id
;
1630 id
= x86_match_cpu(rapl_ids
);
1632 pr_err("driver does not support CPU family %d model %d\n",
1633 boot_cpu_data
.x86
, boot_cpu_data
.x86_model
);
1638 rapl_defaults
= (struct rapl_defaults
*)id
->driver_data
;
1640 ret
= rapl_register_powercap();
1644 ret
= cpuhp_setup_state(CPUHP_AP_ONLINE_DYN
, "powercap/rapl:online",
1645 rapl_cpu_online
, rapl_cpu_down_prep
);
1648 pcap_rapl_online
= ret
;
1650 /* Don't bail out if PSys is not supported */
1651 rapl_register_psys();
1653 ret
= register_pm_notifier(&rapl_pm_notifier
);
1660 cpuhp_remove_state(pcap_rapl_online
);
1663 rapl_unregister_powercap();
1667 static void __exit
rapl_exit(void)
1669 unregister_pm_notifier(&rapl_pm_notifier
);
1670 cpuhp_remove_state(pcap_rapl_online
);
1671 rapl_unregister_powercap();
1674 module_init(rapl_init
);
1675 module_exit(rapl_exit
);
1677 MODULE_DESCRIPTION("Driver for Intel RAPL (Running Average Power Limit)");
1678 MODULE_AUTHOR("Jacob Pan <jacob.jun.pan@intel.com>");
1679 MODULE_LICENSE("GPL v2");