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