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