Thomas Gleixner | f6cc69f | 2019-05-29 16:57:24 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Jacob Pan | 2d281d8 | 2013-10-17 10:28:35 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Intel Running Average Power Limit (RAPL) Driver |
| 4 | * Copyright (c) 2013, Intel Corporation. |
Jacob Pan | 2d281d8 | 2013-10-17 10:28:35 -0700 | [diff] [blame] | 5 | */ |
| 6 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 7 | |
| 8 | #include <linux/kernel.h> |
| 9 | #include <linux/module.h> |
| 10 | #include <linux/list.h> |
| 11 | #include <linux/types.h> |
| 12 | #include <linux/device.h> |
| 13 | #include <linux/slab.h> |
| 14 | #include <linux/log2.h> |
| 15 | #include <linux/bitmap.h> |
| 16 | #include <linux/delay.h> |
| 17 | #include <linux/sysfs.h> |
| 18 | #include <linux/cpu.h> |
| 19 | #include <linux/powercap.h> |
Zhen Han | 52b3672 | 2018-01-10 08:38:23 +0800 | [diff] [blame] | 20 | #include <linux/suspend.h> |
Jacob Pan | 3c2c084 | 2014-11-07 09:29:26 -0800 | [diff] [blame] | 21 | #include <asm/iosf_mbi.h> |
Jacob Pan | 2d281d8 | 2013-10-17 10:28:35 -0700 | [diff] [blame] | 22 | |
| 23 | #include <asm/processor.h> |
| 24 | #include <asm/cpu_device_id.h> |
Dave Hansen | 62d1673 | 2016-06-02 17:19:36 -0700 | [diff] [blame] | 25 | #include <asm/intel-family.h> |
Jacob Pan | 2d281d8 | 2013-10-17 10:28:35 -0700 | [diff] [blame] | 26 | |
Srinivas Pandruvada | 3521ba1 | 2016-04-17 15:03:01 -0700 | [diff] [blame] | 27 | /* Local defines */ |
| 28 | #define MSR_PLATFORM_POWER_LIMIT 0x0000065C |
| 29 | |
Jacob Pan | 2d281d8 | 2013-10-17 10:28:35 -0700 | [diff] [blame] | 30 | /* bitmasks for RAPL MSRs, used by primitive access functions */ |
| 31 | #define ENERGY_STATUS_MASK 0xffffffff |
| 32 | |
| 33 | #define POWER_LIMIT1_MASK 0x7FFF |
| 34 | #define POWER_LIMIT1_ENABLE BIT(15) |
| 35 | #define POWER_LIMIT1_CLAMP BIT(16) |
| 36 | |
| 37 | #define POWER_LIMIT2_MASK (0x7FFFULL<<32) |
| 38 | #define POWER_LIMIT2_ENABLE BIT_ULL(47) |
| 39 | #define POWER_LIMIT2_CLAMP BIT_ULL(48) |
| 40 | #define POWER_PACKAGE_LOCK BIT_ULL(63) |
| 41 | #define POWER_PP_LOCK BIT(31) |
| 42 | |
| 43 | #define TIME_WINDOW1_MASK (0x7FULL<<17) |
| 44 | #define TIME_WINDOW2_MASK (0x7FULL<<49) |
| 45 | |
| 46 | #define POWER_UNIT_OFFSET 0 |
| 47 | #define POWER_UNIT_MASK 0x0F |
| 48 | |
| 49 | #define ENERGY_UNIT_OFFSET 0x08 |
| 50 | #define ENERGY_UNIT_MASK 0x1F00 |
| 51 | |
| 52 | #define TIME_UNIT_OFFSET 0x10 |
| 53 | #define TIME_UNIT_MASK 0xF0000 |
| 54 | |
| 55 | #define POWER_INFO_MAX_MASK (0x7fffULL<<32) |
| 56 | #define POWER_INFO_MIN_MASK (0x7fffULL<<16) |
| 57 | #define POWER_INFO_MAX_TIME_WIN_MASK (0x3fULL<<48) |
| 58 | #define POWER_INFO_THERMAL_SPEC_MASK 0x7fff |
| 59 | |
| 60 | #define PERF_STATUS_THROTTLE_TIME_MASK 0xffffffff |
| 61 | #define PP_POLICY_MASK 0x1F |
| 62 | |
| 63 | /* Non HW constants */ |
| 64 | #define RAPL_PRIMITIVE_DERIVED BIT(1) /* not from raw data */ |
| 65 | #define RAPL_PRIMITIVE_DUMMY BIT(2) |
| 66 | |
Jacob Pan | 2d281d8 | 2013-10-17 10:28:35 -0700 | [diff] [blame] | 67 | #define TIME_WINDOW_MAX_MSEC 40000 |
| 68 | #define TIME_WINDOW_MIN_MSEC 250 |
Jacob Pan | d474a4d | 2015-03-13 03:48:56 -0700 | [diff] [blame] | 69 | #define ENERGY_UNIT_SCALE 1000 /* scale from driver unit to powercap unit */ |
Jacob Pan | 2d281d8 | 2013-10-17 10:28:35 -0700 | [diff] [blame] | 70 | enum unit_type { |
| 71 | ARBITRARY_UNIT, /* no translation */ |
| 72 | POWER_UNIT, |
| 73 | ENERGY_UNIT, |
| 74 | TIME_UNIT, |
| 75 | }; |
| 76 | |
| 77 | enum rapl_domain_type { |
| 78 | RAPL_DOMAIN_PACKAGE, /* entire package/socket */ |
| 79 | RAPL_DOMAIN_PP0, /* core power plane */ |
| 80 | RAPL_DOMAIN_PP1, /* graphics uncore */ |
| 81 | RAPL_DOMAIN_DRAM,/* DRAM control_type */ |
Srinivas Pandruvada | 3521ba1 | 2016-04-17 15:03:01 -0700 | [diff] [blame] | 82 | RAPL_DOMAIN_PLATFORM, /* PSys control_type */ |
Jacob Pan | 2d281d8 | 2013-10-17 10:28:35 -0700 | [diff] [blame] | 83 | RAPL_DOMAIN_MAX, |
| 84 | }; |
| 85 | |
Zhang Rui | f7c4e0c | 2019-07-10 21:44:22 +0800 | [diff] [blame^] | 86 | enum rapl_domain_reg_id { |
| 87 | RAPL_DOMAIN_REG_LIMIT, |
| 88 | RAPL_DOMAIN_REG_STATUS, |
| 89 | RAPL_DOMAIN_REG_PERF, |
| 90 | RAPL_DOMAIN_REG_POLICY, |
| 91 | RAPL_DOMAIN_REG_INFO, |
| 92 | RAPL_DOMAIN_REG_MAX, |
Jacob Pan | 2d281d8 | 2013-10-17 10:28:35 -0700 | [diff] [blame] | 93 | }; |
| 94 | |
| 95 | /* per domain data, some are optional */ |
| 96 | enum rapl_primitives { |
| 97 | ENERGY_COUNTER, |
| 98 | POWER_LIMIT1, |
| 99 | POWER_LIMIT2, |
| 100 | FW_LOCK, |
| 101 | |
| 102 | PL1_ENABLE, /* power limit 1, aka long term */ |
| 103 | PL1_CLAMP, /* allow frequency to go below OS request */ |
| 104 | PL2_ENABLE, /* power limit 2, aka short term, instantaneous */ |
| 105 | PL2_CLAMP, |
| 106 | |
| 107 | TIME_WINDOW1, /* long term */ |
| 108 | TIME_WINDOW2, /* short term */ |
| 109 | THERMAL_SPEC_POWER, |
| 110 | MAX_POWER, |
| 111 | |
| 112 | MIN_POWER, |
| 113 | MAX_TIME_WINDOW, |
| 114 | THROTTLED_TIME, |
| 115 | PRIORITY_LEVEL, |
| 116 | |
| 117 | /* below are not raw primitive data */ |
| 118 | AVERAGE_POWER, |
| 119 | NR_RAPL_PRIMITIVES, |
| 120 | }; |
| 121 | |
| 122 | #define NR_RAW_PRIMITIVES (NR_RAPL_PRIMITIVES - 2) |
| 123 | |
| 124 | /* Can be expanded to include events, etc.*/ |
| 125 | struct rapl_domain_data { |
| 126 | u64 primitives[NR_RAPL_PRIMITIVES]; |
| 127 | unsigned long timestamp; |
| 128 | }; |
| 129 | |
Jacob Pan | f14a139 | 2016-02-24 13:31:36 -0800 | [diff] [blame] | 130 | struct msrl_action { |
| 131 | u32 msr_no; |
| 132 | u64 clear_mask; |
| 133 | u64 set_mask; |
| 134 | int err; |
| 135 | }; |
Jacob Pan | 2d281d8 | 2013-10-17 10:28:35 -0700 | [diff] [blame] | 136 | |
| 137 | #define DOMAIN_STATE_INACTIVE BIT(0) |
| 138 | #define DOMAIN_STATE_POWER_LIMIT_SET BIT(1) |
| 139 | #define DOMAIN_STATE_BIOS_LOCKED BIT(2) |
| 140 | |
| 141 | #define NR_POWER_LIMITS (2) |
| 142 | struct rapl_power_limit { |
| 143 | struct powercap_zone_constraint *constraint; |
| 144 | int prim_id; /* primitive ID used to enable */ |
| 145 | struct rapl_domain *domain; |
| 146 | const char *name; |
Zhen Han | 52b3672 | 2018-01-10 08:38:23 +0800 | [diff] [blame] | 147 | u64 last_power_limit; |
Jacob Pan | 2d281d8 | 2013-10-17 10:28:35 -0700 | [diff] [blame] | 148 | }; |
| 149 | |
| 150 | static const char pl1_name[] = "long_term"; |
| 151 | static const char pl2_name[] = "short_term"; |
| 152 | |
Jacob Pan | 309557f | 2016-02-24 13:31:37 -0800 | [diff] [blame] | 153 | struct rapl_package; |
Jacob Pan | 2d281d8 | 2013-10-17 10:28:35 -0700 | [diff] [blame] | 154 | struct rapl_domain { |
| 155 | const char *name; |
| 156 | enum rapl_domain_type id; |
Zhang Rui | f7c4e0c | 2019-07-10 21:44:22 +0800 | [diff] [blame^] | 157 | int regs[RAPL_DOMAIN_REG_MAX]; |
Jacob Pan | 2d281d8 | 2013-10-17 10:28:35 -0700 | [diff] [blame] | 158 | struct powercap_zone power_zone; |
| 159 | struct rapl_domain_data rdd; |
| 160 | struct rapl_power_limit rpl[NR_POWER_LIMITS]; |
| 161 | u64 attr_map; /* track capabilities */ |
| 162 | unsigned int state; |
Jacob Pan | d474a4d | 2015-03-13 03:48:56 -0700 | [diff] [blame] | 163 | unsigned int domain_energy_unit; |
Jacob Pan | 309557f | 2016-02-24 13:31:37 -0800 | [diff] [blame] | 164 | struct rapl_package *rp; |
Jacob Pan | 2d281d8 | 2013-10-17 10:28:35 -0700 | [diff] [blame] | 165 | }; |
| 166 | #define power_zone_to_rapl_domain(_zone) \ |
| 167 | container_of(_zone, struct rapl_domain, power_zone) |
| 168 | |
Zhang Rui | 9ea7612 | 2019-05-13 13:58:53 -0400 | [diff] [blame] | 169 | /* maximum rapl package domain name: package-%d-die-%d */ |
| 170 | #define PACKAGE_DOMAIN_NAME_LENGTH 30 |
Jacob Pan | 2d281d8 | 2013-10-17 10:28:35 -0700 | [diff] [blame] | 171 | |
Zhang Rui | 9ea7612 | 2019-05-13 13:58:53 -0400 | [diff] [blame] | 172 | |
| 173 | /* Each rapl package contains multiple domains, these are the common |
Jacob Pan | 2d281d8 | 2013-10-17 10:28:35 -0700 | [diff] [blame] | 174 | * data across RAPL domains within a package. |
| 175 | */ |
| 176 | struct rapl_package { |
Zhang Rui | 9ea7612 | 2019-05-13 13:58:53 -0400 | [diff] [blame] | 177 | unsigned int id; /* logical die id, equals physical 1-die systems */ |
Jacob Pan | 2d281d8 | 2013-10-17 10:28:35 -0700 | [diff] [blame] | 178 | unsigned int nr_domains; |
| 179 | unsigned long domain_map; /* bit map of active domains */ |
Jacob Pan | 3c2c084 | 2014-11-07 09:29:26 -0800 | [diff] [blame] | 180 | unsigned int power_unit; |
| 181 | unsigned int energy_unit; |
| 182 | unsigned int time_unit; |
Jacob Pan | 2d281d8 | 2013-10-17 10:28:35 -0700 | [diff] [blame] | 183 | struct rapl_domain *domains; /* array of domains, sized at runtime */ |
| 184 | struct powercap_zone *power_zone; /* keep track of parent zone */ |
Jacob Pan | 2d281d8 | 2013-10-17 10:28:35 -0700 | [diff] [blame] | 185 | unsigned long power_limit_irq; /* keep track of package power limit |
| 186 | * notify interrupt enable status. |
| 187 | */ |
| 188 | struct list_head plist; |
Jacob Pan | 323ee64 | 2016-02-24 13:31:38 -0800 | [diff] [blame] | 189 | int lead_cpu; /* one active cpu per package for access */ |
Thomas Gleixner | b4005e9 | 2016-11-22 21:16:05 +0000 | [diff] [blame] | 190 | /* Track active cpus */ |
| 191 | struct cpumask cpumask; |
Zhang Rui | 9ea7612 | 2019-05-13 13:58:53 -0400 | [diff] [blame] | 192 | char name[PACKAGE_DOMAIN_NAME_LENGTH]; |
Jacob Pan | 2d281d8 | 2013-10-17 10:28:35 -0700 | [diff] [blame] | 193 | }; |
Jacob Pan | 087e9cb | 2014-11-07 09:29:25 -0800 | [diff] [blame] | 194 | |
| 195 | struct rapl_defaults { |
Ajay Thomas | 51b6340 | 2015-04-30 01:43:23 +0530 | [diff] [blame] | 196 | u8 floor_freq_reg_addr; |
Jacob Pan | 087e9cb | 2014-11-07 09:29:25 -0800 | [diff] [blame] | 197 | int (*check_unit)(struct rapl_package *rp, int cpu); |
| 198 | void (*set_floor_freq)(struct rapl_domain *rd, bool mode); |
| 199 | u64 (*compute_time_window)(struct rapl_package *rp, u64 val, |
| 200 | bool to_raw); |
Jacob Pan | d474a4d | 2015-03-13 03:48:56 -0700 | [diff] [blame] | 201 | unsigned int dram_domain_energy_unit; |
Jacob Pan | 087e9cb | 2014-11-07 09:29:25 -0800 | [diff] [blame] | 202 | }; |
| 203 | static struct rapl_defaults *rapl_defaults; |
| 204 | |
Jacob Pan | 3c2c084 | 2014-11-07 09:29:26 -0800 | [diff] [blame] | 205 | /* Sideband MBI registers */ |
Ajay Thomas | 51b6340 | 2015-04-30 01:43:23 +0530 | [diff] [blame] | 206 | #define IOSF_CPU_POWER_BUDGET_CTL_BYT (0x2) |
| 207 | #define IOSF_CPU_POWER_BUDGET_CTL_TNG (0xdf) |
Jacob Pan | 3c2c084 | 2014-11-07 09:29:26 -0800 | [diff] [blame] | 208 | |
Jacob Pan | 2d281d8 | 2013-10-17 10:28:35 -0700 | [diff] [blame] | 209 | #define PACKAGE_PLN_INT_SAVED BIT(0) |
| 210 | #define MAX_PRIM_NAME (32) |
| 211 | |
| 212 | /* per domain data. used to describe individual knobs such that access function |
| 213 | * can be consolidated into one instead of many inline functions. |
| 214 | */ |
| 215 | struct rapl_primitive_info { |
| 216 | const char *name; |
| 217 | u64 mask; |
| 218 | int shift; |
Zhang Rui | f7c4e0c | 2019-07-10 21:44:22 +0800 | [diff] [blame^] | 219 | enum rapl_domain_reg_id id; |
Jacob Pan | 2d281d8 | 2013-10-17 10:28:35 -0700 | [diff] [blame] | 220 | enum unit_type unit; |
| 221 | u32 flag; |
| 222 | }; |
| 223 | |
| 224 | #define PRIMITIVE_INFO_INIT(p, m, s, i, u, f) { \ |
| 225 | .name = #p, \ |
| 226 | .mask = m, \ |
| 227 | .shift = s, \ |
| 228 | .id = i, \ |
| 229 | .unit = u, \ |
| 230 | .flag = f \ |
| 231 | } |
| 232 | |
| 233 | static void rapl_init_domains(struct rapl_package *rp); |
| 234 | static int rapl_read_data_raw(struct rapl_domain *rd, |
| 235 | enum rapl_primitives prim, |
| 236 | bool xlate, u64 *data); |
| 237 | static int rapl_write_data_raw(struct rapl_domain *rd, |
| 238 | enum rapl_primitives prim, |
| 239 | unsigned long long value); |
Jacob Pan | 309557f | 2016-02-24 13:31:37 -0800 | [diff] [blame] | 240 | static u64 rapl_unit_xlate(struct rapl_domain *rd, |
Jacob Pan | d474a4d | 2015-03-13 03:48:56 -0700 | [diff] [blame] | 241 | enum unit_type type, u64 value, |
Jacob Pan | 2d281d8 | 2013-10-17 10:28:35 -0700 | [diff] [blame] | 242 | int to_raw); |
Jacob Pan | 309557f | 2016-02-24 13:31:37 -0800 | [diff] [blame] | 243 | static void package_power_limit_irq_save(struct rapl_package *rp); |
Jacob Pan | 2d281d8 | 2013-10-17 10:28:35 -0700 | [diff] [blame] | 244 | |
| 245 | static LIST_HEAD(rapl_packages); /* guarded by CPU hotplug lock */ |
| 246 | |
| 247 | static const char * const rapl_domain_names[] = { |
| 248 | "package", |
| 249 | "core", |
| 250 | "uncore", |
| 251 | "dram", |
Srinivas Pandruvada | 3521ba1 | 2016-04-17 15:03:01 -0700 | [diff] [blame] | 252 | "psys", |
Jacob Pan | 2d281d8 | 2013-10-17 10:28:35 -0700 | [diff] [blame] | 253 | }; |
| 254 | |
| 255 | static struct powercap_control_type *control_type; /* PowerCap Controller */ |
Srinivas Pandruvada | 3521ba1 | 2016-04-17 15:03:01 -0700 | [diff] [blame] | 256 | static struct rapl_domain *platform_rapl_domain; /* Platform (PSys) domain */ |
Jacob Pan | 2d281d8 | 2013-10-17 10:28:35 -0700 | [diff] [blame] | 257 | |
| 258 | /* caller to ensure CPU hotplug lock is held */ |
Zhang Rui | aadf7b3 | 2019-05-13 13:58:50 -0400 | [diff] [blame] | 259 | static struct rapl_package *rapl_find_package_domain(int cpu) |
Jacob Pan | 2d281d8 | 2013-10-17 10:28:35 -0700 | [diff] [blame] | 260 | { |
Zhang Rui | 32fb480 | 2019-05-13 13:58:51 -0400 | [diff] [blame] | 261 | int id = topology_logical_die_id(cpu); |
Jacob Pan | 2d281d8 | 2013-10-17 10:28:35 -0700 | [diff] [blame] | 262 | struct rapl_package *rp; |
| 263 | |
| 264 | list_for_each_entry(rp, &rapl_packages, plist) { |
| 265 | if (rp->id == id) |
| 266 | return rp; |
| 267 | } |
| 268 | |
| 269 | return NULL; |
| 270 | } |
| 271 | |
Jacob Pan | 2d281d8 | 2013-10-17 10:28:35 -0700 | [diff] [blame] | 272 | static int get_energy_counter(struct powercap_zone *power_zone, u64 *energy_raw) |
| 273 | { |
| 274 | struct rapl_domain *rd; |
| 275 | u64 energy_now; |
| 276 | |
| 277 | /* prevent CPU hotplug, make sure the RAPL domain does not go |
| 278 | * away while reading the counter. |
| 279 | */ |
| 280 | get_online_cpus(); |
| 281 | rd = power_zone_to_rapl_domain(power_zone); |
| 282 | |
| 283 | if (!rapl_read_data_raw(rd, ENERGY_COUNTER, true, &energy_now)) { |
| 284 | *energy_raw = energy_now; |
| 285 | put_online_cpus(); |
| 286 | |
| 287 | return 0; |
| 288 | } |
| 289 | put_online_cpus(); |
| 290 | |
| 291 | return -EIO; |
| 292 | } |
| 293 | |
| 294 | static int get_max_energy_counter(struct powercap_zone *pcd_dev, u64 *energy) |
| 295 | { |
Jacob Pan | d474a4d | 2015-03-13 03:48:56 -0700 | [diff] [blame] | 296 | struct rapl_domain *rd = power_zone_to_rapl_domain(pcd_dev); |
| 297 | |
Jacob Pan | 309557f | 2016-02-24 13:31:37 -0800 | [diff] [blame] | 298 | *energy = rapl_unit_xlate(rd, ENERGY_UNIT, ENERGY_STATUS_MASK, 0); |
Jacob Pan | 2d281d8 | 2013-10-17 10:28:35 -0700 | [diff] [blame] | 299 | return 0; |
| 300 | } |
| 301 | |
| 302 | static int release_zone(struct powercap_zone *power_zone) |
| 303 | { |
| 304 | struct rapl_domain *rd = power_zone_to_rapl_domain(power_zone); |
Jacob Pan | 309557f | 2016-02-24 13:31:37 -0800 | [diff] [blame] | 305 | struct rapl_package *rp = rd->rp; |
Jacob Pan | 2d281d8 | 2013-10-17 10:28:35 -0700 | [diff] [blame] | 306 | |
| 307 | /* package zone is the last zone of a package, we can free |
| 308 | * memory here since all children has been unregistered. |
| 309 | */ |
| 310 | if (rd->id == RAPL_DOMAIN_PACKAGE) { |
Jacob Pan | 2d281d8 | 2013-10-17 10:28:35 -0700 | [diff] [blame] | 311 | kfree(rd); |
| 312 | rp->domains = NULL; |
| 313 | } |
| 314 | |
| 315 | return 0; |
| 316 | |
| 317 | } |
| 318 | |
| 319 | static int find_nr_power_limit(struct rapl_domain *rd) |
| 320 | { |
Jacob Pan | e1399ba | 2016-05-31 13:41:29 -0700 | [diff] [blame] | 321 | int i, nr_pl = 0; |
Jacob Pan | 2d281d8 | 2013-10-17 10:28:35 -0700 | [diff] [blame] | 322 | |
| 323 | for (i = 0; i < NR_POWER_LIMITS; i++) { |
Jacob Pan | e1399ba | 2016-05-31 13:41:29 -0700 | [diff] [blame] | 324 | if (rd->rpl[i].name) |
| 325 | nr_pl++; |
Jacob Pan | 2d281d8 | 2013-10-17 10:28:35 -0700 | [diff] [blame] | 326 | } |
| 327 | |
Jacob Pan | e1399ba | 2016-05-31 13:41:29 -0700 | [diff] [blame] | 328 | return nr_pl; |
Jacob Pan | 2d281d8 | 2013-10-17 10:28:35 -0700 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | static int set_domain_enable(struct powercap_zone *power_zone, bool mode) |
| 332 | { |
| 333 | struct rapl_domain *rd = power_zone_to_rapl_domain(power_zone); |
Jacob Pan | 2d281d8 | 2013-10-17 10:28:35 -0700 | [diff] [blame] | 334 | |
| 335 | if (rd->state & DOMAIN_STATE_BIOS_LOCKED) |
| 336 | return -EACCES; |
Jacob Pan | 3c2c084 | 2014-11-07 09:29:26 -0800 | [diff] [blame] | 337 | |
Jacob Pan | 2d281d8 | 2013-10-17 10:28:35 -0700 | [diff] [blame] | 338 | get_online_cpus(); |
Jacob Pan | 2d281d8 | 2013-10-17 10:28:35 -0700 | [diff] [blame] | 339 | rapl_write_data_raw(rd, PL1_ENABLE, mode); |
Ajay Thomas | 51b6340 | 2015-04-30 01:43:23 +0530 | [diff] [blame] | 340 | if (rapl_defaults->set_floor_freq) |
| 341 | rapl_defaults->set_floor_freq(rd, mode); |
Jacob Pan | 2d281d8 | 2013-10-17 10:28:35 -0700 | [diff] [blame] | 342 | put_online_cpus(); |
| 343 | |
| 344 | return 0; |
| 345 | } |
| 346 | |
| 347 | static int get_domain_enable(struct powercap_zone *power_zone, bool *mode) |
| 348 | { |
| 349 | struct rapl_domain *rd = power_zone_to_rapl_domain(power_zone); |
| 350 | u64 val; |
| 351 | |
| 352 | if (rd->state & DOMAIN_STATE_BIOS_LOCKED) { |
| 353 | *mode = false; |
| 354 | return 0; |
| 355 | } |
| 356 | get_online_cpus(); |
| 357 | if (rapl_read_data_raw(rd, PL1_ENABLE, true, &val)) { |
| 358 | put_online_cpus(); |
| 359 | return -EIO; |
| 360 | } |
| 361 | *mode = val; |
| 362 | put_online_cpus(); |
| 363 | |
| 364 | return 0; |
| 365 | } |
| 366 | |
| 367 | /* per RAPL domain ops, in the order of rapl_domain_type */ |
Julia Lawall | 600c395 | 2015-12-23 22:59:55 +0100 | [diff] [blame] | 368 | static const struct powercap_zone_ops zone_ops[] = { |
Jacob Pan | 2d281d8 | 2013-10-17 10:28:35 -0700 | [diff] [blame] | 369 | /* RAPL_DOMAIN_PACKAGE */ |
| 370 | { |
| 371 | .get_energy_uj = get_energy_counter, |
| 372 | .get_max_energy_range_uj = get_max_energy_counter, |
| 373 | .release = release_zone, |
| 374 | .set_enable = set_domain_enable, |
| 375 | .get_enable = get_domain_enable, |
| 376 | }, |
| 377 | /* RAPL_DOMAIN_PP0 */ |
| 378 | { |
| 379 | .get_energy_uj = get_energy_counter, |
| 380 | .get_max_energy_range_uj = get_max_energy_counter, |
| 381 | .release = release_zone, |
| 382 | .set_enable = set_domain_enable, |
| 383 | .get_enable = get_domain_enable, |
| 384 | }, |
| 385 | /* RAPL_DOMAIN_PP1 */ |
| 386 | { |
| 387 | .get_energy_uj = get_energy_counter, |
| 388 | .get_max_energy_range_uj = get_max_energy_counter, |
| 389 | .release = release_zone, |
| 390 | .set_enable = set_domain_enable, |
| 391 | .get_enable = get_domain_enable, |
| 392 | }, |
| 393 | /* RAPL_DOMAIN_DRAM */ |
| 394 | { |
| 395 | .get_energy_uj = get_energy_counter, |
| 396 | .get_max_energy_range_uj = get_max_energy_counter, |
| 397 | .release = release_zone, |
| 398 | .set_enable = set_domain_enable, |
| 399 | .get_enable = get_domain_enable, |
| 400 | }, |
Srinivas Pandruvada | 3521ba1 | 2016-04-17 15:03:01 -0700 | [diff] [blame] | 401 | /* RAPL_DOMAIN_PLATFORM */ |
| 402 | { |
| 403 | .get_energy_uj = get_energy_counter, |
| 404 | .get_max_energy_range_uj = get_max_energy_counter, |
| 405 | .release = release_zone, |
| 406 | .set_enable = set_domain_enable, |
| 407 | .get_enable = get_domain_enable, |
| 408 | }, |
Jacob Pan | 2d281d8 | 2013-10-17 10:28:35 -0700 | [diff] [blame] | 409 | }; |
| 410 | |
Jacob Pan | e1399ba | 2016-05-31 13:41:29 -0700 | [diff] [blame] | 411 | |
| 412 | /* |
| 413 | * Constraint index used by powercap can be different than power limit (PL) |
| 414 | * index in that some PLs maybe missing due to non-existant MSRs. So we |
| 415 | * need to convert here by finding the valid PLs only (name populated). |
| 416 | */ |
| 417 | static int contraint_to_pl(struct rapl_domain *rd, int cid) |
| 418 | { |
| 419 | int i, j; |
| 420 | |
| 421 | for (i = 0, j = 0; i < NR_POWER_LIMITS; i++) { |
| 422 | if ((rd->rpl[i].name) && j++ == cid) { |
| 423 | pr_debug("%s: index %d\n", __func__, i); |
| 424 | return i; |
| 425 | } |
| 426 | } |
Jacob Pan | cb43f81 | 2016-11-28 13:53:11 -0800 | [diff] [blame] | 427 | pr_err("Cannot find matching power limit for constraint %d\n", cid); |
Jacob Pan | e1399ba | 2016-05-31 13:41:29 -0700 | [diff] [blame] | 428 | |
| 429 | return -EINVAL; |
| 430 | } |
| 431 | |
| 432 | static int set_power_limit(struct powercap_zone *power_zone, int cid, |
Jacob Pan | 2d281d8 | 2013-10-17 10:28:35 -0700 | [diff] [blame] | 433 | u64 power_limit) |
| 434 | { |
| 435 | struct rapl_domain *rd; |
| 436 | struct rapl_package *rp; |
| 437 | int ret = 0; |
Jacob Pan | e1399ba | 2016-05-31 13:41:29 -0700 | [diff] [blame] | 438 | int id; |
Jacob Pan | 2d281d8 | 2013-10-17 10:28:35 -0700 | [diff] [blame] | 439 | |
| 440 | get_online_cpus(); |
| 441 | rd = power_zone_to_rapl_domain(power_zone); |
Jacob Pan | e1399ba | 2016-05-31 13:41:29 -0700 | [diff] [blame] | 442 | id = contraint_to_pl(rd, cid); |
Jacob Pan | cb43f81 | 2016-11-28 13:53:11 -0800 | [diff] [blame] | 443 | if (id < 0) { |
| 444 | ret = id; |
| 445 | goto set_exit; |
| 446 | } |
Jacob Pan | e1399ba | 2016-05-31 13:41:29 -0700 | [diff] [blame] | 447 | |
Jacob Pan | 309557f | 2016-02-24 13:31:37 -0800 | [diff] [blame] | 448 | rp = rd->rp; |
Jacob Pan | 2d281d8 | 2013-10-17 10:28:35 -0700 | [diff] [blame] | 449 | |
| 450 | if (rd->state & DOMAIN_STATE_BIOS_LOCKED) { |
| 451 | dev_warn(&power_zone->dev, "%s locked by BIOS, monitoring only\n", |
| 452 | rd->name); |
| 453 | ret = -EACCES; |
| 454 | goto set_exit; |
| 455 | } |
| 456 | |
| 457 | switch (rd->rpl[id].prim_id) { |
| 458 | case PL1_ENABLE: |
| 459 | rapl_write_data_raw(rd, POWER_LIMIT1, power_limit); |
| 460 | break; |
| 461 | case PL2_ENABLE: |
| 462 | rapl_write_data_raw(rd, POWER_LIMIT2, power_limit); |
| 463 | break; |
| 464 | default: |
| 465 | ret = -EINVAL; |
| 466 | } |
| 467 | if (!ret) |
Jacob Pan | 309557f | 2016-02-24 13:31:37 -0800 | [diff] [blame] | 468 | package_power_limit_irq_save(rp); |
Jacob Pan | 2d281d8 | 2013-10-17 10:28:35 -0700 | [diff] [blame] | 469 | set_exit: |
| 470 | put_online_cpus(); |
| 471 | return ret; |
| 472 | } |
| 473 | |
Jacob Pan | e1399ba | 2016-05-31 13:41:29 -0700 | [diff] [blame] | 474 | static int get_current_power_limit(struct powercap_zone *power_zone, int cid, |
Jacob Pan | 2d281d8 | 2013-10-17 10:28:35 -0700 | [diff] [blame] | 475 | u64 *data) |
| 476 | { |
| 477 | struct rapl_domain *rd; |
| 478 | u64 val; |
| 479 | int prim; |
| 480 | int ret = 0; |
Jacob Pan | e1399ba | 2016-05-31 13:41:29 -0700 | [diff] [blame] | 481 | int id; |
Jacob Pan | 2d281d8 | 2013-10-17 10:28:35 -0700 | [diff] [blame] | 482 | |
| 483 | get_online_cpus(); |
| 484 | rd = power_zone_to_rapl_domain(power_zone); |
Jacob Pan | e1399ba | 2016-05-31 13:41:29 -0700 | [diff] [blame] | 485 | id = contraint_to_pl(rd, cid); |
Jacob Pan | cb43f81 | 2016-11-28 13:53:11 -0800 | [diff] [blame] | 486 | if (id < 0) { |
| 487 | ret = id; |
| 488 | goto get_exit; |
| 489 | } |
| 490 | |
Jacob Pan | 2d281d8 | 2013-10-17 10:28:35 -0700 | [diff] [blame] | 491 | switch (rd->rpl[id].prim_id) { |
| 492 | case PL1_ENABLE: |
| 493 | prim = POWER_LIMIT1; |
| 494 | break; |
| 495 | case PL2_ENABLE: |
| 496 | prim = POWER_LIMIT2; |
| 497 | break; |
| 498 | default: |
| 499 | put_online_cpus(); |
| 500 | return -EINVAL; |
| 501 | } |
| 502 | if (rapl_read_data_raw(rd, prim, true, &val)) |
| 503 | ret = -EIO; |
| 504 | else |
| 505 | *data = val; |
| 506 | |
Jacob Pan | cb43f81 | 2016-11-28 13:53:11 -0800 | [diff] [blame] | 507 | get_exit: |
Jacob Pan | 2d281d8 | 2013-10-17 10:28:35 -0700 | [diff] [blame] | 508 | put_online_cpus(); |
| 509 | |
| 510 | return ret; |
| 511 | } |
| 512 | |
Jacob Pan | e1399ba | 2016-05-31 13:41:29 -0700 | [diff] [blame] | 513 | static int set_time_window(struct powercap_zone *power_zone, int cid, |
Jacob Pan | 2d281d8 | 2013-10-17 10:28:35 -0700 | [diff] [blame] | 514 | u64 window) |
| 515 | { |
| 516 | struct rapl_domain *rd; |
| 517 | int ret = 0; |
Jacob Pan | e1399ba | 2016-05-31 13:41:29 -0700 | [diff] [blame] | 518 | int id; |
Jacob Pan | 2d281d8 | 2013-10-17 10:28:35 -0700 | [diff] [blame] | 519 | |
| 520 | get_online_cpus(); |
| 521 | rd = power_zone_to_rapl_domain(power_zone); |
Jacob Pan | e1399ba | 2016-05-31 13:41:29 -0700 | [diff] [blame] | 522 | id = contraint_to_pl(rd, cid); |
Jacob Pan | cb43f81 | 2016-11-28 13:53:11 -0800 | [diff] [blame] | 523 | if (id < 0) { |
| 524 | ret = id; |
| 525 | goto set_time_exit; |
| 526 | } |
Jacob Pan | e1399ba | 2016-05-31 13:41:29 -0700 | [diff] [blame] | 527 | |
Jacob Pan | 2d281d8 | 2013-10-17 10:28:35 -0700 | [diff] [blame] | 528 | switch (rd->rpl[id].prim_id) { |
| 529 | case PL1_ENABLE: |
| 530 | rapl_write_data_raw(rd, TIME_WINDOW1, window); |
| 531 | break; |
| 532 | case PL2_ENABLE: |
| 533 | rapl_write_data_raw(rd, TIME_WINDOW2, window); |
| 534 | break; |
| 535 | default: |
| 536 | ret = -EINVAL; |
| 537 | } |
Jacob Pan | cb43f81 | 2016-11-28 13:53:11 -0800 | [diff] [blame] | 538 | |
| 539 | set_time_exit: |
Jacob Pan | 2d281d8 | 2013-10-17 10:28:35 -0700 | [diff] [blame] | 540 | put_online_cpus(); |
| 541 | return ret; |
| 542 | } |
| 543 | |
Jacob Pan | e1399ba | 2016-05-31 13:41:29 -0700 | [diff] [blame] | 544 | static int get_time_window(struct powercap_zone *power_zone, int cid, u64 *data) |
Jacob Pan | 2d281d8 | 2013-10-17 10:28:35 -0700 | [diff] [blame] | 545 | { |
| 546 | struct rapl_domain *rd; |
| 547 | u64 val; |
| 548 | int ret = 0; |
Jacob Pan | e1399ba | 2016-05-31 13:41:29 -0700 | [diff] [blame] | 549 | int id; |
Jacob Pan | 2d281d8 | 2013-10-17 10:28:35 -0700 | [diff] [blame] | 550 | |
| 551 | get_online_cpus(); |
| 552 | rd = power_zone_to_rapl_domain(power_zone); |
Jacob Pan | e1399ba | 2016-05-31 13:41:29 -0700 | [diff] [blame] | 553 | id = contraint_to_pl(rd, cid); |
Jacob Pan | cb43f81 | 2016-11-28 13:53:11 -0800 | [diff] [blame] | 554 | if (id < 0) { |
| 555 | ret = id; |
| 556 | goto get_time_exit; |
| 557 | } |
Jacob Pan | e1399ba | 2016-05-31 13:41:29 -0700 | [diff] [blame] | 558 | |
Jacob Pan | 2d281d8 | 2013-10-17 10:28:35 -0700 | [diff] [blame] | 559 | switch (rd->rpl[id].prim_id) { |
| 560 | case PL1_ENABLE: |
| 561 | ret = rapl_read_data_raw(rd, TIME_WINDOW1, true, &val); |
| 562 | break; |
| 563 | case PL2_ENABLE: |
| 564 | ret = rapl_read_data_raw(rd, TIME_WINDOW2, true, &val); |
| 565 | break; |
| 566 | default: |
| 567 | put_online_cpus(); |
| 568 | return -EINVAL; |
| 569 | } |
| 570 | if (!ret) |
| 571 | *data = val; |
Jacob Pan | cb43f81 | 2016-11-28 13:53:11 -0800 | [diff] [blame] | 572 | |
| 573 | get_time_exit: |
Jacob Pan | 2d281d8 | 2013-10-17 10:28:35 -0700 | [diff] [blame] | 574 | put_online_cpus(); |
| 575 | |
| 576 | return ret; |
| 577 | } |
| 578 | |
Jacob Pan | e1399ba | 2016-05-31 13:41:29 -0700 | [diff] [blame] | 579 | static const char *get_constraint_name(struct powercap_zone *power_zone, int cid) |
Jacob Pan | 2d281d8 | 2013-10-17 10:28:35 -0700 | [diff] [blame] | 580 | { |
Jacob Pan | 2d281d8 | 2013-10-17 10:28:35 -0700 | [diff] [blame] | 581 | struct rapl_domain *rd; |
Jacob Pan | e1399ba | 2016-05-31 13:41:29 -0700 | [diff] [blame] | 582 | int id; |
Jacob Pan | 2d281d8 | 2013-10-17 10:28:35 -0700 | [diff] [blame] | 583 | |
| 584 | rd = power_zone_to_rapl_domain(power_zone); |
Jacob Pan | e1399ba | 2016-05-31 13:41:29 -0700 | [diff] [blame] | 585 | id = contraint_to_pl(rd, cid); |
| 586 | if (id >= 0) |
| 587 | return rd->rpl[id].name; |
Jacob Pan | 2d281d8 | 2013-10-17 10:28:35 -0700 | [diff] [blame] | 588 | |
Jacob Pan | e1399ba | 2016-05-31 13:41:29 -0700 | [diff] [blame] | 589 | return NULL; |
Jacob Pan | 2d281d8 | 2013-10-17 10:28:35 -0700 | [diff] [blame] | 590 | } |
| 591 | |
| 592 | |
| 593 | static int get_max_power(struct powercap_zone *power_zone, int id, |
| 594 | u64 *data) |
| 595 | { |
| 596 | struct rapl_domain *rd; |
| 597 | u64 val; |
| 598 | int prim; |
| 599 | int ret = 0; |
| 600 | |
| 601 | get_online_cpus(); |
| 602 | rd = power_zone_to_rapl_domain(power_zone); |
| 603 | switch (rd->rpl[id].prim_id) { |
| 604 | case PL1_ENABLE: |
| 605 | prim = THERMAL_SPEC_POWER; |
| 606 | break; |
| 607 | case PL2_ENABLE: |
| 608 | prim = MAX_POWER; |
| 609 | break; |
| 610 | default: |
| 611 | put_online_cpus(); |
| 612 | return -EINVAL; |
| 613 | } |
| 614 | if (rapl_read_data_raw(rd, prim, true, &val)) |
| 615 | ret = -EIO; |
| 616 | else |
| 617 | *data = val; |
| 618 | |
| 619 | put_online_cpus(); |
| 620 | |
| 621 | return ret; |
| 622 | } |
| 623 | |
Julia Lawall | 600c395 | 2015-12-23 22:59:55 +0100 | [diff] [blame] | 624 | static const struct powercap_zone_constraint_ops constraint_ops = { |
Jacob Pan | 2d281d8 | 2013-10-17 10:28:35 -0700 | [diff] [blame] | 625 | .set_power_limit_uw = set_power_limit, |
| 626 | .get_power_limit_uw = get_current_power_limit, |
| 627 | .set_time_window_us = set_time_window, |
| 628 | .get_time_window_us = get_time_window, |
| 629 | .get_max_power_uw = get_max_power, |
| 630 | .get_name = get_constraint_name, |
| 631 | }; |
| 632 | |
| 633 | /* called after domain detection and package level data are set */ |
| 634 | static void rapl_init_domains(struct rapl_package *rp) |
| 635 | { |
| 636 | int i; |
| 637 | struct rapl_domain *rd = rp->domains; |
| 638 | |
| 639 | for (i = 0; i < RAPL_DOMAIN_MAX; i++) { |
| 640 | unsigned int mask = rp->domain_map & (1 << i); |
| 641 | switch (mask) { |
| 642 | case BIT(RAPL_DOMAIN_PACKAGE): |
| 643 | rd->name = rapl_domain_names[RAPL_DOMAIN_PACKAGE]; |
| 644 | rd->id = RAPL_DOMAIN_PACKAGE; |
Zhang Rui | f7c4e0c | 2019-07-10 21:44:22 +0800 | [diff] [blame^] | 645 | rd->regs[0] = MSR_PKG_POWER_LIMIT; |
| 646 | rd->regs[1] = MSR_PKG_ENERGY_STATUS; |
| 647 | rd->regs[2] = MSR_PKG_PERF_STATUS; |
| 648 | rd->regs[3] = 0; |
| 649 | rd->regs[4] = MSR_PKG_POWER_INFO; |
Jacob Pan | 2d281d8 | 2013-10-17 10:28:35 -0700 | [diff] [blame] | 650 | rd->rpl[0].prim_id = PL1_ENABLE; |
| 651 | rd->rpl[0].name = pl1_name; |
| 652 | rd->rpl[1].prim_id = PL2_ENABLE; |
| 653 | rd->rpl[1].name = pl2_name; |
| 654 | break; |
| 655 | case BIT(RAPL_DOMAIN_PP0): |
| 656 | rd->name = rapl_domain_names[RAPL_DOMAIN_PP0]; |
| 657 | rd->id = RAPL_DOMAIN_PP0; |
Zhang Rui | f7c4e0c | 2019-07-10 21:44:22 +0800 | [diff] [blame^] | 658 | rd->regs[0] = MSR_PP0_POWER_LIMIT; |
| 659 | rd->regs[1] = MSR_PP0_ENERGY_STATUS; |
| 660 | rd->regs[2] = 0; |
| 661 | rd->regs[3] = MSR_PP0_POLICY; |
| 662 | rd->regs[4] = 0; |
Jacob Pan | 2d281d8 | 2013-10-17 10:28:35 -0700 | [diff] [blame] | 663 | rd->rpl[0].prim_id = PL1_ENABLE; |
| 664 | rd->rpl[0].name = pl1_name; |
| 665 | break; |
| 666 | case BIT(RAPL_DOMAIN_PP1): |
| 667 | rd->name = rapl_domain_names[RAPL_DOMAIN_PP1]; |
| 668 | rd->id = RAPL_DOMAIN_PP1; |
Zhang Rui | f7c4e0c | 2019-07-10 21:44:22 +0800 | [diff] [blame^] | 669 | rd->regs[0] = MSR_PP1_POWER_LIMIT; |
| 670 | rd->regs[1] = MSR_PP1_ENERGY_STATUS; |
| 671 | rd->regs[2] = 0; |
| 672 | rd->regs[3] = MSR_PP1_POLICY; |
| 673 | rd->regs[4] = 0; |
Jacob Pan | 2d281d8 | 2013-10-17 10:28:35 -0700 | [diff] [blame] | 674 | rd->rpl[0].prim_id = PL1_ENABLE; |
| 675 | rd->rpl[0].name = pl1_name; |
| 676 | break; |
| 677 | case BIT(RAPL_DOMAIN_DRAM): |
| 678 | rd->name = rapl_domain_names[RAPL_DOMAIN_DRAM]; |
| 679 | rd->id = RAPL_DOMAIN_DRAM; |
Zhang Rui | f7c4e0c | 2019-07-10 21:44:22 +0800 | [diff] [blame^] | 680 | rd->regs[0] = MSR_DRAM_POWER_LIMIT; |
| 681 | rd->regs[1] = MSR_DRAM_ENERGY_STATUS; |
| 682 | rd->regs[2] = MSR_DRAM_PERF_STATUS; |
| 683 | rd->regs[3] = 0; |
| 684 | rd->regs[4] = MSR_DRAM_POWER_INFO; |
Jacob Pan | 2d281d8 | 2013-10-17 10:28:35 -0700 | [diff] [blame] | 685 | rd->rpl[0].prim_id = PL1_ENABLE; |
| 686 | rd->rpl[0].name = pl1_name; |
Jacob Pan | d474a4d | 2015-03-13 03:48:56 -0700 | [diff] [blame] | 687 | rd->domain_energy_unit = |
| 688 | rapl_defaults->dram_domain_energy_unit; |
| 689 | if (rd->domain_energy_unit) |
| 690 | pr_info("DRAM domain energy unit %dpj\n", |
| 691 | rd->domain_energy_unit); |
Jacob Pan | 2d281d8 | 2013-10-17 10:28:35 -0700 | [diff] [blame] | 692 | break; |
| 693 | } |
| 694 | if (mask) { |
Jacob Pan | 309557f | 2016-02-24 13:31:37 -0800 | [diff] [blame] | 695 | rd->rp = rp; |
Jacob Pan | 2d281d8 | 2013-10-17 10:28:35 -0700 | [diff] [blame] | 696 | rd++; |
| 697 | } |
| 698 | } |
| 699 | } |
| 700 | |
Jacob Pan | 309557f | 2016-02-24 13:31:37 -0800 | [diff] [blame] | 701 | static u64 rapl_unit_xlate(struct rapl_domain *rd, enum unit_type type, |
| 702 | u64 value, int to_raw) |
Jacob Pan | 2d281d8 | 2013-10-17 10:28:35 -0700 | [diff] [blame] | 703 | { |
Jacob Pan | 3c2c084 | 2014-11-07 09:29:26 -0800 | [diff] [blame] | 704 | u64 units = 1; |
Jacob Pan | 309557f | 2016-02-24 13:31:37 -0800 | [diff] [blame] | 705 | struct rapl_package *rp = rd->rp; |
Jacob Pan | d474a4d | 2015-03-13 03:48:56 -0700 | [diff] [blame] | 706 | u64 scale = 1; |
Jacob Pan | 2d281d8 | 2013-10-17 10:28:35 -0700 | [diff] [blame] | 707 | |
Jacob Pan | 2d281d8 | 2013-10-17 10:28:35 -0700 | [diff] [blame] | 708 | switch (type) { |
| 709 | case POWER_UNIT: |
Jacob Pan | 3c2c084 | 2014-11-07 09:29:26 -0800 | [diff] [blame] | 710 | units = rp->power_unit; |
Jacob Pan | 2d281d8 | 2013-10-17 10:28:35 -0700 | [diff] [blame] | 711 | break; |
| 712 | case ENERGY_UNIT: |
Jacob Pan | d474a4d | 2015-03-13 03:48:56 -0700 | [diff] [blame] | 713 | scale = ENERGY_UNIT_SCALE; |
| 714 | /* per domain unit takes precedence */ |
Jacob Pan | cb43f81 | 2016-11-28 13:53:11 -0800 | [diff] [blame] | 715 | if (rd->domain_energy_unit) |
Jacob Pan | d474a4d | 2015-03-13 03:48:56 -0700 | [diff] [blame] | 716 | units = rd->domain_energy_unit; |
| 717 | else |
| 718 | units = rp->energy_unit; |
Jacob Pan | 2d281d8 | 2013-10-17 10:28:35 -0700 | [diff] [blame] | 719 | break; |
| 720 | case TIME_UNIT: |
Jacob Pan | 3c2c084 | 2014-11-07 09:29:26 -0800 | [diff] [blame] | 721 | return rapl_defaults->compute_time_window(rp, value, to_raw); |
Jacob Pan | 2d281d8 | 2013-10-17 10:28:35 -0700 | [diff] [blame] | 722 | case ARBITRARY_UNIT: |
| 723 | default: |
| 724 | return value; |
| 725 | }; |
| 726 | |
| 727 | if (to_raw) |
Jacob Pan | d474a4d | 2015-03-13 03:48:56 -0700 | [diff] [blame] | 728 | return div64_u64(value, units) * scale; |
Jacob Pan | 3c2c084 | 2014-11-07 09:29:26 -0800 | [diff] [blame] | 729 | |
| 730 | value *= units; |
| 731 | |
Jacob Pan | d474a4d | 2015-03-13 03:48:56 -0700 | [diff] [blame] | 732 | return div64_u64(value, scale); |
Jacob Pan | 2d281d8 | 2013-10-17 10:28:35 -0700 | [diff] [blame] | 733 | } |
| 734 | |
| 735 | /* in the order of enum rapl_primitives */ |
| 736 | static struct rapl_primitive_info rpi[] = { |
| 737 | /* name, mask, shift, msr index, unit divisor */ |
| 738 | PRIMITIVE_INFO_INIT(ENERGY_COUNTER, ENERGY_STATUS_MASK, 0, |
Zhang Rui | f7c4e0c | 2019-07-10 21:44:22 +0800 | [diff] [blame^] | 739 | RAPL_DOMAIN_REG_STATUS, ENERGY_UNIT, 0), |
Jacob Pan | 2d281d8 | 2013-10-17 10:28:35 -0700 | [diff] [blame] | 740 | PRIMITIVE_INFO_INIT(POWER_LIMIT1, POWER_LIMIT1_MASK, 0, |
Zhang Rui | f7c4e0c | 2019-07-10 21:44:22 +0800 | [diff] [blame^] | 741 | RAPL_DOMAIN_REG_LIMIT, POWER_UNIT, 0), |
Jacob Pan | 2d281d8 | 2013-10-17 10:28:35 -0700 | [diff] [blame] | 742 | PRIMITIVE_INFO_INIT(POWER_LIMIT2, POWER_LIMIT2_MASK, 32, |
Zhang Rui | f7c4e0c | 2019-07-10 21:44:22 +0800 | [diff] [blame^] | 743 | RAPL_DOMAIN_REG_LIMIT, POWER_UNIT, 0), |
Jacob Pan | 2d281d8 | 2013-10-17 10:28:35 -0700 | [diff] [blame] | 744 | PRIMITIVE_INFO_INIT(FW_LOCK, POWER_PP_LOCK, 31, |
Zhang Rui | f7c4e0c | 2019-07-10 21:44:22 +0800 | [diff] [blame^] | 745 | RAPL_DOMAIN_REG_LIMIT, ARBITRARY_UNIT, 0), |
Jacob Pan | 2d281d8 | 2013-10-17 10:28:35 -0700 | [diff] [blame] | 746 | PRIMITIVE_INFO_INIT(PL1_ENABLE, POWER_LIMIT1_ENABLE, 15, |
Zhang Rui | f7c4e0c | 2019-07-10 21:44:22 +0800 | [diff] [blame^] | 747 | RAPL_DOMAIN_REG_LIMIT, ARBITRARY_UNIT, 0), |
Jacob Pan | 2d281d8 | 2013-10-17 10:28:35 -0700 | [diff] [blame] | 748 | PRIMITIVE_INFO_INIT(PL1_CLAMP, POWER_LIMIT1_CLAMP, 16, |
Zhang Rui | f7c4e0c | 2019-07-10 21:44:22 +0800 | [diff] [blame^] | 749 | RAPL_DOMAIN_REG_LIMIT, ARBITRARY_UNIT, 0), |
Jacob Pan | 2d281d8 | 2013-10-17 10:28:35 -0700 | [diff] [blame] | 750 | PRIMITIVE_INFO_INIT(PL2_ENABLE, POWER_LIMIT2_ENABLE, 47, |
Zhang Rui | f7c4e0c | 2019-07-10 21:44:22 +0800 | [diff] [blame^] | 751 | RAPL_DOMAIN_REG_LIMIT, ARBITRARY_UNIT, 0), |
Jacob Pan | 2d281d8 | 2013-10-17 10:28:35 -0700 | [diff] [blame] | 752 | PRIMITIVE_INFO_INIT(PL2_CLAMP, POWER_LIMIT2_CLAMP, 48, |
Zhang Rui | f7c4e0c | 2019-07-10 21:44:22 +0800 | [diff] [blame^] | 753 | RAPL_DOMAIN_REG_LIMIT, ARBITRARY_UNIT, 0), |
Jacob Pan | 2d281d8 | 2013-10-17 10:28:35 -0700 | [diff] [blame] | 754 | PRIMITIVE_INFO_INIT(TIME_WINDOW1, TIME_WINDOW1_MASK, 17, |
Zhang Rui | f7c4e0c | 2019-07-10 21:44:22 +0800 | [diff] [blame^] | 755 | RAPL_DOMAIN_REG_LIMIT, TIME_UNIT, 0), |
Jacob Pan | 2d281d8 | 2013-10-17 10:28:35 -0700 | [diff] [blame] | 756 | PRIMITIVE_INFO_INIT(TIME_WINDOW2, TIME_WINDOW2_MASK, 49, |
Zhang Rui | f7c4e0c | 2019-07-10 21:44:22 +0800 | [diff] [blame^] | 757 | RAPL_DOMAIN_REG_LIMIT, TIME_UNIT, 0), |
Jacob Pan | 2d281d8 | 2013-10-17 10:28:35 -0700 | [diff] [blame] | 758 | PRIMITIVE_INFO_INIT(THERMAL_SPEC_POWER, POWER_INFO_THERMAL_SPEC_MASK, |
Zhang Rui | f7c4e0c | 2019-07-10 21:44:22 +0800 | [diff] [blame^] | 759 | 0, RAPL_DOMAIN_REG_INFO, POWER_UNIT, 0), |
Jacob Pan | 2d281d8 | 2013-10-17 10:28:35 -0700 | [diff] [blame] | 760 | PRIMITIVE_INFO_INIT(MAX_POWER, POWER_INFO_MAX_MASK, 32, |
Zhang Rui | f7c4e0c | 2019-07-10 21:44:22 +0800 | [diff] [blame^] | 761 | RAPL_DOMAIN_REG_INFO, POWER_UNIT, 0), |
Jacob Pan | 2d281d8 | 2013-10-17 10:28:35 -0700 | [diff] [blame] | 762 | PRIMITIVE_INFO_INIT(MIN_POWER, POWER_INFO_MIN_MASK, 16, |
Zhang Rui | f7c4e0c | 2019-07-10 21:44:22 +0800 | [diff] [blame^] | 763 | RAPL_DOMAIN_REG_INFO, POWER_UNIT, 0), |
Jacob Pan | 2d281d8 | 2013-10-17 10:28:35 -0700 | [diff] [blame] | 764 | PRIMITIVE_INFO_INIT(MAX_TIME_WINDOW, POWER_INFO_MAX_TIME_WIN_MASK, 48, |
Zhang Rui | f7c4e0c | 2019-07-10 21:44:22 +0800 | [diff] [blame^] | 765 | RAPL_DOMAIN_REG_INFO, TIME_UNIT, 0), |
Jacob Pan | 2d281d8 | 2013-10-17 10:28:35 -0700 | [diff] [blame] | 766 | PRIMITIVE_INFO_INIT(THROTTLED_TIME, PERF_STATUS_THROTTLE_TIME_MASK, 0, |
Zhang Rui | f7c4e0c | 2019-07-10 21:44:22 +0800 | [diff] [blame^] | 767 | RAPL_DOMAIN_REG_PERF, TIME_UNIT, 0), |
Jacob Pan | 2d281d8 | 2013-10-17 10:28:35 -0700 | [diff] [blame] | 768 | PRIMITIVE_INFO_INIT(PRIORITY_LEVEL, PP_POLICY_MASK, 0, |
Zhang Rui | f7c4e0c | 2019-07-10 21:44:22 +0800 | [diff] [blame^] | 769 | RAPL_DOMAIN_REG_POLICY, ARBITRARY_UNIT, 0), |
Jacob Pan | 2d281d8 | 2013-10-17 10:28:35 -0700 | [diff] [blame] | 770 | /* non-hardware */ |
| 771 | PRIMITIVE_INFO_INIT(AVERAGE_POWER, 0, 0, 0, POWER_UNIT, |
| 772 | RAPL_PRIMITIVE_DERIVED), |
| 773 | {NULL, 0, 0, 0}, |
| 774 | }; |
| 775 | |
| 776 | /* Read primitive data based on its related struct rapl_primitive_info. |
| 777 | * if xlate flag is set, return translated data based on data units, i.e. |
| 778 | * time, energy, and power. |
| 779 | * RAPL MSRs are non-architectual and are laid out not consistently across |
| 780 | * domains. Here we use primitive info to allow writing consolidated access |
| 781 | * functions. |
| 782 | * For a given primitive, it is processed by MSR mask and shift. Unit conversion |
| 783 | * is pre-assigned based on RAPL unit MSRs read at init time. |
| 784 | * 63-------------------------- 31--------------------------- 0 |
| 785 | * | xxxxx (mask) | |
| 786 | * | |<- shift ----------------| |
| 787 | * 63-------------------------- 31--------------------------- 0 |
| 788 | */ |
| 789 | static int rapl_read_data_raw(struct rapl_domain *rd, |
| 790 | enum rapl_primitives prim, |
| 791 | bool xlate, u64 *data) |
| 792 | { |
| 793 | u64 value, final; |
| 794 | u32 msr; |
| 795 | struct rapl_primitive_info *rp = &rpi[prim]; |
| 796 | int cpu; |
| 797 | |
| 798 | if (!rp->name || rp->flag & RAPL_PRIMITIVE_DUMMY) |
| 799 | return -EINVAL; |
| 800 | |
Zhang Rui | f7c4e0c | 2019-07-10 21:44:22 +0800 | [diff] [blame^] | 801 | msr = rd->regs[rp->id]; |
Jacob Pan | 2d281d8 | 2013-10-17 10:28:35 -0700 | [diff] [blame] | 802 | if (!msr) |
| 803 | return -EINVAL; |
Jacob Pan | 323ee64 | 2016-02-24 13:31:38 -0800 | [diff] [blame] | 804 | |
| 805 | cpu = rd->rp->lead_cpu; |
Jacob Pan | 2d281d8 | 2013-10-17 10:28:35 -0700 | [diff] [blame] | 806 | |
| 807 | /* special-case package domain, which uses a different bit*/ |
| 808 | if (prim == FW_LOCK && rd->id == RAPL_DOMAIN_PACKAGE) { |
| 809 | rp->mask = POWER_PACKAGE_LOCK; |
| 810 | rp->shift = 63; |
| 811 | } |
| 812 | /* non-hardware data are collected by the polling thread */ |
| 813 | if (rp->flag & RAPL_PRIMITIVE_DERIVED) { |
| 814 | *data = rd->rdd.primitives[prim]; |
| 815 | return 0; |
| 816 | } |
| 817 | |
| 818 | if (rdmsrl_safe_on_cpu(cpu, msr, &value)) { |
| 819 | pr_debug("failed to read msr 0x%x on cpu %d\n", msr, cpu); |
| 820 | return -EIO; |
| 821 | } |
| 822 | |
| 823 | final = value & rp->mask; |
| 824 | final = final >> rp->shift; |
| 825 | if (xlate) |
Jacob Pan | 309557f | 2016-02-24 13:31:37 -0800 | [diff] [blame] | 826 | *data = rapl_unit_xlate(rd, rp->unit, final, 0); |
Jacob Pan | 2d281d8 | 2013-10-17 10:28:35 -0700 | [diff] [blame] | 827 | else |
| 828 | *data = final; |
| 829 | |
| 830 | return 0; |
| 831 | } |
| 832 | |
Jacob Pan | f14a139 | 2016-02-24 13:31:36 -0800 | [diff] [blame] | 833 | |
| 834 | static int msrl_update_safe(u32 msr_no, u64 clear_mask, u64 set_mask) |
| 835 | { |
| 836 | int err; |
| 837 | u64 val; |
| 838 | |
| 839 | err = rdmsrl_safe(msr_no, &val); |
| 840 | if (err) |
| 841 | goto out; |
| 842 | |
| 843 | val &= ~clear_mask; |
| 844 | val |= set_mask; |
| 845 | |
| 846 | err = wrmsrl_safe(msr_no, val); |
| 847 | |
| 848 | out: |
| 849 | return err; |
| 850 | } |
| 851 | |
| 852 | static void msrl_update_func(void *info) |
| 853 | { |
| 854 | struct msrl_action *ma = info; |
| 855 | |
| 856 | ma->err = msrl_update_safe(ma->msr_no, ma->clear_mask, ma->set_mask); |
| 857 | } |
| 858 | |
Jacob Pan | 2d281d8 | 2013-10-17 10:28:35 -0700 | [diff] [blame] | 859 | /* Similar use of primitive info in the read counterpart */ |
| 860 | static int rapl_write_data_raw(struct rapl_domain *rd, |
| 861 | enum rapl_primitives prim, |
| 862 | unsigned long long value) |
| 863 | { |
Jacob Pan | 2d281d8 | 2013-10-17 10:28:35 -0700 | [diff] [blame] | 864 | struct rapl_primitive_info *rp = &rpi[prim]; |
| 865 | int cpu; |
Jacob Pan | f14a139 | 2016-02-24 13:31:36 -0800 | [diff] [blame] | 866 | u64 bits; |
| 867 | struct msrl_action ma; |
| 868 | int ret; |
Jacob Pan | 2d281d8 | 2013-10-17 10:28:35 -0700 | [diff] [blame] | 869 | |
Jacob Pan | 323ee64 | 2016-02-24 13:31:38 -0800 | [diff] [blame] | 870 | cpu = rd->rp->lead_cpu; |
Jacob Pan | 309557f | 2016-02-24 13:31:37 -0800 | [diff] [blame] | 871 | bits = rapl_unit_xlate(rd, rp->unit, value, 1); |
Adam Lessnau | edbdabc | 2017-06-01 11:21:50 +0200 | [diff] [blame] | 872 | bits <<= rp->shift; |
| 873 | bits &= rp->mask; |
| 874 | |
Jacob Pan | f14a139 | 2016-02-24 13:31:36 -0800 | [diff] [blame] | 875 | memset(&ma, 0, sizeof(ma)); |
| 876 | |
Zhang Rui | f7c4e0c | 2019-07-10 21:44:22 +0800 | [diff] [blame^] | 877 | ma.msr_no = rd->regs[rp->id]; |
Jacob Pan | f14a139 | 2016-02-24 13:31:36 -0800 | [diff] [blame] | 878 | ma.clear_mask = rp->mask; |
| 879 | ma.set_mask = bits; |
| 880 | |
| 881 | ret = smp_call_function_single(cpu, msrl_update_func, &ma, 1); |
| 882 | if (ret) |
| 883 | WARN_ON_ONCE(ret); |
| 884 | else |
| 885 | ret = ma.err; |
| 886 | |
| 887 | return ret; |
Jacob Pan | 2d281d8 | 2013-10-17 10:28:35 -0700 | [diff] [blame] | 888 | } |
| 889 | |
Jacob Pan | 3c2c084 | 2014-11-07 09:29:26 -0800 | [diff] [blame] | 890 | /* |
| 891 | * Raw RAPL data stored in MSRs are in certain scales. We need to |
| 892 | * convert them into standard units based on the units reported in |
| 893 | * the RAPL unit MSRs. This is specific to CPUs as the method to |
| 894 | * calculate units differ on different CPUs. |
| 895 | * We convert the units to below format based on CPUs. |
| 896 | * i.e. |
Jacob Pan | d474a4d | 2015-03-13 03:48:56 -0700 | [diff] [blame] | 897 | * energy unit: picoJoules : Represented in picoJoules by default |
Jacob Pan | 3c2c084 | 2014-11-07 09:29:26 -0800 | [diff] [blame] | 898 | * power unit : microWatts : Represented in milliWatts by default |
| 899 | * time unit : microseconds: Represented in seconds by default |
| 900 | */ |
| 901 | static int rapl_check_unit_core(struct rapl_package *rp, int cpu) |
Jacob Pan | 2d281d8 | 2013-10-17 10:28:35 -0700 | [diff] [blame] | 902 | { |
| 903 | u64 msr_val; |
| 904 | u32 value; |
| 905 | |
| 906 | if (rdmsrl_safe_on_cpu(cpu, MSR_RAPL_POWER_UNIT, &msr_val)) { |
| 907 | pr_err("Failed to read power unit MSR 0x%x on CPU %d, exit.\n", |
| 908 | MSR_RAPL_POWER_UNIT, cpu); |
| 909 | return -ENODEV; |
| 910 | } |
| 911 | |
Jacob Pan | 2d281d8 | 2013-10-17 10:28:35 -0700 | [diff] [blame] | 912 | value = (msr_val & ENERGY_UNIT_MASK) >> ENERGY_UNIT_OFFSET; |
Jacob Pan | d474a4d | 2015-03-13 03:48:56 -0700 | [diff] [blame] | 913 | rp->energy_unit = ENERGY_UNIT_SCALE * 1000000 / (1 << value); |
Jacob Pan | 2d281d8 | 2013-10-17 10:28:35 -0700 | [diff] [blame] | 914 | |
| 915 | value = (msr_val & POWER_UNIT_MASK) >> POWER_UNIT_OFFSET; |
Jacob Pan | 3c2c084 | 2014-11-07 09:29:26 -0800 | [diff] [blame] | 916 | rp->power_unit = 1000000 / (1 << value); |
Jacob Pan | 2d281d8 | 2013-10-17 10:28:35 -0700 | [diff] [blame] | 917 | |
| 918 | value = (msr_val & TIME_UNIT_MASK) >> TIME_UNIT_OFFSET; |
Jacob Pan | 3c2c084 | 2014-11-07 09:29:26 -0800 | [diff] [blame] | 919 | rp->time_unit = 1000000 / (1 << value); |
Jacob Pan | 2d281d8 | 2013-10-17 10:28:35 -0700 | [diff] [blame] | 920 | |
Zhang Rui | 9ea7612 | 2019-05-13 13:58:53 -0400 | [diff] [blame] | 921 | pr_debug("Core CPU %s energy=%dpJ, time=%dus, power=%duW\n", |
| 922 | rp->name, rp->energy_unit, rp->time_unit, rp->power_unit); |
Jacob Pan | 2d281d8 | 2013-10-17 10:28:35 -0700 | [diff] [blame] | 923 | |
| 924 | return 0; |
| 925 | } |
| 926 | |
Jacob Pan | 3c2c084 | 2014-11-07 09:29:26 -0800 | [diff] [blame] | 927 | static int rapl_check_unit_atom(struct rapl_package *rp, int cpu) |
| 928 | { |
| 929 | u64 msr_val; |
| 930 | u32 value; |
| 931 | |
| 932 | if (rdmsrl_safe_on_cpu(cpu, MSR_RAPL_POWER_UNIT, &msr_val)) { |
| 933 | pr_err("Failed to read power unit MSR 0x%x on CPU %d, exit.\n", |
| 934 | MSR_RAPL_POWER_UNIT, cpu); |
| 935 | return -ENODEV; |
| 936 | } |
| 937 | value = (msr_val & ENERGY_UNIT_MASK) >> ENERGY_UNIT_OFFSET; |
Jacob Pan | d474a4d | 2015-03-13 03:48:56 -0700 | [diff] [blame] | 938 | rp->energy_unit = ENERGY_UNIT_SCALE * 1 << value; |
Jacob Pan | 3c2c084 | 2014-11-07 09:29:26 -0800 | [diff] [blame] | 939 | |
| 940 | value = (msr_val & POWER_UNIT_MASK) >> POWER_UNIT_OFFSET; |
| 941 | rp->power_unit = (1 << value) * 1000; |
| 942 | |
| 943 | value = (msr_val & TIME_UNIT_MASK) >> TIME_UNIT_OFFSET; |
| 944 | rp->time_unit = 1000000 / (1 << value); |
| 945 | |
Zhang Rui | 9ea7612 | 2019-05-13 13:58:53 -0400 | [diff] [blame] | 946 | pr_debug("Atom %s energy=%dpJ, time=%dus, power=%duW\n", |
| 947 | rp->name, rp->energy_unit, rp->time_unit, rp->power_unit); |
Jacob Pan | 3c2c084 | 2014-11-07 09:29:26 -0800 | [diff] [blame] | 948 | |
| 949 | return 0; |
| 950 | } |
| 951 | |
Jacob Pan | f14a139 | 2016-02-24 13:31:36 -0800 | [diff] [blame] | 952 | static void power_limit_irq_save_cpu(void *info) |
| 953 | { |
| 954 | u32 l, h = 0; |
| 955 | struct rapl_package *rp = (struct rapl_package *)info; |
| 956 | |
| 957 | /* save the state of PLN irq mask bit before disabling it */ |
| 958 | rdmsr_safe(MSR_IA32_PACKAGE_THERM_INTERRUPT, &l, &h); |
| 959 | if (!(rp->power_limit_irq & PACKAGE_PLN_INT_SAVED)) { |
| 960 | rp->power_limit_irq = l & PACKAGE_THERM_INT_PLN_ENABLE; |
| 961 | rp->power_limit_irq |= PACKAGE_PLN_INT_SAVED; |
| 962 | } |
| 963 | l &= ~PACKAGE_THERM_INT_PLN_ENABLE; |
| 964 | wrmsr_safe(MSR_IA32_PACKAGE_THERM_INTERRUPT, l, h); |
| 965 | } |
| 966 | |
Jacob Pan | 3c2c084 | 2014-11-07 09:29:26 -0800 | [diff] [blame] | 967 | |
Jacob Pan | 2d281d8 | 2013-10-17 10:28:35 -0700 | [diff] [blame] | 968 | /* REVISIT: |
| 969 | * When package power limit is set artificially low by RAPL, LVT |
| 970 | * thermal interrupt for package power limit should be ignored |
| 971 | * since we are not really exceeding the real limit. The intention |
| 972 | * is to avoid excessive interrupts while we are trying to save power. |
| 973 | * A useful feature might be routing the package_power_limit interrupt |
| 974 | * to userspace via eventfd. once we have a usecase, this is simple |
| 975 | * to do by adding an atomic notifier. |
| 976 | */ |
| 977 | |
Jacob Pan | 309557f | 2016-02-24 13:31:37 -0800 | [diff] [blame] | 978 | static void package_power_limit_irq_save(struct rapl_package *rp) |
Jacob Pan | 2d281d8 | 2013-10-17 10:28:35 -0700 | [diff] [blame] | 979 | { |
Jacob Pan | 2d281d8 | 2013-10-17 10:28:35 -0700 | [diff] [blame] | 980 | if (!boot_cpu_has(X86_FEATURE_PTS) || !boot_cpu_has(X86_FEATURE_PLN)) |
| 981 | return; |
| 982 | |
Jacob Pan | 323ee64 | 2016-02-24 13:31:38 -0800 | [diff] [blame] | 983 | smp_call_function_single(rp->lead_cpu, power_limit_irq_save_cpu, rp, 1); |
Jacob Pan | f14a139 | 2016-02-24 13:31:36 -0800 | [diff] [blame] | 984 | } |
| 985 | |
Thomas Gleixner | 5870506 | 2016-11-22 21:16:02 +0000 | [diff] [blame] | 986 | /* |
| 987 | * Restore per package power limit interrupt enable state. Called from cpu |
| 988 | * hotplug code on package removal. |
| 989 | */ |
| 990 | static void package_power_limit_irq_restore(struct rapl_package *rp) |
Jacob Pan | f14a139 | 2016-02-24 13:31:36 -0800 | [diff] [blame] | 991 | { |
Thomas Gleixner | 5870506 | 2016-11-22 21:16:02 +0000 | [diff] [blame] | 992 | u32 l, h; |
| 993 | |
| 994 | if (!boot_cpu_has(X86_FEATURE_PTS) || !boot_cpu_has(X86_FEATURE_PLN)) |
| 995 | return; |
| 996 | |
| 997 | /* irq enable state not saved, nothing to restore */ |
| 998 | if (!(rp->power_limit_irq & PACKAGE_PLN_INT_SAVED)) |
| 999 | return; |
Jacob Pan | f14a139 | 2016-02-24 13:31:36 -0800 | [diff] [blame] | 1000 | |
| 1001 | rdmsr_safe(MSR_IA32_PACKAGE_THERM_INTERRUPT, &l, &h); |
| 1002 | |
| 1003 | if (rp->power_limit_irq & PACKAGE_THERM_INT_PLN_ENABLE) |
| 1004 | l |= PACKAGE_THERM_INT_PLN_ENABLE; |
| 1005 | else |
| 1006 | l &= ~PACKAGE_THERM_INT_PLN_ENABLE; |
| 1007 | |
| 1008 | wrmsr_safe(MSR_IA32_PACKAGE_THERM_INTERRUPT, l, h); |
Jacob Pan | 2d281d8 | 2013-10-17 10:28:35 -0700 | [diff] [blame] | 1009 | } |
| 1010 | |
Jacob Pan | 3c2c084 | 2014-11-07 09:29:26 -0800 | [diff] [blame] | 1011 | static void set_floor_freq_default(struct rapl_domain *rd, bool mode) |
| 1012 | { |
| 1013 | int nr_powerlimit = find_nr_power_limit(rd); |
| 1014 | |
| 1015 | /* always enable clamp such that p-state can go below OS requested |
| 1016 | * range. power capping priority over guranteed frequency. |
| 1017 | */ |
| 1018 | rapl_write_data_raw(rd, PL1_CLAMP, mode); |
| 1019 | |
| 1020 | /* some domains have pl2 */ |
| 1021 | if (nr_powerlimit > 1) { |
| 1022 | rapl_write_data_raw(rd, PL2_ENABLE, mode); |
| 1023 | rapl_write_data_raw(rd, PL2_CLAMP, mode); |
| 1024 | } |
| 1025 | } |
| 1026 | |
| 1027 | static void set_floor_freq_atom(struct rapl_domain *rd, bool enable) |
| 1028 | { |
| 1029 | static u32 power_ctrl_orig_val; |
| 1030 | u32 mdata; |
| 1031 | |
Ajay Thomas | 51b6340 | 2015-04-30 01:43:23 +0530 | [diff] [blame] | 1032 | if (!rapl_defaults->floor_freq_reg_addr) { |
| 1033 | pr_err("Invalid floor frequency config register\n"); |
| 1034 | return; |
| 1035 | } |
| 1036 | |
Jacob Pan | 3c2c084 | 2014-11-07 09:29:26 -0800 | [diff] [blame] | 1037 | if (!power_ctrl_orig_val) |
Andy Shevchenko | 4077a38 | 2015-11-11 19:59:29 +0200 | [diff] [blame] | 1038 | iosf_mbi_read(BT_MBI_UNIT_PMC, MBI_CR_READ, |
| 1039 | rapl_defaults->floor_freq_reg_addr, |
| 1040 | &power_ctrl_orig_val); |
Jacob Pan | 3c2c084 | 2014-11-07 09:29:26 -0800 | [diff] [blame] | 1041 | mdata = power_ctrl_orig_val; |
| 1042 | if (enable) { |
| 1043 | mdata &= ~(0x7f << 8); |
| 1044 | mdata |= 1 << 8; |
| 1045 | } |
Andy Shevchenko | 4077a38 | 2015-11-11 19:59:29 +0200 | [diff] [blame] | 1046 | iosf_mbi_write(BT_MBI_UNIT_PMC, MBI_CR_WRITE, |
| 1047 | rapl_defaults->floor_freq_reg_addr, mdata); |
Jacob Pan | 3c2c084 | 2014-11-07 09:29:26 -0800 | [diff] [blame] | 1048 | } |
| 1049 | |
| 1050 | static u64 rapl_compute_time_window_core(struct rapl_package *rp, u64 value, |
| 1051 | bool to_raw) |
| 1052 | { |
| 1053 | u64 f, y; /* fraction and exp. used for time unit */ |
| 1054 | |
| 1055 | /* |
| 1056 | * Special processing based on 2^Y*(1+F/4), refer |
| 1057 | * to Intel Software Developer's manual Vol.3B: CH 14.9.3. |
| 1058 | */ |
| 1059 | if (!to_raw) { |
| 1060 | f = (value & 0x60) >> 5; |
| 1061 | y = value & 0x1f; |
| 1062 | value = (1 << y) * (4 + f) * rp->time_unit / 4; |
| 1063 | } else { |
| 1064 | do_div(value, rp->time_unit); |
| 1065 | y = ilog2(value); |
| 1066 | f = div64_u64(4 * (value - (1 << y)), 1 << y); |
| 1067 | value = (y & 0x1f) | ((f & 0x3) << 5); |
| 1068 | } |
| 1069 | return value; |
| 1070 | } |
| 1071 | |
| 1072 | static u64 rapl_compute_time_window_atom(struct rapl_package *rp, u64 value, |
| 1073 | bool to_raw) |
| 1074 | { |
| 1075 | /* |
| 1076 | * Atom time unit encoding is straight forward val * time_unit, |
| 1077 | * where time_unit is default to 1 sec. Never 0. |
| 1078 | */ |
| 1079 | if (!to_raw) |
| 1080 | return (value) ? value *= rp->time_unit : rp->time_unit; |
| 1081 | else |
| 1082 | value = div64_u64(value, rp->time_unit); |
| 1083 | |
| 1084 | return value; |
| 1085 | } |
| 1086 | |
Jacob Pan | 087e9cb | 2014-11-07 09:29:25 -0800 | [diff] [blame] | 1087 | static const struct rapl_defaults rapl_defaults_core = { |
Ajay Thomas | 51b6340 | 2015-04-30 01:43:23 +0530 | [diff] [blame] | 1088 | .floor_freq_reg_addr = 0, |
Jacob Pan | 3c2c084 | 2014-11-07 09:29:26 -0800 | [diff] [blame] | 1089 | .check_unit = rapl_check_unit_core, |
| 1090 | .set_floor_freq = set_floor_freq_default, |
| 1091 | .compute_time_window = rapl_compute_time_window_core, |
Jacob Pan | 087e9cb | 2014-11-07 09:29:25 -0800 | [diff] [blame] | 1092 | }; |
| 1093 | |
Jacob Pan | d474a4d | 2015-03-13 03:48:56 -0700 | [diff] [blame] | 1094 | static const struct rapl_defaults rapl_defaults_hsw_server = { |
| 1095 | .check_unit = rapl_check_unit_core, |
| 1096 | .set_floor_freq = set_floor_freq_default, |
| 1097 | .compute_time_window = rapl_compute_time_window_core, |
| 1098 | .dram_domain_energy_unit = 15300, |
| 1099 | }; |
| 1100 | |
Ajay Thomas | 51b6340 | 2015-04-30 01:43:23 +0530 | [diff] [blame] | 1101 | static const struct rapl_defaults rapl_defaults_byt = { |
| 1102 | .floor_freq_reg_addr = IOSF_CPU_POWER_BUDGET_CTL_BYT, |
Jacob Pan | 3c2c084 | 2014-11-07 09:29:26 -0800 | [diff] [blame] | 1103 | .check_unit = rapl_check_unit_atom, |
| 1104 | .set_floor_freq = set_floor_freq_atom, |
| 1105 | .compute_time_window = rapl_compute_time_window_atom, |
Jacob Pan | 087e9cb | 2014-11-07 09:29:25 -0800 | [diff] [blame] | 1106 | }; |
| 1107 | |
Ajay Thomas | 51b6340 | 2015-04-30 01:43:23 +0530 | [diff] [blame] | 1108 | static const struct rapl_defaults rapl_defaults_tng = { |
| 1109 | .floor_freq_reg_addr = IOSF_CPU_POWER_BUDGET_CTL_TNG, |
| 1110 | .check_unit = rapl_check_unit_atom, |
| 1111 | .set_floor_freq = set_floor_freq_atom, |
| 1112 | .compute_time_window = rapl_compute_time_window_atom, |
| 1113 | }; |
| 1114 | |
| 1115 | static const struct rapl_defaults rapl_defaults_ann = { |
| 1116 | .floor_freq_reg_addr = 0, |
| 1117 | .check_unit = rapl_check_unit_atom, |
| 1118 | .set_floor_freq = NULL, |
| 1119 | .compute_time_window = rapl_compute_time_window_atom, |
| 1120 | }; |
| 1121 | |
| 1122 | static const struct rapl_defaults rapl_defaults_cht = { |
| 1123 | .floor_freq_reg_addr = 0, |
| 1124 | .check_unit = rapl_check_unit_atom, |
| 1125 | .set_floor_freq = NULL, |
| 1126 | .compute_time_window = rapl_compute_time_window_atom, |
| 1127 | }; |
| 1128 | |
Mathias Krause | ea85dbc | 2015-03-25 22:15:52 +0100 | [diff] [blame] | 1129 | static const struct x86_cpu_id rapl_ids[] __initconst = { |
Andy Shevchenko | 17ed151 | 2018-08-31 11:25:13 +0300 | [diff] [blame] | 1130 | INTEL_CPU_FAM6(SANDYBRIDGE, rapl_defaults_core), |
| 1131 | INTEL_CPU_FAM6(SANDYBRIDGE_X, rapl_defaults_core), |
Dave Hansen | 0bb04b5 | 2016-06-02 17:19:37 -0700 | [diff] [blame] | 1132 | |
Andy Shevchenko | 17ed151 | 2018-08-31 11:25:13 +0300 | [diff] [blame] | 1133 | INTEL_CPU_FAM6(IVYBRIDGE, rapl_defaults_core), |
| 1134 | INTEL_CPU_FAM6(IVYBRIDGE_X, rapl_defaults_core), |
Dave Hansen | 0bb04b5 | 2016-06-02 17:19:37 -0700 | [diff] [blame] | 1135 | |
Andy Shevchenko | 17ed151 | 2018-08-31 11:25:13 +0300 | [diff] [blame] | 1136 | INTEL_CPU_FAM6(HASWELL_CORE, rapl_defaults_core), |
| 1137 | INTEL_CPU_FAM6(HASWELL_ULT, rapl_defaults_core), |
| 1138 | INTEL_CPU_FAM6(HASWELL_GT3E, rapl_defaults_core), |
| 1139 | INTEL_CPU_FAM6(HASWELL_X, rapl_defaults_hsw_server), |
Dave Hansen | 0bb04b5 | 2016-06-02 17:19:37 -0700 | [diff] [blame] | 1140 | |
Andy Shevchenko | 17ed151 | 2018-08-31 11:25:13 +0300 | [diff] [blame] | 1141 | INTEL_CPU_FAM6(BROADWELL_CORE, rapl_defaults_core), |
| 1142 | INTEL_CPU_FAM6(BROADWELL_GT3E, rapl_defaults_core), |
| 1143 | INTEL_CPU_FAM6(BROADWELL_XEON_D, rapl_defaults_core), |
| 1144 | INTEL_CPU_FAM6(BROADWELL_X, rapl_defaults_hsw_server), |
Dave Hansen | 0bb04b5 | 2016-06-02 17:19:37 -0700 | [diff] [blame] | 1145 | |
Andy Shevchenko | 17ed151 | 2018-08-31 11:25:13 +0300 | [diff] [blame] | 1146 | INTEL_CPU_FAM6(SKYLAKE_DESKTOP, rapl_defaults_core), |
| 1147 | INTEL_CPU_FAM6(SKYLAKE_MOBILE, rapl_defaults_core), |
| 1148 | INTEL_CPU_FAM6(SKYLAKE_X, rapl_defaults_hsw_server), |
| 1149 | INTEL_CPU_FAM6(KABYLAKE_MOBILE, rapl_defaults_core), |
| 1150 | INTEL_CPU_FAM6(KABYLAKE_DESKTOP, rapl_defaults_core), |
| 1151 | INTEL_CPU_FAM6(CANNONLAKE_MOBILE, rapl_defaults_core), |
Gayatri Kammela | ba6f3ec | 2019-02-18 15:01:02 +0800 | [diff] [blame] | 1152 | INTEL_CPU_FAM6(ICELAKE_MOBILE, rapl_defaults_core), |
Dave Hansen | 0bb04b5 | 2016-06-02 17:19:37 -0700 | [diff] [blame] | 1153 | |
Linus Torvalds | c05f364 | 2018-10-23 13:32:18 +0100 | [diff] [blame] | 1154 | INTEL_CPU_FAM6(ATOM_SILVERMONT, rapl_defaults_byt), |
Andy Shevchenko | 17ed151 | 2018-08-31 11:25:13 +0300 | [diff] [blame] | 1155 | INTEL_CPU_FAM6(ATOM_AIRMONT, rapl_defaults_cht), |
Linus Torvalds | c05f364 | 2018-10-23 13:32:18 +0100 | [diff] [blame] | 1156 | INTEL_CPU_FAM6(ATOM_SILVERMONT_MID, rapl_defaults_tng), |
| 1157 | INTEL_CPU_FAM6(ATOM_AIRMONT_MID, rapl_defaults_ann), |
Andy Shevchenko | 17ed151 | 2018-08-31 11:25:13 +0300 | [diff] [blame] | 1158 | INTEL_CPU_FAM6(ATOM_GOLDMONT, rapl_defaults_core), |
Linus Torvalds | c05f364 | 2018-10-23 13:32:18 +0100 | [diff] [blame] | 1159 | INTEL_CPU_FAM6(ATOM_GOLDMONT_PLUS, rapl_defaults_core), |
| 1160 | INTEL_CPU_FAM6(ATOM_GOLDMONT_X, rapl_defaults_core), |
Zhang Rui | df7f8e0 | 2019-02-12 10:47:10 +0800 | [diff] [blame] | 1161 | INTEL_CPU_FAM6(ATOM_TREMONT_X, rapl_defaults_core), |
Dave Hansen | 0bb04b5 | 2016-06-02 17:19:37 -0700 | [diff] [blame] | 1162 | |
Andy Shevchenko | 17ed151 | 2018-08-31 11:25:13 +0300 | [diff] [blame] | 1163 | INTEL_CPU_FAM6(XEON_PHI_KNL, rapl_defaults_hsw_server), |
| 1164 | INTEL_CPU_FAM6(XEON_PHI_KNM, rapl_defaults_hsw_server), |
Jacob Pan | 2d281d8 | 2013-10-17 10:28:35 -0700 | [diff] [blame] | 1165 | {} |
| 1166 | }; |
| 1167 | MODULE_DEVICE_TABLE(x86cpu, rapl_ids); |
| 1168 | |
Thomas Gleixner | bed5ab6 | 2016-11-22 21:15:58 +0000 | [diff] [blame] | 1169 | /* Read once for all raw primitive data for domains */ |
| 1170 | static void rapl_update_domain_data(struct rapl_package *rp) |
Jacob Pan | 2d281d8 | 2013-10-17 10:28:35 -0700 | [diff] [blame] | 1171 | { |
| 1172 | int dmn, prim; |
| 1173 | u64 val; |
Jacob Pan | 2d281d8 | 2013-10-17 10:28:35 -0700 | [diff] [blame] | 1174 | |
Thomas Gleixner | bed5ab6 | 2016-11-22 21:15:58 +0000 | [diff] [blame] | 1175 | for (dmn = 0; dmn < rp->nr_domains; dmn++) { |
Zhang Rui | 9ea7612 | 2019-05-13 13:58:53 -0400 | [diff] [blame] | 1176 | pr_debug("update %s domain %s data\n", rp->name, |
Thomas Gleixner | bed5ab6 | 2016-11-22 21:15:58 +0000 | [diff] [blame] | 1177 | rp->domains[dmn].name); |
| 1178 | /* exclude non-raw primitives */ |
| 1179 | for (prim = 0; prim < NR_RAW_PRIMITIVES; prim++) { |
| 1180 | if (!rapl_read_data_raw(&rp->domains[dmn], prim, |
| 1181 | rpi[prim].unit, &val)) |
| 1182 | rp->domains[dmn].rdd.primitives[prim] = val; |
Jacob Pan | 2d281d8 | 2013-10-17 10:28:35 -0700 | [diff] [blame] | 1183 | } |
| 1184 | } |
| 1185 | |
| 1186 | } |
| 1187 | |
Thomas Gleixner | 5870506 | 2016-11-22 21:16:02 +0000 | [diff] [blame] | 1188 | static void rapl_unregister_powercap(void) |
Jacob Pan | 2d281d8 | 2013-10-17 10:28:35 -0700 | [diff] [blame] | 1189 | { |
Srinivas Pandruvada | 3521ba1 | 2016-04-17 15:03:01 -0700 | [diff] [blame] | 1190 | if (platform_rapl_domain) { |
| 1191 | powercap_unregister_zone(control_type, |
| 1192 | &platform_rapl_domain->power_zone); |
| 1193 | kfree(platform_rapl_domain); |
| 1194 | } |
Jacob Pan | 2d281d8 | 2013-10-17 10:28:35 -0700 | [diff] [blame] | 1195 | powercap_unregister_control_type(control_type); |
Jacob Pan | 2d281d8 | 2013-10-17 10:28:35 -0700 | [diff] [blame] | 1196 | } |
| 1197 | |
| 1198 | static int rapl_package_register_powercap(struct rapl_package *rp) |
| 1199 | { |
| 1200 | struct rapl_domain *rd; |
Jacob Pan | 2d281d8 | 2013-10-17 10:28:35 -0700 | [diff] [blame] | 1201 | struct powercap_zone *power_zone = NULL; |
Luis de Bethencourt | 01857cf | 2018-01-17 10:30:34 +0000 | [diff] [blame] | 1202 | int nr_pl, ret; |
Thomas Gleixner | bed5ab6 | 2016-11-22 21:15:58 +0000 | [diff] [blame] | 1203 | |
| 1204 | /* Update the domain data of the new package */ |
| 1205 | rapl_update_domain_data(rp); |
Jacob Pan | 2d281d8 | 2013-10-17 10:28:35 -0700 | [diff] [blame] | 1206 | |
| 1207 | /* first we register package domain as the parent zone*/ |
| 1208 | for (rd = rp->domains; rd < rp->domains + rp->nr_domains; rd++) { |
| 1209 | if (rd->id == RAPL_DOMAIN_PACKAGE) { |
| 1210 | nr_pl = find_nr_power_limit(rd); |
Zhang Rui | 9ea7612 | 2019-05-13 13:58:53 -0400 | [diff] [blame] | 1211 | pr_debug("register package domain %s\n", rp->name); |
Jacob Pan | 2d281d8 | 2013-10-17 10:28:35 -0700 | [diff] [blame] | 1212 | power_zone = powercap_register_zone(&rd->power_zone, |
| 1213 | control_type, |
Zhang Rui | 9ea7612 | 2019-05-13 13:58:53 -0400 | [diff] [blame] | 1214 | rp->name, NULL, |
Jacob Pan | 2d281d8 | 2013-10-17 10:28:35 -0700 | [diff] [blame] | 1215 | &zone_ops[rd->id], |
| 1216 | nr_pl, |
| 1217 | &constraint_ops); |
| 1218 | if (IS_ERR(power_zone)) { |
Zhang Rui | 9ea7612 | 2019-05-13 13:58:53 -0400 | [diff] [blame] | 1219 | pr_debug("failed to register power zone %s\n", |
| 1220 | rp->name); |
Thomas Gleixner | bed5ab6 | 2016-11-22 21:15:58 +0000 | [diff] [blame] | 1221 | return PTR_ERR(power_zone); |
Jacob Pan | 2d281d8 | 2013-10-17 10:28:35 -0700 | [diff] [blame] | 1222 | } |
| 1223 | /* track parent zone in per package/socket data */ |
| 1224 | rp->power_zone = power_zone; |
| 1225 | /* done, only one package domain per socket */ |
| 1226 | break; |
| 1227 | } |
| 1228 | } |
| 1229 | if (!power_zone) { |
| 1230 | pr_err("no package domain found, unknown topology!\n"); |
Thomas Gleixner | bed5ab6 | 2016-11-22 21:15:58 +0000 | [diff] [blame] | 1231 | return -ENODEV; |
Jacob Pan | 2d281d8 | 2013-10-17 10:28:35 -0700 | [diff] [blame] | 1232 | } |
| 1233 | /* now register domains as children of the socket/package*/ |
| 1234 | for (rd = rp->domains; rd < rp->domains + rp->nr_domains; rd++) { |
| 1235 | if (rd->id == RAPL_DOMAIN_PACKAGE) |
| 1236 | continue; |
| 1237 | /* number of power limits per domain varies */ |
| 1238 | nr_pl = find_nr_power_limit(rd); |
| 1239 | power_zone = powercap_register_zone(&rd->power_zone, |
| 1240 | control_type, rd->name, |
| 1241 | rp->power_zone, |
| 1242 | &zone_ops[rd->id], nr_pl, |
| 1243 | &constraint_ops); |
| 1244 | |
| 1245 | if (IS_ERR(power_zone)) { |
Zhang Rui | 9ea7612 | 2019-05-13 13:58:53 -0400 | [diff] [blame] | 1246 | pr_debug("failed to register power_zone, %s:%s\n", |
| 1247 | rp->name, rd->name); |
Jacob Pan | 2d281d8 | 2013-10-17 10:28:35 -0700 | [diff] [blame] | 1248 | ret = PTR_ERR(power_zone); |
| 1249 | goto err_cleanup; |
| 1250 | } |
| 1251 | } |
Thomas Gleixner | bed5ab6 | 2016-11-22 21:15:58 +0000 | [diff] [blame] | 1252 | return 0; |
Jacob Pan | 2d281d8 | 2013-10-17 10:28:35 -0700 | [diff] [blame] | 1253 | |
Jacob Pan | 2d281d8 | 2013-10-17 10:28:35 -0700 | [diff] [blame] | 1254 | err_cleanup: |
Thomas Gleixner | 5870506 | 2016-11-22 21:16:02 +0000 | [diff] [blame] | 1255 | /* |
| 1256 | * Clean up previously initialized domains within the package if we |
Jacob Pan | 2d281d8 | 2013-10-17 10:28:35 -0700 | [diff] [blame] | 1257 | * failed after the first domain setup. |
| 1258 | */ |
| 1259 | while (--rd >= rp->domains) { |
Zhang Rui | 9ea7612 | 2019-05-13 13:58:53 -0400 | [diff] [blame] | 1260 | pr_debug("unregister %s domain %s\n", rp->name, rd->name); |
Jacob Pan | 2d281d8 | 2013-10-17 10:28:35 -0700 | [diff] [blame] | 1261 | powercap_unregister_zone(control_type, &rd->power_zone); |
| 1262 | } |
| 1263 | |
| 1264 | return ret; |
| 1265 | } |
| 1266 | |
Thomas Gleixner | 5870506 | 2016-11-22 21:16:02 +0000 | [diff] [blame] | 1267 | static int __init rapl_register_psys(void) |
Srinivas Pandruvada | 3521ba1 | 2016-04-17 15:03:01 -0700 | [diff] [blame] | 1268 | { |
| 1269 | struct rapl_domain *rd; |
| 1270 | struct powercap_zone *power_zone; |
| 1271 | u64 val; |
| 1272 | |
| 1273 | if (rdmsrl_safe_on_cpu(0, MSR_PLATFORM_ENERGY_STATUS, &val) || !val) |
| 1274 | return -ENODEV; |
| 1275 | |
| 1276 | if (rdmsrl_safe_on_cpu(0, MSR_PLATFORM_POWER_LIMIT, &val) || !val) |
| 1277 | return -ENODEV; |
| 1278 | |
| 1279 | rd = kzalloc(sizeof(*rd), GFP_KERNEL); |
| 1280 | if (!rd) |
| 1281 | return -ENOMEM; |
| 1282 | |
| 1283 | rd->name = rapl_domain_names[RAPL_DOMAIN_PLATFORM]; |
| 1284 | rd->id = RAPL_DOMAIN_PLATFORM; |
Zhang Rui | f7c4e0c | 2019-07-10 21:44:22 +0800 | [diff] [blame^] | 1285 | rd->regs[0] = MSR_PLATFORM_POWER_LIMIT; |
| 1286 | rd->regs[1] = MSR_PLATFORM_ENERGY_STATUS; |
Srinivas Pandruvada | 3521ba1 | 2016-04-17 15:03:01 -0700 | [diff] [blame] | 1287 | rd->rpl[0].prim_id = PL1_ENABLE; |
| 1288 | rd->rpl[0].name = pl1_name; |
| 1289 | rd->rpl[1].prim_id = PL2_ENABLE; |
| 1290 | rd->rpl[1].name = pl2_name; |
Zhang Rui | aadf7b3 | 2019-05-13 13:58:50 -0400 | [diff] [blame] | 1291 | rd->rp = rapl_find_package_domain(0); |
Srinivas Pandruvada | 3521ba1 | 2016-04-17 15:03:01 -0700 | [diff] [blame] | 1292 | |
| 1293 | power_zone = powercap_register_zone(&rd->power_zone, control_type, |
| 1294 | "psys", NULL, |
| 1295 | &zone_ops[RAPL_DOMAIN_PLATFORM], |
| 1296 | 2, &constraint_ops); |
| 1297 | |
| 1298 | if (IS_ERR(power_zone)) { |
| 1299 | kfree(rd); |
| 1300 | return PTR_ERR(power_zone); |
| 1301 | } |
| 1302 | |
| 1303 | platform_rapl_domain = rd; |
| 1304 | |
| 1305 | return 0; |
| 1306 | } |
| 1307 | |
Thomas Gleixner | 5870506 | 2016-11-22 21:16:02 +0000 | [diff] [blame] | 1308 | static int __init rapl_register_powercap(void) |
Jacob Pan | 2d281d8 | 2013-10-17 10:28:35 -0700 | [diff] [blame] | 1309 | { |
Jacob Pan | 2d281d8 | 2013-10-17 10:28:35 -0700 | [diff] [blame] | 1310 | control_type = powercap_register_control_type(NULL, "intel-rapl", NULL); |
| 1311 | if (IS_ERR(control_type)) { |
| 1312 | pr_debug("failed to register powercap control_type.\n"); |
| 1313 | return PTR_ERR(control_type); |
| 1314 | } |
Thomas Gleixner | 5870506 | 2016-11-22 21:16:02 +0000 | [diff] [blame] | 1315 | return 0; |
Jacob Pan | 2d281d8 | 2013-10-17 10:28:35 -0700 | [diff] [blame] | 1316 | } |
| 1317 | |
| 1318 | static int rapl_check_domain(int cpu, int domain) |
| 1319 | { |
| 1320 | unsigned msr; |
Jacob Pan | 9d31c67 | 2014-04-29 15:33:06 -0700 | [diff] [blame] | 1321 | u64 val = 0; |
Jacob Pan | 2d281d8 | 2013-10-17 10:28:35 -0700 | [diff] [blame] | 1322 | |
| 1323 | switch (domain) { |
| 1324 | case RAPL_DOMAIN_PACKAGE: |
| 1325 | msr = MSR_PKG_ENERGY_STATUS; |
| 1326 | break; |
| 1327 | case RAPL_DOMAIN_PP0: |
| 1328 | msr = MSR_PP0_ENERGY_STATUS; |
| 1329 | break; |
| 1330 | case RAPL_DOMAIN_PP1: |
| 1331 | msr = MSR_PP1_ENERGY_STATUS; |
| 1332 | break; |
| 1333 | case RAPL_DOMAIN_DRAM: |
| 1334 | msr = MSR_DRAM_ENERGY_STATUS; |
| 1335 | break; |
Srinivas Pandruvada | 3521ba1 | 2016-04-17 15:03:01 -0700 | [diff] [blame] | 1336 | case RAPL_DOMAIN_PLATFORM: |
| 1337 | /* PSYS(PLATFORM) is not a CPU domain, so avoid printng error */ |
| 1338 | return -EINVAL; |
Jacob Pan | 2d281d8 | 2013-10-17 10:28:35 -0700 | [diff] [blame] | 1339 | default: |
| 1340 | pr_err("invalid domain id %d\n", domain); |
| 1341 | return -EINVAL; |
| 1342 | } |
Jacob Pan | 9d31c67 | 2014-04-29 15:33:06 -0700 | [diff] [blame] | 1343 | /* make sure domain counters are available and contains non-zero |
| 1344 | * values, otherwise skip it. |
| 1345 | */ |
| 1346 | if (rdmsrl_safe_on_cpu(cpu, msr, &val) || !val) |
Jacob Pan | 2d281d8 | 2013-10-17 10:28:35 -0700 | [diff] [blame] | 1347 | return -ENODEV; |
| 1348 | |
Jacob Pan | 9d31c67 | 2014-04-29 15:33:06 -0700 | [diff] [blame] | 1349 | return 0; |
Jacob Pan | 2d281d8 | 2013-10-17 10:28:35 -0700 | [diff] [blame] | 1350 | } |
| 1351 | |
Jacob Pan | e1399ba | 2016-05-31 13:41:29 -0700 | [diff] [blame] | 1352 | |
| 1353 | /* |
| 1354 | * Check if power limits are available. Two cases when they are not available: |
| 1355 | * 1. Locked by BIOS, in this case we still provide read-only access so that |
| 1356 | * users can see what limit is set by the BIOS. |
| 1357 | * 2. Some CPUs make some domains monitoring only which means PLx MSRs may not |
| 1358 | * exist at all. In this case, we do not show the contraints in powercap. |
| 1359 | * |
| 1360 | * Called after domains are detected and initialized. |
| 1361 | */ |
| 1362 | static void rapl_detect_powerlimit(struct rapl_domain *rd) |
| 1363 | { |
| 1364 | u64 val64; |
| 1365 | int i; |
| 1366 | |
| 1367 | /* check if the domain is locked by BIOS, ignore if MSR doesn't exist */ |
| 1368 | if (!rapl_read_data_raw(rd, FW_LOCK, false, &val64)) { |
| 1369 | if (val64) { |
Zhang Rui | 9ea7612 | 2019-05-13 13:58:53 -0400 | [diff] [blame] | 1370 | pr_info("RAPL %s domain %s locked by BIOS\n", |
| 1371 | rd->rp->name, rd->name); |
Jacob Pan | e1399ba | 2016-05-31 13:41:29 -0700 | [diff] [blame] | 1372 | rd->state |= DOMAIN_STATE_BIOS_LOCKED; |
| 1373 | } |
| 1374 | } |
| 1375 | /* check if power limit MSRs exists, otherwise domain is monitoring only */ |
| 1376 | for (i = 0; i < NR_POWER_LIMITS; i++) { |
| 1377 | int prim = rd->rpl[i].prim_id; |
| 1378 | if (rapl_read_data_raw(rd, prim, false, &val64)) |
| 1379 | rd->rpl[i].name = NULL; |
| 1380 | } |
| 1381 | } |
| 1382 | |
Jacob Pan | 2d281d8 | 2013-10-17 10:28:35 -0700 | [diff] [blame] | 1383 | /* Detect active and valid domains for the given CPU, caller must |
| 1384 | * ensure the CPU belongs to the targeted package and CPU hotlug is disabled. |
| 1385 | */ |
| 1386 | static int rapl_detect_domains(struct rapl_package *rp, int cpu) |
| 1387 | { |
Jacob Pan | 2d281d8 | 2013-10-17 10:28:35 -0700 | [diff] [blame] | 1388 | struct rapl_domain *rd; |
Thomas Gleixner | 5870506 | 2016-11-22 21:16:02 +0000 | [diff] [blame] | 1389 | int i; |
Jacob Pan | 2d281d8 | 2013-10-17 10:28:35 -0700 | [diff] [blame] | 1390 | |
| 1391 | for (i = 0; i < RAPL_DOMAIN_MAX; i++) { |
| 1392 | /* use physical package id to read counters */ |
Jacob Pan | fcdf179 | 2014-09-02 02:55:21 -0700 | [diff] [blame] | 1393 | if (!rapl_check_domain(cpu, i)) { |
Jacob Pan | 2d281d8 | 2013-10-17 10:28:35 -0700 | [diff] [blame] | 1394 | rp->domain_map |= 1 << i; |
Jacob Pan | fcdf179 | 2014-09-02 02:55:21 -0700 | [diff] [blame] | 1395 | pr_info("Found RAPL domain %s\n", rapl_domain_names[i]); |
| 1396 | } |
Jacob Pan | 2d281d8 | 2013-10-17 10:28:35 -0700 | [diff] [blame] | 1397 | } |
| 1398 | rp->nr_domains = bitmap_weight(&rp->domain_map, RAPL_DOMAIN_MAX); |
| 1399 | if (!rp->nr_domains) { |
Zhang Rui | 9ea7612 | 2019-05-13 13:58:53 -0400 | [diff] [blame] | 1400 | pr_debug("no valid rapl domains found in %s\n", rp->name); |
Thomas Gleixner | 5870506 | 2016-11-22 21:16:02 +0000 | [diff] [blame] | 1401 | return -ENODEV; |
Jacob Pan | 2d281d8 | 2013-10-17 10:28:35 -0700 | [diff] [blame] | 1402 | } |
Zhang Rui | 9ea7612 | 2019-05-13 13:58:53 -0400 | [diff] [blame] | 1403 | pr_debug("found %d domains on %s\n", rp->nr_domains, rp->name); |
Jacob Pan | 2d281d8 | 2013-10-17 10:28:35 -0700 | [diff] [blame] | 1404 | |
| 1405 | rp->domains = kcalloc(rp->nr_domains + 1, sizeof(struct rapl_domain), |
| 1406 | GFP_KERNEL); |
Thomas Gleixner | 5870506 | 2016-11-22 21:16:02 +0000 | [diff] [blame] | 1407 | if (!rp->domains) |
| 1408 | return -ENOMEM; |
| 1409 | |
Jacob Pan | 2d281d8 | 2013-10-17 10:28:35 -0700 | [diff] [blame] | 1410 | rapl_init_domains(rp); |
| 1411 | |
Jacob Pan | e1399ba | 2016-05-31 13:41:29 -0700 | [diff] [blame] | 1412 | for (rd = rp->domains; rd < rp->domains + rp->nr_domains; rd++) |
| 1413 | rapl_detect_powerlimit(rd); |
| 1414 | |
Jacob Pan | 2d281d8 | 2013-10-17 10:28:35 -0700 | [diff] [blame] | 1415 | return 0; |
| 1416 | } |
| 1417 | |
| 1418 | /* called from CPU hotplug notifier, hotplug lock held */ |
| 1419 | static void rapl_remove_package(struct rapl_package *rp) |
| 1420 | { |
| 1421 | struct rapl_domain *rd, *rd_package = NULL; |
| 1422 | |
Thomas Gleixner | 5870506 | 2016-11-22 21:16:02 +0000 | [diff] [blame] | 1423 | package_power_limit_irq_restore(rp); |
| 1424 | |
Jacob Pan | 2d281d8 | 2013-10-17 10:28:35 -0700 | [diff] [blame] | 1425 | for (rd = rp->domains; rd < rp->domains + rp->nr_domains; rd++) { |
Thomas Gleixner | 5870506 | 2016-11-22 21:16:02 +0000 | [diff] [blame] | 1426 | rapl_write_data_raw(rd, PL1_ENABLE, 0); |
| 1427 | rapl_write_data_raw(rd, PL1_CLAMP, 0); |
| 1428 | if (find_nr_power_limit(rd) > 1) { |
| 1429 | rapl_write_data_raw(rd, PL2_ENABLE, 0); |
| 1430 | rapl_write_data_raw(rd, PL2_CLAMP, 0); |
| 1431 | } |
Jacob Pan | 2d281d8 | 2013-10-17 10:28:35 -0700 | [diff] [blame] | 1432 | if (rd->id == RAPL_DOMAIN_PACKAGE) { |
| 1433 | rd_package = rd; |
| 1434 | continue; |
| 1435 | } |
Zhang Rui | 9ea7612 | 2019-05-13 13:58:53 -0400 | [diff] [blame] | 1436 | pr_debug("remove package, undo power limit on %s: %s\n", |
| 1437 | rp->name, rd->name); |
Jacob Pan | 2d281d8 | 2013-10-17 10:28:35 -0700 | [diff] [blame] | 1438 | powercap_unregister_zone(control_type, &rd->power_zone); |
| 1439 | } |
| 1440 | /* do parent zone last */ |
| 1441 | powercap_unregister_zone(control_type, &rd_package->power_zone); |
| 1442 | list_del(&rp->plist); |
| 1443 | kfree(rp); |
| 1444 | } |
| 1445 | |
| 1446 | /* called from CPU hotplug notifier, hotplug lock held */ |
Zhang Rui | aadf7b3 | 2019-05-13 13:58:50 -0400 | [diff] [blame] | 1447 | static struct rapl_package *rapl_add_package(int cpu) |
Jacob Pan | 2d281d8 | 2013-10-17 10:28:35 -0700 | [diff] [blame] | 1448 | { |
Zhang Rui | 32fb480 | 2019-05-13 13:58:51 -0400 | [diff] [blame] | 1449 | int id = topology_logical_die_id(cpu); |
Jacob Pan | 2d281d8 | 2013-10-17 10:28:35 -0700 | [diff] [blame] | 1450 | struct rapl_package *rp; |
Zhang Rui | 9ea7612 | 2019-05-13 13:58:53 -0400 | [diff] [blame] | 1451 | struct cpuinfo_x86 *c = &cpu_data(cpu); |
Thomas Gleixner | b4005e9 | 2016-11-22 21:16:05 +0000 | [diff] [blame] | 1452 | int ret; |
Jacob Pan | 2d281d8 | 2013-10-17 10:28:35 -0700 | [diff] [blame] | 1453 | |
Jacob Pan | 2d281d8 | 2013-10-17 10:28:35 -0700 | [diff] [blame] | 1454 | rp = kzalloc(sizeof(struct rapl_package), GFP_KERNEL); |
| 1455 | if (!rp) |
Thomas Gleixner | b4005e9 | 2016-11-22 21:16:05 +0000 | [diff] [blame] | 1456 | return ERR_PTR(-ENOMEM); |
Jacob Pan | 2d281d8 | 2013-10-17 10:28:35 -0700 | [diff] [blame] | 1457 | |
| 1458 | /* add the new package to the list */ |
Zhang Rui | aadf7b3 | 2019-05-13 13:58:50 -0400 | [diff] [blame] | 1459 | rp->id = id; |
Jacob Pan | 323ee64 | 2016-02-24 13:31:38 -0800 | [diff] [blame] | 1460 | rp->lead_cpu = cpu; |
| 1461 | |
Zhang Rui | 9ea7612 | 2019-05-13 13:58:53 -0400 | [diff] [blame] | 1462 | if (topology_max_die_per_package() > 1) |
| 1463 | snprintf(rp->name, PACKAGE_DOMAIN_NAME_LENGTH, |
| 1464 | "package-%d-die-%d", c->phys_proc_id, c->cpu_die_id); |
| 1465 | else |
| 1466 | snprintf(rp->name, PACKAGE_DOMAIN_NAME_LENGTH, "package-%d", |
| 1467 | c->phys_proc_id); |
| 1468 | |
Jacob Pan | 2d281d8 | 2013-10-17 10:28:35 -0700 | [diff] [blame] | 1469 | /* check if the package contains valid domains */ |
| 1470 | if (rapl_detect_domains(rp, cpu) || |
Jacob Pan | 3c2c084 | 2014-11-07 09:29:26 -0800 | [diff] [blame] | 1471 | rapl_defaults->check_unit(rp, cpu)) { |
Jacob Pan | 2d281d8 | 2013-10-17 10:28:35 -0700 | [diff] [blame] | 1472 | ret = -ENODEV; |
| 1473 | goto err_free_package; |
| 1474 | } |
Thomas Gleixner | a74f436 | 2016-11-22 21:15:59 +0000 | [diff] [blame] | 1475 | ret = rapl_package_register_powercap(rp); |
| 1476 | if (!ret) { |
Jacob Pan | 2d281d8 | 2013-10-17 10:28:35 -0700 | [diff] [blame] | 1477 | INIT_LIST_HEAD(&rp->plist); |
| 1478 | list_add(&rp->plist, &rapl_packages); |
Thomas Gleixner | b4005e9 | 2016-11-22 21:16:05 +0000 | [diff] [blame] | 1479 | return rp; |
Jacob Pan | 2d281d8 | 2013-10-17 10:28:35 -0700 | [diff] [blame] | 1480 | } |
| 1481 | |
| 1482 | err_free_package: |
| 1483 | kfree(rp->domains); |
| 1484 | kfree(rp); |
Thomas Gleixner | b4005e9 | 2016-11-22 21:16:05 +0000 | [diff] [blame] | 1485 | return ERR_PTR(ret); |
Jacob Pan | 2d281d8 | 2013-10-17 10:28:35 -0700 | [diff] [blame] | 1486 | } |
| 1487 | |
| 1488 | /* Handles CPU hotplug on multi-socket systems. |
| 1489 | * If a CPU goes online as the first CPU of the physical package |
| 1490 | * we add the RAPL package to the system. Similarly, when the last |
| 1491 | * CPU of the package is removed, we remove the RAPL package and its |
| 1492 | * associated domains. Cooling devices are handled accordingly at |
| 1493 | * per-domain level. |
| 1494 | */ |
Sebastian Andrzej Siewior | 5e4dc79 | 2016-11-22 21:16:00 +0000 | [diff] [blame] | 1495 | static int rapl_cpu_online(unsigned int cpu) |
Jacob Pan | 2d281d8 | 2013-10-17 10:28:35 -0700 | [diff] [blame] | 1496 | { |
Sebastian Andrzej Siewior | 5e4dc79 | 2016-11-22 21:16:00 +0000 | [diff] [blame] | 1497 | struct rapl_package *rp; |
Sebastian Andrzej Siewior | 5e4dc79 | 2016-11-22 21:16:00 +0000 | [diff] [blame] | 1498 | |
Zhang Rui | aadf7b3 | 2019-05-13 13:58:50 -0400 | [diff] [blame] | 1499 | rp = rapl_find_package_domain(cpu); |
Thomas Gleixner | b4005e9 | 2016-11-22 21:16:05 +0000 | [diff] [blame] | 1500 | if (!rp) { |
Zhang Rui | aadf7b3 | 2019-05-13 13:58:50 -0400 | [diff] [blame] | 1501 | rp = rapl_add_package(cpu); |
Thomas Gleixner | b4005e9 | 2016-11-22 21:16:05 +0000 | [diff] [blame] | 1502 | if (IS_ERR(rp)) |
| 1503 | return PTR_ERR(rp); |
Thomas Gleixner | 5870506 | 2016-11-22 21:16:02 +0000 | [diff] [blame] | 1504 | } |
Thomas Gleixner | b4005e9 | 2016-11-22 21:16:05 +0000 | [diff] [blame] | 1505 | cpumask_set_cpu(cpu, &rp->cpumask); |
| 1506 | return 0; |
Sebastian Andrzej Siewior | 5e4dc79 | 2016-11-22 21:16:00 +0000 | [diff] [blame] | 1507 | } |
| 1508 | |
| 1509 | static int rapl_cpu_down_prep(unsigned int cpu) |
| 1510 | { |
Jacob Pan | 2d281d8 | 2013-10-17 10:28:35 -0700 | [diff] [blame] | 1511 | struct rapl_package *rp; |
Jacob Pan | 323ee64 | 2016-02-24 13:31:38 -0800 | [diff] [blame] | 1512 | int lead_cpu; |
Jacob Pan | 2d281d8 | 2013-10-17 10:28:35 -0700 | [diff] [blame] | 1513 | |
Zhang Rui | aadf7b3 | 2019-05-13 13:58:50 -0400 | [diff] [blame] | 1514 | rp = rapl_find_package_domain(cpu); |
Sebastian Andrzej Siewior | 5e4dc79 | 2016-11-22 21:16:00 +0000 | [diff] [blame] | 1515 | if (!rp) |
| 1516 | return 0; |
Thomas Gleixner | b4005e9 | 2016-11-22 21:16:05 +0000 | [diff] [blame] | 1517 | |
| 1518 | cpumask_clear_cpu(cpu, &rp->cpumask); |
| 1519 | lead_cpu = cpumask_first(&rp->cpumask); |
| 1520 | if (lead_cpu >= nr_cpu_ids) |
Sebastian Andrzej Siewior | 5e4dc79 | 2016-11-22 21:16:00 +0000 | [diff] [blame] | 1521 | rapl_remove_package(rp); |
Thomas Gleixner | b4005e9 | 2016-11-22 21:16:05 +0000 | [diff] [blame] | 1522 | else if (rp->lead_cpu == cpu) |
| 1523 | rp->lead_cpu = lead_cpu; |
Sebastian Andrzej Siewior | 5e4dc79 | 2016-11-22 21:16:00 +0000 | [diff] [blame] | 1524 | return 0; |
Jacob Pan | 2d281d8 | 2013-10-17 10:28:35 -0700 | [diff] [blame] | 1525 | } |
| 1526 | |
Sebastian Andrzej Siewior | 5e4dc79 | 2016-11-22 21:16:00 +0000 | [diff] [blame] | 1527 | static enum cpuhp_state pcap_rapl_online; |
Jacob Pan | 2d281d8 | 2013-10-17 10:28:35 -0700 | [diff] [blame] | 1528 | |
Zhen Han | 52b3672 | 2018-01-10 08:38:23 +0800 | [diff] [blame] | 1529 | static void power_limit_state_save(void) |
| 1530 | { |
| 1531 | struct rapl_package *rp; |
| 1532 | struct rapl_domain *rd; |
| 1533 | int nr_pl, ret, i; |
| 1534 | |
| 1535 | get_online_cpus(); |
| 1536 | list_for_each_entry(rp, &rapl_packages, plist) { |
| 1537 | if (!rp->power_zone) |
| 1538 | continue; |
| 1539 | rd = power_zone_to_rapl_domain(rp->power_zone); |
| 1540 | nr_pl = find_nr_power_limit(rd); |
| 1541 | for (i = 0; i < nr_pl; i++) { |
| 1542 | switch (rd->rpl[i].prim_id) { |
| 1543 | case PL1_ENABLE: |
| 1544 | ret = rapl_read_data_raw(rd, |
| 1545 | POWER_LIMIT1, |
| 1546 | true, |
| 1547 | &rd->rpl[i].last_power_limit); |
| 1548 | if (ret) |
| 1549 | rd->rpl[i].last_power_limit = 0; |
| 1550 | break; |
| 1551 | case PL2_ENABLE: |
| 1552 | ret = rapl_read_data_raw(rd, |
| 1553 | POWER_LIMIT2, |
| 1554 | true, |
| 1555 | &rd->rpl[i].last_power_limit); |
| 1556 | if (ret) |
| 1557 | rd->rpl[i].last_power_limit = 0; |
| 1558 | break; |
| 1559 | } |
| 1560 | } |
| 1561 | } |
| 1562 | put_online_cpus(); |
| 1563 | } |
| 1564 | |
| 1565 | static void power_limit_state_restore(void) |
| 1566 | { |
| 1567 | struct rapl_package *rp; |
| 1568 | struct rapl_domain *rd; |
| 1569 | int nr_pl, i; |
| 1570 | |
| 1571 | get_online_cpus(); |
| 1572 | list_for_each_entry(rp, &rapl_packages, plist) { |
| 1573 | if (!rp->power_zone) |
| 1574 | continue; |
| 1575 | rd = power_zone_to_rapl_domain(rp->power_zone); |
| 1576 | nr_pl = find_nr_power_limit(rd); |
| 1577 | for (i = 0; i < nr_pl; i++) { |
| 1578 | switch (rd->rpl[i].prim_id) { |
| 1579 | case PL1_ENABLE: |
| 1580 | if (rd->rpl[i].last_power_limit) |
| 1581 | rapl_write_data_raw(rd, |
| 1582 | POWER_LIMIT1, |
| 1583 | rd->rpl[i].last_power_limit); |
| 1584 | break; |
| 1585 | case PL2_ENABLE: |
| 1586 | if (rd->rpl[i].last_power_limit) |
| 1587 | rapl_write_data_raw(rd, |
| 1588 | POWER_LIMIT2, |
| 1589 | rd->rpl[i].last_power_limit); |
| 1590 | break; |
| 1591 | } |
| 1592 | } |
| 1593 | } |
| 1594 | put_online_cpus(); |
| 1595 | } |
| 1596 | |
| 1597 | static int rapl_pm_callback(struct notifier_block *nb, |
| 1598 | unsigned long mode, void *_unused) |
| 1599 | { |
| 1600 | switch (mode) { |
| 1601 | case PM_SUSPEND_PREPARE: |
| 1602 | power_limit_state_save(); |
| 1603 | break; |
| 1604 | case PM_POST_SUSPEND: |
| 1605 | power_limit_state_restore(); |
| 1606 | break; |
| 1607 | } |
| 1608 | return NOTIFY_OK; |
| 1609 | } |
| 1610 | |
| 1611 | static struct notifier_block rapl_pm_notifier = { |
| 1612 | .notifier_call = rapl_pm_callback, |
| 1613 | }; |
| 1614 | |
Jacob Pan | 2d281d8 | 2013-10-17 10:28:35 -0700 | [diff] [blame] | 1615 | static int __init rapl_init(void) |
| 1616 | { |
Jacob Pan | 087e9cb | 2014-11-07 09:29:25 -0800 | [diff] [blame] | 1617 | const struct x86_cpu_id *id; |
Thomas Gleixner | 5870506 | 2016-11-22 21:16:02 +0000 | [diff] [blame] | 1618 | int ret; |
Jacob Pan | 2d281d8 | 2013-10-17 10:28:35 -0700 | [diff] [blame] | 1619 | |
Jacob Pan | 087e9cb | 2014-11-07 09:29:25 -0800 | [diff] [blame] | 1620 | id = x86_match_cpu(rapl_ids); |
| 1621 | if (!id) { |
Jacob Pan | 2d281d8 | 2013-10-17 10:28:35 -0700 | [diff] [blame] | 1622 | pr_err("driver does not support CPU family %d model %d\n", |
| 1623 | boot_cpu_data.x86, boot_cpu_data.x86_model); |
| 1624 | |
| 1625 | return -ENODEV; |
| 1626 | } |
Srivatsa S. Bhat | 009f225 | 2014-03-11 02:09:26 +0530 | [diff] [blame] | 1627 | |
Jacob Pan | 087e9cb | 2014-11-07 09:29:25 -0800 | [diff] [blame] | 1628 | rapl_defaults = (struct rapl_defaults *)id->driver_data; |
| 1629 | |
Thomas Gleixner | 5870506 | 2016-11-22 21:16:02 +0000 | [diff] [blame] | 1630 | ret = rapl_register_powercap(); |
Jacob Pan | 2d281d8 | 2013-10-17 10:28:35 -0700 | [diff] [blame] | 1631 | if (ret) |
Thomas Gleixner | 5870506 | 2016-11-22 21:16:02 +0000 | [diff] [blame] | 1632 | return ret; |
Jacob Pan | 2d281d8 | 2013-10-17 10:28:35 -0700 | [diff] [blame] | 1633 | |
Thomas Gleixner | 5870506 | 2016-11-22 21:16:02 +0000 | [diff] [blame] | 1634 | ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "powercap/rapl:online", |
| 1635 | rapl_cpu_online, rapl_cpu_down_prep); |
Sebastian Andrzej Siewior | 5e4dc79 | 2016-11-22 21:16:00 +0000 | [diff] [blame] | 1636 | if (ret < 0) |
| 1637 | goto err_unreg; |
| 1638 | pcap_rapl_online = ret; |
Thomas Gleixner | 5870506 | 2016-11-22 21:16:02 +0000 | [diff] [blame] | 1639 | |
| 1640 | /* Don't bail out if PSys is not supported */ |
| 1641 | rapl_register_psys(); |
Zhen Han | 52b3672 | 2018-01-10 08:38:23 +0800 | [diff] [blame] | 1642 | |
| 1643 | ret = register_pm_notifier(&rapl_pm_notifier); |
| 1644 | if (ret) |
| 1645 | goto err_unreg_all; |
| 1646 | |
Sebastian Andrzej Siewior | 5e4dc79 | 2016-11-22 21:16:00 +0000 | [diff] [blame] | 1647 | return 0; |
Jacob Pan | 2d281d8 | 2013-10-17 10:28:35 -0700 | [diff] [blame] | 1648 | |
Zhen Han | 52b3672 | 2018-01-10 08:38:23 +0800 | [diff] [blame] | 1649 | err_unreg_all: |
| 1650 | cpuhp_remove_state(pcap_rapl_online); |
| 1651 | |
Sebastian Andrzej Siewior | 5e4dc79 | 2016-11-22 21:16:00 +0000 | [diff] [blame] | 1652 | err_unreg: |
| 1653 | rapl_unregister_powercap(); |
Jacob Pan | 2d281d8 | 2013-10-17 10:28:35 -0700 | [diff] [blame] | 1654 | return ret; |
| 1655 | } |
| 1656 | |
| 1657 | static void __exit rapl_exit(void) |
| 1658 | { |
Zhen Han | 52b3672 | 2018-01-10 08:38:23 +0800 | [diff] [blame] | 1659 | unregister_pm_notifier(&rapl_pm_notifier); |
Sebastian Andrzej Siewior | 5e4dc79 | 2016-11-22 21:16:00 +0000 | [diff] [blame] | 1660 | cpuhp_remove_state(pcap_rapl_online); |
Jacob Pan | 2d281d8 | 2013-10-17 10:28:35 -0700 | [diff] [blame] | 1661 | rapl_unregister_powercap(); |
Jacob Pan | 2d281d8 | 2013-10-17 10:28:35 -0700 | [diff] [blame] | 1662 | } |
| 1663 | |
| 1664 | module_init(rapl_init); |
| 1665 | module_exit(rapl_exit); |
| 1666 | |
| 1667 | MODULE_DESCRIPTION("Driver for Intel RAPL (Running Average Power Limit)"); |
| 1668 | MODULE_AUTHOR("Jacob Pan <jacob.jun.pan@intel.com>"); |
| 1669 | MODULE_LICENSE("GPL v2"); |