blob: 07611a00b78fd1d9d899b2f50bb91d7b3d8ff2eb [file] [log] [blame]
Thomas Gleixnerf6cc69f2019-05-29 16:57:24 -07001// SPDX-License-Identifier: GPL-2.0-only
Jacob Pan2d281d82013-10-17 10:28:35 -07002/*
Zhang Rui33823882019-07-10 21:44:30 +08003 * Common code for Intel Running Average Power Limit (RAPL) support.
4 * Copyright (c) 2019, Intel Corporation.
Jacob Pan2d281d82013-10-17 10:28:35 -07005 */
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 Han52b36722018-01-10 08:38:23 +080020#include <linux/suspend.h>
Zhang Ruiff956822019-07-10 21:44:24 +080021#include <linux/intel_rapl.h>
Zhang Rui33823882019-07-10 21:44:30 +080022#include <linux/processor.h>
Zhang Ruiabcfaeb2019-07-10 21:44:34 +080023#include <linux/platform_device.h>
24
25#include <asm/iosf_mbi.h>
Jacob Pan2d281d82013-10-17 10:28:35 -070026#include <asm/cpu_device_id.h>
Dave Hansen62d16732016-06-02 17:19:36 -070027#include <asm/intel-family.h>
Jacob Pan2d281d82013-10-17 10:28:35 -070028
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 Rui0c2dded2019-07-10 21:44:32 +080039#define POWER_HIGH_LOCK BIT_ULL(63)
40#define POWER_LOW_LOCK BIT(31)
Jacob Pan2d281d82013-10-17 10:28:35 -070041
Sumeet Pawnikar8365a892020-07-16 23:14:55 +053042#define POWER_LIMIT4_MASK 0x1FFF
43
Jacob Pan2d281d82013-10-17 10:28:35 -070044#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
Zhang Rui931da6a2021-12-07 21:17:34 +080064/*
65 * SPR has different layout for Psys Domain PowerLimit registers.
66 * There are 17 bits of PL1 and PL2 instead of 15 bits.
67 * The Enable bits and TimeWindow bits are also shifted as a result.
68 */
69#define PSYS_POWER_LIMIT1_MASK 0x1FFFF
70#define PSYS_POWER_LIMIT1_ENABLE BIT(17)
71
72#define PSYS_POWER_LIMIT2_MASK (0x1FFFFULL<<32)
73#define PSYS_POWER_LIMIT2_ENABLE BIT_ULL(49)
74
75#define PSYS_TIME_WINDOW1_MASK (0x7FULL<<19)
76#define PSYS_TIME_WINDOW2_MASK (0x7FULL<<51)
77
Jacob Pan2d281d82013-10-17 10:28:35 -070078/* Non HW constants */
Zhang Rui33823882019-07-10 21:44:30 +080079#define RAPL_PRIMITIVE_DERIVED BIT(1) /* not from raw data */
Jacob Pan2d281d82013-10-17 10:28:35 -070080#define RAPL_PRIMITIVE_DUMMY BIT(2)
81
Jacob Pan2d281d82013-10-17 10:28:35 -070082#define TIME_WINDOW_MAX_MSEC 40000
83#define TIME_WINDOW_MIN_MSEC 250
Zhang Rui33823882019-07-10 21:44:30 +080084#define ENERGY_UNIT_SCALE 1000 /* scale from driver unit to powercap unit */
Jacob Pan2d281d82013-10-17 10:28:35 -070085enum unit_type {
Zhang Rui33823882019-07-10 21:44:30 +080086 ARBITRARY_UNIT, /* no translation */
Jacob Pan2d281d82013-10-17 10:28:35 -070087 POWER_UNIT,
88 ENERGY_UNIT,
89 TIME_UNIT,
90};
91
Jacob Pan2d281d82013-10-17 10:28:35 -070092/* per domain data, some are optional */
Jacob Pan2d281d82013-10-17 10:28:35 -070093#define NR_RAW_PRIMITIVES (NR_RAPL_PRIMITIVES - 2)
94
Jacob Pan2d281d82013-10-17 10:28:35 -070095#define DOMAIN_STATE_INACTIVE BIT(0)
96#define DOMAIN_STATE_POWER_LIMIT_SET BIT(1)
97#define DOMAIN_STATE_BIOS_LOCKED BIT(2)
98
Jacob Pan2d281d82013-10-17 10:28:35 -070099static const char pl1_name[] = "long_term";
100static const char pl2_name[] = "short_term";
Sumeet Pawnikar8365a892020-07-16 23:14:55 +0530101static const char pl4_name[] = "peak_power";
Jacob Pan2d281d82013-10-17 10:28:35 -0700102
Jacob Pan2d281d82013-10-17 10:28:35 -0700103#define power_zone_to_rapl_domain(_zone) \
104 container_of(_zone, struct rapl_domain, power_zone)
105
Jacob Pan087e9cb2014-11-07 09:29:25 -0800106struct rapl_defaults {
Ajay Thomas51b63402015-04-30 01:43:23 +0530107 u8 floor_freq_reg_addr;
Jacob Pan087e9cb2014-11-07 09:29:25 -0800108 int (*check_unit)(struct rapl_package *rp, int cpu);
109 void (*set_floor_freq)(struct rapl_domain *rd, bool mode);
110 u64 (*compute_time_window)(struct rapl_package *rp, u64 val,
Zhang Rui33823882019-07-10 21:44:30 +0800111 bool to_raw);
Jacob Pand474a4d2015-03-13 03:48:56 -0700112 unsigned int dram_domain_energy_unit;
Zhang Rui2d798d92020-06-29 13:34:50 +0800113 unsigned int psys_domain_energy_unit;
Zhang Rui931da6a2021-12-07 21:17:34 +0800114 bool spr_psys_bits;
Jacob Pan087e9cb2014-11-07 09:29:25 -0800115};
116static struct rapl_defaults *rapl_defaults;
117
Jacob Pan3c2c0842014-11-07 09:29:26 -0800118/* Sideband MBI registers */
Ajay Thomas51b63402015-04-30 01:43:23 +0530119#define IOSF_CPU_POWER_BUDGET_CTL_BYT (0x2)
120#define IOSF_CPU_POWER_BUDGET_CTL_TNG (0xdf)
Jacob Pan3c2c0842014-11-07 09:29:26 -0800121
Jacob Pan2d281d82013-10-17 10:28:35 -0700122#define PACKAGE_PLN_INT_SAVED BIT(0)
123#define MAX_PRIM_NAME (32)
124
125/* per domain data. used to describe individual knobs such that access function
126 * can be consolidated into one instead of many inline functions.
127 */
128struct rapl_primitive_info {
129 const char *name;
130 u64 mask;
131 int shift;
Zhang Ruif7c4e0c2019-07-10 21:44:22 +0800132 enum rapl_domain_reg_id id;
Jacob Pan2d281d82013-10-17 10:28:35 -0700133 enum unit_type unit;
134 u32 flag;
135};
136
137#define PRIMITIVE_INFO_INIT(p, m, s, i, u, f) { \
138 .name = #p, \
139 .mask = m, \
140 .shift = s, \
141 .id = i, \
142 .unit = u, \
143 .flag = f \
144 }
145
146static void rapl_init_domains(struct rapl_package *rp);
147static int rapl_read_data_raw(struct rapl_domain *rd,
Zhang Rui33823882019-07-10 21:44:30 +0800148 enum rapl_primitives prim,
149 bool xlate, u64 *data);
Jacob Pan2d281d82013-10-17 10:28:35 -0700150static int rapl_write_data_raw(struct rapl_domain *rd,
Zhang Rui33823882019-07-10 21:44:30 +0800151 enum rapl_primitives prim,
152 unsigned long long value);
Jacob Pan309557f2016-02-24 13:31:37 -0800153static u64 rapl_unit_xlate(struct rapl_domain *rd,
Zhang Rui33823882019-07-10 21:44:30 +0800154 enum unit_type type, u64 value, int to_raw);
Jacob Pan309557f2016-02-24 13:31:37 -0800155static void package_power_limit_irq_save(struct rapl_package *rp);
Jacob Pan2d281d82013-10-17 10:28:35 -0700156
Zhang Rui33823882019-07-10 21:44:30 +0800157static LIST_HEAD(rapl_packages); /* guarded by CPU hotplug lock */
Jacob Pan2d281d82013-10-17 10:28:35 -0700158
Zhang Rui33823882019-07-10 21:44:30 +0800159static const char *const rapl_domain_names[] = {
Jacob Pan2d281d82013-10-17 10:28:35 -0700160 "package",
161 "core",
162 "uncore",
163 "dram",
Srinivas Pandruvada3521ba12016-04-17 15:03:01 -0700164 "psys",
Jacob Pan2d281d82013-10-17 10:28:35 -0700165};
166
Zhang Rui33823882019-07-10 21:44:30 +0800167static int get_energy_counter(struct powercap_zone *power_zone,
168 u64 *energy_raw)
Jacob Pan2d281d82013-10-17 10:28:35 -0700169{
170 struct rapl_domain *rd;
171 u64 energy_now;
172
173 /* prevent CPU hotplug, make sure the RAPL domain does not go
174 * away while reading the counter.
175 */
Sebastian Andrzej Siewior5d4c7792021-08-03 16:16:01 +0200176 cpus_read_lock();
Jacob Pan2d281d82013-10-17 10:28:35 -0700177 rd = power_zone_to_rapl_domain(power_zone);
178
179 if (!rapl_read_data_raw(rd, ENERGY_COUNTER, true, &energy_now)) {
180 *energy_raw = energy_now;
Sebastian Andrzej Siewior5d4c7792021-08-03 16:16:01 +0200181 cpus_read_unlock();
Jacob Pan2d281d82013-10-17 10:28:35 -0700182
183 return 0;
184 }
Sebastian Andrzej Siewior5d4c7792021-08-03 16:16:01 +0200185 cpus_read_unlock();
Jacob Pan2d281d82013-10-17 10:28:35 -0700186
187 return -EIO;
188}
189
190static int get_max_energy_counter(struct powercap_zone *pcd_dev, u64 *energy)
191{
Jacob Pand474a4d2015-03-13 03:48:56 -0700192 struct rapl_domain *rd = power_zone_to_rapl_domain(pcd_dev);
193
Jacob Pan309557f2016-02-24 13:31:37 -0800194 *energy = rapl_unit_xlate(rd, ENERGY_UNIT, ENERGY_STATUS_MASK, 0);
Jacob Pan2d281d82013-10-17 10:28:35 -0700195 return 0;
196}
197
198static int release_zone(struct powercap_zone *power_zone)
199{
200 struct rapl_domain *rd = power_zone_to_rapl_domain(power_zone);
Jacob Pan309557f2016-02-24 13:31:37 -0800201 struct rapl_package *rp = rd->rp;
Jacob Pan2d281d82013-10-17 10:28:35 -0700202
203 /* package zone is the last zone of a package, we can free
204 * memory here since all children has been unregistered.
205 */
206 if (rd->id == RAPL_DOMAIN_PACKAGE) {
Jacob Pan2d281d82013-10-17 10:28:35 -0700207 kfree(rd);
208 rp->domains = NULL;
209 }
210
211 return 0;
212
213}
214
215static int find_nr_power_limit(struct rapl_domain *rd)
216{
Jacob Pane1399ba2016-05-31 13:41:29 -0700217 int i, nr_pl = 0;
Jacob Pan2d281d82013-10-17 10:28:35 -0700218
219 for (i = 0; i < NR_POWER_LIMITS; i++) {
Jacob Pane1399ba2016-05-31 13:41:29 -0700220 if (rd->rpl[i].name)
221 nr_pl++;
Jacob Pan2d281d82013-10-17 10:28:35 -0700222 }
223
Jacob Pane1399ba2016-05-31 13:41:29 -0700224 return nr_pl;
Jacob Pan2d281d82013-10-17 10:28:35 -0700225}
226
227static int set_domain_enable(struct powercap_zone *power_zone, bool mode)
228{
229 struct rapl_domain *rd = power_zone_to_rapl_domain(power_zone);
Jacob Pan2d281d82013-10-17 10:28:35 -0700230
231 if (rd->state & DOMAIN_STATE_BIOS_LOCKED)
232 return -EACCES;
Jacob Pan3c2c0842014-11-07 09:29:26 -0800233
Sebastian Andrzej Siewior5d4c7792021-08-03 16:16:01 +0200234 cpus_read_lock();
Jacob Pan2d281d82013-10-17 10:28:35 -0700235 rapl_write_data_raw(rd, PL1_ENABLE, mode);
Ajay Thomas51b63402015-04-30 01:43:23 +0530236 if (rapl_defaults->set_floor_freq)
237 rapl_defaults->set_floor_freq(rd, mode);
Sebastian Andrzej Siewior5d4c7792021-08-03 16:16:01 +0200238 cpus_read_unlock();
Jacob Pan2d281d82013-10-17 10:28:35 -0700239
240 return 0;
241}
242
243static int get_domain_enable(struct powercap_zone *power_zone, bool *mode)
244{
245 struct rapl_domain *rd = power_zone_to_rapl_domain(power_zone);
246 u64 val;
247
248 if (rd->state & DOMAIN_STATE_BIOS_LOCKED) {
249 *mode = false;
250 return 0;
251 }
Sebastian Andrzej Siewior5d4c7792021-08-03 16:16:01 +0200252 cpus_read_lock();
Jacob Pan2d281d82013-10-17 10:28:35 -0700253 if (rapl_read_data_raw(rd, PL1_ENABLE, true, &val)) {
Sebastian Andrzej Siewior5d4c7792021-08-03 16:16:01 +0200254 cpus_read_unlock();
Jacob Pan2d281d82013-10-17 10:28:35 -0700255 return -EIO;
256 }
257 *mode = val;
Sebastian Andrzej Siewior5d4c7792021-08-03 16:16:01 +0200258 cpus_read_unlock();
Jacob Pan2d281d82013-10-17 10:28:35 -0700259
260 return 0;
261}
262
263/* per RAPL domain ops, in the order of rapl_domain_type */
Julia Lawall600c3952015-12-23 22:59:55 +0100264static const struct powercap_zone_ops zone_ops[] = {
Jacob Pan2d281d82013-10-17 10:28:35 -0700265 /* RAPL_DOMAIN_PACKAGE */
266 {
Zhang Rui33823882019-07-10 21:44:30 +0800267 .get_energy_uj = get_energy_counter,
268 .get_max_energy_range_uj = get_max_energy_counter,
269 .release = release_zone,
270 .set_enable = set_domain_enable,
271 .get_enable = get_domain_enable,
272 },
Jacob Pan2d281d82013-10-17 10:28:35 -0700273 /* RAPL_DOMAIN_PP0 */
274 {
Zhang Rui33823882019-07-10 21:44:30 +0800275 .get_energy_uj = get_energy_counter,
276 .get_max_energy_range_uj = get_max_energy_counter,
277 .release = release_zone,
278 .set_enable = set_domain_enable,
279 .get_enable = get_domain_enable,
280 },
Jacob Pan2d281d82013-10-17 10:28:35 -0700281 /* RAPL_DOMAIN_PP1 */
282 {
Zhang Rui33823882019-07-10 21:44:30 +0800283 .get_energy_uj = get_energy_counter,
284 .get_max_energy_range_uj = get_max_energy_counter,
285 .release = release_zone,
286 .set_enable = set_domain_enable,
287 .get_enable = get_domain_enable,
288 },
Jacob Pan2d281d82013-10-17 10:28:35 -0700289 /* RAPL_DOMAIN_DRAM */
290 {
Zhang Rui33823882019-07-10 21:44:30 +0800291 .get_energy_uj = get_energy_counter,
292 .get_max_energy_range_uj = get_max_energy_counter,
293 .release = release_zone,
294 .set_enable = set_domain_enable,
295 .get_enable = get_domain_enable,
296 },
Srinivas Pandruvada3521ba12016-04-17 15:03:01 -0700297 /* RAPL_DOMAIN_PLATFORM */
298 {
Zhang Rui33823882019-07-10 21:44:30 +0800299 .get_energy_uj = get_energy_counter,
300 .get_max_energy_range_uj = get_max_energy_counter,
301 .release = release_zone,
302 .set_enable = set_domain_enable,
303 .get_enable = get_domain_enable,
304 },
Jacob Pan2d281d82013-10-17 10:28:35 -0700305};
306
Jacob Pane1399ba2016-05-31 13:41:29 -0700307/*
308 * Constraint index used by powercap can be different than power limit (PL)
Zhang Rui33823882019-07-10 21:44:30 +0800309 * index in that some PLs maybe missing due to non-existent MSRs. So we
Jacob Pane1399ba2016-05-31 13:41:29 -0700310 * need to convert here by finding the valid PLs only (name populated).
311 */
312static int contraint_to_pl(struct rapl_domain *rd, int cid)
313{
314 int i, j;
315
316 for (i = 0, j = 0; i < NR_POWER_LIMITS; i++) {
317 if ((rd->rpl[i].name) && j++ == cid) {
318 pr_debug("%s: index %d\n", __func__, i);
319 return i;
320 }
321 }
Jacob Pancb43f812016-11-28 13:53:11 -0800322 pr_err("Cannot find matching power limit for constraint %d\n", cid);
Jacob Pane1399ba2016-05-31 13:41:29 -0700323
324 return -EINVAL;
325}
326
327static int set_power_limit(struct powercap_zone *power_zone, int cid,
Zhang Rui33823882019-07-10 21:44:30 +0800328 u64 power_limit)
Jacob Pan2d281d82013-10-17 10:28:35 -0700329{
330 struct rapl_domain *rd;
331 struct rapl_package *rp;
332 int ret = 0;
Jacob Pane1399ba2016-05-31 13:41:29 -0700333 int id;
Jacob Pan2d281d82013-10-17 10:28:35 -0700334
Sebastian Andrzej Siewior5d4c7792021-08-03 16:16:01 +0200335 cpus_read_lock();
Jacob Pan2d281d82013-10-17 10:28:35 -0700336 rd = power_zone_to_rapl_domain(power_zone);
Jacob Pane1399ba2016-05-31 13:41:29 -0700337 id = contraint_to_pl(rd, cid);
Jacob Pancb43f812016-11-28 13:53:11 -0800338 if (id < 0) {
339 ret = id;
340 goto set_exit;
341 }
Jacob Pane1399ba2016-05-31 13:41:29 -0700342
Jacob Pan309557f2016-02-24 13:31:37 -0800343 rp = rd->rp;
Jacob Pan2d281d82013-10-17 10:28:35 -0700344
345 if (rd->state & DOMAIN_STATE_BIOS_LOCKED) {
Zhang Rui33823882019-07-10 21:44:30 +0800346 dev_warn(&power_zone->dev,
347 "%s locked by BIOS, monitoring only\n", rd->name);
Jacob Pan2d281d82013-10-17 10:28:35 -0700348 ret = -EACCES;
349 goto set_exit;
350 }
351
352 switch (rd->rpl[id].prim_id) {
353 case PL1_ENABLE:
354 rapl_write_data_raw(rd, POWER_LIMIT1, power_limit);
355 break;
356 case PL2_ENABLE:
357 rapl_write_data_raw(rd, POWER_LIMIT2, power_limit);
358 break;
Sumeet Pawnikar8365a892020-07-16 23:14:55 +0530359 case PL4_ENABLE:
360 rapl_write_data_raw(rd, POWER_LIMIT4, power_limit);
361 break;
Jacob Pan2d281d82013-10-17 10:28:35 -0700362 default:
363 ret = -EINVAL;
364 }
365 if (!ret)
Jacob Pan309557f2016-02-24 13:31:37 -0800366 package_power_limit_irq_save(rp);
Jacob Pan2d281d82013-10-17 10:28:35 -0700367set_exit:
Sebastian Andrzej Siewior5d4c7792021-08-03 16:16:01 +0200368 cpus_read_unlock();
Jacob Pan2d281d82013-10-17 10:28:35 -0700369 return ret;
370}
371
Jacob Pane1399ba2016-05-31 13:41:29 -0700372static int get_current_power_limit(struct powercap_zone *power_zone, int cid,
Zhang Rui33823882019-07-10 21:44:30 +0800373 u64 *data)
Jacob Pan2d281d82013-10-17 10:28:35 -0700374{
375 struct rapl_domain *rd;
376 u64 val;
377 int prim;
378 int ret = 0;
Jacob Pane1399ba2016-05-31 13:41:29 -0700379 int id;
Jacob Pan2d281d82013-10-17 10:28:35 -0700380
Sebastian Andrzej Siewior5d4c7792021-08-03 16:16:01 +0200381 cpus_read_lock();
Jacob Pan2d281d82013-10-17 10:28:35 -0700382 rd = power_zone_to_rapl_domain(power_zone);
Jacob Pane1399ba2016-05-31 13:41:29 -0700383 id = contraint_to_pl(rd, cid);
Jacob Pancb43f812016-11-28 13:53:11 -0800384 if (id < 0) {
385 ret = id;
386 goto get_exit;
387 }
388
Jacob Pan2d281d82013-10-17 10:28:35 -0700389 switch (rd->rpl[id].prim_id) {
390 case PL1_ENABLE:
391 prim = POWER_LIMIT1;
392 break;
393 case PL2_ENABLE:
394 prim = POWER_LIMIT2;
395 break;
Sumeet Pawnikar8365a892020-07-16 23:14:55 +0530396 case PL4_ENABLE:
397 prim = POWER_LIMIT4;
398 break;
Jacob Pan2d281d82013-10-17 10:28:35 -0700399 default:
Sebastian Andrzej Siewior5d4c7792021-08-03 16:16:01 +0200400 cpus_read_unlock();
Jacob Pan2d281d82013-10-17 10:28:35 -0700401 return -EINVAL;
402 }
403 if (rapl_read_data_raw(rd, prim, true, &val))
404 ret = -EIO;
405 else
406 *data = val;
407
Jacob Pancb43f812016-11-28 13:53:11 -0800408get_exit:
Sebastian Andrzej Siewior5d4c7792021-08-03 16:16:01 +0200409 cpus_read_unlock();
Jacob Pan2d281d82013-10-17 10:28:35 -0700410
411 return ret;
412}
413
Jacob Pane1399ba2016-05-31 13:41:29 -0700414static int set_time_window(struct powercap_zone *power_zone, int cid,
Zhang Rui33823882019-07-10 21:44:30 +0800415 u64 window)
Jacob Pan2d281d82013-10-17 10:28:35 -0700416{
417 struct rapl_domain *rd;
418 int ret = 0;
Jacob Pane1399ba2016-05-31 13:41:29 -0700419 int id;
Jacob Pan2d281d82013-10-17 10:28:35 -0700420
Sebastian Andrzej Siewior5d4c7792021-08-03 16:16:01 +0200421 cpus_read_lock();
Jacob Pan2d281d82013-10-17 10:28:35 -0700422 rd = power_zone_to_rapl_domain(power_zone);
Jacob Pane1399ba2016-05-31 13:41:29 -0700423 id = contraint_to_pl(rd, cid);
Jacob Pancb43f812016-11-28 13:53:11 -0800424 if (id < 0) {
425 ret = id;
426 goto set_time_exit;
427 }
Jacob Pane1399ba2016-05-31 13:41:29 -0700428
Jacob Pan2d281d82013-10-17 10:28:35 -0700429 switch (rd->rpl[id].prim_id) {
430 case PL1_ENABLE:
431 rapl_write_data_raw(rd, TIME_WINDOW1, window);
432 break;
433 case PL2_ENABLE:
434 rapl_write_data_raw(rd, TIME_WINDOW2, window);
435 break;
436 default:
437 ret = -EINVAL;
438 }
Jacob Pancb43f812016-11-28 13:53:11 -0800439
440set_time_exit:
Sebastian Andrzej Siewior5d4c7792021-08-03 16:16:01 +0200441 cpus_read_unlock();
Jacob Pan2d281d82013-10-17 10:28:35 -0700442 return ret;
443}
444
Zhang Rui33823882019-07-10 21:44:30 +0800445static int get_time_window(struct powercap_zone *power_zone, int cid,
446 u64 *data)
Jacob Pan2d281d82013-10-17 10:28:35 -0700447{
448 struct rapl_domain *rd;
449 u64 val;
450 int ret = 0;
Jacob Pane1399ba2016-05-31 13:41:29 -0700451 int id;
Jacob Pan2d281d82013-10-17 10:28:35 -0700452
Sebastian Andrzej Siewior5d4c7792021-08-03 16:16:01 +0200453 cpus_read_lock();
Jacob Pan2d281d82013-10-17 10:28:35 -0700454 rd = power_zone_to_rapl_domain(power_zone);
Jacob Pane1399ba2016-05-31 13:41:29 -0700455 id = contraint_to_pl(rd, cid);
Jacob Pancb43f812016-11-28 13:53:11 -0800456 if (id < 0) {
457 ret = id;
458 goto get_time_exit;
459 }
Jacob Pane1399ba2016-05-31 13:41:29 -0700460
Jacob Pan2d281d82013-10-17 10:28:35 -0700461 switch (rd->rpl[id].prim_id) {
462 case PL1_ENABLE:
463 ret = rapl_read_data_raw(rd, TIME_WINDOW1, true, &val);
464 break;
465 case PL2_ENABLE:
466 ret = rapl_read_data_raw(rd, TIME_WINDOW2, true, &val);
467 break;
Sumeet Pawnikar8365a892020-07-16 23:14:55 +0530468 case PL4_ENABLE:
469 /*
470 * Time window parameter is not applicable for PL4 entry
471 * so assigining '0' as default value.
472 */
473 val = 0;
474 break;
Jacob Pan2d281d82013-10-17 10:28:35 -0700475 default:
Sebastian Andrzej Siewior5d4c7792021-08-03 16:16:01 +0200476 cpus_read_unlock();
Jacob Pan2d281d82013-10-17 10:28:35 -0700477 return -EINVAL;
478 }
479 if (!ret)
480 *data = val;
Jacob Pancb43f812016-11-28 13:53:11 -0800481
482get_time_exit:
Sebastian Andrzej Siewior5d4c7792021-08-03 16:16:01 +0200483 cpus_read_unlock();
Jacob Pan2d281d82013-10-17 10:28:35 -0700484
485 return ret;
486}
487
Zhang Rui33823882019-07-10 21:44:30 +0800488static const char *get_constraint_name(struct powercap_zone *power_zone,
489 int cid)
Jacob Pan2d281d82013-10-17 10:28:35 -0700490{
Jacob Pan2d281d82013-10-17 10:28:35 -0700491 struct rapl_domain *rd;
Jacob Pane1399ba2016-05-31 13:41:29 -0700492 int id;
Jacob Pan2d281d82013-10-17 10:28:35 -0700493
494 rd = power_zone_to_rapl_domain(power_zone);
Jacob Pane1399ba2016-05-31 13:41:29 -0700495 id = contraint_to_pl(rd, cid);
496 if (id >= 0)
497 return rd->rpl[id].name;
Jacob Pan2d281d82013-10-17 10:28:35 -0700498
Jacob Pane1399ba2016-05-31 13:41:29 -0700499 return NULL;
Jacob Pan2d281d82013-10-17 10:28:35 -0700500}
501
Zhang Rui33823882019-07-10 21:44:30 +0800502static int get_max_power(struct powercap_zone *power_zone, int id, u64 *data)
Jacob Pan2d281d82013-10-17 10:28:35 -0700503{
504 struct rapl_domain *rd;
505 u64 val;
506 int prim;
507 int ret = 0;
508
Sebastian Andrzej Siewior5d4c7792021-08-03 16:16:01 +0200509 cpus_read_lock();
Jacob Pan2d281d82013-10-17 10:28:35 -0700510 rd = power_zone_to_rapl_domain(power_zone);
511 switch (rd->rpl[id].prim_id) {
512 case PL1_ENABLE:
513 prim = THERMAL_SPEC_POWER;
514 break;
515 case PL2_ENABLE:
516 prim = MAX_POWER;
517 break;
Sumeet Pawnikar8365a892020-07-16 23:14:55 +0530518 case PL4_ENABLE:
519 prim = MAX_POWER;
520 break;
Jacob Pan2d281d82013-10-17 10:28:35 -0700521 default:
Sebastian Andrzej Siewior5d4c7792021-08-03 16:16:01 +0200522 cpus_read_unlock();
Jacob Pan2d281d82013-10-17 10:28:35 -0700523 return -EINVAL;
524 }
525 if (rapl_read_data_raw(rd, prim, true, &val))
526 ret = -EIO;
527 else
528 *data = val;
529
Sumeet Pawnikar8365a892020-07-16 23:14:55 +0530530 /* As a generalization rule, PL4 would be around two times PL2. */
531 if (rd->rpl[id].prim_id == PL4_ENABLE)
532 *data = *data * 2;
533
Sebastian Andrzej Siewior5d4c7792021-08-03 16:16:01 +0200534 cpus_read_unlock();
Jacob Pan2d281d82013-10-17 10:28:35 -0700535
536 return ret;
537}
538
Julia Lawall600c3952015-12-23 22:59:55 +0100539static const struct powercap_zone_constraint_ops constraint_ops = {
Jacob Pan2d281d82013-10-17 10:28:35 -0700540 .set_power_limit_uw = set_power_limit,
541 .get_power_limit_uw = get_current_power_limit,
542 .set_time_window_us = set_time_window,
543 .get_time_window_us = get_time_window,
544 .get_max_power_uw = get_max_power,
545 .get_name = get_constraint_name,
546};
547
548/* called after domain detection and package level data are set */
549static void rapl_init_domains(struct rapl_package *rp)
550{
Zhang Rui0c2dded2019-07-10 21:44:32 +0800551 enum rapl_domain_type i;
552 enum rapl_domain_reg_id j;
Jacob Pan2d281d82013-10-17 10:28:35 -0700553 struct rapl_domain *rd = rp->domains;
554
555 for (i = 0; i < RAPL_DOMAIN_MAX; i++) {
556 unsigned int mask = rp->domain_map & (1 << i);
Zhang Rui7fde2712019-07-10 21:44:26 +0800557
Zhang Rui0c2dded2019-07-10 21:44:32 +0800558 if (!mask)
559 continue;
Zhang Rui7fde2712019-07-10 21:44:26 +0800560
Zhang Rui0c2dded2019-07-10 21:44:32 +0800561 rd->rp = rp;
Zhang Ruif1e8d752020-10-13 15:42:41 +0800562
563 if (i == RAPL_DOMAIN_PLATFORM && rp->id > 0) {
564 snprintf(rd->name, RAPL_DOMAIN_NAME_LENGTH, "psys-%d",
Yunfeng Ye65348ba2021-01-23 05:06:08 -0500565 topology_physical_package_id(rp->lead_cpu));
Zhang Ruif1e8d752020-10-13 15:42:41 +0800566 } else
567 snprintf(rd->name, RAPL_DOMAIN_NAME_LENGTH, "%s",
568 rapl_domain_names[i]);
569
Zhang Rui0c2dded2019-07-10 21:44:32 +0800570 rd->id = i;
571 rd->rpl[0].prim_id = PL1_ENABLE;
572 rd->rpl[0].name = pl1_name;
Sumeet Pawnikar8365a892020-07-16 23:14:55 +0530573
574 /*
575 * The PL2 power domain is applicable for limits two
576 * and limits three
577 */
578 if (rp->priv->limits[i] >= 2) {
Jacob Pan2d281d82013-10-17 10:28:35 -0700579 rd->rpl[1].prim_id = PL2_ENABLE;
580 rd->rpl[1].name = pl2_name;
Zhang Rui0c2dded2019-07-10 21:44:32 +0800581 }
582
Sumeet Pawnikar8365a892020-07-16 23:14:55 +0530583 /* Enable PL4 domain if the total power limits are three */
584 if (rp->priv->limits[i] == 3) {
585 rd->rpl[2].prim_id = PL4_ENABLE;
586 rd->rpl[2].name = pl4_name;
587 }
588
Zhang Rui0c2dded2019-07-10 21:44:32 +0800589 for (j = 0; j < RAPL_DOMAIN_REG_MAX; j++)
590 rd->regs[j] = rp->priv->regs[i][j];
591
Zhang Rui2d798d92020-06-29 13:34:50 +0800592 switch (i) {
593 case RAPL_DOMAIN_DRAM:
Jacob Pand474a4d2015-03-13 03:48:56 -0700594 rd->domain_energy_unit =
Zhang Rui33823882019-07-10 21:44:30 +0800595 rapl_defaults->dram_domain_energy_unit;
Jacob Pand474a4d2015-03-13 03:48:56 -0700596 if (rd->domain_energy_unit)
597 pr_info("DRAM domain energy unit %dpj\n",
598 rd->domain_energy_unit);
Zhang Rui2d798d92020-06-29 13:34:50 +0800599 break;
600 case RAPL_DOMAIN_PLATFORM:
601 rd->domain_energy_unit =
602 rapl_defaults->psys_domain_energy_unit;
603 if (rd->domain_energy_unit)
604 pr_info("Platform domain energy unit %dpj\n",
605 rd->domain_energy_unit);
606 break;
607 default:
608 break;
Jacob Pan2d281d82013-10-17 10:28:35 -0700609 }
Zhang Rui0c2dded2019-07-10 21:44:32 +0800610 rd++;
Jacob Pan2d281d82013-10-17 10:28:35 -0700611 }
612}
613
Jacob Pan309557f2016-02-24 13:31:37 -0800614static u64 rapl_unit_xlate(struct rapl_domain *rd, enum unit_type type,
Zhang Rui33823882019-07-10 21:44:30 +0800615 u64 value, int to_raw)
Jacob Pan2d281d82013-10-17 10:28:35 -0700616{
Jacob Pan3c2c0842014-11-07 09:29:26 -0800617 u64 units = 1;
Jacob Pan309557f2016-02-24 13:31:37 -0800618 struct rapl_package *rp = rd->rp;
Jacob Pand474a4d2015-03-13 03:48:56 -0700619 u64 scale = 1;
Jacob Pan2d281d82013-10-17 10:28:35 -0700620
Jacob Pan2d281d82013-10-17 10:28:35 -0700621 switch (type) {
622 case POWER_UNIT:
Jacob Pan3c2c0842014-11-07 09:29:26 -0800623 units = rp->power_unit;
Jacob Pan2d281d82013-10-17 10:28:35 -0700624 break;
625 case ENERGY_UNIT:
Jacob Pand474a4d2015-03-13 03:48:56 -0700626 scale = ENERGY_UNIT_SCALE;
627 /* per domain unit takes precedence */
Jacob Pancb43f812016-11-28 13:53:11 -0800628 if (rd->domain_energy_unit)
Jacob Pand474a4d2015-03-13 03:48:56 -0700629 units = rd->domain_energy_unit;
630 else
631 units = rp->energy_unit;
Jacob Pan2d281d82013-10-17 10:28:35 -0700632 break;
633 case TIME_UNIT:
Jacob Pan3c2c0842014-11-07 09:29:26 -0800634 return rapl_defaults->compute_time_window(rp, value, to_raw);
Jacob Pan2d281d82013-10-17 10:28:35 -0700635 case ARBITRARY_UNIT:
636 default:
637 return value;
Tom Rixa8193af2020-11-01 06:11:29 -0800638 }
Jacob Pan2d281d82013-10-17 10:28:35 -0700639
640 if (to_raw)
Jacob Pand474a4d2015-03-13 03:48:56 -0700641 return div64_u64(value, units) * scale;
Jacob Pan3c2c0842014-11-07 09:29:26 -0800642
643 value *= units;
644
Jacob Pand474a4d2015-03-13 03:48:56 -0700645 return div64_u64(value, scale);
Jacob Pan2d281d82013-10-17 10:28:35 -0700646}
647
648/* in the order of enum rapl_primitives */
649static struct rapl_primitive_info rpi[] = {
650 /* name, mask, shift, msr index, unit divisor */
651 PRIMITIVE_INFO_INIT(ENERGY_COUNTER, ENERGY_STATUS_MASK, 0,
Zhang Rui33823882019-07-10 21:44:30 +0800652 RAPL_DOMAIN_REG_STATUS, ENERGY_UNIT, 0),
Jacob Pan2d281d82013-10-17 10:28:35 -0700653 PRIMITIVE_INFO_INIT(POWER_LIMIT1, POWER_LIMIT1_MASK, 0,
Zhang Rui33823882019-07-10 21:44:30 +0800654 RAPL_DOMAIN_REG_LIMIT, POWER_UNIT, 0),
Jacob Pan2d281d82013-10-17 10:28:35 -0700655 PRIMITIVE_INFO_INIT(POWER_LIMIT2, POWER_LIMIT2_MASK, 32,
Zhang Rui33823882019-07-10 21:44:30 +0800656 RAPL_DOMAIN_REG_LIMIT, POWER_UNIT, 0),
Sumeet Pawnikar8365a892020-07-16 23:14:55 +0530657 PRIMITIVE_INFO_INIT(POWER_LIMIT4, POWER_LIMIT4_MASK, 0,
658 RAPL_DOMAIN_REG_PL4, POWER_UNIT, 0),
Zhang Rui0c2dded2019-07-10 21:44:32 +0800659 PRIMITIVE_INFO_INIT(FW_LOCK, POWER_LOW_LOCK, 31,
Zhang Rui33823882019-07-10 21:44:30 +0800660 RAPL_DOMAIN_REG_LIMIT, ARBITRARY_UNIT, 0),
Jacob Pan2d281d82013-10-17 10:28:35 -0700661 PRIMITIVE_INFO_INIT(PL1_ENABLE, POWER_LIMIT1_ENABLE, 15,
Zhang Rui33823882019-07-10 21:44:30 +0800662 RAPL_DOMAIN_REG_LIMIT, ARBITRARY_UNIT, 0),
Jacob Pan2d281d82013-10-17 10:28:35 -0700663 PRIMITIVE_INFO_INIT(PL1_CLAMP, POWER_LIMIT1_CLAMP, 16,
Zhang Rui33823882019-07-10 21:44:30 +0800664 RAPL_DOMAIN_REG_LIMIT, ARBITRARY_UNIT, 0),
Jacob Pan2d281d82013-10-17 10:28:35 -0700665 PRIMITIVE_INFO_INIT(PL2_ENABLE, POWER_LIMIT2_ENABLE, 47,
Zhang Rui33823882019-07-10 21:44:30 +0800666 RAPL_DOMAIN_REG_LIMIT, ARBITRARY_UNIT, 0),
Jacob Pan2d281d82013-10-17 10:28:35 -0700667 PRIMITIVE_INFO_INIT(PL2_CLAMP, POWER_LIMIT2_CLAMP, 48,
Zhang Rui33823882019-07-10 21:44:30 +0800668 RAPL_DOMAIN_REG_LIMIT, ARBITRARY_UNIT, 0),
Sumeet Pawnikar8365a892020-07-16 23:14:55 +0530669 PRIMITIVE_INFO_INIT(PL4_ENABLE, POWER_LIMIT4_MASK, 0,
670 RAPL_DOMAIN_REG_PL4, ARBITRARY_UNIT, 0),
Jacob Pan2d281d82013-10-17 10:28:35 -0700671 PRIMITIVE_INFO_INIT(TIME_WINDOW1, TIME_WINDOW1_MASK, 17,
Zhang Rui33823882019-07-10 21:44:30 +0800672 RAPL_DOMAIN_REG_LIMIT, TIME_UNIT, 0),
Jacob Pan2d281d82013-10-17 10:28:35 -0700673 PRIMITIVE_INFO_INIT(TIME_WINDOW2, TIME_WINDOW2_MASK, 49,
Zhang Rui33823882019-07-10 21:44:30 +0800674 RAPL_DOMAIN_REG_LIMIT, TIME_UNIT, 0),
Jacob Pan2d281d82013-10-17 10:28:35 -0700675 PRIMITIVE_INFO_INIT(THERMAL_SPEC_POWER, POWER_INFO_THERMAL_SPEC_MASK,
Zhang Rui33823882019-07-10 21:44:30 +0800676 0, RAPL_DOMAIN_REG_INFO, POWER_UNIT, 0),
Jacob Pan2d281d82013-10-17 10:28:35 -0700677 PRIMITIVE_INFO_INIT(MAX_POWER, POWER_INFO_MAX_MASK, 32,
Zhang Rui33823882019-07-10 21:44:30 +0800678 RAPL_DOMAIN_REG_INFO, POWER_UNIT, 0),
Jacob Pan2d281d82013-10-17 10:28:35 -0700679 PRIMITIVE_INFO_INIT(MIN_POWER, POWER_INFO_MIN_MASK, 16,
Zhang Rui33823882019-07-10 21:44:30 +0800680 RAPL_DOMAIN_REG_INFO, POWER_UNIT, 0),
Jacob Pan2d281d82013-10-17 10:28:35 -0700681 PRIMITIVE_INFO_INIT(MAX_TIME_WINDOW, POWER_INFO_MAX_TIME_WIN_MASK, 48,
Zhang Rui33823882019-07-10 21:44:30 +0800682 RAPL_DOMAIN_REG_INFO, TIME_UNIT, 0),
Jacob Pan2d281d82013-10-17 10:28:35 -0700683 PRIMITIVE_INFO_INIT(THROTTLED_TIME, PERF_STATUS_THROTTLE_TIME_MASK, 0,
Zhang Rui33823882019-07-10 21:44:30 +0800684 RAPL_DOMAIN_REG_PERF, TIME_UNIT, 0),
Jacob Pan2d281d82013-10-17 10:28:35 -0700685 PRIMITIVE_INFO_INIT(PRIORITY_LEVEL, PP_POLICY_MASK, 0,
Zhang Rui33823882019-07-10 21:44:30 +0800686 RAPL_DOMAIN_REG_POLICY, ARBITRARY_UNIT, 0),
Zhang Rui931da6a2021-12-07 21:17:34 +0800687 PRIMITIVE_INFO_INIT(PSYS_POWER_LIMIT1, PSYS_POWER_LIMIT1_MASK, 0,
688 RAPL_DOMAIN_REG_LIMIT, POWER_UNIT, 0),
689 PRIMITIVE_INFO_INIT(PSYS_POWER_LIMIT2, PSYS_POWER_LIMIT2_MASK, 32,
690 RAPL_DOMAIN_REG_LIMIT, POWER_UNIT, 0),
691 PRIMITIVE_INFO_INIT(PSYS_PL1_ENABLE, PSYS_POWER_LIMIT1_ENABLE, 17,
692 RAPL_DOMAIN_REG_LIMIT, ARBITRARY_UNIT, 0),
693 PRIMITIVE_INFO_INIT(PSYS_PL2_ENABLE, PSYS_POWER_LIMIT2_ENABLE, 49,
694 RAPL_DOMAIN_REG_LIMIT, ARBITRARY_UNIT, 0),
695 PRIMITIVE_INFO_INIT(PSYS_TIME_WINDOW1, PSYS_TIME_WINDOW1_MASK, 19,
696 RAPL_DOMAIN_REG_LIMIT, TIME_UNIT, 0),
697 PRIMITIVE_INFO_INIT(PSYS_TIME_WINDOW2, PSYS_TIME_WINDOW2_MASK, 51,
698 RAPL_DOMAIN_REG_LIMIT, TIME_UNIT, 0),
Jacob Pan2d281d82013-10-17 10:28:35 -0700699 /* non-hardware */
700 PRIMITIVE_INFO_INIT(AVERAGE_POWER, 0, 0, 0, POWER_UNIT,
Zhang Rui33823882019-07-10 21:44:30 +0800701 RAPL_PRIMITIVE_DERIVED),
Jacob Pan2d281d82013-10-17 10:28:35 -0700702 {NULL, 0, 0, 0},
703};
704
Zhang Rui931da6a2021-12-07 21:17:34 +0800705static enum rapl_primitives
706prim_fixups(struct rapl_domain *rd, enum rapl_primitives prim)
707{
708 if (!rapl_defaults->spr_psys_bits)
709 return prim;
710
711 if (rd->id != RAPL_DOMAIN_PLATFORM)
712 return prim;
713
714 switch (prim) {
715 case POWER_LIMIT1:
716 return PSYS_POWER_LIMIT1;
717 case POWER_LIMIT2:
718 return PSYS_POWER_LIMIT2;
719 case PL1_ENABLE:
720 return PSYS_PL1_ENABLE;
721 case PL2_ENABLE:
722 return PSYS_PL2_ENABLE;
723 case TIME_WINDOW1:
724 return PSYS_TIME_WINDOW1;
725 case TIME_WINDOW2:
726 return PSYS_TIME_WINDOW2;
727 default:
728 return prim;
729 }
730}
731
Jacob Pan2d281d82013-10-17 10:28:35 -0700732/* Read primitive data based on its related struct rapl_primitive_info.
733 * if xlate flag is set, return translated data based on data units, i.e.
734 * time, energy, and power.
735 * RAPL MSRs are non-architectual and are laid out not consistently across
736 * domains. Here we use primitive info to allow writing consolidated access
737 * functions.
738 * For a given primitive, it is processed by MSR mask and shift. Unit conversion
739 * is pre-assigned based on RAPL unit MSRs read at init time.
740 * 63-------------------------- 31--------------------------- 0
741 * | xxxxx (mask) |
742 * | |<- shift ----------------|
743 * 63-------------------------- 31--------------------------- 0
744 */
745static int rapl_read_data_raw(struct rapl_domain *rd,
Zhang Rui33823882019-07-10 21:44:30 +0800746 enum rapl_primitives prim, bool xlate, u64 *data)
Jacob Pan2d281d82013-10-17 10:28:35 -0700747{
Zhang Ruibeea8df2019-07-10 21:44:27 +0800748 u64 value;
Zhang Rui931da6a2021-12-07 21:17:34 +0800749 enum rapl_primitives prim_fixed = prim_fixups(rd, prim);
750 struct rapl_primitive_info *rp = &rpi[prim_fixed];
Zhang Ruibeea8df2019-07-10 21:44:27 +0800751 struct reg_action ra;
Jacob Pan2d281d82013-10-17 10:28:35 -0700752 int cpu;
753
754 if (!rp->name || rp->flag & RAPL_PRIMITIVE_DUMMY)
755 return -EINVAL;
756
Zhang Ruibeea8df2019-07-10 21:44:27 +0800757 ra.reg = rd->regs[rp->id];
758 if (!ra.reg)
Jacob Pan2d281d82013-10-17 10:28:35 -0700759 return -EINVAL;
Jacob Pan323ee642016-02-24 13:31:38 -0800760
761 cpu = rd->rp->lead_cpu;
Jacob Pan2d281d82013-10-17 10:28:35 -0700762
Zhang Rui0c2dded2019-07-10 21:44:32 +0800763 /* domain with 2 limits has different bit */
764 if (prim == FW_LOCK && rd->rp->priv->limits[rd->id] == 2) {
765 rp->mask = POWER_HIGH_LOCK;
Jacob Pan2d281d82013-10-17 10:28:35 -0700766 rp->shift = 63;
767 }
768 /* non-hardware data are collected by the polling thread */
769 if (rp->flag & RAPL_PRIMITIVE_DERIVED) {
770 *data = rd->rdd.primitives[prim];
771 return 0;
772 }
773
Zhang Ruibeea8df2019-07-10 21:44:27 +0800774 ra.mask = rp->mask;
775
776 if (rd->rp->priv->read_raw(cpu, &ra)) {
Zhang Ruid978e752019-07-10 21:44:31 +0800777 pr_debug("failed to read reg 0x%llx on cpu %d\n", ra.reg, cpu);
Jacob Pan2d281d82013-10-17 10:28:35 -0700778 return -EIO;
779 }
780
Zhang Ruibeea8df2019-07-10 21:44:27 +0800781 value = ra.value >> rp->shift;
782
Jacob Pan2d281d82013-10-17 10:28:35 -0700783 if (xlate)
Zhang Ruibeea8df2019-07-10 21:44:27 +0800784 *data = rapl_unit_xlate(rd, rp->unit, value, 0);
Jacob Pan2d281d82013-10-17 10:28:35 -0700785 else
Zhang Ruibeea8df2019-07-10 21:44:27 +0800786 *data = value;
Jacob Pan2d281d82013-10-17 10:28:35 -0700787
788 return 0;
789}
790
791/* Similar use of primitive info in the read counterpart */
792static int rapl_write_data_raw(struct rapl_domain *rd,
Zhang Rui33823882019-07-10 21:44:30 +0800793 enum rapl_primitives prim,
794 unsigned long long value)
Jacob Pan2d281d82013-10-17 10:28:35 -0700795{
Zhang Rui931da6a2021-12-07 21:17:34 +0800796 enum rapl_primitives prim_fixed = prim_fixups(rd, prim);
797 struct rapl_primitive_info *rp = &rpi[prim_fixed];
Jacob Pan2d281d82013-10-17 10:28:35 -0700798 int cpu;
Jacob Panf14a1392016-02-24 13:31:36 -0800799 u64 bits;
Zhang Ruibeea8df2019-07-10 21:44:27 +0800800 struct reg_action ra;
Jacob Panf14a1392016-02-24 13:31:36 -0800801 int ret;
Jacob Pan2d281d82013-10-17 10:28:35 -0700802
Jacob Pan323ee642016-02-24 13:31:38 -0800803 cpu = rd->rp->lead_cpu;
Jacob Pan309557f2016-02-24 13:31:37 -0800804 bits = rapl_unit_xlate(rd, rp->unit, value, 1);
Adam Lessnauedbdabc2017-06-01 11:21:50 +0200805 bits <<= rp->shift;
806 bits &= rp->mask;
807
Zhang Ruibeea8df2019-07-10 21:44:27 +0800808 memset(&ra, 0, sizeof(ra));
Jacob Panf14a1392016-02-24 13:31:36 -0800809
Zhang Ruibeea8df2019-07-10 21:44:27 +0800810 ra.reg = rd->regs[rp->id];
811 ra.mask = rp->mask;
812 ra.value = bits;
Jacob Panf14a1392016-02-24 13:31:36 -0800813
Zhang Ruibeea8df2019-07-10 21:44:27 +0800814 ret = rd->rp->priv->write_raw(cpu, &ra);
Jacob Panf14a1392016-02-24 13:31:36 -0800815
816 return ret;
Jacob Pan2d281d82013-10-17 10:28:35 -0700817}
818
Jacob Pan3c2c0842014-11-07 09:29:26 -0800819/*
820 * Raw RAPL data stored in MSRs are in certain scales. We need to
821 * convert them into standard units based on the units reported in
822 * the RAPL unit MSRs. This is specific to CPUs as the method to
823 * calculate units differ on different CPUs.
824 * We convert the units to below format based on CPUs.
825 * i.e.
Jacob Pand474a4d2015-03-13 03:48:56 -0700826 * energy unit: picoJoules : Represented in picoJoules by default
Jacob Pan3c2c0842014-11-07 09:29:26 -0800827 * power unit : microWatts : Represented in milliWatts by default
828 * time unit : microseconds: Represented in seconds by default
829 */
830static int rapl_check_unit_core(struct rapl_package *rp, int cpu)
Jacob Pan2d281d82013-10-17 10:28:35 -0700831{
Zhang Rui1193b162019-07-10 21:44:29 +0800832 struct reg_action ra;
Jacob Pan2d281d82013-10-17 10:28:35 -0700833 u32 value;
834
Zhang Rui1193b162019-07-10 21:44:29 +0800835 ra.reg = rp->priv->reg_unit;
836 ra.mask = ~0;
837 if (rp->priv->read_raw(cpu, &ra)) {
Zhang Ruid978e752019-07-10 21:44:31 +0800838 pr_err("Failed to read power unit REG 0x%llx on CPU %d, exit.\n",
Zhang Rui33823882019-07-10 21:44:30 +0800839 rp->priv->reg_unit, cpu);
Jacob Pan2d281d82013-10-17 10:28:35 -0700840 return -ENODEV;
841 }
842
Zhang Rui1193b162019-07-10 21:44:29 +0800843 value = (ra.value & ENERGY_UNIT_MASK) >> ENERGY_UNIT_OFFSET;
Jacob Pand474a4d2015-03-13 03:48:56 -0700844 rp->energy_unit = ENERGY_UNIT_SCALE * 1000000 / (1 << value);
Jacob Pan2d281d82013-10-17 10:28:35 -0700845
Zhang Rui1193b162019-07-10 21:44:29 +0800846 value = (ra.value & POWER_UNIT_MASK) >> POWER_UNIT_OFFSET;
Jacob Pan3c2c0842014-11-07 09:29:26 -0800847 rp->power_unit = 1000000 / (1 << value);
Jacob Pan2d281d82013-10-17 10:28:35 -0700848
Zhang Rui1193b162019-07-10 21:44:29 +0800849 value = (ra.value & TIME_UNIT_MASK) >> TIME_UNIT_OFFSET;
Jacob Pan3c2c0842014-11-07 09:29:26 -0800850 rp->time_unit = 1000000 / (1 << value);
Jacob Pan2d281d82013-10-17 10:28:35 -0700851
Zhang Rui9ea76122019-05-13 13:58:53 -0400852 pr_debug("Core CPU %s energy=%dpJ, time=%dus, power=%duW\n",
Zhang Rui33823882019-07-10 21:44:30 +0800853 rp->name, rp->energy_unit, rp->time_unit, rp->power_unit);
Jacob Pan2d281d82013-10-17 10:28:35 -0700854
855 return 0;
856}
857
Jacob Pan3c2c0842014-11-07 09:29:26 -0800858static int rapl_check_unit_atom(struct rapl_package *rp, int cpu)
859{
Zhang Rui1193b162019-07-10 21:44:29 +0800860 struct reg_action ra;
Jacob Pan3c2c0842014-11-07 09:29:26 -0800861 u32 value;
862
Zhang Rui1193b162019-07-10 21:44:29 +0800863 ra.reg = rp->priv->reg_unit;
864 ra.mask = ~0;
865 if (rp->priv->read_raw(cpu, &ra)) {
Zhang Ruid978e752019-07-10 21:44:31 +0800866 pr_err("Failed to read power unit REG 0x%llx on CPU %d, exit.\n",
Zhang Rui33823882019-07-10 21:44:30 +0800867 rp->priv->reg_unit, cpu);
Jacob Pan3c2c0842014-11-07 09:29:26 -0800868 return -ENODEV;
869 }
Zhang Rui1193b162019-07-10 21:44:29 +0800870
871 value = (ra.value & ENERGY_UNIT_MASK) >> ENERGY_UNIT_OFFSET;
Jacob Pand474a4d2015-03-13 03:48:56 -0700872 rp->energy_unit = ENERGY_UNIT_SCALE * 1 << value;
Jacob Pan3c2c0842014-11-07 09:29:26 -0800873
Zhang Rui1193b162019-07-10 21:44:29 +0800874 value = (ra.value & POWER_UNIT_MASK) >> POWER_UNIT_OFFSET;
Jacob Pan3c2c0842014-11-07 09:29:26 -0800875 rp->power_unit = (1 << value) * 1000;
876
Zhang Rui1193b162019-07-10 21:44:29 +0800877 value = (ra.value & TIME_UNIT_MASK) >> TIME_UNIT_OFFSET;
Jacob Pan3c2c0842014-11-07 09:29:26 -0800878 rp->time_unit = 1000000 / (1 << value);
879
Zhang Rui9ea76122019-05-13 13:58:53 -0400880 pr_debug("Atom %s energy=%dpJ, time=%dus, power=%duW\n",
Zhang Rui33823882019-07-10 21:44:30 +0800881 rp->name, rp->energy_unit, rp->time_unit, rp->power_unit);
Jacob Pan3c2c0842014-11-07 09:29:26 -0800882
883 return 0;
884}
885
Jacob Panf14a1392016-02-24 13:31:36 -0800886static void power_limit_irq_save_cpu(void *info)
887{
888 u32 l, h = 0;
889 struct rapl_package *rp = (struct rapl_package *)info;
890
891 /* save the state of PLN irq mask bit before disabling it */
892 rdmsr_safe(MSR_IA32_PACKAGE_THERM_INTERRUPT, &l, &h);
893 if (!(rp->power_limit_irq & PACKAGE_PLN_INT_SAVED)) {
894 rp->power_limit_irq = l & PACKAGE_THERM_INT_PLN_ENABLE;
895 rp->power_limit_irq |= PACKAGE_PLN_INT_SAVED;
896 }
897 l &= ~PACKAGE_THERM_INT_PLN_ENABLE;
898 wrmsr_safe(MSR_IA32_PACKAGE_THERM_INTERRUPT, l, h);
899}
900
Jacob Pan2d281d82013-10-17 10:28:35 -0700901/* REVISIT:
902 * When package power limit is set artificially low by RAPL, LVT
903 * thermal interrupt for package power limit should be ignored
904 * since we are not really exceeding the real limit. The intention
905 * is to avoid excessive interrupts while we are trying to save power.
906 * A useful feature might be routing the package_power_limit interrupt
907 * to userspace via eventfd. once we have a usecase, this is simple
908 * to do by adding an atomic notifier.
909 */
910
Jacob Pan309557f2016-02-24 13:31:37 -0800911static void package_power_limit_irq_save(struct rapl_package *rp)
Jacob Pan2d281d82013-10-17 10:28:35 -0700912{
Jacob Pan2d281d82013-10-17 10:28:35 -0700913 if (!boot_cpu_has(X86_FEATURE_PTS) || !boot_cpu_has(X86_FEATURE_PLN))
914 return;
915
Jacob Pan323ee642016-02-24 13:31:38 -0800916 smp_call_function_single(rp->lead_cpu, power_limit_irq_save_cpu, rp, 1);
Jacob Panf14a1392016-02-24 13:31:36 -0800917}
918
Thomas Gleixner58705062016-11-22 21:16:02 +0000919/*
920 * Restore per package power limit interrupt enable state. Called from cpu
921 * hotplug code on package removal.
922 */
923static void package_power_limit_irq_restore(struct rapl_package *rp)
Jacob Panf14a1392016-02-24 13:31:36 -0800924{
Thomas Gleixner58705062016-11-22 21:16:02 +0000925 u32 l, h;
926
927 if (!boot_cpu_has(X86_FEATURE_PTS) || !boot_cpu_has(X86_FEATURE_PLN))
928 return;
929
930 /* irq enable state not saved, nothing to restore */
931 if (!(rp->power_limit_irq & PACKAGE_PLN_INT_SAVED))
932 return;
Jacob Panf14a1392016-02-24 13:31:36 -0800933
934 rdmsr_safe(MSR_IA32_PACKAGE_THERM_INTERRUPT, &l, &h);
935
936 if (rp->power_limit_irq & PACKAGE_THERM_INT_PLN_ENABLE)
937 l |= PACKAGE_THERM_INT_PLN_ENABLE;
938 else
939 l &= ~PACKAGE_THERM_INT_PLN_ENABLE;
940
941 wrmsr_safe(MSR_IA32_PACKAGE_THERM_INTERRUPT, l, h);
Jacob Pan2d281d82013-10-17 10:28:35 -0700942}
943
Jacob Pan3c2c0842014-11-07 09:29:26 -0800944static void set_floor_freq_default(struct rapl_domain *rd, bool mode)
945{
946 int nr_powerlimit = find_nr_power_limit(rd);
947
948 /* always enable clamp such that p-state can go below OS requested
949 * range. power capping priority over guranteed frequency.
950 */
951 rapl_write_data_raw(rd, PL1_CLAMP, mode);
952
953 /* some domains have pl2 */
954 if (nr_powerlimit > 1) {
955 rapl_write_data_raw(rd, PL2_ENABLE, mode);
956 rapl_write_data_raw(rd, PL2_CLAMP, mode);
957 }
958}
959
960static void set_floor_freq_atom(struct rapl_domain *rd, bool enable)
961{
962 static u32 power_ctrl_orig_val;
963 u32 mdata;
964
Ajay Thomas51b63402015-04-30 01:43:23 +0530965 if (!rapl_defaults->floor_freq_reg_addr) {
966 pr_err("Invalid floor frequency config register\n");
967 return;
968 }
969
Jacob Pan3c2c0842014-11-07 09:29:26 -0800970 if (!power_ctrl_orig_val)
Andy Shevchenko4077a382015-11-11 19:59:29 +0200971 iosf_mbi_read(BT_MBI_UNIT_PMC, MBI_CR_READ,
972 rapl_defaults->floor_freq_reg_addr,
973 &power_ctrl_orig_val);
Jacob Pan3c2c0842014-11-07 09:29:26 -0800974 mdata = power_ctrl_orig_val;
975 if (enable) {
976 mdata &= ~(0x7f << 8);
977 mdata |= 1 << 8;
978 }
Andy Shevchenko4077a382015-11-11 19:59:29 +0200979 iosf_mbi_write(BT_MBI_UNIT_PMC, MBI_CR_WRITE,
980 rapl_defaults->floor_freq_reg_addr, mdata);
Jacob Pan3c2c0842014-11-07 09:29:26 -0800981}
982
983static u64 rapl_compute_time_window_core(struct rapl_package *rp, u64 value,
Zhang Rui33823882019-07-10 21:44:30 +0800984 bool to_raw)
Jacob Pan3c2c0842014-11-07 09:29:26 -0800985{
Zhang Rui33823882019-07-10 21:44:30 +0800986 u64 f, y; /* fraction and exp. used for time unit */
Jacob Pan3c2c0842014-11-07 09:29:26 -0800987
988 /*
989 * Special processing based on 2^Y*(1+F/4), refer
990 * to Intel Software Developer's manual Vol.3B: CH 14.9.3.
991 */
992 if (!to_raw) {
993 f = (value & 0x60) >> 5;
994 y = value & 0x1f;
995 value = (1 << y) * (4 + f) * rp->time_unit / 4;
996 } else {
997 do_div(value, rp->time_unit);
998 y = ilog2(value);
999 f = div64_u64(4 * (value - (1 << y)), 1 << y);
1000 value = (y & 0x1f) | ((f & 0x3) << 5);
1001 }
1002 return value;
1003}
1004
1005static u64 rapl_compute_time_window_atom(struct rapl_package *rp, u64 value,
Zhang Rui33823882019-07-10 21:44:30 +08001006 bool to_raw)
Jacob Pan3c2c0842014-11-07 09:29:26 -08001007{
1008 /*
1009 * Atom time unit encoding is straight forward val * time_unit,
1010 * where time_unit is default to 1 sec. Never 0.
1011 */
1012 if (!to_raw)
1013 return (value) ? value *= rp->time_unit : rp->time_unit;
Zhang Rui33823882019-07-10 21:44:30 +08001014
1015 value = div64_u64(value, rp->time_unit);
Jacob Pan3c2c0842014-11-07 09:29:26 -08001016
1017 return value;
1018}
1019
Jacob Pan087e9cb2014-11-07 09:29:25 -08001020static const struct rapl_defaults rapl_defaults_core = {
Ajay Thomas51b63402015-04-30 01:43:23 +05301021 .floor_freq_reg_addr = 0,
Jacob Pan3c2c0842014-11-07 09:29:26 -08001022 .check_unit = rapl_check_unit_core,
1023 .set_floor_freq = set_floor_freq_default,
1024 .compute_time_window = rapl_compute_time_window_core,
Jacob Pan087e9cb2014-11-07 09:29:25 -08001025};
1026
Jacob Pand474a4d2015-03-13 03:48:56 -07001027static const struct rapl_defaults rapl_defaults_hsw_server = {
1028 .check_unit = rapl_check_unit_core,
1029 .set_floor_freq = set_floor_freq_default,
1030 .compute_time_window = rapl_compute_time_window_core,
1031 .dram_domain_energy_unit = 15300,
1032};
1033
Zhang Rui2d798d92020-06-29 13:34:50 +08001034static const struct rapl_defaults rapl_defaults_spr_server = {
1035 .check_unit = rapl_check_unit_core,
1036 .set_floor_freq = set_floor_freq_default,
1037 .compute_time_window = rapl_compute_time_window_core,
1038 .dram_domain_energy_unit = 15300,
1039 .psys_domain_energy_unit = 1000000000,
Zhang Rui931da6a2021-12-07 21:17:34 +08001040 .spr_psys_bits = true,
Zhang Rui2d798d92020-06-29 13:34:50 +08001041};
1042
Ajay Thomas51b63402015-04-30 01:43:23 +05301043static const struct rapl_defaults rapl_defaults_byt = {
1044 .floor_freq_reg_addr = IOSF_CPU_POWER_BUDGET_CTL_BYT,
Jacob Pan3c2c0842014-11-07 09:29:26 -08001045 .check_unit = rapl_check_unit_atom,
1046 .set_floor_freq = set_floor_freq_atom,
1047 .compute_time_window = rapl_compute_time_window_atom,
Jacob Pan087e9cb2014-11-07 09:29:25 -08001048};
1049
Ajay Thomas51b63402015-04-30 01:43:23 +05301050static const struct rapl_defaults rapl_defaults_tng = {
1051 .floor_freq_reg_addr = IOSF_CPU_POWER_BUDGET_CTL_TNG,
1052 .check_unit = rapl_check_unit_atom,
1053 .set_floor_freq = set_floor_freq_atom,
1054 .compute_time_window = rapl_compute_time_window_atom,
1055};
1056
1057static const struct rapl_defaults rapl_defaults_ann = {
1058 .floor_freq_reg_addr = 0,
1059 .check_unit = rapl_check_unit_atom,
1060 .set_floor_freq = NULL,
1061 .compute_time_window = rapl_compute_time_window_atom,
1062};
1063
1064static const struct rapl_defaults rapl_defaults_cht = {
1065 .floor_freq_reg_addr = 0,
1066 .check_unit = rapl_check_unit_atom,
1067 .set_floor_freq = NULL,
1068 .compute_time_window = rapl_compute_time_window_atom,
1069};
1070
Victor Ding43756a22020-10-27 07:23:56 +00001071static const struct rapl_defaults rapl_defaults_amd = {
1072 .check_unit = rapl_check_unit_core,
1073};
1074
Mathias Krauseea85dbc2015-03-25 22:15:52 +01001075static const struct x86_cpu_id rapl_ids[] __initconst = {
Thomas Gleixnerf0722512020-03-20 14:14:03 +01001076 X86_MATCH_INTEL_FAM6_MODEL(SANDYBRIDGE, &rapl_defaults_core),
1077 X86_MATCH_INTEL_FAM6_MODEL(SANDYBRIDGE_X, &rapl_defaults_core),
Dave Hansen0bb04b52016-06-02 17:19:37 -07001078
Thomas Gleixnerf0722512020-03-20 14:14:03 +01001079 X86_MATCH_INTEL_FAM6_MODEL(IVYBRIDGE, &rapl_defaults_core),
1080 X86_MATCH_INTEL_FAM6_MODEL(IVYBRIDGE_X, &rapl_defaults_core),
Dave Hansen0bb04b52016-06-02 17:19:37 -07001081
Thomas Gleixnerf0722512020-03-20 14:14:03 +01001082 X86_MATCH_INTEL_FAM6_MODEL(HASWELL, &rapl_defaults_core),
1083 X86_MATCH_INTEL_FAM6_MODEL(HASWELL_L, &rapl_defaults_core),
1084 X86_MATCH_INTEL_FAM6_MODEL(HASWELL_G, &rapl_defaults_core),
1085 X86_MATCH_INTEL_FAM6_MODEL(HASWELL_X, &rapl_defaults_hsw_server),
Dave Hansen0bb04b52016-06-02 17:19:37 -07001086
Thomas Gleixnerf0722512020-03-20 14:14:03 +01001087 X86_MATCH_INTEL_FAM6_MODEL(BROADWELL, &rapl_defaults_core),
1088 X86_MATCH_INTEL_FAM6_MODEL(BROADWELL_G, &rapl_defaults_core),
1089 X86_MATCH_INTEL_FAM6_MODEL(BROADWELL_D, &rapl_defaults_core),
1090 X86_MATCH_INTEL_FAM6_MODEL(BROADWELL_X, &rapl_defaults_hsw_server),
Dave Hansen0bb04b52016-06-02 17:19:37 -07001091
Thomas Gleixnerf0722512020-03-20 14:14:03 +01001092 X86_MATCH_INTEL_FAM6_MODEL(SKYLAKE, &rapl_defaults_core),
1093 X86_MATCH_INTEL_FAM6_MODEL(SKYLAKE_L, &rapl_defaults_core),
1094 X86_MATCH_INTEL_FAM6_MODEL(SKYLAKE_X, &rapl_defaults_hsw_server),
1095 X86_MATCH_INTEL_FAM6_MODEL(KABYLAKE_L, &rapl_defaults_core),
1096 X86_MATCH_INTEL_FAM6_MODEL(KABYLAKE, &rapl_defaults_core),
1097 X86_MATCH_INTEL_FAM6_MODEL(CANNONLAKE_L, &rapl_defaults_core),
1098 X86_MATCH_INTEL_FAM6_MODEL(ICELAKE_L, &rapl_defaults_core),
1099 X86_MATCH_INTEL_FAM6_MODEL(ICELAKE, &rapl_defaults_core),
1100 X86_MATCH_INTEL_FAM6_MODEL(ICELAKE_NNPI, &rapl_defaults_core),
1101 X86_MATCH_INTEL_FAM6_MODEL(ICELAKE_X, &rapl_defaults_hsw_server),
1102 X86_MATCH_INTEL_FAM6_MODEL(ICELAKE_D, &rapl_defaults_hsw_server),
1103 X86_MATCH_INTEL_FAM6_MODEL(COMETLAKE_L, &rapl_defaults_core),
1104 X86_MATCH_INTEL_FAM6_MODEL(COMETLAKE, &rapl_defaults_core),
1105 X86_MATCH_INTEL_FAM6_MODEL(TIGERLAKE_L, &rapl_defaults_core),
Zhang Rui57a2fb02020-09-10 15:48:58 +08001106 X86_MATCH_INTEL_FAM6_MODEL(TIGERLAKE, &rapl_defaults_core),
Zhang Rui64e5f362020-09-10 15:49:11 +08001107 X86_MATCH_INTEL_FAM6_MODEL(ROCKETLAKE, &rapl_defaults_core),
Zhang Ruiba92a422020-09-10 15:49:21 +08001108 X86_MATCH_INTEL_FAM6_MODEL(ALDERLAKE, &rapl_defaults_core),
Zhang Ruicca26b62021-01-27 13:42:27 +08001109 X86_MATCH_INTEL_FAM6_MODEL(ALDERLAKE_L, &rapl_defaults_core),
Zhang Rui2d798d92020-06-29 13:34:50 +08001110 X86_MATCH_INTEL_FAM6_MODEL(SAPPHIRERAPIDS_X, &rapl_defaults_spr_server),
Ricardo Nerie1c2d962020-08-21 11:48:10 -07001111 X86_MATCH_INTEL_FAM6_MODEL(LAKEFIELD, &rapl_defaults_core),
Dave Hansen0bb04b52016-06-02 17:19:37 -07001112
Thomas Gleixnerf0722512020-03-20 14:14:03 +01001113 X86_MATCH_INTEL_FAM6_MODEL(ATOM_SILVERMONT, &rapl_defaults_byt),
1114 X86_MATCH_INTEL_FAM6_MODEL(ATOM_AIRMONT, &rapl_defaults_cht),
1115 X86_MATCH_INTEL_FAM6_MODEL(ATOM_SILVERMONT_MID, &rapl_defaults_tng),
1116 X86_MATCH_INTEL_FAM6_MODEL(ATOM_AIRMONT_MID, &rapl_defaults_ann),
1117 X86_MATCH_INTEL_FAM6_MODEL(ATOM_GOLDMONT, &rapl_defaults_core),
1118 X86_MATCH_INTEL_FAM6_MODEL(ATOM_GOLDMONT_PLUS, &rapl_defaults_core),
1119 X86_MATCH_INTEL_FAM6_MODEL(ATOM_GOLDMONT_D, &rapl_defaults_core),
Jacob Pan33c98002020-05-15 15:30:41 +08001120 X86_MATCH_INTEL_FAM6_MODEL(ATOM_TREMONT, &rapl_defaults_core),
Thomas Gleixnerf0722512020-03-20 14:14:03 +01001121 X86_MATCH_INTEL_FAM6_MODEL(ATOM_TREMONT_D, &rapl_defaults_core),
1122 X86_MATCH_INTEL_FAM6_MODEL(ATOM_TREMONT_L, &rapl_defaults_core),
Dave Hansen0bb04b52016-06-02 17:19:37 -07001123
Thomas Gleixnerf0722512020-03-20 14:14:03 +01001124 X86_MATCH_INTEL_FAM6_MODEL(XEON_PHI_KNL, &rapl_defaults_hsw_server),
1125 X86_MATCH_INTEL_FAM6_MODEL(XEON_PHI_KNM, &rapl_defaults_hsw_server),
Victor Ding43756a22020-10-27 07:23:56 +00001126
1127 X86_MATCH_VENDOR_FAM(AMD, 0x17, &rapl_defaults_amd),
Kim Phillips8a9d8812020-10-27 07:23:57 +00001128 X86_MATCH_VENDOR_FAM(AMD, 0x19, &rapl_defaults_amd),
Pu Wena7405612021-03-02 10:01:08 +08001129 X86_MATCH_VENDOR_FAM(HYGON, 0x18, &rapl_defaults_amd),
Jacob Pan2d281d82013-10-17 10:28:35 -07001130 {}
1131};
1132MODULE_DEVICE_TABLE(x86cpu, rapl_ids);
1133
Thomas Gleixnerbed5ab62016-11-22 21:15:58 +00001134/* Read once for all raw primitive data for domains */
1135static void rapl_update_domain_data(struct rapl_package *rp)
Jacob Pan2d281d82013-10-17 10:28:35 -07001136{
1137 int dmn, prim;
1138 u64 val;
Jacob Pan2d281d82013-10-17 10:28:35 -07001139
Thomas Gleixnerbed5ab62016-11-22 21:15:58 +00001140 for (dmn = 0; dmn < rp->nr_domains; dmn++) {
Zhang Rui9ea76122019-05-13 13:58:53 -04001141 pr_debug("update %s domain %s data\n", rp->name,
Thomas Gleixnerbed5ab62016-11-22 21:15:58 +00001142 rp->domains[dmn].name);
1143 /* exclude non-raw primitives */
1144 for (prim = 0; prim < NR_RAW_PRIMITIVES; prim++) {
1145 if (!rapl_read_data_raw(&rp->domains[dmn], prim,
1146 rpi[prim].unit, &val))
Zhang Rui33823882019-07-10 21:44:30 +08001147 rp->domains[dmn].rdd.primitives[prim] = val;
Jacob Pan2d281d82013-10-17 10:28:35 -07001148 }
1149 }
1150
1151}
1152
Jacob Pan2d281d82013-10-17 10:28:35 -07001153static int rapl_package_register_powercap(struct rapl_package *rp)
1154{
1155 struct rapl_domain *rd;
Jacob Pan2d281d82013-10-17 10:28:35 -07001156 struct powercap_zone *power_zone = NULL;
Luis de Bethencourt01857cf2018-01-17 10:30:34 +00001157 int nr_pl, ret;
Thomas Gleixnerbed5ab62016-11-22 21:15:58 +00001158
1159 /* Update the domain data of the new package */
1160 rapl_update_domain_data(rp);
Jacob Pan2d281d82013-10-17 10:28:35 -07001161
Zhang Rui33823882019-07-10 21:44:30 +08001162 /* first we register package domain as the parent zone */
Jacob Pan2d281d82013-10-17 10:28:35 -07001163 for (rd = rp->domains; rd < rp->domains + rp->nr_domains; rd++) {
1164 if (rd->id == RAPL_DOMAIN_PACKAGE) {
1165 nr_pl = find_nr_power_limit(rd);
Zhang Rui9ea76122019-05-13 13:58:53 -04001166 pr_debug("register package domain %s\n", rp->name);
Jacob Pan2d281d82013-10-17 10:28:35 -07001167 power_zone = powercap_register_zone(&rd->power_zone,
Zhang Rui33823882019-07-10 21:44:30 +08001168 rp->priv->control_type, rp->name,
1169 NULL, &zone_ops[rd->id], nr_pl,
1170 &constraint_ops);
Jacob Pan2d281d82013-10-17 10:28:35 -07001171 if (IS_ERR(power_zone)) {
Zhang Rui9ea76122019-05-13 13:58:53 -04001172 pr_debug("failed to register power zone %s\n",
Zhang Rui33823882019-07-10 21:44:30 +08001173 rp->name);
Thomas Gleixnerbed5ab62016-11-22 21:15:58 +00001174 return PTR_ERR(power_zone);
Jacob Pan2d281d82013-10-17 10:28:35 -07001175 }
1176 /* track parent zone in per package/socket data */
1177 rp->power_zone = power_zone;
1178 /* done, only one package domain per socket */
1179 break;
1180 }
1181 }
1182 if (!power_zone) {
1183 pr_err("no package domain found, unknown topology!\n");
Thomas Gleixnerbed5ab62016-11-22 21:15:58 +00001184 return -ENODEV;
Jacob Pan2d281d82013-10-17 10:28:35 -07001185 }
Zhang Rui33823882019-07-10 21:44:30 +08001186 /* now register domains as children of the socket/package */
Jacob Pan2d281d82013-10-17 10:28:35 -07001187 for (rd = rp->domains; rd < rp->domains + rp->nr_domains; rd++) {
Zhang Ruif1e8d752020-10-13 15:42:41 +08001188 struct powercap_zone *parent = rp->power_zone;
1189
Jacob Pan2d281d82013-10-17 10:28:35 -07001190 if (rd->id == RAPL_DOMAIN_PACKAGE)
1191 continue;
Zhang Ruif1e8d752020-10-13 15:42:41 +08001192 if (rd->id == RAPL_DOMAIN_PLATFORM)
1193 parent = NULL;
Jacob Pan2d281d82013-10-17 10:28:35 -07001194 /* number of power limits per domain varies */
1195 nr_pl = find_nr_power_limit(rd);
1196 power_zone = powercap_register_zone(&rd->power_zone,
Zhang Rui33823882019-07-10 21:44:30 +08001197 rp->priv->control_type,
Zhang Ruif1e8d752020-10-13 15:42:41 +08001198 rd->name, parent,
Zhang Rui33823882019-07-10 21:44:30 +08001199 &zone_ops[rd->id], nr_pl,
1200 &constraint_ops);
Jacob Pan2d281d82013-10-17 10:28:35 -07001201
1202 if (IS_ERR(power_zone)) {
Zhang Rui9ea76122019-05-13 13:58:53 -04001203 pr_debug("failed to register power_zone, %s:%s\n",
Zhang Rui33823882019-07-10 21:44:30 +08001204 rp->name, rd->name);
Jacob Pan2d281d82013-10-17 10:28:35 -07001205 ret = PTR_ERR(power_zone);
1206 goto err_cleanup;
1207 }
1208 }
Thomas Gleixnerbed5ab62016-11-22 21:15:58 +00001209 return 0;
Jacob Pan2d281d82013-10-17 10:28:35 -07001210
Jacob Pan2d281d82013-10-17 10:28:35 -07001211err_cleanup:
Thomas Gleixner58705062016-11-22 21:16:02 +00001212 /*
1213 * Clean up previously initialized domains within the package if we
Jacob Pan2d281d82013-10-17 10:28:35 -07001214 * failed after the first domain setup.
1215 */
1216 while (--rd >= rp->domains) {
Zhang Rui9ea76122019-05-13 13:58:53 -04001217 pr_debug("unregister %s domain %s\n", rp->name, rd->name);
Zhang Rui33823882019-07-10 21:44:30 +08001218 powercap_unregister_zone(rp->priv->control_type,
1219 &rd->power_zone);
Jacob Pan2d281d82013-10-17 10:28:35 -07001220 }
1221
1222 return ret;
1223}
1224
Zhang Rui7fde2712019-07-10 21:44:26 +08001225static int rapl_check_domain(int cpu, int domain, struct rapl_package *rp)
Jacob Pan2d281d82013-10-17 10:28:35 -07001226{
Zhang Rui1193b162019-07-10 21:44:29 +08001227 struct reg_action ra;
Jacob Pan2d281d82013-10-17 10:28:35 -07001228
1229 switch (domain) {
1230 case RAPL_DOMAIN_PACKAGE:
Jacob Pan2d281d82013-10-17 10:28:35 -07001231 case RAPL_DOMAIN_PP0:
Jacob Pan2d281d82013-10-17 10:28:35 -07001232 case RAPL_DOMAIN_PP1:
Jacob Pan2d281d82013-10-17 10:28:35 -07001233 case RAPL_DOMAIN_DRAM:
Zhang Ruif1e8d752020-10-13 15:42:41 +08001234 case RAPL_DOMAIN_PLATFORM:
Zhang Rui1193b162019-07-10 21:44:29 +08001235 ra.reg = rp->priv->regs[domain][RAPL_DOMAIN_REG_STATUS];
Jacob Pan2d281d82013-10-17 10:28:35 -07001236 break;
1237 default:
1238 pr_err("invalid domain id %d\n", domain);
1239 return -EINVAL;
1240 }
Jacob Pan9d31c672014-04-29 15:33:06 -07001241 /* make sure domain counters are available and contains non-zero
1242 * values, otherwise skip it.
1243 */
Zhang Rui1193b162019-07-10 21:44:29 +08001244
Zhang Rui7a57e9f2020-10-13 15:42:40 +08001245 ra.mask = ENERGY_STATUS_MASK;
Zhang Rui1193b162019-07-10 21:44:29 +08001246 if (rp->priv->read_raw(cpu, &ra) || !ra.value)
Jacob Pan2d281d82013-10-17 10:28:35 -07001247 return -ENODEV;
1248
Jacob Pan9d31c672014-04-29 15:33:06 -07001249 return 0;
Jacob Pan2d281d82013-10-17 10:28:35 -07001250}
1251
Jacob Pane1399ba2016-05-31 13:41:29 -07001252/*
1253 * Check if power limits are available. Two cases when they are not available:
1254 * 1. Locked by BIOS, in this case we still provide read-only access so that
1255 * users can see what limit is set by the BIOS.
1256 * 2. Some CPUs make some domains monitoring only which means PLx MSRs may not
Zhang Rui33823882019-07-10 21:44:30 +08001257 * exist at all. In this case, we do not show the constraints in powercap.
Jacob Pane1399ba2016-05-31 13:41:29 -07001258 *
1259 * Called after domains are detected and initialized.
1260 */
1261static void rapl_detect_powerlimit(struct rapl_domain *rd)
1262{
1263 u64 val64;
1264 int i;
1265
1266 /* check if the domain is locked by BIOS, ignore if MSR doesn't exist */
1267 if (!rapl_read_data_raw(rd, FW_LOCK, false, &val64)) {
1268 if (val64) {
Zhang Rui9ea76122019-05-13 13:58:53 -04001269 pr_info("RAPL %s domain %s locked by BIOS\n",
1270 rd->rp->name, rd->name);
Jacob Pane1399ba2016-05-31 13:41:29 -07001271 rd->state |= DOMAIN_STATE_BIOS_LOCKED;
1272 }
1273 }
Zhang Rui33823882019-07-10 21:44:30 +08001274 /* check if power limit MSR exists, otherwise domain is monitoring only */
Jacob Pane1399ba2016-05-31 13:41:29 -07001275 for (i = 0; i < NR_POWER_LIMITS; i++) {
1276 int prim = rd->rpl[i].prim_id;
Zhang Rui33823882019-07-10 21:44:30 +08001277
Jacob Pane1399ba2016-05-31 13:41:29 -07001278 if (rapl_read_data_raw(rd, prim, false, &val64))
1279 rd->rpl[i].name = NULL;
1280 }
1281}
1282
Jacob Pan2d281d82013-10-17 10:28:35 -07001283/* Detect active and valid domains for the given CPU, caller must
1284 * ensure the CPU belongs to the targeted package and CPU hotlug is disabled.
1285 */
1286static int rapl_detect_domains(struct rapl_package *rp, int cpu)
1287{
Jacob Pan2d281d82013-10-17 10:28:35 -07001288 struct rapl_domain *rd;
Thomas Gleixner58705062016-11-22 21:16:02 +00001289 int i;
Jacob Pan2d281d82013-10-17 10:28:35 -07001290
1291 for (i = 0; i < RAPL_DOMAIN_MAX; i++) {
1292 /* use physical package id to read counters */
Zhang Rui7fde2712019-07-10 21:44:26 +08001293 if (!rapl_check_domain(cpu, i, rp)) {
Jacob Pan2d281d82013-10-17 10:28:35 -07001294 rp->domain_map |= 1 << i;
Jacob Panfcdf1792014-09-02 02:55:21 -07001295 pr_info("Found RAPL domain %s\n", rapl_domain_names[i]);
1296 }
Jacob Pan2d281d82013-10-17 10:28:35 -07001297 }
Zhang Rui33823882019-07-10 21:44:30 +08001298 rp->nr_domains = bitmap_weight(&rp->domain_map, RAPL_DOMAIN_MAX);
Jacob Pan2d281d82013-10-17 10:28:35 -07001299 if (!rp->nr_domains) {
Zhang Rui9ea76122019-05-13 13:58:53 -04001300 pr_debug("no valid rapl domains found in %s\n", rp->name);
Thomas Gleixner58705062016-11-22 21:16:02 +00001301 return -ENODEV;
Jacob Pan2d281d82013-10-17 10:28:35 -07001302 }
Zhang Rui9ea76122019-05-13 13:58:53 -04001303 pr_debug("found %d domains on %s\n", rp->nr_domains, rp->name);
Jacob Pan2d281d82013-10-17 10:28:35 -07001304
1305 rp->domains = kcalloc(rp->nr_domains + 1, sizeof(struct rapl_domain),
Zhang Rui33823882019-07-10 21:44:30 +08001306 GFP_KERNEL);
Thomas Gleixner58705062016-11-22 21:16:02 +00001307 if (!rp->domains)
1308 return -ENOMEM;
1309
Jacob Pan2d281d82013-10-17 10:28:35 -07001310 rapl_init_domains(rp);
1311
Jacob Pane1399ba2016-05-31 13:41:29 -07001312 for (rd = rp->domains; rd < rp->domains + rp->nr_domains; rd++)
1313 rapl_detect_powerlimit(rd);
1314
Jacob Pan2d281d82013-10-17 10:28:35 -07001315 return 0;
1316}
1317
1318/* called from CPU hotplug notifier, hotplug lock held */
Zhang Rui33823882019-07-10 21:44:30 +08001319void rapl_remove_package(struct rapl_package *rp)
Jacob Pan2d281d82013-10-17 10:28:35 -07001320{
1321 struct rapl_domain *rd, *rd_package = NULL;
1322
Thomas Gleixner58705062016-11-22 21:16:02 +00001323 package_power_limit_irq_restore(rp);
1324
Jacob Pan2d281d82013-10-17 10:28:35 -07001325 for (rd = rp->domains; rd < rp->domains + rp->nr_domains; rd++) {
Thomas Gleixner58705062016-11-22 21:16:02 +00001326 rapl_write_data_raw(rd, PL1_ENABLE, 0);
1327 rapl_write_data_raw(rd, PL1_CLAMP, 0);
1328 if (find_nr_power_limit(rd) > 1) {
1329 rapl_write_data_raw(rd, PL2_ENABLE, 0);
1330 rapl_write_data_raw(rd, PL2_CLAMP, 0);
Sumeet Pawnikar8365a892020-07-16 23:14:55 +05301331 rapl_write_data_raw(rd, PL4_ENABLE, 0);
Thomas Gleixner58705062016-11-22 21:16:02 +00001332 }
Jacob Pan2d281d82013-10-17 10:28:35 -07001333 if (rd->id == RAPL_DOMAIN_PACKAGE) {
1334 rd_package = rd;
1335 continue;
1336 }
Zhang Rui9ea76122019-05-13 13:58:53 -04001337 pr_debug("remove package, undo power limit on %s: %s\n",
1338 rp->name, rd->name);
Zhang Rui33823882019-07-10 21:44:30 +08001339 powercap_unregister_zone(rp->priv->control_type,
1340 &rd->power_zone);
Jacob Pan2d281d82013-10-17 10:28:35 -07001341 }
1342 /* do parent zone last */
Zhang Rui33823882019-07-10 21:44:30 +08001343 powercap_unregister_zone(rp->priv->control_type,
1344 &rd_package->power_zone);
Jacob Pan2d281d82013-10-17 10:28:35 -07001345 list_del(&rp->plist);
1346 kfree(rp);
1347}
Zhang Rui33823882019-07-10 21:44:30 +08001348EXPORT_SYMBOL_GPL(rapl_remove_package);
1349
1350/* caller to ensure CPU hotplug lock is held */
1351struct rapl_package *rapl_find_package_domain(int cpu, struct rapl_if_priv *priv)
1352{
1353 int id = topology_logical_die_id(cpu);
1354 struct rapl_package *rp;
1355
1356 list_for_each_entry(rp, &rapl_packages, plist) {
1357 if (rp->id == id
1358 && rp->priv->control_type == priv->control_type)
1359 return rp;
1360 }
1361
1362 return NULL;
1363}
1364EXPORT_SYMBOL_GPL(rapl_find_package_domain);
Jacob Pan2d281d82013-10-17 10:28:35 -07001365
1366/* called from CPU hotplug notifier, hotplug lock held */
Zhang Rui33823882019-07-10 21:44:30 +08001367struct rapl_package *rapl_add_package(int cpu, struct rapl_if_priv *priv)
Jacob Pan2d281d82013-10-17 10:28:35 -07001368{
Zhang Rui32fb4802019-05-13 13:58:51 -04001369 int id = topology_logical_die_id(cpu);
Jacob Pan2d281d82013-10-17 10:28:35 -07001370 struct rapl_package *rp;
Thomas Gleixnerb4005e92016-11-22 21:16:05 +00001371 int ret;
Jacob Pan2d281d82013-10-17 10:28:35 -07001372
Harry Pan3aa3c582019-12-30 22:36:56 +08001373 if (!rapl_defaults)
1374 return ERR_PTR(-ENODEV);
1375
Jacob Pan2d281d82013-10-17 10:28:35 -07001376 rp = kzalloc(sizeof(struct rapl_package), GFP_KERNEL);
1377 if (!rp)
Thomas Gleixnerb4005e92016-11-22 21:16:05 +00001378 return ERR_PTR(-ENOMEM);
Jacob Pan2d281d82013-10-17 10:28:35 -07001379
1380 /* add the new package to the list */
Zhang Ruiaadf7b32019-05-13 13:58:50 -04001381 rp->id = id;
Jacob Pan323ee642016-02-24 13:31:38 -08001382 rp->lead_cpu = cpu;
Zhang Rui7ebf8ef2019-07-10 21:44:25 +08001383 rp->priv = priv;
Jacob Pan323ee642016-02-24 13:31:38 -08001384
Zhang Rui9ea76122019-05-13 13:58:53 -04001385 if (topology_max_die_per_package() > 1)
1386 snprintf(rp->name, PACKAGE_DOMAIN_NAME_LENGTH,
Yunfeng Ye88ffce92021-01-23 05:06:07 -05001387 "package-%d-die-%d",
1388 topology_physical_package_id(cpu), topology_die_id(cpu));
Zhang Rui9ea76122019-05-13 13:58:53 -04001389 else
1390 snprintf(rp->name, PACKAGE_DOMAIN_NAME_LENGTH, "package-%d",
Yunfeng Ye88ffce92021-01-23 05:06:07 -05001391 topology_physical_package_id(cpu));
Zhang Rui9ea76122019-05-13 13:58:53 -04001392
Jacob Pan2d281d82013-10-17 10:28:35 -07001393 /* check if the package contains valid domains */
Zhang Rui33823882019-07-10 21:44:30 +08001394 if (rapl_detect_domains(rp, cpu) || rapl_defaults->check_unit(rp, cpu)) {
Jacob Pan2d281d82013-10-17 10:28:35 -07001395 ret = -ENODEV;
1396 goto err_free_package;
1397 }
Thomas Gleixnera74f4362016-11-22 21:15:59 +00001398 ret = rapl_package_register_powercap(rp);
1399 if (!ret) {
Jacob Pan2d281d82013-10-17 10:28:35 -07001400 INIT_LIST_HEAD(&rp->plist);
1401 list_add(&rp->plist, &rapl_packages);
Thomas Gleixnerb4005e92016-11-22 21:16:05 +00001402 return rp;
Jacob Pan2d281d82013-10-17 10:28:35 -07001403 }
1404
1405err_free_package:
1406 kfree(rp->domains);
1407 kfree(rp);
Thomas Gleixnerb4005e92016-11-22 21:16:05 +00001408 return ERR_PTR(ret);
Jacob Pan2d281d82013-10-17 10:28:35 -07001409}
Zhang Rui33823882019-07-10 21:44:30 +08001410EXPORT_SYMBOL_GPL(rapl_add_package);
Jacob Pan2d281d82013-10-17 10:28:35 -07001411
Zhen Han52b36722018-01-10 08:38:23 +08001412static void power_limit_state_save(void)
1413{
1414 struct rapl_package *rp;
1415 struct rapl_domain *rd;
1416 int nr_pl, ret, i;
1417
Sebastian Andrzej Siewior5d4c7792021-08-03 16:16:01 +02001418 cpus_read_lock();
Zhen Han52b36722018-01-10 08:38:23 +08001419 list_for_each_entry(rp, &rapl_packages, plist) {
1420 if (!rp->power_zone)
1421 continue;
1422 rd = power_zone_to_rapl_domain(rp->power_zone);
1423 nr_pl = find_nr_power_limit(rd);
1424 for (i = 0; i < nr_pl; i++) {
1425 switch (rd->rpl[i].prim_id) {
1426 case PL1_ENABLE:
1427 ret = rapl_read_data_raw(rd,
Zhang Rui33823882019-07-10 21:44:30 +08001428 POWER_LIMIT1, true,
1429 &rd->rpl[i].last_power_limit);
Zhen Han52b36722018-01-10 08:38:23 +08001430 if (ret)
1431 rd->rpl[i].last_power_limit = 0;
1432 break;
1433 case PL2_ENABLE:
1434 ret = rapl_read_data_raw(rd,
Zhang Rui33823882019-07-10 21:44:30 +08001435 POWER_LIMIT2, true,
1436 &rd->rpl[i].last_power_limit);
Zhen Han52b36722018-01-10 08:38:23 +08001437 if (ret)
1438 rd->rpl[i].last_power_limit = 0;
1439 break;
Sumeet Pawnikar8365a892020-07-16 23:14:55 +05301440 case PL4_ENABLE:
1441 ret = rapl_read_data_raw(rd,
1442 POWER_LIMIT4, true,
1443 &rd->rpl[i].last_power_limit);
1444 if (ret)
1445 rd->rpl[i].last_power_limit = 0;
1446 break;
Zhen Han52b36722018-01-10 08:38:23 +08001447 }
1448 }
1449 }
Sebastian Andrzej Siewior5d4c7792021-08-03 16:16:01 +02001450 cpus_read_unlock();
Zhen Han52b36722018-01-10 08:38:23 +08001451}
1452
1453static void power_limit_state_restore(void)
1454{
1455 struct rapl_package *rp;
1456 struct rapl_domain *rd;
1457 int nr_pl, i;
1458
Sebastian Andrzej Siewior5d4c7792021-08-03 16:16:01 +02001459 cpus_read_lock();
Zhen Han52b36722018-01-10 08:38:23 +08001460 list_for_each_entry(rp, &rapl_packages, plist) {
1461 if (!rp->power_zone)
1462 continue;
1463 rd = power_zone_to_rapl_domain(rp->power_zone);
1464 nr_pl = find_nr_power_limit(rd);
1465 for (i = 0; i < nr_pl; i++) {
1466 switch (rd->rpl[i].prim_id) {
1467 case PL1_ENABLE:
1468 if (rd->rpl[i].last_power_limit)
Zhang Rui33823882019-07-10 21:44:30 +08001469 rapl_write_data_raw(rd, POWER_LIMIT1,
1470 rd->rpl[i].last_power_limit);
Zhen Han52b36722018-01-10 08:38:23 +08001471 break;
1472 case PL2_ENABLE:
1473 if (rd->rpl[i].last_power_limit)
Zhang Rui33823882019-07-10 21:44:30 +08001474 rapl_write_data_raw(rd, POWER_LIMIT2,
1475 rd->rpl[i].last_power_limit);
Zhen Han52b36722018-01-10 08:38:23 +08001476 break;
Sumeet Pawnikar8365a892020-07-16 23:14:55 +05301477 case PL4_ENABLE:
1478 if (rd->rpl[i].last_power_limit)
1479 rapl_write_data_raw(rd, POWER_LIMIT4,
1480 rd->rpl[i].last_power_limit);
1481 break;
Zhen Han52b36722018-01-10 08:38:23 +08001482 }
1483 }
1484 }
Sebastian Andrzej Siewior5d4c7792021-08-03 16:16:01 +02001485 cpus_read_unlock();
Zhen Han52b36722018-01-10 08:38:23 +08001486}
1487
1488static int rapl_pm_callback(struct notifier_block *nb,
Zhang Rui33823882019-07-10 21:44:30 +08001489 unsigned long mode, void *_unused)
Zhen Han52b36722018-01-10 08:38:23 +08001490{
1491 switch (mode) {
1492 case PM_SUSPEND_PREPARE:
1493 power_limit_state_save();
1494 break;
1495 case PM_POST_SUSPEND:
1496 power_limit_state_restore();
1497 break;
1498 }
1499 return NOTIFY_OK;
1500}
1501
1502static struct notifier_block rapl_pm_notifier = {
1503 .notifier_call = rapl_pm_callback,
1504};
1505
Zhang Ruiabcfaeb2019-07-10 21:44:34 +08001506static struct platform_device *rapl_msr_platdev;
1507
1508static int __init rapl_init(void)
Jacob Pan2d281d82013-10-17 10:28:35 -07001509{
Jacob Pan087e9cb2014-11-07 09:29:25 -08001510 const struct x86_cpu_id *id;
Thomas Gleixner58705062016-11-22 21:16:02 +00001511 int ret;
Jacob Pan2d281d82013-10-17 10:28:35 -07001512
Jacob Pan087e9cb2014-11-07 09:29:25 -08001513 id = x86_match_cpu(rapl_ids);
1514 if (!id) {
Jacob Pan2d281d82013-10-17 10:28:35 -07001515 pr_err("driver does not support CPU family %d model %d\n",
Zhang Rui33823882019-07-10 21:44:30 +08001516 boot_cpu_data.x86, boot_cpu_data.x86_model);
Jacob Pan2d281d82013-10-17 10:28:35 -07001517
1518 return -ENODEV;
1519 }
Srivatsa S. Bhat009f2252014-03-11 02:09:26 +05301520
Jacob Pan087e9cb2014-11-07 09:29:25 -08001521 rapl_defaults = (struct rapl_defaults *)id->driver_data;
1522
Zhen Han52b36722018-01-10 08:38:23 +08001523 ret = register_pm_notifier(&rapl_pm_notifier);
Zhang Ruiabcfaeb2019-07-10 21:44:34 +08001524 if (ret)
1525 return ret;
Zhen Han52b36722018-01-10 08:38:23 +08001526
Zhang Ruiabcfaeb2019-07-10 21:44:34 +08001527 rapl_msr_platdev = platform_device_alloc("intel_rapl_msr", 0);
1528 if (!rapl_msr_platdev) {
1529 ret = -ENOMEM;
1530 goto end;
1531 }
1532
1533 ret = platform_device_add(rapl_msr_platdev);
1534 if (ret)
1535 platform_device_put(rapl_msr_platdev);
1536
1537end:
1538 if (ret)
1539 unregister_pm_notifier(&rapl_pm_notifier);
1540
1541 return ret;
Jacob Pan2d281d82013-10-17 10:28:35 -07001542}
1543
Zhang Ruiabcfaeb2019-07-10 21:44:34 +08001544static void __exit rapl_exit(void)
Jacob Pan2d281d82013-10-17 10:28:35 -07001545{
Zhang Ruiabcfaeb2019-07-10 21:44:34 +08001546 platform_device_unregister(rapl_msr_platdev);
Zhen Han52b36722018-01-10 08:38:23 +08001547 unregister_pm_notifier(&rapl_pm_notifier);
Jacob Pan2d281d82013-10-17 10:28:35 -07001548}
1549
Zhang Ruif76cb062019-07-19 23:25:14 +08001550fs_initcall(rapl_init);
Zhang Ruiabcfaeb2019-07-10 21:44:34 +08001551module_exit(rapl_exit);
1552
Zhang Rui33823882019-07-10 21:44:30 +08001553MODULE_DESCRIPTION("Intel Runtime Average Power Limit (RAPL) common code");
Jacob Pan2d281d82013-10-17 10:28:35 -07001554MODULE_AUTHOR("Jacob Pan <jacob.jun.pan@intel.com>");
1555MODULE_LICENSE("GPL v2");