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