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>
33 #include <asm/processor.h>
34 #include <asm/cpu_device_id.h>
36 /* bitmasks for RAPL MSRs, used by primitive access functions */
37 #define ENERGY_STATUS_MASK 0xffffffff
39 #define POWER_LIMIT1_MASK 0x7FFF
40 #define POWER_LIMIT1_ENABLE BIT(15)
41 #define POWER_LIMIT1_CLAMP BIT(16)
43 #define POWER_LIMIT2_MASK (0x7FFFULL<<32)
44 #define POWER_LIMIT2_ENABLE BIT_ULL(47)
45 #define POWER_LIMIT2_CLAMP BIT_ULL(48)
46 #define POWER_PACKAGE_LOCK BIT_ULL(63)
47 #define POWER_PP_LOCK BIT(31)
49 #define TIME_WINDOW1_MASK (0x7FULL<<17)
50 #define TIME_WINDOW2_MASK (0x7FULL<<49)
52 #define POWER_UNIT_OFFSET 0
53 #define POWER_UNIT_MASK 0x0F
55 #define ENERGY_UNIT_OFFSET 0x08
56 #define ENERGY_UNIT_MASK 0x1F00
58 #define TIME_UNIT_OFFSET 0x10
59 #define TIME_UNIT_MASK 0xF0000
61 #define POWER_INFO_MAX_MASK (0x7fffULL<<32)
62 #define POWER_INFO_MIN_MASK (0x7fffULL<<16)
63 #define POWER_INFO_MAX_TIME_WIN_MASK (0x3fULL<<48)
64 #define POWER_INFO_THERMAL_SPEC_MASK 0x7fff
66 #define PERF_STATUS_THROTTLE_TIME_MASK 0xffffffff
67 #define PP_POLICY_MASK 0x1F
69 /* Non HW constants */
70 #define RAPL_PRIMITIVE_DERIVED BIT(1) /* not from raw data */
71 #define RAPL_PRIMITIVE_DUMMY BIT(2)
73 /* scale RAPL units to avoid floating point math inside kernel */
74 #define POWER_UNIT_SCALE (1000000)
75 #define ENERGY_UNIT_SCALE (1000000)
76 #define TIME_UNIT_SCALE (1000000)
78 #define TIME_WINDOW_MAX_MSEC 40000
79 #define TIME_WINDOW_MIN_MSEC 250
82 ARBITRARY_UNIT, /* no translation */
88 enum rapl_domain_type {
89 RAPL_DOMAIN_PACKAGE, /* entire package/socket */
90 RAPL_DOMAIN_PP0, /* core power plane */
91 RAPL_DOMAIN_PP1, /* graphics uncore */
92 RAPL_DOMAIN_DRAM,/* DRAM control_type */
96 enum rapl_domain_msr_id {
97 RAPL_DOMAIN_MSR_LIMIT,
98 RAPL_DOMAIN_MSR_STATUS,
100 RAPL_DOMAIN_MSR_POLICY,
101 RAPL_DOMAIN_MSR_INFO,
105 /* per domain data, some are optional */
106 enum rapl_primitives {
112 PL1_ENABLE, /* power limit 1, aka long term */
113 PL1_CLAMP, /* allow frequency to go below OS request */
114 PL2_ENABLE, /* power limit 2, aka short term, instantaneous */
117 TIME_WINDOW1, /* long term */
118 TIME_WINDOW2, /* short term */
127 /* below are not raw primitive data */
132 #define NR_RAW_PRIMITIVES (NR_RAPL_PRIMITIVES - 2)
134 /* Can be expanded to include events, etc.*/
135 struct rapl_domain_data {
136 u64 primitives[NR_RAPL_PRIMITIVES];
137 unsigned long timestamp;
141 #define DOMAIN_STATE_INACTIVE BIT(0)
142 #define DOMAIN_STATE_POWER_LIMIT_SET BIT(1)
143 #define DOMAIN_STATE_BIOS_LOCKED BIT(2)
145 #define NR_POWER_LIMITS (2)
146 struct rapl_power_limit {
147 struct powercap_zone_constraint *constraint;
148 int prim_id; /* primitive ID used to enable */
149 struct rapl_domain *domain;
153 static const char pl1_name[] = "long_term";
154 static const char pl2_name[] = "short_term";
158 enum rapl_domain_type id;
159 int msrs[RAPL_DOMAIN_MSR_MAX];
160 struct powercap_zone power_zone;
161 struct rapl_domain_data rdd;
162 struct rapl_power_limit rpl[NR_POWER_LIMITS];
163 u64 attr_map; /* track capabilities */
167 #define power_zone_to_rapl_domain(_zone) \
168 container_of(_zone, struct rapl_domain, power_zone)
171 /* Each physical package contains multiple domains, these are the common
172 * data across RAPL domains within a package.
174 struct rapl_package {
175 unsigned int id; /* physical package/socket id */
176 unsigned int nr_domains;
177 unsigned long domain_map; /* bit map of active domains */
178 unsigned int power_unit_divisor;
179 unsigned int energy_unit_divisor;
180 unsigned int time_unit_divisor;
181 struct rapl_domain *domains; /* array of domains, sized at runtime */
182 struct powercap_zone *power_zone; /* keep track of parent zone */
183 int nr_cpus; /* active cpus on the package, topology info is lost during
184 * cpu hotplug. so we have to track ourselves.
186 unsigned long power_limit_irq; /* keep track of package power limit
187 * notify interrupt enable status.
189 struct list_head plist;
191 #define PACKAGE_PLN_INT_SAVED BIT(0)
192 #define MAX_PRIM_NAME (32)
194 /* per domain data. used to describe individual knobs such that access function
195 * can be consolidated into one instead of many inline functions.
197 struct rapl_primitive_info {
201 enum rapl_domain_msr_id id;
206 #define PRIMITIVE_INFO_INIT(p, m, s, i, u, f) { \
215 static void rapl_init_domains(struct rapl_package *rp);
216 static int rapl_read_data_raw(struct rapl_domain *rd,
217 enum rapl_primitives prim,
218 bool xlate, u64 *data);
219 static int rapl_write_data_raw(struct rapl_domain *rd,
220 enum rapl_primitives prim,
221 unsigned long long value);
222 static u64 rapl_unit_xlate(int package, enum unit_type type, u64 value,
224 static void package_power_limit_irq_save(int package_id);
226 static LIST_HEAD(rapl_packages); /* guarded by CPU hotplug lock */
228 static const char * const rapl_domain_names[] = {
235 static struct powercap_control_type *control_type; /* PowerCap Controller */
237 /* caller to ensure CPU hotplug lock is held */
238 static struct rapl_package *find_package_by_id(int id)
240 struct rapl_package *rp;
242 list_for_each_entry(rp, &rapl_packages, plist) {
250 /* caller to ensure CPU hotplug lock is held */
251 static int find_active_cpu_on_package(int package_id)
255 for_each_online_cpu(i) {
256 if (topology_physical_package_id(i) == package_id)
259 /* all CPUs on this package are offline */
264 /* caller must hold cpu hotplug lock */
265 static void rapl_cleanup_data(void)
267 struct rapl_package *p, *tmp;
269 list_for_each_entry_safe(p, tmp, &rapl_packages, plist) {
276 static int get_energy_counter(struct powercap_zone *power_zone, u64 *energy_raw)
278 struct rapl_domain *rd;
281 /* prevent CPU hotplug, make sure the RAPL domain does not go
282 * away while reading the counter.
285 rd = power_zone_to_rapl_domain(power_zone);
287 if (!rapl_read_data_raw(rd, ENERGY_COUNTER, true, &energy_now)) {
288 *energy_raw = energy_now;
298 static int get_max_energy_counter(struct powercap_zone *pcd_dev, u64 *energy)
300 *energy = rapl_unit_xlate(0, ENERGY_UNIT, ENERGY_STATUS_MASK, 0);
304 static int release_zone(struct powercap_zone *power_zone)
306 struct rapl_domain *rd = power_zone_to_rapl_domain(power_zone);
307 struct rapl_package *rp;
309 /* package zone is the last zone of a package, we can free
310 * memory here since all children has been unregistered.
312 if (rd->id == RAPL_DOMAIN_PACKAGE) {
313 rp = find_package_by_id(rd->package_id);
315 dev_warn(&power_zone->dev, "no package id %s\n",
327 static int find_nr_power_limit(struct rapl_domain *rd)
331 for (i = 0; i < NR_POWER_LIMITS; i++) {
332 if (rd->rpl[i].name == NULL)
339 static int set_domain_enable(struct powercap_zone *power_zone, bool mode)
341 struct rapl_domain *rd = power_zone_to_rapl_domain(power_zone);
344 if (rd->state & DOMAIN_STATE_BIOS_LOCKED)
347 nr_powerlimit = find_nr_power_limit(rd);
348 /* here we activate/deactivate the hardware for power limiting */
349 rapl_write_data_raw(rd, PL1_ENABLE, mode);
350 /* always enable clamp such that p-state can go below OS requested
351 * range. power capping priority over guranteed frequency.
353 rapl_write_data_raw(rd, PL1_CLAMP, mode);
354 /* some domains have pl2 */
355 if (nr_powerlimit > 1) {
356 rapl_write_data_raw(rd, PL2_ENABLE, mode);
357 rapl_write_data_raw(rd, PL2_CLAMP, mode);
364 static int get_domain_enable(struct powercap_zone *power_zone, bool *mode)
366 struct rapl_domain *rd = power_zone_to_rapl_domain(power_zone);
369 if (rd->state & DOMAIN_STATE_BIOS_LOCKED) {
374 if (rapl_read_data_raw(rd, PL1_ENABLE, true, &val)) {
384 /* per RAPL domain ops, in the order of rapl_domain_type */
385 static struct powercap_zone_ops zone_ops[] = {
386 /* RAPL_DOMAIN_PACKAGE */
388 .get_energy_uj = get_energy_counter,
389 .get_max_energy_range_uj = get_max_energy_counter,
390 .release = release_zone,
391 .set_enable = set_domain_enable,
392 .get_enable = get_domain_enable,
394 /* RAPL_DOMAIN_PP0 */
396 .get_energy_uj = get_energy_counter,
397 .get_max_energy_range_uj = get_max_energy_counter,
398 .release = release_zone,
399 .set_enable = set_domain_enable,
400 .get_enable = get_domain_enable,
402 /* RAPL_DOMAIN_PP1 */
404 .get_energy_uj = get_energy_counter,
405 .get_max_energy_range_uj = get_max_energy_counter,
406 .release = release_zone,
407 .set_enable = set_domain_enable,
408 .get_enable = get_domain_enable,
410 /* RAPL_DOMAIN_DRAM */
412 .get_energy_uj = get_energy_counter,
413 .get_max_energy_range_uj = get_max_energy_counter,
414 .release = release_zone,
415 .set_enable = set_domain_enable,
416 .get_enable = get_domain_enable,
420 static int set_power_limit(struct powercap_zone *power_zone, int id,
423 struct rapl_domain *rd;
424 struct rapl_package *rp;
428 rd = power_zone_to_rapl_domain(power_zone);
429 rp = find_package_by_id(rd->package_id);
435 if (rd->state & DOMAIN_STATE_BIOS_LOCKED) {
436 dev_warn(&power_zone->dev, "%s locked by BIOS, monitoring only\n",
442 switch (rd->rpl[id].prim_id) {
444 rapl_write_data_raw(rd, POWER_LIMIT1, power_limit);
447 rapl_write_data_raw(rd, POWER_LIMIT2, power_limit);
453 package_power_limit_irq_save(rd->package_id);
459 static int get_current_power_limit(struct powercap_zone *power_zone, int id,
462 struct rapl_domain *rd;
468 rd = power_zone_to_rapl_domain(power_zone);
469 switch (rd->rpl[id].prim_id) {
480 if (rapl_read_data_raw(rd, prim, true, &val))
490 static int set_time_window(struct powercap_zone *power_zone, int id,
493 struct rapl_domain *rd;
497 rd = power_zone_to_rapl_domain(power_zone);
498 switch (rd->rpl[id].prim_id) {
500 rapl_write_data_raw(rd, TIME_WINDOW1, window);
503 rapl_write_data_raw(rd, TIME_WINDOW2, window);
512 static int get_time_window(struct powercap_zone *power_zone, int id, u64 *data)
514 struct rapl_domain *rd;
519 rd = power_zone_to_rapl_domain(power_zone);
520 switch (rd->rpl[id].prim_id) {
522 ret = rapl_read_data_raw(rd, TIME_WINDOW1, true, &val);
525 ret = rapl_read_data_raw(rd, TIME_WINDOW2, true, &val);
538 static const char *get_constraint_name(struct powercap_zone *power_zone, int id)
540 struct rapl_power_limit *rpl;
541 struct rapl_domain *rd;
543 rd = power_zone_to_rapl_domain(power_zone);
544 rpl = (struct rapl_power_limit *) &rd->rpl[id];
550 static int get_max_power(struct powercap_zone *power_zone, int id,
553 struct rapl_domain *rd;
559 rd = power_zone_to_rapl_domain(power_zone);
560 switch (rd->rpl[id].prim_id) {
562 prim = THERMAL_SPEC_POWER;
571 if (rapl_read_data_raw(rd, prim, true, &val))
581 static struct powercap_zone_constraint_ops constraint_ops = {
582 .set_power_limit_uw = set_power_limit,
583 .get_power_limit_uw = get_current_power_limit,
584 .set_time_window_us = set_time_window,
585 .get_time_window_us = get_time_window,
586 .get_max_power_uw = get_max_power,
587 .get_name = get_constraint_name,
590 /* called after domain detection and package level data are set */
591 static void rapl_init_domains(struct rapl_package *rp)
594 struct rapl_domain *rd = rp->domains;
596 for (i = 0; i < RAPL_DOMAIN_MAX; i++) {
597 unsigned int mask = rp->domain_map & (1 << i);
599 case BIT(RAPL_DOMAIN_PACKAGE):
600 rd->name = rapl_domain_names[RAPL_DOMAIN_PACKAGE];
601 rd->id = RAPL_DOMAIN_PACKAGE;
602 rd->msrs[0] = MSR_PKG_POWER_LIMIT;
603 rd->msrs[1] = MSR_PKG_ENERGY_STATUS;
604 rd->msrs[2] = MSR_PKG_PERF_STATUS;
606 rd->msrs[4] = MSR_PKG_POWER_INFO;
607 rd->rpl[0].prim_id = PL1_ENABLE;
608 rd->rpl[0].name = pl1_name;
609 rd->rpl[1].prim_id = PL2_ENABLE;
610 rd->rpl[1].name = pl2_name;
612 case BIT(RAPL_DOMAIN_PP0):
613 rd->name = rapl_domain_names[RAPL_DOMAIN_PP0];
614 rd->id = RAPL_DOMAIN_PP0;
615 rd->msrs[0] = MSR_PP0_POWER_LIMIT;
616 rd->msrs[1] = MSR_PP0_ENERGY_STATUS;
618 rd->msrs[3] = MSR_PP0_POLICY;
620 rd->rpl[0].prim_id = PL1_ENABLE;
621 rd->rpl[0].name = pl1_name;
623 case BIT(RAPL_DOMAIN_PP1):
624 rd->name = rapl_domain_names[RAPL_DOMAIN_PP1];
625 rd->id = RAPL_DOMAIN_PP1;
626 rd->msrs[0] = MSR_PP1_POWER_LIMIT;
627 rd->msrs[1] = MSR_PP1_ENERGY_STATUS;
629 rd->msrs[3] = MSR_PP1_POLICY;
631 rd->rpl[0].prim_id = PL1_ENABLE;
632 rd->rpl[0].name = pl1_name;
634 case BIT(RAPL_DOMAIN_DRAM):
635 rd->name = rapl_domain_names[RAPL_DOMAIN_DRAM];
636 rd->id = RAPL_DOMAIN_DRAM;
637 rd->msrs[0] = MSR_DRAM_POWER_LIMIT;
638 rd->msrs[1] = MSR_DRAM_ENERGY_STATUS;
639 rd->msrs[2] = MSR_DRAM_PERF_STATUS;
641 rd->msrs[4] = MSR_DRAM_POWER_INFO;
642 rd->rpl[0].prim_id = PL1_ENABLE;
643 rd->rpl[0].name = pl1_name;
647 rd->package_id = rp->id;
653 static u64 rapl_unit_xlate(int package, enum unit_type type, u64 value,
657 int scale = 1; /* scale to user friendly data without floating point */
658 u64 f, y; /* fraction and exp. used for time unit */
659 struct rapl_package *rp;
661 rp = find_package_by_id(package);
667 divisor = rp->power_unit_divisor;
668 scale = POWER_UNIT_SCALE;
671 scale = ENERGY_UNIT_SCALE;
672 divisor = rp->energy_unit_divisor;
675 divisor = rp->time_unit_divisor;
676 scale = TIME_UNIT_SCALE;
677 /* special processing based on 2^Y*(1+F)/4 = val/divisor, refer
678 * to Intel Software Developer's manual Vol. 3a, CH 14.7.4.
681 f = (value & 0x60) >> 5;
683 value = (1 << y) * (4 + f) * scale / 4;
684 return div64_u64(value, divisor);
686 do_div(value, scale);
689 f = div64_u64(4 * (value - (1 << y)), 1 << y);
690 value = (y & 0x1f) | ((f & 0x3) << 5);
700 return div64_u64(value * divisor, scale);
702 return div64_u64(value * scale, divisor);
705 /* in the order of enum rapl_primitives */
706 static struct rapl_primitive_info rpi[] = {
707 /* name, mask, shift, msr index, unit divisor */
708 PRIMITIVE_INFO_INIT(ENERGY_COUNTER, ENERGY_STATUS_MASK, 0,
709 RAPL_DOMAIN_MSR_STATUS, ENERGY_UNIT, 0),
710 PRIMITIVE_INFO_INIT(POWER_LIMIT1, POWER_LIMIT1_MASK, 0,
711 RAPL_DOMAIN_MSR_LIMIT, POWER_UNIT, 0),
712 PRIMITIVE_INFO_INIT(POWER_LIMIT2, POWER_LIMIT2_MASK, 32,
713 RAPL_DOMAIN_MSR_LIMIT, POWER_UNIT, 0),
714 PRIMITIVE_INFO_INIT(FW_LOCK, POWER_PP_LOCK, 31,
715 RAPL_DOMAIN_MSR_LIMIT, ARBITRARY_UNIT, 0),
716 PRIMITIVE_INFO_INIT(PL1_ENABLE, POWER_LIMIT1_ENABLE, 15,
717 RAPL_DOMAIN_MSR_LIMIT, ARBITRARY_UNIT, 0),
718 PRIMITIVE_INFO_INIT(PL1_CLAMP, POWER_LIMIT1_CLAMP, 16,
719 RAPL_DOMAIN_MSR_LIMIT, ARBITRARY_UNIT, 0),
720 PRIMITIVE_INFO_INIT(PL2_ENABLE, POWER_LIMIT2_ENABLE, 47,
721 RAPL_DOMAIN_MSR_LIMIT, ARBITRARY_UNIT, 0),
722 PRIMITIVE_INFO_INIT(PL2_CLAMP, POWER_LIMIT2_CLAMP, 48,
723 RAPL_DOMAIN_MSR_LIMIT, ARBITRARY_UNIT, 0),
724 PRIMITIVE_INFO_INIT(TIME_WINDOW1, TIME_WINDOW1_MASK, 17,
725 RAPL_DOMAIN_MSR_LIMIT, TIME_UNIT, 0),
726 PRIMITIVE_INFO_INIT(TIME_WINDOW2, TIME_WINDOW2_MASK, 49,
727 RAPL_DOMAIN_MSR_LIMIT, TIME_UNIT, 0),
728 PRIMITIVE_INFO_INIT(THERMAL_SPEC_POWER, POWER_INFO_THERMAL_SPEC_MASK,
729 0, RAPL_DOMAIN_MSR_INFO, POWER_UNIT, 0),
730 PRIMITIVE_INFO_INIT(MAX_POWER, POWER_INFO_MAX_MASK, 32,
731 RAPL_DOMAIN_MSR_INFO, POWER_UNIT, 0),
732 PRIMITIVE_INFO_INIT(MIN_POWER, POWER_INFO_MIN_MASK, 16,
733 RAPL_DOMAIN_MSR_INFO, POWER_UNIT, 0),
734 PRIMITIVE_INFO_INIT(MAX_TIME_WINDOW, POWER_INFO_MAX_TIME_WIN_MASK, 48,
735 RAPL_DOMAIN_MSR_INFO, TIME_UNIT, 0),
736 PRIMITIVE_INFO_INIT(THROTTLED_TIME, PERF_STATUS_THROTTLE_TIME_MASK, 0,
737 RAPL_DOMAIN_MSR_PERF, TIME_UNIT, 0),
738 PRIMITIVE_INFO_INIT(PRIORITY_LEVEL, PP_POLICY_MASK, 0,
739 RAPL_DOMAIN_MSR_POLICY, ARBITRARY_UNIT, 0),
741 PRIMITIVE_INFO_INIT(AVERAGE_POWER, 0, 0, 0, POWER_UNIT,
742 RAPL_PRIMITIVE_DERIVED),
746 /* Read primitive data based on its related struct rapl_primitive_info.
747 * if xlate flag is set, return translated data based on data units, i.e.
748 * time, energy, and power.
749 * RAPL MSRs are non-architectual and are laid out not consistently across
750 * domains. Here we use primitive info to allow writing consolidated access
752 * For a given primitive, it is processed by MSR mask and shift. Unit conversion
753 * is pre-assigned based on RAPL unit MSRs read at init time.
754 * 63-------------------------- 31--------------------------- 0
756 * | |<- shift ----------------|
757 * 63-------------------------- 31--------------------------- 0
759 static int rapl_read_data_raw(struct rapl_domain *rd,
760 enum rapl_primitives prim,
761 bool xlate, u64 *data)
765 struct rapl_primitive_info *rp = &rpi[prim];
768 if (!rp->name || rp->flag & RAPL_PRIMITIVE_DUMMY)
771 msr = rd->msrs[rp->id];
774 /* use physical package id to look up active cpus */
775 cpu = find_active_cpu_on_package(rd->package_id);
779 /* special-case package domain, which uses a different bit*/
780 if (prim == FW_LOCK && rd->id == RAPL_DOMAIN_PACKAGE) {
781 rp->mask = POWER_PACKAGE_LOCK;
784 /* non-hardware data are collected by the polling thread */
785 if (rp->flag & RAPL_PRIMITIVE_DERIVED) {
786 *data = rd->rdd.primitives[prim];
790 if (rdmsrl_safe_on_cpu(cpu, msr, &value)) {
791 pr_debug("failed to read msr 0x%x on cpu %d\n", msr, cpu);
795 final = value & rp->mask;
796 final = final >> rp->shift;
798 *data = rapl_unit_xlate(rd->package_id, rp->unit, final, 0);
805 /* Similar use of primitive info in the read counterpart */
806 static int rapl_write_data_raw(struct rapl_domain *rd,
807 enum rapl_primitives prim,
808 unsigned long long value)
812 struct rapl_primitive_info *rp = &rpi[prim];
815 cpu = find_active_cpu_on_package(rd->package_id);
818 msr = rd->msrs[rp->id];
819 if (rdmsrl_safe_on_cpu(cpu, msr, &msr_val)) {
820 dev_dbg(&rd->power_zone.dev,
821 "failed to read msr 0x%x on cpu %d\n", msr, cpu);
824 value = rapl_unit_xlate(rd->package_id, rp->unit, value, 1);
825 msr_val &= ~rp->mask;
826 msr_val |= value << rp->shift;
827 if (wrmsrl_safe_on_cpu(cpu, msr, msr_val)) {
828 dev_dbg(&rd->power_zone.dev,
829 "failed to write msr 0x%x on cpu %d\n", msr, cpu);
836 static int rapl_check_unit(struct rapl_package *rp, int cpu)
841 if (rdmsrl_safe_on_cpu(cpu, MSR_RAPL_POWER_UNIT, &msr_val)) {
842 pr_err("Failed to read power unit MSR 0x%x on CPU %d, exit.\n",
843 MSR_RAPL_POWER_UNIT, cpu);
847 /* Raw RAPL data stored in MSRs are in certain scales. We need to
848 * convert them into standard units based on the divisors reported in
849 * the RAPL unit MSRs.
851 * energy unit: 1/enery_unit_divisor Joules
852 * power unit: 1/power_unit_divisor Watts
853 * time unit: 1/time_unit_divisor Seconds
855 value = (msr_val & ENERGY_UNIT_MASK) >> ENERGY_UNIT_OFFSET;
856 rp->energy_unit_divisor = 1 << value;
859 value = (msr_val & POWER_UNIT_MASK) >> POWER_UNIT_OFFSET;
860 rp->power_unit_divisor = 1 << value;
862 value = (msr_val & TIME_UNIT_MASK) >> TIME_UNIT_OFFSET;
863 rp->time_unit_divisor = 1 << value;
865 pr_debug("Physical package %d units: energy=%d, time=%d, power=%d\n",
867 rp->energy_unit_divisor,
868 rp->time_unit_divisor,
869 rp->power_unit_divisor);
875 * When package power limit is set artificially low by RAPL, LVT
876 * thermal interrupt for package power limit should be ignored
877 * since we are not really exceeding the real limit. The intention
878 * is to avoid excessive interrupts while we are trying to save power.
879 * A useful feature might be routing the package_power_limit interrupt
880 * to userspace via eventfd. once we have a usecase, this is simple
881 * to do by adding an atomic notifier.
884 static void package_power_limit_irq_save(int package_id)
888 struct rapl_package *rp;
890 rp = find_package_by_id(package_id);
894 if (!boot_cpu_has(X86_FEATURE_PTS) || !boot_cpu_has(X86_FEATURE_PLN))
897 cpu = find_active_cpu_on_package(package_id);
900 /* save the state of PLN irq mask bit before disabling it */
901 rdmsr_safe_on_cpu(cpu, MSR_IA32_PACKAGE_THERM_INTERRUPT, &l, &h);
902 if (!(rp->power_limit_irq & PACKAGE_PLN_INT_SAVED)) {
903 rp->power_limit_irq = l & PACKAGE_THERM_INT_PLN_ENABLE;
904 rp->power_limit_irq |= PACKAGE_PLN_INT_SAVED;
906 l &= ~PACKAGE_THERM_INT_PLN_ENABLE;
907 wrmsr_on_cpu(cpu, MSR_IA32_PACKAGE_THERM_INTERRUPT, l, h);
910 /* restore per package power limit interrupt enable state */
911 static void package_power_limit_irq_restore(int package_id)
915 struct rapl_package *rp;
917 rp = find_package_by_id(package_id);
921 if (!boot_cpu_has(X86_FEATURE_PTS) || !boot_cpu_has(X86_FEATURE_PLN))
924 cpu = find_active_cpu_on_package(package_id);
928 /* irq enable state not saved, nothing to restore */
929 if (!(rp->power_limit_irq & PACKAGE_PLN_INT_SAVED))
931 rdmsr_safe_on_cpu(cpu, MSR_IA32_PACKAGE_THERM_INTERRUPT, &l, &h);
933 if (rp->power_limit_irq & PACKAGE_THERM_INT_PLN_ENABLE)
934 l |= PACKAGE_THERM_INT_PLN_ENABLE;
936 l &= ~PACKAGE_THERM_INT_PLN_ENABLE;
938 wrmsr_on_cpu(cpu, MSR_IA32_PACKAGE_THERM_INTERRUPT, l, h);
941 static const struct x86_cpu_id rapl_ids[] = {
942 { X86_VENDOR_INTEL, 6, 0x2a},/* SNB */
943 { X86_VENDOR_INTEL, 6, 0x2d},/* SNB EP */
944 { X86_VENDOR_INTEL, 6, 0x3a},/* IVB */
945 { X86_VENDOR_INTEL, 6, 0x45},/* HSW */
946 /* TODO: Add more CPU IDs after testing */
949 MODULE_DEVICE_TABLE(x86cpu, rapl_ids);
951 /* read once for all raw primitive data for all packages, domains */
952 static void rapl_update_domain_data(void)
956 struct rapl_package *rp;
958 list_for_each_entry(rp, &rapl_packages, plist) {
959 for (dmn = 0; dmn < rp->nr_domains; dmn++) {
960 pr_debug("update package %d domain %s data\n", rp->id,
961 rp->domains[dmn].name);
962 /* exclude non-raw primitives */
963 for (prim = 0; prim < NR_RAW_PRIMITIVES; prim++)
964 if (!rapl_read_data_raw(&rp->domains[dmn], prim,
967 rp->domains[dmn].rdd.primitives[prim] =
974 static int rapl_unregister_powercap(void)
976 struct rapl_package *rp;
977 struct rapl_domain *rd, *rd_package = NULL;
979 /* unregister all active rapl packages from the powercap layer,
982 list_for_each_entry(rp, &rapl_packages, plist) {
983 package_power_limit_irq_restore(rp->id);
985 for (rd = rp->domains; rd < rp->domains + rp->nr_domains;
987 pr_debug("remove package, undo power limit on %d: %s\n",
989 rapl_write_data_raw(rd, PL1_ENABLE, 0);
990 rapl_write_data_raw(rd, PL2_ENABLE, 0);
991 rapl_write_data_raw(rd, PL1_CLAMP, 0);
992 rapl_write_data_raw(rd, PL2_CLAMP, 0);
993 if (rd->id == RAPL_DOMAIN_PACKAGE) {
997 powercap_unregister_zone(control_type, &rd->power_zone);
999 /* do the package zone last */
1001 powercap_unregister_zone(control_type,
1002 &rd_package->power_zone);
1004 powercap_unregister_control_type(control_type);
1009 static int rapl_package_register_powercap(struct rapl_package *rp)
1011 struct rapl_domain *rd;
1013 char dev_name[17]; /* max domain name = 7 + 1 + 8 for int + 1 for null*/
1014 struct powercap_zone *power_zone = NULL;
1017 /* first we register package domain as the parent zone*/
1018 for (rd = rp->domains; rd < rp->domains + rp->nr_domains; rd++) {
1019 if (rd->id == RAPL_DOMAIN_PACKAGE) {
1020 nr_pl = find_nr_power_limit(rd);
1021 pr_debug("register socket %d package domain %s\n",
1023 memset(dev_name, 0, sizeof(dev_name));
1024 snprintf(dev_name, sizeof(dev_name), "%s-%d",
1026 power_zone = powercap_register_zone(&rd->power_zone,
1032 if (IS_ERR(power_zone)) {
1033 pr_debug("failed to register package, %d\n",
1035 ret = PTR_ERR(power_zone);
1038 /* track parent zone in per package/socket data */
1039 rp->power_zone = power_zone;
1040 /* done, only one package domain per socket */
1045 pr_err("no package domain found, unknown topology!\n");
1049 /* now register domains as children of the socket/package*/
1050 for (rd = rp->domains; rd < rp->domains + rp->nr_domains; rd++) {
1051 if (rd->id == RAPL_DOMAIN_PACKAGE)
1053 /* number of power limits per domain varies */
1054 nr_pl = find_nr_power_limit(rd);
1055 power_zone = powercap_register_zone(&rd->power_zone,
1056 control_type, rd->name,
1058 &zone_ops[rd->id], nr_pl,
1061 if (IS_ERR(power_zone)) {
1062 pr_debug("failed to register power_zone, %d:%s:%s\n",
1063 rp->id, rd->name, dev_name);
1064 ret = PTR_ERR(power_zone);
1072 /* clean up previously initialized domains within the package if we
1073 * failed after the first domain setup.
1075 while (--rd >= rp->domains) {
1076 pr_debug("unregister package %d domain %s\n", rp->id, rd->name);
1077 powercap_unregister_zone(control_type, &rd->power_zone);
1083 static int rapl_register_powercap(void)
1085 struct rapl_domain *rd;
1086 struct rapl_package *rp;
1089 control_type = powercap_register_control_type(NULL, "intel-rapl", NULL);
1090 if (IS_ERR(control_type)) {
1091 pr_debug("failed to register powercap control_type.\n");
1092 return PTR_ERR(control_type);
1094 /* read the initial data */
1095 rapl_update_domain_data();
1096 list_for_each_entry(rp, &rapl_packages, plist)
1097 if (rapl_package_register_powercap(rp))
1098 goto err_cleanup_package;
1101 err_cleanup_package:
1102 /* clean up previously initialized packages */
1103 list_for_each_entry_continue_reverse(rp, &rapl_packages, plist) {
1104 for (rd = rp->domains; rd < rp->domains + rp->nr_domains;
1106 pr_debug("unregister zone/package %d, %s domain\n",
1108 powercap_unregister_zone(control_type, &rd->power_zone);
1115 static int rapl_check_domain(int cpu, int domain)
1122 case RAPL_DOMAIN_PACKAGE:
1123 msr = MSR_PKG_ENERGY_STATUS;
1125 case RAPL_DOMAIN_PP0:
1126 msr = MSR_PP0_ENERGY_STATUS;
1128 case RAPL_DOMAIN_PP1:
1129 msr = MSR_PP1_ENERGY_STATUS;
1131 case RAPL_DOMAIN_DRAM:
1132 msr = MSR_DRAM_ENERGY_STATUS;
1135 pr_err("invalid domain id %d\n", domain);
1138 if (rdmsrl_safe_on_cpu(cpu, msr, &val1))
1141 /* energy counters roll slowly on some domains */
1142 while (++retry < 10) {
1143 usleep_range(10000, 15000);
1144 rdmsrl_safe_on_cpu(cpu, msr, &val2);
1145 if ((val1 & ENERGY_STATUS_MASK) != (val2 & ENERGY_STATUS_MASK))
1148 /* if energy counter does not change, report as bad domain */
1149 pr_info("domain %s energy ctr %llu:%llu not working, skip\n",
1150 rapl_domain_names[domain], val1, val2);
1155 /* Detect active and valid domains for the given CPU, caller must
1156 * ensure the CPU belongs to the targeted package and CPU hotlug is disabled.
1158 static int rapl_detect_domains(struct rapl_package *rp, int cpu)
1162 struct rapl_domain *rd;
1165 for (i = 0; i < RAPL_DOMAIN_MAX; i++) {
1166 /* use physical package id to read counters */
1167 if (!rapl_check_domain(cpu, i))
1168 rp->domain_map |= 1 << i;
1170 rp->nr_domains = bitmap_weight(&rp->domain_map, RAPL_DOMAIN_MAX);
1171 if (!rp->nr_domains) {
1172 pr_err("no valid rapl domains found in package %d\n", rp->id);
1176 pr_debug("found %d domains on package %d\n", rp->nr_domains, rp->id);
1178 rp->domains = kcalloc(rp->nr_domains + 1, sizeof(struct rapl_domain),
1184 rapl_init_domains(rp);
1186 for (rd = rp->domains; rd < rp->domains + rp->nr_domains; rd++) {
1187 /* check if the domain is locked by BIOS */
1188 if (rapl_read_data_raw(rd, FW_LOCK, false, &locked)) {
1189 pr_info("RAPL package %d domain %s locked by BIOS\n",
1191 rd->state |= DOMAIN_STATE_BIOS_LOCKED;
1200 static bool is_package_new(int package)
1202 struct rapl_package *rp;
1204 /* caller prevents cpu hotplug, there will be no new packages added
1205 * or deleted while traversing the package list, no need for locking.
1207 list_for_each_entry(rp, &rapl_packages, plist)
1208 if (package == rp->id)
1214 /* RAPL interface can be made of a two-level hierarchy: package level and domain
1215 * level. We first detect the number of packages then domains of each package.
1216 * We have to consider the possiblity of CPU online/offline due to hotplug and
1219 static int rapl_detect_topology(void)
1223 struct rapl_package *new_package, *rp;
1225 for_each_online_cpu(i) {
1226 phy_package_id = topology_physical_package_id(i);
1227 if (is_package_new(phy_package_id)) {
1228 new_package = kzalloc(sizeof(*rp), GFP_KERNEL);
1230 rapl_cleanup_data();
1233 /* add the new package to the list */
1234 new_package->id = phy_package_id;
1235 new_package->nr_cpus = 1;
1237 /* check if the package contains valid domains */
1238 if (rapl_detect_domains(new_package, i) ||
1239 rapl_check_unit(new_package, i)) {
1240 kfree(new_package->domains);
1242 /* free up the packages already initialized */
1243 rapl_cleanup_data();
1246 INIT_LIST_HEAD(&new_package->plist);
1247 list_add(&new_package->plist, &rapl_packages);
1249 rp = find_package_by_id(phy_package_id);
1258 /* called from CPU hotplug notifier, hotplug lock held */
1259 static void rapl_remove_package(struct rapl_package *rp)
1261 struct rapl_domain *rd, *rd_package = NULL;
1263 for (rd = rp->domains; rd < rp->domains + rp->nr_domains; rd++) {
1264 if (rd->id == RAPL_DOMAIN_PACKAGE) {
1268 pr_debug("remove package %d, %s domain\n", rp->id, rd->name);
1269 powercap_unregister_zone(control_type, &rd->power_zone);
1271 /* do parent zone last */
1272 powercap_unregister_zone(control_type, &rd_package->power_zone);
1273 list_del(&rp->plist);
1277 /* called from CPU hotplug notifier, hotplug lock held */
1278 static int rapl_add_package(int cpu)
1282 struct rapl_package *rp;
1284 phy_package_id = topology_physical_package_id(cpu);
1285 rp = kzalloc(sizeof(struct rapl_package), GFP_KERNEL);
1289 /* add the new package to the list */
1290 rp->id = phy_package_id;
1292 /* check if the package contains valid domains */
1293 if (rapl_detect_domains(rp, cpu) ||
1294 rapl_check_unit(rp, cpu)) {
1296 goto err_free_package;
1298 if (!rapl_package_register_powercap(rp)) {
1299 INIT_LIST_HEAD(&rp->plist);
1300 list_add(&rp->plist, &rapl_packages);
1311 /* Handles CPU hotplug on multi-socket systems.
1312 * If a CPU goes online as the first CPU of the physical package
1313 * we add the RAPL package to the system. Similarly, when the last
1314 * CPU of the package is removed, we remove the RAPL package and its
1315 * associated domains. Cooling devices are handled accordingly at
1318 static int rapl_cpu_callback(struct notifier_block *nfb,
1319 unsigned long action, void *hcpu)
1321 unsigned long cpu = (unsigned long)hcpu;
1323 struct rapl_package *rp;
1325 phy_package_id = topology_physical_package_id(cpu);
1328 case CPU_ONLINE_FROZEN:
1329 case CPU_DOWN_FAILED:
1330 case CPU_DOWN_FAILED_FROZEN:
1331 rp = find_package_by_id(phy_package_id);
1335 rapl_add_package(cpu);
1337 case CPU_DOWN_PREPARE:
1338 case CPU_DOWN_PREPARE_FROZEN:
1339 rp = find_package_by_id(phy_package_id);
1342 if (--rp->nr_cpus == 0)
1343 rapl_remove_package(rp);
1349 static struct notifier_block rapl_cpu_notifier = {
1350 .notifier_call = rapl_cpu_callback,
1353 static int __init rapl_init(void)
1357 if (!x86_match_cpu(rapl_ids)) {
1358 pr_err("driver does not support CPU family %d model %d\n",
1359 boot_cpu_data.x86, boot_cpu_data.x86_model);
1363 /* prevent CPU hotplug during detection */
1365 ret = rapl_detect_topology();
1369 if (rapl_register_powercap()) {
1370 rapl_cleanup_data();
1374 register_hotcpu_notifier(&rapl_cpu_notifier);
1381 static void __exit rapl_exit(void)
1384 unregister_hotcpu_notifier(&rapl_cpu_notifier);
1385 rapl_unregister_powercap();
1386 rapl_cleanup_data();
1390 module_init(rapl_init);
1391 module_exit(rapl_exit);
1393 MODULE_DESCRIPTION("Driver for Intel RAPL (Running Average Power Limit)");
1394 MODULE_AUTHOR("Jacob Pan <jacob.jun.pan@intel.com>");
1395 MODULE_LICENSE("GPL v2");