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