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 <asm/iosf_mbi.h>
34 #include <asm/processor.h>
35 #include <asm/cpu_device_id.h>
37 /* bitmasks for RAPL MSRs, used by primitive access functions */
38 #define ENERGY_STATUS_MASK 0xffffffff
40 #define POWER_LIMIT1_MASK 0x7FFF
41 #define POWER_LIMIT1_ENABLE BIT(15)
42 #define POWER_LIMIT1_CLAMP BIT(16)
44 #define POWER_LIMIT2_MASK (0x7FFFULL<<32)
45 #define POWER_LIMIT2_ENABLE BIT_ULL(47)
46 #define POWER_LIMIT2_CLAMP BIT_ULL(48)
47 #define POWER_PACKAGE_LOCK BIT_ULL(63)
48 #define POWER_PP_LOCK BIT(31)
50 #define TIME_WINDOW1_MASK (0x7FULL<<17)
51 #define TIME_WINDOW2_MASK (0x7FULL<<49)
53 #define POWER_UNIT_OFFSET 0
54 #define POWER_UNIT_MASK 0x0F
56 #define ENERGY_UNIT_OFFSET 0x08
57 #define ENERGY_UNIT_MASK 0x1F00
59 #define TIME_UNIT_OFFSET 0x10
60 #define TIME_UNIT_MASK 0xF0000
62 #define POWER_INFO_MAX_MASK (0x7fffULL<<32)
63 #define POWER_INFO_MIN_MASK (0x7fffULL<<16)
64 #define POWER_INFO_MAX_TIME_WIN_MASK (0x3fULL<<48)
65 #define POWER_INFO_THERMAL_SPEC_MASK 0x7fff
67 #define PERF_STATUS_THROTTLE_TIME_MASK 0xffffffff
68 #define PP_POLICY_MASK 0x1F
70 /* Non HW constants */
71 #define RAPL_PRIMITIVE_DERIVED BIT(1) /* not from raw data */
72 #define RAPL_PRIMITIVE_DUMMY BIT(2)
74 #define TIME_WINDOW_MAX_MSEC 40000
75 #define TIME_WINDOW_MIN_MSEC 250
76 #define ENERGY_UNIT_SCALE 1000 /* scale from driver unit to powercap unit */
78 ARBITRARY_UNIT
, /* no translation */
84 enum rapl_domain_type
{
85 RAPL_DOMAIN_PACKAGE
, /* entire package/socket */
86 RAPL_DOMAIN_PP0
, /* core power plane */
87 RAPL_DOMAIN_PP1
, /* graphics uncore */
88 RAPL_DOMAIN_DRAM
,/* DRAM control_type */
92 enum rapl_domain_msr_id
{
93 RAPL_DOMAIN_MSR_LIMIT
,
94 RAPL_DOMAIN_MSR_STATUS
,
96 RAPL_DOMAIN_MSR_POLICY
,
101 /* per domain data, some are optional */
102 enum rapl_primitives
{
108 PL1_ENABLE
, /* power limit 1, aka long term */
109 PL1_CLAMP
, /* allow frequency to go below OS request */
110 PL2_ENABLE
, /* power limit 2, aka short term, instantaneous */
113 TIME_WINDOW1
, /* long term */
114 TIME_WINDOW2
, /* short term */
123 /* below are not raw primitive data */
128 #define NR_RAW_PRIMITIVES (NR_RAPL_PRIMITIVES - 2)
130 /* Can be expanded to include events, etc.*/
131 struct rapl_domain_data
{
132 u64 primitives
[NR_RAPL_PRIMITIVES
];
133 unsigned long timestamp
;
143 #define DOMAIN_STATE_INACTIVE BIT(0)
144 #define DOMAIN_STATE_POWER_LIMIT_SET BIT(1)
145 #define DOMAIN_STATE_BIOS_LOCKED BIT(2)
147 #define NR_POWER_LIMITS (2)
148 struct rapl_power_limit
{
149 struct powercap_zone_constraint
*constraint
;
150 int prim_id
; /* primitive ID used to enable */
151 struct rapl_domain
*domain
;
155 static const char pl1_name
[] = "long_term";
156 static const char pl2_name
[] = "short_term";
161 enum rapl_domain_type id
;
162 int msrs
[RAPL_DOMAIN_MSR_MAX
];
163 struct powercap_zone power_zone
;
164 struct rapl_domain_data rdd
;
165 struct rapl_power_limit rpl
[NR_POWER_LIMITS
];
166 u64 attr_map
; /* track capabilities */
168 unsigned int domain_energy_unit
;
169 struct rapl_package
*rp
;
171 #define power_zone_to_rapl_domain(_zone) \
172 container_of(_zone, struct rapl_domain, power_zone)
175 /* Each physical package contains multiple domains, these are the common
176 * data across RAPL domains within a package.
178 struct rapl_package
{
179 unsigned int id
; /* physical package/socket id */
180 unsigned int nr_domains
;
181 unsigned long domain_map
; /* bit map of active domains */
182 unsigned int power_unit
;
183 unsigned int energy_unit
;
184 unsigned int time_unit
;
185 struct rapl_domain
*domains
; /* array of domains, sized at runtime */
186 struct powercap_zone
*power_zone
; /* keep track of parent zone */
187 int nr_cpus
; /* active cpus on the package, topology info is lost during
188 * cpu hotplug. so we have to track ourselves.
190 unsigned long power_limit_irq
; /* keep track of package power limit
191 * notify interrupt enable status.
193 struct list_head plist
;
194 int lead_cpu
; /* one active cpu per package for access */
197 struct rapl_defaults
{
198 u8 floor_freq_reg_addr
;
199 int (*check_unit
)(struct rapl_package
*rp
, int cpu
);
200 void (*set_floor_freq
)(struct rapl_domain
*rd
, bool mode
);
201 u64 (*compute_time_window
)(struct rapl_package
*rp
, u64 val
,
203 unsigned int dram_domain_energy_unit
;
205 static struct rapl_defaults
*rapl_defaults
;
207 /* Sideband MBI registers */
208 #define IOSF_CPU_POWER_BUDGET_CTL_BYT (0x2)
209 #define IOSF_CPU_POWER_BUDGET_CTL_TNG (0xdf)
211 #define PACKAGE_PLN_INT_SAVED BIT(0)
212 #define MAX_PRIM_NAME (32)
214 /* per domain data. used to describe individual knobs such that access function
215 * can be consolidated into one instead of many inline functions.
217 struct rapl_primitive_info
{
221 enum rapl_domain_msr_id id
;
226 #define PRIMITIVE_INFO_INIT(p, m, s, i, u, f) { \
235 static void rapl_init_domains(struct rapl_package
*rp
);
236 static int rapl_read_data_raw(struct rapl_domain
*rd
,
237 enum rapl_primitives prim
,
238 bool xlate
, u64
*data
);
239 static int rapl_write_data_raw(struct rapl_domain
*rd
,
240 enum rapl_primitives prim
,
241 unsigned long long value
);
242 static u64
rapl_unit_xlate(struct rapl_domain
*rd
,
243 enum unit_type type
, u64 value
,
245 static void package_power_limit_irq_save(struct rapl_package
*rp
);
247 static LIST_HEAD(rapl_packages
); /* guarded by CPU hotplug lock */
249 static const char * const rapl_domain_names
[] = {
256 static struct powercap_control_type
*control_type
; /* PowerCap Controller */
258 /* caller to ensure CPU hotplug lock is held */
259 static struct rapl_package
*find_package_by_id(int id
)
261 struct rapl_package
*rp
;
263 list_for_each_entry(rp
, &rapl_packages
, plist
) {
271 /* caller must hold cpu hotplug lock */
272 static void rapl_cleanup_data(void)
274 struct rapl_package
*p
, *tmp
;
276 list_for_each_entry_safe(p
, tmp
, &rapl_packages
, plist
) {
283 static int get_energy_counter(struct powercap_zone
*power_zone
, u64
*energy_raw
)
285 struct rapl_domain
*rd
;
288 /* prevent CPU hotplug, make sure the RAPL domain does not go
289 * away while reading the counter.
292 rd
= power_zone_to_rapl_domain(power_zone
);
294 if (!rapl_read_data_raw(rd
, ENERGY_COUNTER
, true, &energy_now
)) {
295 *energy_raw
= energy_now
;
305 static int get_max_energy_counter(struct powercap_zone
*pcd_dev
, u64
*energy
)
307 struct rapl_domain
*rd
= power_zone_to_rapl_domain(pcd_dev
);
309 *energy
= rapl_unit_xlate(rd
, ENERGY_UNIT
, ENERGY_STATUS_MASK
, 0);
313 static int release_zone(struct powercap_zone
*power_zone
)
315 struct rapl_domain
*rd
= power_zone_to_rapl_domain(power_zone
);
316 struct rapl_package
*rp
= rd
->rp
;
318 /* package zone is the last zone of a package, we can free
319 * memory here since all children has been unregistered.
321 if (rd
->id
== RAPL_DOMAIN_PACKAGE
) {
330 static int find_nr_power_limit(struct rapl_domain
*rd
)
334 for (i
= 0; i
< NR_POWER_LIMITS
; i
++) {
335 if (rd
->rpl
[i
].name
== NULL
)
342 static int set_domain_enable(struct powercap_zone
*power_zone
, bool mode
)
344 struct rapl_domain
*rd
= power_zone_to_rapl_domain(power_zone
);
346 if (rd
->state
& DOMAIN_STATE_BIOS_LOCKED
)
350 rapl_write_data_raw(rd
, PL1_ENABLE
, mode
);
351 if (rapl_defaults
->set_floor_freq
)
352 rapl_defaults
->set_floor_freq(rd
, mode
);
358 static int get_domain_enable(struct powercap_zone
*power_zone
, bool *mode
)
360 struct rapl_domain
*rd
= power_zone_to_rapl_domain(power_zone
);
363 if (rd
->state
& DOMAIN_STATE_BIOS_LOCKED
) {
368 if (rapl_read_data_raw(rd
, PL1_ENABLE
, true, &val
)) {
378 /* per RAPL domain ops, in the order of rapl_domain_type */
379 static const struct powercap_zone_ops zone_ops
[] = {
380 /* RAPL_DOMAIN_PACKAGE */
382 .get_energy_uj
= get_energy_counter
,
383 .get_max_energy_range_uj
= get_max_energy_counter
,
384 .release
= release_zone
,
385 .set_enable
= set_domain_enable
,
386 .get_enable
= get_domain_enable
,
388 /* RAPL_DOMAIN_PP0 */
390 .get_energy_uj
= get_energy_counter
,
391 .get_max_energy_range_uj
= get_max_energy_counter
,
392 .release
= release_zone
,
393 .set_enable
= set_domain_enable
,
394 .get_enable
= get_domain_enable
,
396 /* RAPL_DOMAIN_PP1 */
398 .get_energy_uj
= get_energy_counter
,
399 .get_max_energy_range_uj
= get_max_energy_counter
,
400 .release
= release_zone
,
401 .set_enable
= set_domain_enable
,
402 .get_enable
= get_domain_enable
,
404 /* RAPL_DOMAIN_DRAM */
406 .get_energy_uj
= get_energy_counter
,
407 .get_max_energy_range_uj
= get_max_energy_counter
,
408 .release
= release_zone
,
409 .set_enable
= set_domain_enable
,
410 .get_enable
= get_domain_enable
,
414 static int set_power_limit(struct powercap_zone
*power_zone
, int id
,
417 struct rapl_domain
*rd
;
418 struct rapl_package
*rp
;
422 rd
= power_zone_to_rapl_domain(power_zone
);
425 if (rd
->state
& DOMAIN_STATE_BIOS_LOCKED
) {
426 dev_warn(&power_zone
->dev
, "%s locked by BIOS, monitoring only\n",
432 switch (rd
->rpl
[id
].prim_id
) {
434 rapl_write_data_raw(rd
, POWER_LIMIT1
, power_limit
);
437 rapl_write_data_raw(rd
, POWER_LIMIT2
, power_limit
);
443 package_power_limit_irq_save(rp
);
449 static int get_current_power_limit(struct powercap_zone
*power_zone
, int id
,
452 struct rapl_domain
*rd
;
458 rd
= power_zone_to_rapl_domain(power_zone
);
459 switch (rd
->rpl
[id
].prim_id
) {
470 if (rapl_read_data_raw(rd
, prim
, true, &val
))
480 static int set_time_window(struct powercap_zone
*power_zone
, int id
,
483 struct rapl_domain
*rd
;
487 rd
= power_zone_to_rapl_domain(power_zone
);
488 switch (rd
->rpl
[id
].prim_id
) {
490 rapl_write_data_raw(rd
, TIME_WINDOW1
, window
);
493 rapl_write_data_raw(rd
, TIME_WINDOW2
, window
);
502 static int get_time_window(struct powercap_zone
*power_zone
, int id
, u64
*data
)
504 struct rapl_domain
*rd
;
509 rd
= power_zone_to_rapl_domain(power_zone
);
510 switch (rd
->rpl
[id
].prim_id
) {
512 ret
= rapl_read_data_raw(rd
, TIME_WINDOW1
, true, &val
);
515 ret
= rapl_read_data_raw(rd
, TIME_WINDOW2
, true, &val
);
528 static const char *get_constraint_name(struct powercap_zone
*power_zone
, int id
)
530 struct rapl_power_limit
*rpl
;
531 struct rapl_domain
*rd
;
533 rd
= power_zone_to_rapl_domain(power_zone
);
534 rpl
= (struct rapl_power_limit
*) &rd
->rpl
[id
];
540 static int get_max_power(struct powercap_zone
*power_zone
, int id
,
543 struct rapl_domain
*rd
;
549 rd
= power_zone_to_rapl_domain(power_zone
);
550 switch (rd
->rpl
[id
].prim_id
) {
552 prim
= THERMAL_SPEC_POWER
;
561 if (rapl_read_data_raw(rd
, prim
, true, &val
))
571 static const struct powercap_zone_constraint_ops constraint_ops
= {
572 .set_power_limit_uw
= set_power_limit
,
573 .get_power_limit_uw
= get_current_power_limit
,
574 .set_time_window_us
= set_time_window
,
575 .get_time_window_us
= get_time_window
,
576 .get_max_power_uw
= get_max_power
,
577 .get_name
= get_constraint_name
,
580 /* called after domain detection and package level data are set */
581 static void rapl_init_domains(struct rapl_package
*rp
)
584 struct rapl_domain
*rd
= rp
->domains
;
586 for (i
= 0; i
< RAPL_DOMAIN_MAX
; i
++) {
587 unsigned int mask
= rp
->domain_map
& (1 << i
);
589 case BIT(RAPL_DOMAIN_PACKAGE
):
590 rd
->name
= rapl_domain_names
[RAPL_DOMAIN_PACKAGE
];
591 rd
->id
= RAPL_DOMAIN_PACKAGE
;
592 rd
->msrs
[0] = MSR_PKG_POWER_LIMIT
;
593 rd
->msrs
[1] = MSR_PKG_ENERGY_STATUS
;
594 rd
->msrs
[2] = MSR_PKG_PERF_STATUS
;
596 rd
->msrs
[4] = MSR_PKG_POWER_INFO
;
597 rd
->rpl
[0].prim_id
= PL1_ENABLE
;
598 rd
->rpl
[0].name
= pl1_name
;
599 rd
->rpl
[1].prim_id
= PL2_ENABLE
;
600 rd
->rpl
[1].name
= pl2_name
;
602 case BIT(RAPL_DOMAIN_PP0
):
603 rd
->name
= rapl_domain_names
[RAPL_DOMAIN_PP0
];
604 rd
->id
= RAPL_DOMAIN_PP0
;
605 rd
->msrs
[0] = MSR_PP0_POWER_LIMIT
;
606 rd
->msrs
[1] = MSR_PP0_ENERGY_STATUS
;
608 rd
->msrs
[3] = MSR_PP0_POLICY
;
610 rd
->rpl
[0].prim_id
= PL1_ENABLE
;
611 rd
->rpl
[0].name
= pl1_name
;
613 case BIT(RAPL_DOMAIN_PP1
):
614 rd
->name
= rapl_domain_names
[RAPL_DOMAIN_PP1
];
615 rd
->id
= RAPL_DOMAIN_PP1
;
616 rd
->msrs
[0] = MSR_PP1_POWER_LIMIT
;
617 rd
->msrs
[1] = MSR_PP1_ENERGY_STATUS
;
619 rd
->msrs
[3] = MSR_PP1_POLICY
;
621 rd
->rpl
[0].prim_id
= PL1_ENABLE
;
622 rd
->rpl
[0].name
= pl1_name
;
624 case BIT(RAPL_DOMAIN_DRAM
):
625 rd
->name
= rapl_domain_names
[RAPL_DOMAIN_DRAM
];
626 rd
->id
= RAPL_DOMAIN_DRAM
;
627 rd
->msrs
[0] = MSR_DRAM_POWER_LIMIT
;
628 rd
->msrs
[1] = MSR_DRAM_ENERGY_STATUS
;
629 rd
->msrs
[2] = MSR_DRAM_PERF_STATUS
;
631 rd
->msrs
[4] = MSR_DRAM_POWER_INFO
;
632 rd
->rpl
[0].prim_id
= PL1_ENABLE
;
633 rd
->rpl
[0].name
= pl1_name
;
634 rd
->domain_energy_unit
=
635 rapl_defaults
->dram_domain_energy_unit
;
636 if (rd
->domain_energy_unit
)
637 pr_info("DRAM domain energy unit %dpj\n",
638 rd
->domain_energy_unit
);
648 static u64
rapl_unit_xlate(struct rapl_domain
*rd
, enum unit_type type
,
649 u64 value
, int to_raw
)
652 struct rapl_package
*rp
= rd
->rp
;
657 units
= rp
->power_unit
;
660 scale
= ENERGY_UNIT_SCALE
;
661 /* per domain unit takes precedence */
662 if (rd
&& rd
->domain_energy_unit
)
663 units
= rd
->domain_energy_unit
;
665 units
= rp
->energy_unit
;
668 return rapl_defaults
->compute_time_window(rp
, value
, to_raw
);
675 return div64_u64(value
, units
) * scale
;
679 return div64_u64(value
, scale
);
682 /* in the order of enum rapl_primitives */
683 static struct rapl_primitive_info rpi
[] = {
684 /* name, mask, shift, msr index, unit divisor */
685 PRIMITIVE_INFO_INIT(ENERGY_COUNTER
, ENERGY_STATUS_MASK
, 0,
686 RAPL_DOMAIN_MSR_STATUS
, ENERGY_UNIT
, 0),
687 PRIMITIVE_INFO_INIT(POWER_LIMIT1
, POWER_LIMIT1_MASK
, 0,
688 RAPL_DOMAIN_MSR_LIMIT
, POWER_UNIT
, 0),
689 PRIMITIVE_INFO_INIT(POWER_LIMIT2
, POWER_LIMIT2_MASK
, 32,
690 RAPL_DOMAIN_MSR_LIMIT
, POWER_UNIT
, 0),
691 PRIMITIVE_INFO_INIT(FW_LOCK
, POWER_PP_LOCK
, 31,
692 RAPL_DOMAIN_MSR_LIMIT
, ARBITRARY_UNIT
, 0),
693 PRIMITIVE_INFO_INIT(PL1_ENABLE
, POWER_LIMIT1_ENABLE
, 15,
694 RAPL_DOMAIN_MSR_LIMIT
, ARBITRARY_UNIT
, 0),
695 PRIMITIVE_INFO_INIT(PL1_CLAMP
, POWER_LIMIT1_CLAMP
, 16,
696 RAPL_DOMAIN_MSR_LIMIT
, ARBITRARY_UNIT
, 0),
697 PRIMITIVE_INFO_INIT(PL2_ENABLE
, POWER_LIMIT2_ENABLE
, 47,
698 RAPL_DOMAIN_MSR_LIMIT
, ARBITRARY_UNIT
, 0),
699 PRIMITIVE_INFO_INIT(PL2_CLAMP
, POWER_LIMIT2_CLAMP
, 48,
700 RAPL_DOMAIN_MSR_LIMIT
, ARBITRARY_UNIT
, 0),
701 PRIMITIVE_INFO_INIT(TIME_WINDOW1
, TIME_WINDOW1_MASK
, 17,
702 RAPL_DOMAIN_MSR_LIMIT
, TIME_UNIT
, 0),
703 PRIMITIVE_INFO_INIT(TIME_WINDOW2
, TIME_WINDOW2_MASK
, 49,
704 RAPL_DOMAIN_MSR_LIMIT
, TIME_UNIT
, 0),
705 PRIMITIVE_INFO_INIT(THERMAL_SPEC_POWER
, POWER_INFO_THERMAL_SPEC_MASK
,
706 0, RAPL_DOMAIN_MSR_INFO
, POWER_UNIT
, 0),
707 PRIMITIVE_INFO_INIT(MAX_POWER
, POWER_INFO_MAX_MASK
, 32,
708 RAPL_DOMAIN_MSR_INFO
, POWER_UNIT
, 0),
709 PRIMITIVE_INFO_INIT(MIN_POWER
, POWER_INFO_MIN_MASK
, 16,
710 RAPL_DOMAIN_MSR_INFO
, POWER_UNIT
, 0),
711 PRIMITIVE_INFO_INIT(MAX_TIME_WINDOW
, POWER_INFO_MAX_TIME_WIN_MASK
, 48,
712 RAPL_DOMAIN_MSR_INFO
, TIME_UNIT
, 0),
713 PRIMITIVE_INFO_INIT(THROTTLED_TIME
, PERF_STATUS_THROTTLE_TIME_MASK
, 0,
714 RAPL_DOMAIN_MSR_PERF
, TIME_UNIT
, 0),
715 PRIMITIVE_INFO_INIT(PRIORITY_LEVEL
, PP_POLICY_MASK
, 0,
716 RAPL_DOMAIN_MSR_POLICY
, ARBITRARY_UNIT
, 0),
718 PRIMITIVE_INFO_INIT(AVERAGE_POWER
, 0, 0, 0, POWER_UNIT
,
719 RAPL_PRIMITIVE_DERIVED
),
723 /* Read primitive data based on its related struct rapl_primitive_info.
724 * if xlate flag is set, return translated data based on data units, i.e.
725 * time, energy, and power.
726 * RAPL MSRs are non-architectual and are laid out not consistently across
727 * domains. Here we use primitive info to allow writing consolidated access
729 * For a given primitive, it is processed by MSR mask and shift. Unit conversion
730 * is pre-assigned based on RAPL unit MSRs read at init time.
731 * 63-------------------------- 31--------------------------- 0
733 * | |<- shift ----------------|
734 * 63-------------------------- 31--------------------------- 0
736 static int rapl_read_data_raw(struct rapl_domain
*rd
,
737 enum rapl_primitives prim
,
738 bool xlate
, u64
*data
)
742 struct rapl_primitive_info
*rp
= &rpi
[prim
];
745 if (!rp
->name
|| rp
->flag
& RAPL_PRIMITIVE_DUMMY
)
748 msr
= rd
->msrs
[rp
->id
];
752 cpu
= rd
->rp
->lead_cpu
;
754 /* special-case package domain, which uses a different bit*/
755 if (prim
== FW_LOCK
&& rd
->id
== RAPL_DOMAIN_PACKAGE
) {
756 rp
->mask
= POWER_PACKAGE_LOCK
;
759 /* non-hardware data are collected by the polling thread */
760 if (rp
->flag
& RAPL_PRIMITIVE_DERIVED
) {
761 *data
= rd
->rdd
.primitives
[prim
];
765 if (rdmsrl_safe_on_cpu(cpu
, msr
, &value
)) {
766 pr_debug("failed to read msr 0x%x on cpu %d\n", msr
, cpu
);
770 final
= value
& rp
->mask
;
771 final
= final
>> rp
->shift
;
773 *data
= rapl_unit_xlate(rd
, rp
->unit
, final
, 0);
781 static int msrl_update_safe(u32 msr_no
, u64 clear_mask
, u64 set_mask
)
786 err
= rdmsrl_safe(msr_no
, &val
);
793 err
= wrmsrl_safe(msr_no
, val
);
799 static void msrl_update_func(void *info
)
801 struct msrl_action
*ma
= info
;
803 ma
->err
= msrl_update_safe(ma
->msr_no
, ma
->clear_mask
, ma
->set_mask
);
806 /* Similar use of primitive info in the read counterpart */
807 static int rapl_write_data_raw(struct rapl_domain
*rd
,
808 enum rapl_primitives prim
,
809 unsigned long long value
)
811 struct rapl_primitive_info
*rp
= &rpi
[prim
];
814 struct msrl_action ma
;
817 cpu
= rd
->rp
->lead_cpu
;
818 bits
= rapl_unit_xlate(rd
, rp
->unit
, value
, 1);
819 bits
|= bits
<< rp
->shift
;
820 memset(&ma
, 0, sizeof(ma
));
822 ma
.msr_no
= rd
->msrs
[rp
->id
];
823 ma
.clear_mask
= rp
->mask
;
826 ret
= smp_call_function_single(cpu
, msrl_update_func
, &ma
, 1);
836 * Raw RAPL data stored in MSRs are in certain scales. We need to
837 * convert them into standard units based on the units reported in
838 * the RAPL unit MSRs. This is specific to CPUs as the method to
839 * calculate units differ on different CPUs.
840 * We convert the units to below format based on CPUs.
842 * energy unit: picoJoules : Represented in picoJoules by default
843 * power unit : microWatts : Represented in milliWatts by default
844 * time unit : microseconds: Represented in seconds by default
846 static int rapl_check_unit_core(struct rapl_package
*rp
, int cpu
)
851 if (rdmsrl_safe_on_cpu(cpu
, MSR_RAPL_POWER_UNIT
, &msr_val
)) {
852 pr_err("Failed to read power unit MSR 0x%x on CPU %d, exit.\n",
853 MSR_RAPL_POWER_UNIT
, cpu
);
857 value
= (msr_val
& ENERGY_UNIT_MASK
) >> ENERGY_UNIT_OFFSET
;
858 rp
->energy_unit
= ENERGY_UNIT_SCALE
* 1000000 / (1 << value
);
860 value
= (msr_val
& POWER_UNIT_MASK
) >> POWER_UNIT_OFFSET
;
861 rp
->power_unit
= 1000000 / (1 << value
);
863 value
= (msr_val
& TIME_UNIT_MASK
) >> TIME_UNIT_OFFSET
;
864 rp
->time_unit
= 1000000 / (1 << value
);
866 pr_debug("Core CPU package %d energy=%dpJ, time=%dus, power=%duW\n",
867 rp
->id
, rp
->energy_unit
, rp
->time_unit
, rp
->power_unit
);
872 static int rapl_check_unit_atom(struct rapl_package
*rp
, int cpu
)
877 if (rdmsrl_safe_on_cpu(cpu
, MSR_RAPL_POWER_UNIT
, &msr_val
)) {
878 pr_err("Failed to read power unit MSR 0x%x on CPU %d, exit.\n",
879 MSR_RAPL_POWER_UNIT
, cpu
);
882 value
= (msr_val
& ENERGY_UNIT_MASK
) >> ENERGY_UNIT_OFFSET
;
883 rp
->energy_unit
= ENERGY_UNIT_SCALE
* 1 << value
;
885 value
= (msr_val
& POWER_UNIT_MASK
) >> POWER_UNIT_OFFSET
;
886 rp
->power_unit
= (1 << value
) * 1000;
888 value
= (msr_val
& TIME_UNIT_MASK
) >> TIME_UNIT_OFFSET
;
889 rp
->time_unit
= 1000000 / (1 << value
);
891 pr_debug("Atom package %d energy=%dpJ, time=%dus, power=%duW\n",
892 rp
->id
, rp
->energy_unit
, rp
->time_unit
, rp
->power_unit
);
897 static void power_limit_irq_save_cpu(void *info
)
900 struct rapl_package
*rp
= (struct rapl_package
*)info
;
902 /* save the state of PLN irq mask bit before disabling it */
903 rdmsr_safe(MSR_IA32_PACKAGE_THERM_INTERRUPT
, &l
, &h
);
904 if (!(rp
->power_limit_irq
& PACKAGE_PLN_INT_SAVED
)) {
905 rp
->power_limit_irq
= l
& PACKAGE_THERM_INT_PLN_ENABLE
;
906 rp
->power_limit_irq
|= PACKAGE_PLN_INT_SAVED
;
908 l
&= ~PACKAGE_THERM_INT_PLN_ENABLE
;
909 wrmsr_safe(MSR_IA32_PACKAGE_THERM_INTERRUPT
, l
, h
);
914 * When package power limit is set artificially low by RAPL, LVT
915 * thermal interrupt for package power limit should be ignored
916 * since we are not really exceeding the real limit. The intention
917 * is to avoid excessive interrupts while we are trying to save power.
918 * A useful feature might be routing the package_power_limit interrupt
919 * to userspace via eventfd. once we have a usecase, this is simple
920 * to do by adding an atomic notifier.
923 static void package_power_limit_irq_save(struct rapl_package
*rp
)
925 if (!boot_cpu_has(X86_FEATURE_PTS
) || !boot_cpu_has(X86_FEATURE_PLN
))
928 smp_call_function_single(rp
->lead_cpu
, power_limit_irq_save_cpu
, rp
, 1);
931 static void power_limit_irq_restore_cpu(void *info
)
934 struct rapl_package
*rp
= (struct rapl_package
*)info
;
936 rdmsr_safe(MSR_IA32_PACKAGE_THERM_INTERRUPT
, &l
, &h
);
938 if (rp
->power_limit_irq
& PACKAGE_THERM_INT_PLN_ENABLE
)
939 l
|= PACKAGE_THERM_INT_PLN_ENABLE
;
941 l
&= ~PACKAGE_THERM_INT_PLN_ENABLE
;
943 wrmsr_safe(MSR_IA32_PACKAGE_THERM_INTERRUPT
, l
, h
);
946 /* restore per package power limit interrupt enable state */
947 static void package_power_limit_irq_restore(struct rapl_package
*rp
)
949 if (!boot_cpu_has(X86_FEATURE_PTS
) || !boot_cpu_has(X86_FEATURE_PLN
))
952 /* irq enable state not saved, nothing to restore */
953 if (!(rp
->power_limit_irq
& PACKAGE_PLN_INT_SAVED
))
956 smp_call_function_single(rp
->lead_cpu
, power_limit_irq_restore_cpu
, rp
, 1);
959 static void set_floor_freq_default(struct rapl_domain
*rd
, bool mode
)
961 int nr_powerlimit
= find_nr_power_limit(rd
);
963 /* always enable clamp such that p-state can go below OS requested
964 * range. power capping priority over guranteed frequency.
966 rapl_write_data_raw(rd
, PL1_CLAMP
, mode
);
968 /* some domains have pl2 */
969 if (nr_powerlimit
> 1) {
970 rapl_write_data_raw(rd
, PL2_ENABLE
, mode
);
971 rapl_write_data_raw(rd
, PL2_CLAMP
, mode
);
975 static void set_floor_freq_atom(struct rapl_domain
*rd
, bool enable
)
977 static u32 power_ctrl_orig_val
;
980 if (!rapl_defaults
->floor_freq_reg_addr
) {
981 pr_err("Invalid floor frequency config register\n");
985 if (!power_ctrl_orig_val
)
986 iosf_mbi_read(BT_MBI_UNIT_PMC
, MBI_CR_READ
,
987 rapl_defaults
->floor_freq_reg_addr
,
988 &power_ctrl_orig_val
);
989 mdata
= power_ctrl_orig_val
;
991 mdata
&= ~(0x7f << 8);
994 iosf_mbi_write(BT_MBI_UNIT_PMC
, MBI_CR_WRITE
,
995 rapl_defaults
->floor_freq_reg_addr
, mdata
);
998 static u64
rapl_compute_time_window_core(struct rapl_package
*rp
, u64 value
,
1001 u64 f
, y
; /* fraction and exp. used for time unit */
1004 * Special processing based on 2^Y*(1+F/4), refer
1005 * to Intel Software Developer's manual Vol.3B: CH 14.9.3.
1008 f
= (value
& 0x60) >> 5;
1010 value
= (1 << y
) * (4 + f
) * rp
->time_unit
/ 4;
1012 do_div(value
, rp
->time_unit
);
1014 f
= div64_u64(4 * (value
- (1 << y
)), 1 << y
);
1015 value
= (y
& 0x1f) | ((f
& 0x3) << 5);
1020 static u64
rapl_compute_time_window_atom(struct rapl_package
*rp
, u64 value
,
1024 * Atom time unit encoding is straight forward val * time_unit,
1025 * where time_unit is default to 1 sec. Never 0.
1028 return (value
) ? value
*= rp
->time_unit
: rp
->time_unit
;
1030 value
= div64_u64(value
, rp
->time_unit
);
1035 static const struct rapl_defaults rapl_defaults_core
= {
1036 .floor_freq_reg_addr
= 0,
1037 .check_unit
= rapl_check_unit_core
,
1038 .set_floor_freq
= set_floor_freq_default
,
1039 .compute_time_window
= rapl_compute_time_window_core
,
1042 static const struct rapl_defaults rapl_defaults_hsw_server
= {
1043 .check_unit
= rapl_check_unit_core
,
1044 .set_floor_freq
= set_floor_freq_default
,
1045 .compute_time_window
= rapl_compute_time_window_core
,
1046 .dram_domain_energy_unit
= 15300,
1049 static const struct rapl_defaults rapl_defaults_byt
= {
1050 .floor_freq_reg_addr
= IOSF_CPU_POWER_BUDGET_CTL_BYT
,
1051 .check_unit
= rapl_check_unit_atom
,
1052 .set_floor_freq
= set_floor_freq_atom
,
1053 .compute_time_window
= rapl_compute_time_window_atom
,
1056 static const struct rapl_defaults rapl_defaults_tng
= {
1057 .floor_freq_reg_addr
= IOSF_CPU_POWER_BUDGET_CTL_TNG
,
1058 .check_unit
= rapl_check_unit_atom
,
1059 .set_floor_freq
= set_floor_freq_atom
,
1060 .compute_time_window
= rapl_compute_time_window_atom
,
1063 static const struct rapl_defaults rapl_defaults_ann
= {
1064 .floor_freq_reg_addr
= 0,
1065 .check_unit
= rapl_check_unit_atom
,
1066 .set_floor_freq
= NULL
,
1067 .compute_time_window
= rapl_compute_time_window_atom
,
1070 static const struct rapl_defaults rapl_defaults_cht
= {
1071 .floor_freq_reg_addr
= 0,
1072 .check_unit
= rapl_check_unit_atom
,
1073 .set_floor_freq
= NULL
,
1074 .compute_time_window
= rapl_compute_time_window_atom
,
1077 #define RAPL_CPU(_model, _ops) { \
1078 .vendor = X86_VENDOR_INTEL, \
1081 .driver_data = (kernel_ulong_t)&_ops, \
1084 static const struct x86_cpu_id rapl_ids
[] __initconst
= {
1085 RAPL_CPU(0x2a, rapl_defaults_core
),/* Sandy Bridge */
1086 RAPL_CPU(0x2d, rapl_defaults_core
),/* Sandy Bridge EP */
1087 RAPL_CPU(0x37, rapl_defaults_byt
),/* Valleyview */
1088 RAPL_CPU(0x3a, rapl_defaults_core
),/* Ivy Bridge */
1089 RAPL_CPU(0x3c, rapl_defaults_core
),/* Haswell */
1090 RAPL_CPU(0x3d, rapl_defaults_core
),/* Broadwell */
1091 RAPL_CPU(0x3f, rapl_defaults_hsw_server
),/* Haswell servers */
1092 RAPL_CPU(0x4f, rapl_defaults_hsw_server
),/* Broadwell servers */
1093 RAPL_CPU(0x45, rapl_defaults_core
),/* Haswell ULT */
1094 RAPL_CPU(0x46, rapl_defaults_core
),/* Haswell */
1095 RAPL_CPU(0x47, rapl_defaults_core
),/* Broadwell-H */
1096 RAPL_CPU(0x4E, rapl_defaults_core
),/* Skylake */
1097 RAPL_CPU(0x4C, rapl_defaults_cht
),/* Braswell/Cherryview */
1098 RAPL_CPU(0x4A, rapl_defaults_tng
),/* Tangier */
1099 RAPL_CPU(0x56, rapl_defaults_core
),/* Future Xeon */
1100 RAPL_CPU(0x5A, rapl_defaults_ann
),/* Annidale */
1101 RAPL_CPU(0X5C, rapl_defaults_core
),/* Broxton */
1102 RAPL_CPU(0x5E, rapl_defaults_core
),/* Skylake-H/S */
1103 RAPL_CPU(0x57, rapl_defaults_hsw_server
),/* Knights Landing */
1106 MODULE_DEVICE_TABLE(x86cpu
, rapl_ids
);
1108 /* read once for all raw primitive data for all packages, domains */
1109 static void rapl_update_domain_data(void)
1113 struct rapl_package
*rp
;
1115 list_for_each_entry(rp
, &rapl_packages
, plist
) {
1116 for (dmn
= 0; dmn
< rp
->nr_domains
; dmn
++) {
1117 pr_debug("update package %d domain %s data\n", rp
->id
,
1118 rp
->domains
[dmn
].name
);
1119 /* exclude non-raw primitives */
1120 for (prim
= 0; prim
< NR_RAW_PRIMITIVES
; prim
++)
1121 if (!rapl_read_data_raw(&rp
->domains
[dmn
], prim
,
1124 rp
->domains
[dmn
].rdd
.primitives
[prim
] =
1131 static int rapl_unregister_powercap(void)
1133 struct rapl_package
*rp
;
1134 struct rapl_domain
*rd
, *rd_package
= NULL
;
1136 /* unregister all active rapl packages from the powercap layer,
1139 list_for_each_entry(rp
, &rapl_packages
, plist
) {
1140 package_power_limit_irq_restore(rp
);
1142 for (rd
= rp
->domains
; rd
< rp
->domains
+ rp
->nr_domains
;
1144 pr_debug("remove package, undo power limit on %d: %s\n",
1146 rapl_write_data_raw(rd
, PL1_ENABLE
, 0);
1147 rapl_write_data_raw(rd
, PL1_CLAMP
, 0);
1148 if (find_nr_power_limit(rd
) > 1) {
1149 rapl_write_data_raw(rd
, PL2_ENABLE
, 0);
1150 rapl_write_data_raw(rd
, PL2_CLAMP
, 0);
1152 if (rd
->id
== RAPL_DOMAIN_PACKAGE
) {
1156 powercap_unregister_zone(control_type
, &rd
->power_zone
);
1158 /* do the package zone last */
1160 powercap_unregister_zone(control_type
,
1161 &rd_package
->power_zone
);
1163 powercap_unregister_control_type(control_type
);
1168 static int rapl_package_register_powercap(struct rapl_package
*rp
)
1170 struct rapl_domain
*rd
;
1172 char dev_name
[17]; /* max domain name = 7 + 1 + 8 for int + 1 for null*/
1173 struct powercap_zone
*power_zone
= NULL
;
1176 /* first we register package domain as the parent zone*/
1177 for (rd
= rp
->domains
; rd
< rp
->domains
+ rp
->nr_domains
; rd
++) {
1178 if (rd
->id
== RAPL_DOMAIN_PACKAGE
) {
1179 nr_pl
= find_nr_power_limit(rd
);
1180 pr_debug("register socket %d package domain %s\n",
1182 memset(dev_name
, 0, sizeof(dev_name
));
1183 snprintf(dev_name
, sizeof(dev_name
), "%s-%d",
1185 power_zone
= powercap_register_zone(&rd
->power_zone
,
1191 if (IS_ERR(power_zone
)) {
1192 pr_debug("failed to register package, %d\n",
1194 ret
= PTR_ERR(power_zone
);
1197 /* track parent zone in per package/socket data */
1198 rp
->power_zone
= power_zone
;
1199 /* done, only one package domain per socket */
1204 pr_err("no package domain found, unknown topology!\n");
1208 /* now register domains as children of the socket/package*/
1209 for (rd
= rp
->domains
; rd
< rp
->domains
+ rp
->nr_domains
; rd
++) {
1210 if (rd
->id
== RAPL_DOMAIN_PACKAGE
)
1212 /* number of power limits per domain varies */
1213 nr_pl
= find_nr_power_limit(rd
);
1214 power_zone
= powercap_register_zone(&rd
->power_zone
,
1215 control_type
, rd
->name
,
1217 &zone_ops
[rd
->id
], nr_pl
,
1220 if (IS_ERR(power_zone
)) {
1221 pr_debug("failed to register power_zone, %d:%s:%s\n",
1222 rp
->id
, rd
->name
, dev_name
);
1223 ret
= PTR_ERR(power_zone
);
1231 /* clean up previously initialized domains within the package if we
1232 * failed after the first domain setup.
1234 while (--rd
>= rp
->domains
) {
1235 pr_debug("unregister package %d domain %s\n", rp
->id
, rd
->name
);
1236 powercap_unregister_zone(control_type
, &rd
->power_zone
);
1242 static int rapl_register_powercap(void)
1244 struct rapl_domain
*rd
;
1245 struct rapl_package
*rp
;
1248 control_type
= powercap_register_control_type(NULL
, "intel-rapl", NULL
);
1249 if (IS_ERR(control_type
)) {
1250 pr_debug("failed to register powercap control_type.\n");
1251 return PTR_ERR(control_type
);
1253 /* read the initial data */
1254 rapl_update_domain_data();
1255 list_for_each_entry(rp
, &rapl_packages
, plist
)
1256 if (rapl_package_register_powercap(rp
))
1257 goto err_cleanup_package
;
1260 err_cleanup_package
:
1261 /* clean up previously initialized packages */
1262 list_for_each_entry_continue_reverse(rp
, &rapl_packages
, plist
) {
1263 for (rd
= rp
->domains
; rd
< rp
->domains
+ rp
->nr_domains
;
1265 pr_debug("unregister zone/package %d, %s domain\n",
1267 powercap_unregister_zone(control_type
, &rd
->power_zone
);
1274 static int rapl_check_domain(int cpu
, int domain
)
1280 case RAPL_DOMAIN_PACKAGE
:
1281 msr
= MSR_PKG_ENERGY_STATUS
;
1283 case RAPL_DOMAIN_PP0
:
1284 msr
= MSR_PP0_ENERGY_STATUS
;
1286 case RAPL_DOMAIN_PP1
:
1287 msr
= MSR_PP1_ENERGY_STATUS
;
1289 case RAPL_DOMAIN_DRAM
:
1290 msr
= MSR_DRAM_ENERGY_STATUS
;
1293 pr_err("invalid domain id %d\n", domain
);
1296 /* make sure domain counters are available and contains non-zero
1297 * values, otherwise skip it.
1299 if (rdmsrl_safe_on_cpu(cpu
, msr
, &val
) || !val
)
1305 /* Detect active and valid domains for the given CPU, caller must
1306 * ensure the CPU belongs to the targeted package and CPU hotlug is disabled.
1308 static int rapl_detect_domains(struct rapl_package
*rp
, int cpu
)
1312 struct rapl_domain
*rd
;
1315 for (i
= 0; i
< RAPL_DOMAIN_MAX
; i
++) {
1316 /* use physical package id to read counters */
1317 if (!rapl_check_domain(cpu
, i
)) {
1318 rp
->domain_map
|= 1 << i
;
1319 pr_info("Found RAPL domain %s\n", rapl_domain_names
[i
]);
1322 rp
->nr_domains
= bitmap_weight(&rp
->domain_map
, RAPL_DOMAIN_MAX
);
1323 if (!rp
->nr_domains
) {
1324 pr_err("no valid rapl domains found in package %d\n", rp
->id
);
1328 pr_debug("found %d domains on package %d\n", rp
->nr_domains
, rp
->id
);
1330 rp
->domains
= kcalloc(rp
->nr_domains
+ 1, sizeof(struct rapl_domain
),
1336 rapl_init_domains(rp
);
1338 for (rd
= rp
->domains
; rd
< rp
->domains
+ rp
->nr_domains
; rd
++) {
1339 /* check if the domain is locked by BIOS */
1340 ret
= rapl_read_data_raw(rd
, FW_LOCK
, false, &locked
);
1344 pr_info("RAPL package %d domain %s locked by BIOS\n",
1346 rd
->state
|= DOMAIN_STATE_BIOS_LOCKED
;
1355 static bool is_package_new(int package
)
1357 struct rapl_package
*rp
;
1359 /* caller prevents cpu hotplug, there will be no new packages added
1360 * or deleted while traversing the package list, no need for locking.
1362 list_for_each_entry(rp
, &rapl_packages
, plist
)
1363 if (package
== rp
->id
)
1369 /* RAPL interface can be made of a two-level hierarchy: package level and domain
1370 * level. We first detect the number of packages then domains of each package.
1371 * We have to consider the possiblity of CPU online/offline due to hotplug and
1374 static int rapl_detect_topology(void)
1378 struct rapl_package
*new_package
, *rp
;
1380 for_each_online_cpu(i
) {
1381 phy_package_id
= topology_physical_package_id(i
);
1382 if (is_package_new(phy_package_id
)) {
1383 new_package
= kzalloc(sizeof(*rp
), GFP_KERNEL
);
1385 rapl_cleanup_data();
1388 /* add the new package to the list */
1389 new_package
->id
= phy_package_id
;
1390 new_package
->nr_cpus
= 1;
1391 /* use the first active cpu of the package to access */
1392 new_package
->lead_cpu
= i
;
1393 /* check if the package contains valid domains */
1394 if (rapl_detect_domains(new_package
, i
) ||
1395 rapl_defaults
->check_unit(new_package
, i
)) {
1396 kfree(new_package
->domains
);
1398 /* free up the packages already initialized */
1399 rapl_cleanup_data();
1402 INIT_LIST_HEAD(&new_package
->plist
);
1403 list_add(&new_package
->plist
, &rapl_packages
);
1405 rp
= find_package_by_id(phy_package_id
);
1414 /* called from CPU hotplug notifier, hotplug lock held */
1415 static void rapl_remove_package(struct rapl_package
*rp
)
1417 struct rapl_domain
*rd
, *rd_package
= NULL
;
1419 for (rd
= rp
->domains
; rd
< rp
->domains
+ rp
->nr_domains
; rd
++) {
1420 if (rd
->id
== RAPL_DOMAIN_PACKAGE
) {
1424 pr_debug("remove package %d, %s domain\n", rp
->id
, rd
->name
);
1425 powercap_unregister_zone(control_type
, &rd
->power_zone
);
1427 /* do parent zone last */
1428 powercap_unregister_zone(control_type
, &rd_package
->power_zone
);
1429 list_del(&rp
->plist
);
1433 /* called from CPU hotplug notifier, hotplug lock held */
1434 static int rapl_add_package(int cpu
)
1438 struct rapl_package
*rp
;
1440 phy_package_id
= topology_physical_package_id(cpu
);
1441 rp
= kzalloc(sizeof(struct rapl_package
), GFP_KERNEL
);
1445 /* add the new package to the list */
1446 rp
->id
= phy_package_id
;
1450 /* check if the package contains valid domains */
1451 if (rapl_detect_domains(rp
, cpu
) ||
1452 rapl_defaults
->check_unit(rp
, cpu
)) {
1454 goto err_free_package
;
1456 if (!rapl_package_register_powercap(rp
)) {
1457 INIT_LIST_HEAD(&rp
->plist
);
1458 list_add(&rp
->plist
, &rapl_packages
);
1469 /* Handles CPU hotplug on multi-socket systems.
1470 * If a CPU goes online as the first CPU of the physical package
1471 * we add the RAPL package to the system. Similarly, when the last
1472 * CPU of the package is removed, we remove the RAPL package and its
1473 * associated domains. Cooling devices are handled accordingly at
1476 static int rapl_cpu_callback(struct notifier_block
*nfb
,
1477 unsigned long action
, void *hcpu
)
1479 unsigned long cpu
= (unsigned long)hcpu
;
1481 struct rapl_package
*rp
;
1484 phy_package_id
= topology_physical_package_id(cpu
);
1487 case CPU_ONLINE_FROZEN
:
1488 case CPU_DOWN_FAILED
:
1489 case CPU_DOWN_FAILED_FROZEN
:
1490 rp
= find_package_by_id(phy_package_id
);
1494 rapl_add_package(cpu
);
1496 case CPU_DOWN_PREPARE
:
1497 case CPU_DOWN_PREPARE_FROZEN
:
1498 rp
= find_package_by_id(phy_package_id
);
1501 if (--rp
->nr_cpus
== 0)
1502 rapl_remove_package(rp
);
1503 else if (cpu
== rp
->lead_cpu
) {
1504 /* choose another active cpu in the package */
1505 lead_cpu
= cpumask_any_but(topology_core_cpumask(cpu
), cpu
);
1506 if (lead_cpu
< nr_cpu_ids
)
1507 rp
->lead_cpu
= lead_cpu
;
1508 else /* should never go here */
1509 pr_err("no active cpu available for package %d\n",
1517 static struct notifier_block rapl_cpu_notifier
= {
1518 .notifier_call
= rapl_cpu_callback
,
1521 static int __init
rapl_init(void)
1524 const struct x86_cpu_id
*id
;
1526 id
= x86_match_cpu(rapl_ids
);
1528 pr_err("driver does not support CPU family %d model %d\n",
1529 boot_cpu_data
.x86
, boot_cpu_data
.x86_model
);
1534 rapl_defaults
= (struct rapl_defaults
*)id
->driver_data
;
1536 cpu_notifier_register_begin();
1538 /* prevent CPU hotplug during detection */
1540 ret
= rapl_detect_topology();
1544 if (rapl_register_powercap()) {
1545 rapl_cleanup_data();
1549 __register_hotcpu_notifier(&rapl_cpu_notifier
);
1552 cpu_notifier_register_done();
1557 static void __exit
rapl_exit(void)
1559 cpu_notifier_register_begin();
1561 __unregister_hotcpu_notifier(&rapl_cpu_notifier
);
1562 rapl_unregister_powercap();
1563 rapl_cleanup_data();
1565 cpu_notifier_register_done();
1568 module_init(rapl_init
);
1569 module_exit(rapl_exit
);
1571 MODULE_DESCRIPTION("Driver for Intel RAPL (Running Average Power Limit)");
1572 MODULE_AUTHOR("Jacob Pan <jacob.jun.pan@intel.com>");
1573 MODULE_LICENSE("GPL v2");