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