blob: 0b2830efc574993ea22dd3959b6196710f009295 [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
64/* Non HW constants */
Zhang Rui33823882019-07-10 21:44:30 +080065#define RAPL_PRIMITIVE_DERIVED BIT(1) /* not from raw data */
Jacob Pan2d281d82013-10-17 10:28:35 -070066#define RAPL_PRIMITIVE_DUMMY BIT(2)
67
Jacob Pan2d281d82013-10-17 10:28:35 -070068#define TIME_WINDOW_MAX_MSEC 40000
69#define TIME_WINDOW_MIN_MSEC 250
Zhang Rui33823882019-07-10 21:44:30 +080070#define ENERGY_UNIT_SCALE 1000 /* scale from driver unit to powercap unit */
Jacob Pan2d281d82013-10-17 10:28:35 -070071enum unit_type {
Zhang Rui33823882019-07-10 21:44:30 +080072 ARBITRARY_UNIT, /* no translation */
Jacob Pan2d281d82013-10-17 10:28:35 -070073 POWER_UNIT,
74 ENERGY_UNIT,
75 TIME_UNIT,
76};
77
Jacob Pan2d281d82013-10-17 10:28:35 -070078/* per domain data, some are optional */
Jacob Pan2d281d82013-10-17 10:28:35 -070079#define NR_RAW_PRIMITIVES (NR_RAPL_PRIMITIVES - 2)
80
Jacob Pan2d281d82013-10-17 10:28:35 -070081#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 Pan2d281d82013-10-17 10:28:35 -070085static const char pl1_name[] = "long_term";
86static const char pl2_name[] = "short_term";
Sumeet Pawnikar8365a892020-07-16 23:14:55 +053087static const char pl4_name[] = "peak_power";
Jacob Pan2d281d82013-10-17 10:28:35 -070088
Jacob Pan2d281d82013-10-17 10:28:35 -070089#define power_zone_to_rapl_domain(_zone) \
90 container_of(_zone, struct rapl_domain, power_zone)
91
Jacob Pan087e9cb2014-11-07 09:29:25 -080092struct rapl_defaults {
Ajay Thomas51b63402015-04-30 01:43:23 +053093 u8 floor_freq_reg_addr;
Jacob Pan087e9cb2014-11-07 09:29:25 -080094 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 Rui33823882019-07-10 21:44:30 +080097 bool to_raw);
Jacob Pand474a4d2015-03-13 03:48:56 -070098 unsigned int dram_domain_energy_unit;
Zhang Rui2d798d92020-06-29 13:34:50 +080099 unsigned int psys_domain_energy_unit;
Jacob Pan087e9cb2014-11-07 09:29:25 -0800100};
101static struct rapl_defaults *rapl_defaults;
102
Jacob Pan3c2c0842014-11-07 09:29:26 -0800103/* Sideband MBI registers */
Ajay Thomas51b63402015-04-30 01:43:23 +0530104#define IOSF_CPU_POWER_BUDGET_CTL_BYT (0x2)
105#define IOSF_CPU_POWER_BUDGET_CTL_TNG (0xdf)
Jacob Pan3c2c0842014-11-07 09:29:26 -0800106
Jacob Pan2d281d82013-10-17 10:28:35 -0700107#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 */
113struct rapl_primitive_info {
114 const char *name;
115 u64 mask;
116 int shift;
Zhang Ruif7c4e0c2019-07-10 21:44:22 +0800117 enum rapl_domain_reg_id id;
Jacob Pan2d281d82013-10-17 10:28:35 -0700118 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
131static void rapl_init_domains(struct rapl_package *rp);
132static int rapl_read_data_raw(struct rapl_domain *rd,
Zhang Rui33823882019-07-10 21:44:30 +0800133 enum rapl_primitives prim,
134 bool xlate, u64 *data);
Jacob Pan2d281d82013-10-17 10:28:35 -0700135static int rapl_write_data_raw(struct rapl_domain *rd,
Zhang Rui33823882019-07-10 21:44:30 +0800136 enum rapl_primitives prim,
137 unsigned long long value);
Jacob Pan309557f2016-02-24 13:31:37 -0800138static u64 rapl_unit_xlate(struct rapl_domain *rd,
Zhang Rui33823882019-07-10 21:44:30 +0800139 enum unit_type type, u64 value, int to_raw);
Jacob Pan309557f2016-02-24 13:31:37 -0800140static void package_power_limit_irq_save(struct rapl_package *rp);
Jacob Pan2d281d82013-10-17 10:28:35 -0700141
Zhang Rui33823882019-07-10 21:44:30 +0800142static LIST_HEAD(rapl_packages); /* guarded by CPU hotplug lock */
Jacob Pan2d281d82013-10-17 10:28:35 -0700143
Zhang Rui33823882019-07-10 21:44:30 +0800144static const char *const rapl_domain_names[] = {
Jacob Pan2d281d82013-10-17 10:28:35 -0700145 "package",
146 "core",
147 "uncore",
148 "dram",
Srinivas Pandruvada3521ba12016-04-17 15:03:01 -0700149 "psys",
Jacob Pan2d281d82013-10-17 10:28:35 -0700150};
151
Zhang Rui33823882019-07-10 21:44:30 +0800152static int get_energy_counter(struct powercap_zone *power_zone,
153 u64 *energy_raw)
Jacob Pan2d281d82013-10-17 10:28:35 -0700154{
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 */
161 get_online_cpus();
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;
166 put_online_cpus();
167
168 return 0;
169 }
170 put_online_cpus();
171
172 return -EIO;
173}
174
175static int get_max_energy_counter(struct powercap_zone *pcd_dev, u64 *energy)
176{
Jacob Pand474a4d2015-03-13 03:48:56 -0700177 struct rapl_domain *rd = power_zone_to_rapl_domain(pcd_dev);
178
Jacob Pan309557f2016-02-24 13:31:37 -0800179 *energy = rapl_unit_xlate(rd, ENERGY_UNIT, ENERGY_STATUS_MASK, 0);
Jacob Pan2d281d82013-10-17 10:28:35 -0700180 return 0;
181}
182
183static int release_zone(struct powercap_zone *power_zone)
184{
185 struct rapl_domain *rd = power_zone_to_rapl_domain(power_zone);
Jacob Pan309557f2016-02-24 13:31:37 -0800186 struct rapl_package *rp = rd->rp;
Jacob Pan2d281d82013-10-17 10:28:35 -0700187
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 Pan2d281d82013-10-17 10:28:35 -0700192 kfree(rd);
193 rp->domains = NULL;
194 }
195
196 return 0;
197
198}
199
200static int find_nr_power_limit(struct rapl_domain *rd)
201{
Jacob Pane1399ba2016-05-31 13:41:29 -0700202 int i, nr_pl = 0;
Jacob Pan2d281d82013-10-17 10:28:35 -0700203
204 for (i = 0; i < NR_POWER_LIMITS; i++) {
Jacob Pane1399ba2016-05-31 13:41:29 -0700205 if (rd->rpl[i].name)
206 nr_pl++;
Jacob Pan2d281d82013-10-17 10:28:35 -0700207 }
208
Jacob Pane1399ba2016-05-31 13:41:29 -0700209 return nr_pl;
Jacob Pan2d281d82013-10-17 10:28:35 -0700210}
211
212static 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 Pan2d281d82013-10-17 10:28:35 -0700215
216 if (rd->state & DOMAIN_STATE_BIOS_LOCKED)
217 return -EACCES;
Jacob Pan3c2c0842014-11-07 09:29:26 -0800218
Jacob Pan2d281d82013-10-17 10:28:35 -0700219 get_online_cpus();
Jacob Pan2d281d82013-10-17 10:28:35 -0700220 rapl_write_data_raw(rd, PL1_ENABLE, mode);
Ajay Thomas51b63402015-04-30 01:43:23 +0530221 if (rapl_defaults->set_floor_freq)
222 rapl_defaults->set_floor_freq(rd, mode);
Jacob Pan2d281d82013-10-17 10:28:35 -0700223 put_online_cpus();
224
225 return 0;
226}
227
228static 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 }
237 get_online_cpus();
238 if (rapl_read_data_raw(rd, PL1_ENABLE, true, &val)) {
239 put_online_cpus();
240 return -EIO;
241 }
242 *mode = val;
243 put_online_cpus();
244
245 return 0;
246}
247
248/* per RAPL domain ops, in the order of rapl_domain_type */
Julia Lawall600c3952015-12-23 22:59:55 +0100249static const struct powercap_zone_ops zone_ops[] = {
Jacob Pan2d281d82013-10-17 10:28:35 -0700250 /* RAPL_DOMAIN_PACKAGE */
251 {
Zhang Rui33823882019-07-10 21:44:30 +0800252 .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 Pan2d281d82013-10-17 10:28:35 -0700258 /* RAPL_DOMAIN_PP0 */
259 {
Zhang Rui33823882019-07-10 21:44:30 +0800260 .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 Pan2d281d82013-10-17 10:28:35 -0700266 /* RAPL_DOMAIN_PP1 */
267 {
Zhang Rui33823882019-07-10 21:44:30 +0800268 .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 Pan2d281d82013-10-17 10:28:35 -0700274 /* RAPL_DOMAIN_DRAM */
275 {
Zhang Rui33823882019-07-10 21:44:30 +0800276 .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 Pandruvada3521ba12016-04-17 15:03:01 -0700282 /* RAPL_DOMAIN_PLATFORM */
283 {
Zhang Rui33823882019-07-10 21:44:30 +0800284 .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 Pan2d281d82013-10-17 10:28:35 -0700290};
291
Jacob Pane1399ba2016-05-31 13:41:29 -0700292/*
293 * Constraint index used by powercap can be different than power limit (PL)
Zhang Rui33823882019-07-10 21:44:30 +0800294 * index in that some PLs maybe missing due to non-existent MSRs. So we
Jacob Pane1399ba2016-05-31 13:41:29 -0700295 * need to convert here by finding the valid PLs only (name populated).
296 */
297static 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 Pancb43f812016-11-28 13:53:11 -0800307 pr_err("Cannot find matching power limit for constraint %d\n", cid);
Jacob Pane1399ba2016-05-31 13:41:29 -0700308
309 return -EINVAL;
310}
311
312static int set_power_limit(struct powercap_zone *power_zone, int cid,
Zhang Rui33823882019-07-10 21:44:30 +0800313 u64 power_limit)
Jacob Pan2d281d82013-10-17 10:28:35 -0700314{
315 struct rapl_domain *rd;
316 struct rapl_package *rp;
317 int ret = 0;
Jacob Pane1399ba2016-05-31 13:41:29 -0700318 int id;
Jacob Pan2d281d82013-10-17 10:28:35 -0700319
320 get_online_cpus();
321 rd = power_zone_to_rapl_domain(power_zone);
Jacob Pane1399ba2016-05-31 13:41:29 -0700322 id = contraint_to_pl(rd, cid);
Jacob Pancb43f812016-11-28 13:53:11 -0800323 if (id < 0) {
324 ret = id;
325 goto set_exit;
326 }
Jacob Pane1399ba2016-05-31 13:41:29 -0700327
Jacob Pan309557f2016-02-24 13:31:37 -0800328 rp = rd->rp;
Jacob Pan2d281d82013-10-17 10:28:35 -0700329
330 if (rd->state & DOMAIN_STATE_BIOS_LOCKED) {
Zhang Rui33823882019-07-10 21:44:30 +0800331 dev_warn(&power_zone->dev,
332 "%s locked by BIOS, monitoring only\n", rd->name);
Jacob Pan2d281d82013-10-17 10:28:35 -0700333 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 Pawnikar8365a892020-07-16 23:14:55 +0530344 case PL4_ENABLE:
345 rapl_write_data_raw(rd, POWER_LIMIT4, power_limit);
346 break;
Jacob Pan2d281d82013-10-17 10:28:35 -0700347 default:
348 ret = -EINVAL;
349 }
350 if (!ret)
Jacob Pan309557f2016-02-24 13:31:37 -0800351 package_power_limit_irq_save(rp);
Jacob Pan2d281d82013-10-17 10:28:35 -0700352set_exit:
353 put_online_cpus();
354 return ret;
355}
356
Jacob Pane1399ba2016-05-31 13:41:29 -0700357static int get_current_power_limit(struct powercap_zone *power_zone, int cid,
Zhang Rui33823882019-07-10 21:44:30 +0800358 u64 *data)
Jacob Pan2d281d82013-10-17 10:28:35 -0700359{
360 struct rapl_domain *rd;
361 u64 val;
362 int prim;
363 int ret = 0;
Jacob Pane1399ba2016-05-31 13:41:29 -0700364 int id;
Jacob Pan2d281d82013-10-17 10:28:35 -0700365
366 get_online_cpus();
367 rd = power_zone_to_rapl_domain(power_zone);
Jacob Pane1399ba2016-05-31 13:41:29 -0700368 id = contraint_to_pl(rd, cid);
Jacob Pancb43f812016-11-28 13:53:11 -0800369 if (id < 0) {
370 ret = id;
371 goto get_exit;
372 }
373
Jacob Pan2d281d82013-10-17 10:28:35 -0700374 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 Pawnikar8365a892020-07-16 23:14:55 +0530381 case PL4_ENABLE:
382 prim = POWER_LIMIT4;
383 break;
Jacob Pan2d281d82013-10-17 10:28:35 -0700384 default:
385 put_online_cpus();
386 return -EINVAL;
387 }
388 if (rapl_read_data_raw(rd, prim, true, &val))
389 ret = -EIO;
390 else
391 *data = val;
392
Jacob Pancb43f812016-11-28 13:53:11 -0800393get_exit:
Jacob Pan2d281d82013-10-17 10:28:35 -0700394 put_online_cpus();
395
396 return ret;
397}
398
Jacob Pane1399ba2016-05-31 13:41:29 -0700399static int set_time_window(struct powercap_zone *power_zone, int cid,
Zhang Rui33823882019-07-10 21:44:30 +0800400 u64 window)
Jacob Pan2d281d82013-10-17 10:28:35 -0700401{
402 struct rapl_domain *rd;
403 int ret = 0;
Jacob Pane1399ba2016-05-31 13:41:29 -0700404 int id;
Jacob Pan2d281d82013-10-17 10:28:35 -0700405
406 get_online_cpus();
407 rd = power_zone_to_rapl_domain(power_zone);
Jacob Pane1399ba2016-05-31 13:41:29 -0700408 id = contraint_to_pl(rd, cid);
Jacob Pancb43f812016-11-28 13:53:11 -0800409 if (id < 0) {
410 ret = id;
411 goto set_time_exit;
412 }
Jacob Pane1399ba2016-05-31 13:41:29 -0700413
Jacob Pan2d281d82013-10-17 10:28:35 -0700414 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 Pancb43f812016-11-28 13:53:11 -0800424
425set_time_exit:
Jacob Pan2d281d82013-10-17 10:28:35 -0700426 put_online_cpus();
427 return ret;
428}
429
Zhang Rui33823882019-07-10 21:44:30 +0800430static int get_time_window(struct powercap_zone *power_zone, int cid,
431 u64 *data)
Jacob Pan2d281d82013-10-17 10:28:35 -0700432{
433 struct rapl_domain *rd;
434 u64 val;
435 int ret = 0;
Jacob Pane1399ba2016-05-31 13:41:29 -0700436 int id;
Jacob Pan2d281d82013-10-17 10:28:35 -0700437
438 get_online_cpus();
439 rd = power_zone_to_rapl_domain(power_zone);
Jacob Pane1399ba2016-05-31 13:41:29 -0700440 id = contraint_to_pl(rd, cid);
Jacob Pancb43f812016-11-28 13:53:11 -0800441 if (id < 0) {
442 ret = id;
443 goto get_time_exit;
444 }
Jacob Pane1399ba2016-05-31 13:41:29 -0700445
Jacob Pan2d281d82013-10-17 10:28:35 -0700446 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 Pawnikar8365a892020-07-16 23:14:55 +0530453 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 Pan2d281d82013-10-17 10:28:35 -0700460 default:
461 put_online_cpus();
462 return -EINVAL;
463 }
464 if (!ret)
465 *data = val;
Jacob Pancb43f812016-11-28 13:53:11 -0800466
467get_time_exit:
Jacob Pan2d281d82013-10-17 10:28:35 -0700468 put_online_cpus();
469
470 return ret;
471}
472
Zhang Rui33823882019-07-10 21:44:30 +0800473static const char *get_constraint_name(struct powercap_zone *power_zone,
474 int cid)
Jacob Pan2d281d82013-10-17 10:28:35 -0700475{
Jacob Pan2d281d82013-10-17 10:28:35 -0700476 struct rapl_domain *rd;
Jacob Pane1399ba2016-05-31 13:41:29 -0700477 int id;
Jacob Pan2d281d82013-10-17 10:28:35 -0700478
479 rd = power_zone_to_rapl_domain(power_zone);
Jacob Pane1399ba2016-05-31 13:41:29 -0700480 id = contraint_to_pl(rd, cid);
481 if (id >= 0)
482 return rd->rpl[id].name;
Jacob Pan2d281d82013-10-17 10:28:35 -0700483
Jacob Pane1399ba2016-05-31 13:41:29 -0700484 return NULL;
Jacob Pan2d281d82013-10-17 10:28:35 -0700485}
486
Zhang Rui33823882019-07-10 21:44:30 +0800487static int get_max_power(struct powercap_zone *power_zone, int id, u64 *data)
Jacob Pan2d281d82013-10-17 10:28:35 -0700488{
489 struct rapl_domain *rd;
490 u64 val;
491 int prim;
492 int ret = 0;
493
494 get_online_cpus();
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 Pawnikar8365a892020-07-16 23:14:55 +0530503 case PL4_ENABLE:
504 prim = MAX_POWER;
505 break;
Jacob Pan2d281d82013-10-17 10:28:35 -0700506 default:
507 put_online_cpus();
508 return -EINVAL;
509 }
510 if (rapl_read_data_raw(rd, prim, true, &val))
511 ret = -EIO;
512 else
513 *data = val;
514
Sumeet Pawnikar8365a892020-07-16 23:14:55 +0530515 /* 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
Jacob Pan2d281d82013-10-17 10:28:35 -0700519 put_online_cpus();
520
521 return ret;
522}
523
Julia Lawall600c3952015-12-23 22:59:55 +0100524static const struct powercap_zone_constraint_ops constraint_ops = {
Jacob Pan2d281d82013-10-17 10:28:35 -0700525 .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 */
534static void rapl_init_domains(struct rapl_package *rp)
535{
Zhang Rui0c2dded2019-07-10 21:44:32 +0800536 enum rapl_domain_type i;
537 enum rapl_domain_reg_id j;
Jacob Pan2d281d82013-10-17 10:28:35 -0700538 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 Rui7fde2712019-07-10 21:44:26 +0800542
Zhang Rui0c2dded2019-07-10 21:44:32 +0800543 if (!mask)
544 continue;
Zhang Rui7fde2712019-07-10 21:44:26 +0800545
Zhang Rui0c2dded2019-07-10 21:44:32 +0800546 rd->rp = rp;
Zhang Ruif1e8d752020-10-13 15:42:41 +0800547
548 if (i == RAPL_DOMAIN_PLATFORM && rp->id > 0) {
549 snprintf(rd->name, RAPL_DOMAIN_NAME_LENGTH, "psys-%d",
550 cpu_data(rp->lead_cpu).phys_proc_id);
551 } else
552 snprintf(rd->name, RAPL_DOMAIN_NAME_LENGTH, "%s",
553 rapl_domain_names[i]);
554
Zhang Rui0c2dded2019-07-10 21:44:32 +0800555 rd->id = i;
556 rd->rpl[0].prim_id = PL1_ENABLE;
557 rd->rpl[0].name = pl1_name;
Sumeet Pawnikar8365a892020-07-16 23:14:55 +0530558
559 /*
560 * The PL2 power domain is applicable for limits two
561 * and limits three
562 */
563 if (rp->priv->limits[i] >= 2) {
Jacob Pan2d281d82013-10-17 10:28:35 -0700564 rd->rpl[1].prim_id = PL2_ENABLE;
565 rd->rpl[1].name = pl2_name;
Zhang Rui0c2dded2019-07-10 21:44:32 +0800566 }
567
Sumeet Pawnikar8365a892020-07-16 23:14:55 +0530568 /* 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 Rui0c2dded2019-07-10 21:44:32 +0800574 for (j = 0; j < RAPL_DOMAIN_REG_MAX; j++)
575 rd->regs[j] = rp->priv->regs[i][j];
576
Zhang Rui2d798d92020-06-29 13:34:50 +0800577 switch (i) {
578 case RAPL_DOMAIN_DRAM:
Jacob Pand474a4d2015-03-13 03:48:56 -0700579 rd->domain_energy_unit =
Zhang Rui33823882019-07-10 21:44:30 +0800580 rapl_defaults->dram_domain_energy_unit;
Jacob Pand474a4d2015-03-13 03:48:56 -0700581 if (rd->domain_energy_unit)
582 pr_info("DRAM domain energy unit %dpj\n",
583 rd->domain_energy_unit);
Zhang Rui2d798d92020-06-29 13:34:50 +0800584 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 Pan2d281d82013-10-17 10:28:35 -0700594 }
Zhang Rui0c2dded2019-07-10 21:44:32 +0800595 rd++;
Jacob Pan2d281d82013-10-17 10:28:35 -0700596 }
597}
598
Jacob Pan309557f2016-02-24 13:31:37 -0800599static u64 rapl_unit_xlate(struct rapl_domain *rd, enum unit_type type,
Zhang Rui33823882019-07-10 21:44:30 +0800600 u64 value, int to_raw)
Jacob Pan2d281d82013-10-17 10:28:35 -0700601{
Jacob Pan3c2c0842014-11-07 09:29:26 -0800602 u64 units = 1;
Jacob Pan309557f2016-02-24 13:31:37 -0800603 struct rapl_package *rp = rd->rp;
Jacob Pand474a4d2015-03-13 03:48:56 -0700604 u64 scale = 1;
Jacob Pan2d281d82013-10-17 10:28:35 -0700605
Jacob Pan2d281d82013-10-17 10:28:35 -0700606 switch (type) {
607 case POWER_UNIT:
Jacob Pan3c2c0842014-11-07 09:29:26 -0800608 units = rp->power_unit;
Jacob Pan2d281d82013-10-17 10:28:35 -0700609 break;
610 case ENERGY_UNIT:
Jacob Pand474a4d2015-03-13 03:48:56 -0700611 scale = ENERGY_UNIT_SCALE;
612 /* per domain unit takes precedence */
Jacob Pancb43f812016-11-28 13:53:11 -0800613 if (rd->domain_energy_unit)
Jacob Pand474a4d2015-03-13 03:48:56 -0700614 units = rd->domain_energy_unit;
615 else
616 units = rp->energy_unit;
Jacob Pan2d281d82013-10-17 10:28:35 -0700617 break;
618 case TIME_UNIT:
Jacob Pan3c2c0842014-11-07 09:29:26 -0800619 return rapl_defaults->compute_time_window(rp, value, to_raw);
Jacob Pan2d281d82013-10-17 10:28:35 -0700620 case ARBITRARY_UNIT:
621 default:
622 return value;
623 };
624
625 if (to_raw)
Jacob Pand474a4d2015-03-13 03:48:56 -0700626 return div64_u64(value, units) * scale;
Jacob Pan3c2c0842014-11-07 09:29:26 -0800627
628 value *= units;
629
Jacob Pand474a4d2015-03-13 03:48:56 -0700630 return div64_u64(value, scale);
Jacob Pan2d281d82013-10-17 10:28:35 -0700631}
632
633/* in the order of enum rapl_primitives */
634static struct rapl_primitive_info rpi[] = {
635 /* name, mask, shift, msr index, unit divisor */
636 PRIMITIVE_INFO_INIT(ENERGY_COUNTER, ENERGY_STATUS_MASK, 0,
Zhang Rui33823882019-07-10 21:44:30 +0800637 RAPL_DOMAIN_REG_STATUS, ENERGY_UNIT, 0),
Jacob Pan2d281d82013-10-17 10:28:35 -0700638 PRIMITIVE_INFO_INIT(POWER_LIMIT1, POWER_LIMIT1_MASK, 0,
Zhang Rui33823882019-07-10 21:44:30 +0800639 RAPL_DOMAIN_REG_LIMIT, POWER_UNIT, 0),
Jacob Pan2d281d82013-10-17 10:28:35 -0700640 PRIMITIVE_INFO_INIT(POWER_LIMIT2, POWER_LIMIT2_MASK, 32,
Zhang Rui33823882019-07-10 21:44:30 +0800641 RAPL_DOMAIN_REG_LIMIT, POWER_UNIT, 0),
Sumeet Pawnikar8365a892020-07-16 23:14:55 +0530642 PRIMITIVE_INFO_INIT(POWER_LIMIT4, POWER_LIMIT4_MASK, 0,
643 RAPL_DOMAIN_REG_PL4, POWER_UNIT, 0),
Zhang Rui0c2dded2019-07-10 21:44:32 +0800644 PRIMITIVE_INFO_INIT(FW_LOCK, POWER_LOW_LOCK, 31,
Zhang Rui33823882019-07-10 21:44:30 +0800645 RAPL_DOMAIN_REG_LIMIT, ARBITRARY_UNIT, 0),
Jacob Pan2d281d82013-10-17 10:28:35 -0700646 PRIMITIVE_INFO_INIT(PL1_ENABLE, POWER_LIMIT1_ENABLE, 15,
Zhang Rui33823882019-07-10 21:44:30 +0800647 RAPL_DOMAIN_REG_LIMIT, ARBITRARY_UNIT, 0),
Jacob Pan2d281d82013-10-17 10:28:35 -0700648 PRIMITIVE_INFO_INIT(PL1_CLAMP, POWER_LIMIT1_CLAMP, 16,
Zhang Rui33823882019-07-10 21:44:30 +0800649 RAPL_DOMAIN_REG_LIMIT, ARBITRARY_UNIT, 0),
Jacob Pan2d281d82013-10-17 10:28:35 -0700650 PRIMITIVE_INFO_INIT(PL2_ENABLE, POWER_LIMIT2_ENABLE, 47,
Zhang Rui33823882019-07-10 21:44:30 +0800651 RAPL_DOMAIN_REG_LIMIT, ARBITRARY_UNIT, 0),
Jacob Pan2d281d82013-10-17 10:28:35 -0700652 PRIMITIVE_INFO_INIT(PL2_CLAMP, POWER_LIMIT2_CLAMP, 48,
Zhang Rui33823882019-07-10 21:44:30 +0800653 RAPL_DOMAIN_REG_LIMIT, ARBITRARY_UNIT, 0),
Sumeet Pawnikar8365a892020-07-16 23:14:55 +0530654 PRIMITIVE_INFO_INIT(PL4_ENABLE, POWER_LIMIT4_MASK, 0,
655 RAPL_DOMAIN_REG_PL4, ARBITRARY_UNIT, 0),
Jacob Pan2d281d82013-10-17 10:28:35 -0700656 PRIMITIVE_INFO_INIT(TIME_WINDOW1, TIME_WINDOW1_MASK, 17,
Zhang Rui33823882019-07-10 21:44:30 +0800657 RAPL_DOMAIN_REG_LIMIT, TIME_UNIT, 0),
Jacob Pan2d281d82013-10-17 10:28:35 -0700658 PRIMITIVE_INFO_INIT(TIME_WINDOW2, TIME_WINDOW2_MASK, 49,
Zhang Rui33823882019-07-10 21:44:30 +0800659 RAPL_DOMAIN_REG_LIMIT, TIME_UNIT, 0),
Jacob Pan2d281d82013-10-17 10:28:35 -0700660 PRIMITIVE_INFO_INIT(THERMAL_SPEC_POWER, POWER_INFO_THERMAL_SPEC_MASK,
Zhang Rui33823882019-07-10 21:44:30 +0800661 0, RAPL_DOMAIN_REG_INFO, POWER_UNIT, 0),
Jacob Pan2d281d82013-10-17 10:28:35 -0700662 PRIMITIVE_INFO_INIT(MAX_POWER, POWER_INFO_MAX_MASK, 32,
Zhang Rui33823882019-07-10 21:44:30 +0800663 RAPL_DOMAIN_REG_INFO, POWER_UNIT, 0),
Jacob Pan2d281d82013-10-17 10:28:35 -0700664 PRIMITIVE_INFO_INIT(MIN_POWER, POWER_INFO_MIN_MASK, 16,
Zhang Rui33823882019-07-10 21:44:30 +0800665 RAPL_DOMAIN_REG_INFO, POWER_UNIT, 0),
Jacob Pan2d281d82013-10-17 10:28:35 -0700666 PRIMITIVE_INFO_INIT(MAX_TIME_WINDOW, POWER_INFO_MAX_TIME_WIN_MASK, 48,
Zhang Rui33823882019-07-10 21:44:30 +0800667 RAPL_DOMAIN_REG_INFO, TIME_UNIT, 0),
Jacob Pan2d281d82013-10-17 10:28:35 -0700668 PRIMITIVE_INFO_INIT(THROTTLED_TIME, PERF_STATUS_THROTTLE_TIME_MASK, 0,
Zhang Rui33823882019-07-10 21:44:30 +0800669 RAPL_DOMAIN_REG_PERF, TIME_UNIT, 0),
Jacob Pan2d281d82013-10-17 10:28:35 -0700670 PRIMITIVE_INFO_INIT(PRIORITY_LEVEL, PP_POLICY_MASK, 0,
Zhang Rui33823882019-07-10 21:44:30 +0800671 RAPL_DOMAIN_REG_POLICY, ARBITRARY_UNIT, 0),
Jacob Pan2d281d82013-10-17 10:28:35 -0700672 /* non-hardware */
673 PRIMITIVE_INFO_INIT(AVERAGE_POWER, 0, 0, 0, POWER_UNIT,
Zhang Rui33823882019-07-10 21:44:30 +0800674 RAPL_PRIMITIVE_DERIVED),
Jacob Pan2d281d82013-10-17 10:28:35 -0700675 {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 */
691static int rapl_read_data_raw(struct rapl_domain *rd,
Zhang Rui33823882019-07-10 21:44:30 +0800692 enum rapl_primitives prim, bool xlate, u64 *data)
Jacob Pan2d281d82013-10-17 10:28:35 -0700693{
Zhang Ruibeea8df2019-07-10 21:44:27 +0800694 u64 value;
Jacob Pan2d281d82013-10-17 10:28:35 -0700695 struct rapl_primitive_info *rp = &rpi[prim];
Zhang Ruibeea8df2019-07-10 21:44:27 +0800696 struct reg_action ra;
Jacob Pan2d281d82013-10-17 10:28:35 -0700697 int cpu;
698
699 if (!rp->name || rp->flag & RAPL_PRIMITIVE_DUMMY)
700 return -EINVAL;
701
Zhang Ruibeea8df2019-07-10 21:44:27 +0800702 ra.reg = rd->regs[rp->id];
703 if (!ra.reg)
Jacob Pan2d281d82013-10-17 10:28:35 -0700704 return -EINVAL;
Jacob Pan323ee642016-02-24 13:31:38 -0800705
706 cpu = rd->rp->lead_cpu;
Jacob Pan2d281d82013-10-17 10:28:35 -0700707
Zhang Rui0c2dded2019-07-10 21:44:32 +0800708 /* 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 Pan2d281d82013-10-17 10:28:35 -0700711 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 Ruibeea8df2019-07-10 21:44:27 +0800719 ra.mask = rp->mask;
720
721 if (rd->rp->priv->read_raw(cpu, &ra)) {
Zhang Ruid978e752019-07-10 21:44:31 +0800722 pr_debug("failed to read reg 0x%llx on cpu %d\n", ra.reg, cpu);
Jacob Pan2d281d82013-10-17 10:28:35 -0700723 return -EIO;
724 }
725
Zhang Ruibeea8df2019-07-10 21:44:27 +0800726 value = ra.value >> rp->shift;
727
Jacob Pan2d281d82013-10-17 10:28:35 -0700728 if (xlate)
Zhang Ruibeea8df2019-07-10 21:44:27 +0800729 *data = rapl_unit_xlate(rd, rp->unit, value, 0);
Jacob Pan2d281d82013-10-17 10:28:35 -0700730 else
Zhang Ruibeea8df2019-07-10 21:44:27 +0800731 *data = value;
Jacob Pan2d281d82013-10-17 10:28:35 -0700732
733 return 0;
734}
735
736/* Similar use of primitive info in the read counterpart */
737static int rapl_write_data_raw(struct rapl_domain *rd,
Zhang Rui33823882019-07-10 21:44:30 +0800738 enum rapl_primitives prim,
739 unsigned long long value)
Jacob Pan2d281d82013-10-17 10:28:35 -0700740{
Jacob Pan2d281d82013-10-17 10:28:35 -0700741 struct rapl_primitive_info *rp = &rpi[prim];
742 int cpu;
Jacob Panf14a1392016-02-24 13:31:36 -0800743 u64 bits;
Zhang Ruibeea8df2019-07-10 21:44:27 +0800744 struct reg_action ra;
Jacob Panf14a1392016-02-24 13:31:36 -0800745 int ret;
Jacob Pan2d281d82013-10-17 10:28:35 -0700746
Jacob Pan323ee642016-02-24 13:31:38 -0800747 cpu = rd->rp->lead_cpu;
Jacob Pan309557f2016-02-24 13:31:37 -0800748 bits = rapl_unit_xlate(rd, rp->unit, value, 1);
Adam Lessnauedbdabc2017-06-01 11:21:50 +0200749 bits <<= rp->shift;
750 bits &= rp->mask;
751
Zhang Ruibeea8df2019-07-10 21:44:27 +0800752 memset(&ra, 0, sizeof(ra));
Jacob Panf14a1392016-02-24 13:31:36 -0800753
Zhang Ruibeea8df2019-07-10 21:44:27 +0800754 ra.reg = rd->regs[rp->id];
755 ra.mask = rp->mask;
756 ra.value = bits;
Jacob Panf14a1392016-02-24 13:31:36 -0800757
Zhang Ruibeea8df2019-07-10 21:44:27 +0800758 ret = rd->rp->priv->write_raw(cpu, &ra);
Jacob Panf14a1392016-02-24 13:31:36 -0800759
760 return ret;
Jacob Pan2d281d82013-10-17 10:28:35 -0700761}
762
Jacob Pan3c2c0842014-11-07 09:29:26 -0800763/*
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 Pand474a4d2015-03-13 03:48:56 -0700770 * energy unit: picoJoules : Represented in picoJoules by default
Jacob Pan3c2c0842014-11-07 09:29:26 -0800771 * power unit : microWatts : Represented in milliWatts by default
772 * time unit : microseconds: Represented in seconds by default
773 */
774static int rapl_check_unit_core(struct rapl_package *rp, int cpu)
Jacob Pan2d281d82013-10-17 10:28:35 -0700775{
Zhang Rui1193b162019-07-10 21:44:29 +0800776 struct reg_action ra;
Jacob Pan2d281d82013-10-17 10:28:35 -0700777 u32 value;
778
Zhang Rui1193b162019-07-10 21:44:29 +0800779 ra.reg = rp->priv->reg_unit;
780 ra.mask = ~0;
781 if (rp->priv->read_raw(cpu, &ra)) {
Zhang Ruid978e752019-07-10 21:44:31 +0800782 pr_err("Failed to read power unit REG 0x%llx on CPU %d, exit.\n",
Zhang Rui33823882019-07-10 21:44:30 +0800783 rp->priv->reg_unit, cpu);
Jacob Pan2d281d82013-10-17 10:28:35 -0700784 return -ENODEV;
785 }
786
Zhang Rui1193b162019-07-10 21:44:29 +0800787 value = (ra.value & ENERGY_UNIT_MASK) >> ENERGY_UNIT_OFFSET;
Jacob Pand474a4d2015-03-13 03:48:56 -0700788 rp->energy_unit = ENERGY_UNIT_SCALE * 1000000 / (1 << value);
Jacob Pan2d281d82013-10-17 10:28:35 -0700789
Zhang Rui1193b162019-07-10 21:44:29 +0800790 value = (ra.value & POWER_UNIT_MASK) >> POWER_UNIT_OFFSET;
Jacob Pan3c2c0842014-11-07 09:29:26 -0800791 rp->power_unit = 1000000 / (1 << value);
Jacob Pan2d281d82013-10-17 10:28:35 -0700792
Zhang Rui1193b162019-07-10 21:44:29 +0800793 value = (ra.value & TIME_UNIT_MASK) >> TIME_UNIT_OFFSET;
Jacob Pan3c2c0842014-11-07 09:29:26 -0800794 rp->time_unit = 1000000 / (1 << value);
Jacob Pan2d281d82013-10-17 10:28:35 -0700795
Zhang Rui9ea76122019-05-13 13:58:53 -0400796 pr_debug("Core CPU %s energy=%dpJ, time=%dus, power=%duW\n",
Zhang Rui33823882019-07-10 21:44:30 +0800797 rp->name, rp->energy_unit, rp->time_unit, rp->power_unit);
Jacob Pan2d281d82013-10-17 10:28:35 -0700798
799 return 0;
800}
801
Jacob Pan3c2c0842014-11-07 09:29:26 -0800802static int rapl_check_unit_atom(struct rapl_package *rp, int cpu)
803{
Zhang Rui1193b162019-07-10 21:44:29 +0800804 struct reg_action ra;
Jacob Pan3c2c0842014-11-07 09:29:26 -0800805 u32 value;
806
Zhang Rui1193b162019-07-10 21:44:29 +0800807 ra.reg = rp->priv->reg_unit;
808 ra.mask = ~0;
809 if (rp->priv->read_raw(cpu, &ra)) {
Zhang Ruid978e752019-07-10 21:44:31 +0800810 pr_err("Failed to read power unit REG 0x%llx on CPU %d, exit.\n",
Zhang Rui33823882019-07-10 21:44:30 +0800811 rp->priv->reg_unit, cpu);
Jacob Pan3c2c0842014-11-07 09:29:26 -0800812 return -ENODEV;
813 }
Zhang Rui1193b162019-07-10 21:44:29 +0800814
815 value = (ra.value & ENERGY_UNIT_MASK) >> ENERGY_UNIT_OFFSET;
Jacob Pand474a4d2015-03-13 03:48:56 -0700816 rp->energy_unit = ENERGY_UNIT_SCALE * 1 << value;
Jacob Pan3c2c0842014-11-07 09:29:26 -0800817
Zhang Rui1193b162019-07-10 21:44:29 +0800818 value = (ra.value & POWER_UNIT_MASK) >> POWER_UNIT_OFFSET;
Jacob Pan3c2c0842014-11-07 09:29:26 -0800819 rp->power_unit = (1 << value) * 1000;
820
Zhang Rui1193b162019-07-10 21:44:29 +0800821 value = (ra.value & TIME_UNIT_MASK) >> TIME_UNIT_OFFSET;
Jacob Pan3c2c0842014-11-07 09:29:26 -0800822 rp->time_unit = 1000000 / (1 << value);
823
Zhang Rui9ea76122019-05-13 13:58:53 -0400824 pr_debug("Atom %s energy=%dpJ, time=%dus, power=%duW\n",
Zhang Rui33823882019-07-10 21:44:30 +0800825 rp->name, rp->energy_unit, rp->time_unit, rp->power_unit);
Jacob Pan3c2c0842014-11-07 09:29:26 -0800826
827 return 0;
828}
829
Jacob Panf14a1392016-02-24 13:31:36 -0800830static 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 Pan2d281d82013-10-17 10:28:35 -0700845/* 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 Pan309557f2016-02-24 13:31:37 -0800855static void package_power_limit_irq_save(struct rapl_package *rp)
Jacob Pan2d281d82013-10-17 10:28:35 -0700856{
Jacob Pan2d281d82013-10-17 10:28:35 -0700857 if (!boot_cpu_has(X86_FEATURE_PTS) || !boot_cpu_has(X86_FEATURE_PLN))
858 return;
859
Jacob Pan323ee642016-02-24 13:31:38 -0800860 smp_call_function_single(rp->lead_cpu, power_limit_irq_save_cpu, rp, 1);
Jacob Panf14a1392016-02-24 13:31:36 -0800861}
862
Thomas Gleixner58705062016-11-22 21:16:02 +0000863/*
864 * Restore per package power limit interrupt enable state. Called from cpu
865 * hotplug code on package removal.
866 */
867static void package_power_limit_irq_restore(struct rapl_package *rp)
Jacob Panf14a1392016-02-24 13:31:36 -0800868{
Thomas Gleixner58705062016-11-22 21:16:02 +0000869 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 Panf14a1392016-02-24 13:31:36 -0800877
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 Pan2d281d82013-10-17 10:28:35 -0700886}
887
Jacob Pan3c2c0842014-11-07 09:29:26 -0800888static 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
904static void set_floor_freq_atom(struct rapl_domain *rd, bool enable)
905{
906 static u32 power_ctrl_orig_val;
907 u32 mdata;
908
Ajay Thomas51b63402015-04-30 01:43:23 +0530909 if (!rapl_defaults->floor_freq_reg_addr) {
910 pr_err("Invalid floor frequency config register\n");
911 return;
912 }
913
Jacob Pan3c2c0842014-11-07 09:29:26 -0800914 if (!power_ctrl_orig_val)
Andy Shevchenko4077a382015-11-11 19:59:29 +0200915 iosf_mbi_read(BT_MBI_UNIT_PMC, MBI_CR_READ,
916 rapl_defaults->floor_freq_reg_addr,
917 &power_ctrl_orig_val);
Jacob Pan3c2c0842014-11-07 09:29:26 -0800918 mdata = power_ctrl_orig_val;
919 if (enable) {
920 mdata &= ~(0x7f << 8);
921 mdata |= 1 << 8;
922 }
Andy Shevchenko4077a382015-11-11 19:59:29 +0200923 iosf_mbi_write(BT_MBI_UNIT_PMC, MBI_CR_WRITE,
924 rapl_defaults->floor_freq_reg_addr, mdata);
Jacob Pan3c2c0842014-11-07 09:29:26 -0800925}
926
927static u64 rapl_compute_time_window_core(struct rapl_package *rp, u64 value,
Zhang Rui33823882019-07-10 21:44:30 +0800928 bool to_raw)
Jacob Pan3c2c0842014-11-07 09:29:26 -0800929{
Zhang Rui33823882019-07-10 21:44:30 +0800930 u64 f, y; /* fraction and exp. used for time unit */
Jacob Pan3c2c0842014-11-07 09:29:26 -0800931
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
949static u64 rapl_compute_time_window_atom(struct rapl_package *rp, u64 value,
Zhang Rui33823882019-07-10 21:44:30 +0800950 bool to_raw)
Jacob Pan3c2c0842014-11-07 09:29:26 -0800951{
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 Rui33823882019-07-10 21:44:30 +0800958
959 value = div64_u64(value, rp->time_unit);
Jacob Pan3c2c0842014-11-07 09:29:26 -0800960
961 return value;
962}
963
Jacob Pan087e9cb2014-11-07 09:29:25 -0800964static const struct rapl_defaults rapl_defaults_core = {
Ajay Thomas51b63402015-04-30 01:43:23 +0530965 .floor_freq_reg_addr = 0,
Jacob Pan3c2c0842014-11-07 09:29:26 -0800966 .check_unit = rapl_check_unit_core,
967 .set_floor_freq = set_floor_freq_default,
968 .compute_time_window = rapl_compute_time_window_core,
Jacob Pan087e9cb2014-11-07 09:29:25 -0800969};
970
Jacob Pand474a4d2015-03-13 03:48:56 -0700971static 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 Rui2d798d92020-06-29 13:34:50 +0800978static 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 Thomas51b63402015-04-30 01:43:23 +0530986static const struct rapl_defaults rapl_defaults_byt = {
987 .floor_freq_reg_addr = IOSF_CPU_POWER_BUDGET_CTL_BYT,
Jacob Pan3c2c0842014-11-07 09:29:26 -0800988 .check_unit = rapl_check_unit_atom,
989 .set_floor_freq = set_floor_freq_atom,
990 .compute_time_window = rapl_compute_time_window_atom,
Jacob Pan087e9cb2014-11-07 09:29:25 -0800991};
992
Ajay Thomas51b63402015-04-30 01:43:23 +0530993static 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
1000static 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
1007static 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
Mathias Krauseea85dbc2015-03-25 22:15:52 +01001014static const struct x86_cpu_id rapl_ids[] __initconst = {
Thomas Gleixnerf0722512020-03-20 14:14:03 +01001015 X86_MATCH_INTEL_FAM6_MODEL(SANDYBRIDGE, &rapl_defaults_core),
1016 X86_MATCH_INTEL_FAM6_MODEL(SANDYBRIDGE_X, &rapl_defaults_core),
Dave Hansen0bb04b52016-06-02 17:19:37 -07001017
Thomas Gleixnerf0722512020-03-20 14:14:03 +01001018 X86_MATCH_INTEL_FAM6_MODEL(IVYBRIDGE, &rapl_defaults_core),
1019 X86_MATCH_INTEL_FAM6_MODEL(IVYBRIDGE_X, &rapl_defaults_core),
Dave Hansen0bb04b52016-06-02 17:19:37 -07001020
Thomas Gleixnerf0722512020-03-20 14:14:03 +01001021 X86_MATCH_INTEL_FAM6_MODEL(HASWELL, &rapl_defaults_core),
1022 X86_MATCH_INTEL_FAM6_MODEL(HASWELL_L, &rapl_defaults_core),
1023 X86_MATCH_INTEL_FAM6_MODEL(HASWELL_G, &rapl_defaults_core),
1024 X86_MATCH_INTEL_FAM6_MODEL(HASWELL_X, &rapl_defaults_hsw_server),
Dave Hansen0bb04b52016-06-02 17:19:37 -07001025
Thomas Gleixnerf0722512020-03-20 14:14:03 +01001026 X86_MATCH_INTEL_FAM6_MODEL(BROADWELL, &rapl_defaults_core),
1027 X86_MATCH_INTEL_FAM6_MODEL(BROADWELL_G, &rapl_defaults_core),
1028 X86_MATCH_INTEL_FAM6_MODEL(BROADWELL_D, &rapl_defaults_core),
1029 X86_MATCH_INTEL_FAM6_MODEL(BROADWELL_X, &rapl_defaults_hsw_server),
Dave Hansen0bb04b52016-06-02 17:19:37 -07001030
Thomas Gleixnerf0722512020-03-20 14:14:03 +01001031 X86_MATCH_INTEL_FAM6_MODEL(SKYLAKE, &rapl_defaults_core),
1032 X86_MATCH_INTEL_FAM6_MODEL(SKYLAKE_L, &rapl_defaults_core),
1033 X86_MATCH_INTEL_FAM6_MODEL(SKYLAKE_X, &rapl_defaults_hsw_server),
1034 X86_MATCH_INTEL_FAM6_MODEL(KABYLAKE_L, &rapl_defaults_core),
1035 X86_MATCH_INTEL_FAM6_MODEL(KABYLAKE, &rapl_defaults_core),
1036 X86_MATCH_INTEL_FAM6_MODEL(CANNONLAKE_L, &rapl_defaults_core),
1037 X86_MATCH_INTEL_FAM6_MODEL(ICELAKE_L, &rapl_defaults_core),
1038 X86_MATCH_INTEL_FAM6_MODEL(ICELAKE, &rapl_defaults_core),
1039 X86_MATCH_INTEL_FAM6_MODEL(ICELAKE_NNPI, &rapl_defaults_core),
1040 X86_MATCH_INTEL_FAM6_MODEL(ICELAKE_X, &rapl_defaults_hsw_server),
1041 X86_MATCH_INTEL_FAM6_MODEL(ICELAKE_D, &rapl_defaults_hsw_server),
1042 X86_MATCH_INTEL_FAM6_MODEL(COMETLAKE_L, &rapl_defaults_core),
1043 X86_MATCH_INTEL_FAM6_MODEL(COMETLAKE, &rapl_defaults_core),
1044 X86_MATCH_INTEL_FAM6_MODEL(TIGERLAKE_L, &rapl_defaults_core),
Zhang Rui57a2fb02020-09-10 15:48:58 +08001045 X86_MATCH_INTEL_FAM6_MODEL(TIGERLAKE, &rapl_defaults_core),
Zhang Rui64e5f362020-09-10 15:49:11 +08001046 X86_MATCH_INTEL_FAM6_MODEL(ROCKETLAKE, &rapl_defaults_core),
Zhang Ruiba92a422020-09-10 15:49:21 +08001047 X86_MATCH_INTEL_FAM6_MODEL(ALDERLAKE, &rapl_defaults_core),
Zhang Rui2d798d92020-06-29 13:34:50 +08001048 X86_MATCH_INTEL_FAM6_MODEL(SAPPHIRERAPIDS_X, &rapl_defaults_spr_server),
Ricardo Nerie1c2d962020-08-21 11:48:10 -07001049 X86_MATCH_INTEL_FAM6_MODEL(LAKEFIELD, &rapl_defaults_core),
Dave Hansen0bb04b52016-06-02 17:19:37 -07001050
Thomas Gleixnerf0722512020-03-20 14:14:03 +01001051 X86_MATCH_INTEL_FAM6_MODEL(ATOM_SILVERMONT, &rapl_defaults_byt),
1052 X86_MATCH_INTEL_FAM6_MODEL(ATOM_AIRMONT, &rapl_defaults_cht),
1053 X86_MATCH_INTEL_FAM6_MODEL(ATOM_SILVERMONT_MID, &rapl_defaults_tng),
1054 X86_MATCH_INTEL_FAM6_MODEL(ATOM_AIRMONT_MID, &rapl_defaults_ann),
1055 X86_MATCH_INTEL_FAM6_MODEL(ATOM_GOLDMONT, &rapl_defaults_core),
1056 X86_MATCH_INTEL_FAM6_MODEL(ATOM_GOLDMONT_PLUS, &rapl_defaults_core),
1057 X86_MATCH_INTEL_FAM6_MODEL(ATOM_GOLDMONT_D, &rapl_defaults_core),
Jacob Pan33c98002020-05-15 15:30:41 +08001058 X86_MATCH_INTEL_FAM6_MODEL(ATOM_TREMONT, &rapl_defaults_core),
Thomas Gleixnerf0722512020-03-20 14:14:03 +01001059 X86_MATCH_INTEL_FAM6_MODEL(ATOM_TREMONT_D, &rapl_defaults_core),
1060 X86_MATCH_INTEL_FAM6_MODEL(ATOM_TREMONT_L, &rapl_defaults_core),
Dave Hansen0bb04b52016-06-02 17:19:37 -07001061
Thomas Gleixnerf0722512020-03-20 14:14:03 +01001062 X86_MATCH_INTEL_FAM6_MODEL(XEON_PHI_KNL, &rapl_defaults_hsw_server),
1063 X86_MATCH_INTEL_FAM6_MODEL(XEON_PHI_KNM, &rapl_defaults_hsw_server),
Jacob Pan2d281d82013-10-17 10:28:35 -07001064 {}
1065};
1066MODULE_DEVICE_TABLE(x86cpu, rapl_ids);
1067
Thomas Gleixnerbed5ab62016-11-22 21:15:58 +00001068/* Read once for all raw primitive data for domains */
1069static void rapl_update_domain_data(struct rapl_package *rp)
Jacob Pan2d281d82013-10-17 10:28:35 -07001070{
1071 int dmn, prim;
1072 u64 val;
Jacob Pan2d281d82013-10-17 10:28:35 -07001073
Thomas Gleixnerbed5ab62016-11-22 21:15:58 +00001074 for (dmn = 0; dmn < rp->nr_domains; dmn++) {
Zhang Rui9ea76122019-05-13 13:58:53 -04001075 pr_debug("update %s domain %s data\n", rp->name,
Thomas Gleixnerbed5ab62016-11-22 21:15:58 +00001076 rp->domains[dmn].name);
1077 /* exclude non-raw primitives */
1078 for (prim = 0; prim < NR_RAW_PRIMITIVES; prim++) {
1079 if (!rapl_read_data_raw(&rp->domains[dmn], prim,
1080 rpi[prim].unit, &val))
Zhang Rui33823882019-07-10 21:44:30 +08001081 rp->domains[dmn].rdd.primitives[prim] = val;
Jacob Pan2d281d82013-10-17 10:28:35 -07001082 }
1083 }
1084
1085}
1086
Jacob Pan2d281d82013-10-17 10:28:35 -07001087static int rapl_package_register_powercap(struct rapl_package *rp)
1088{
1089 struct rapl_domain *rd;
Jacob Pan2d281d82013-10-17 10:28:35 -07001090 struct powercap_zone *power_zone = NULL;
Luis de Bethencourt01857cf2018-01-17 10:30:34 +00001091 int nr_pl, ret;
Thomas Gleixnerbed5ab62016-11-22 21:15:58 +00001092
1093 /* Update the domain data of the new package */
1094 rapl_update_domain_data(rp);
Jacob Pan2d281d82013-10-17 10:28:35 -07001095
Zhang Rui33823882019-07-10 21:44:30 +08001096 /* first we register package domain as the parent zone */
Jacob Pan2d281d82013-10-17 10:28:35 -07001097 for (rd = rp->domains; rd < rp->domains + rp->nr_domains; rd++) {
1098 if (rd->id == RAPL_DOMAIN_PACKAGE) {
1099 nr_pl = find_nr_power_limit(rd);
Zhang Rui9ea76122019-05-13 13:58:53 -04001100 pr_debug("register package domain %s\n", rp->name);
Jacob Pan2d281d82013-10-17 10:28:35 -07001101 power_zone = powercap_register_zone(&rd->power_zone,
Zhang Rui33823882019-07-10 21:44:30 +08001102 rp->priv->control_type, rp->name,
1103 NULL, &zone_ops[rd->id], nr_pl,
1104 &constraint_ops);
Jacob Pan2d281d82013-10-17 10:28:35 -07001105 if (IS_ERR(power_zone)) {
Zhang Rui9ea76122019-05-13 13:58:53 -04001106 pr_debug("failed to register power zone %s\n",
Zhang Rui33823882019-07-10 21:44:30 +08001107 rp->name);
Thomas Gleixnerbed5ab62016-11-22 21:15:58 +00001108 return PTR_ERR(power_zone);
Jacob Pan2d281d82013-10-17 10:28:35 -07001109 }
1110 /* track parent zone in per package/socket data */
1111 rp->power_zone = power_zone;
1112 /* done, only one package domain per socket */
1113 break;
1114 }
1115 }
1116 if (!power_zone) {
1117 pr_err("no package domain found, unknown topology!\n");
Thomas Gleixnerbed5ab62016-11-22 21:15:58 +00001118 return -ENODEV;
Jacob Pan2d281d82013-10-17 10:28:35 -07001119 }
Zhang Rui33823882019-07-10 21:44:30 +08001120 /* now register domains as children of the socket/package */
Jacob Pan2d281d82013-10-17 10:28:35 -07001121 for (rd = rp->domains; rd < rp->domains + rp->nr_domains; rd++) {
Zhang Ruif1e8d752020-10-13 15:42:41 +08001122 struct powercap_zone *parent = rp->power_zone;
1123
Jacob Pan2d281d82013-10-17 10:28:35 -07001124 if (rd->id == RAPL_DOMAIN_PACKAGE)
1125 continue;
Zhang Ruif1e8d752020-10-13 15:42:41 +08001126 if (rd->id == RAPL_DOMAIN_PLATFORM)
1127 parent = NULL;
Jacob Pan2d281d82013-10-17 10:28:35 -07001128 /* number of power limits per domain varies */
1129 nr_pl = find_nr_power_limit(rd);
1130 power_zone = powercap_register_zone(&rd->power_zone,
Zhang Rui33823882019-07-10 21:44:30 +08001131 rp->priv->control_type,
Zhang Ruif1e8d752020-10-13 15:42:41 +08001132 rd->name, parent,
Zhang Rui33823882019-07-10 21:44:30 +08001133 &zone_ops[rd->id], nr_pl,
1134 &constraint_ops);
Jacob Pan2d281d82013-10-17 10:28:35 -07001135
1136 if (IS_ERR(power_zone)) {
Zhang Rui9ea76122019-05-13 13:58:53 -04001137 pr_debug("failed to register power_zone, %s:%s\n",
Zhang Rui33823882019-07-10 21:44:30 +08001138 rp->name, rd->name);
Jacob Pan2d281d82013-10-17 10:28:35 -07001139 ret = PTR_ERR(power_zone);
1140 goto err_cleanup;
1141 }
1142 }
Thomas Gleixnerbed5ab62016-11-22 21:15:58 +00001143 return 0;
Jacob Pan2d281d82013-10-17 10:28:35 -07001144
Jacob Pan2d281d82013-10-17 10:28:35 -07001145err_cleanup:
Thomas Gleixner58705062016-11-22 21:16:02 +00001146 /*
1147 * Clean up previously initialized domains within the package if we
Jacob Pan2d281d82013-10-17 10:28:35 -07001148 * failed after the first domain setup.
1149 */
1150 while (--rd >= rp->domains) {
Zhang Rui9ea76122019-05-13 13:58:53 -04001151 pr_debug("unregister %s domain %s\n", rp->name, rd->name);
Zhang Rui33823882019-07-10 21:44:30 +08001152 powercap_unregister_zone(rp->priv->control_type,
1153 &rd->power_zone);
Jacob Pan2d281d82013-10-17 10:28:35 -07001154 }
1155
1156 return ret;
1157}
1158
Zhang Rui7fde2712019-07-10 21:44:26 +08001159static int rapl_check_domain(int cpu, int domain, struct rapl_package *rp)
Jacob Pan2d281d82013-10-17 10:28:35 -07001160{
Zhang Rui1193b162019-07-10 21:44:29 +08001161 struct reg_action ra;
Jacob Pan2d281d82013-10-17 10:28:35 -07001162
1163 switch (domain) {
1164 case RAPL_DOMAIN_PACKAGE:
Jacob Pan2d281d82013-10-17 10:28:35 -07001165 case RAPL_DOMAIN_PP0:
Jacob Pan2d281d82013-10-17 10:28:35 -07001166 case RAPL_DOMAIN_PP1:
Jacob Pan2d281d82013-10-17 10:28:35 -07001167 case RAPL_DOMAIN_DRAM:
Zhang Ruif1e8d752020-10-13 15:42:41 +08001168 case RAPL_DOMAIN_PLATFORM:
Zhang Rui1193b162019-07-10 21:44:29 +08001169 ra.reg = rp->priv->regs[domain][RAPL_DOMAIN_REG_STATUS];
Jacob Pan2d281d82013-10-17 10:28:35 -07001170 break;
1171 default:
1172 pr_err("invalid domain id %d\n", domain);
1173 return -EINVAL;
1174 }
Jacob Pan9d31c672014-04-29 15:33:06 -07001175 /* make sure domain counters are available and contains non-zero
1176 * values, otherwise skip it.
1177 */
Zhang Rui1193b162019-07-10 21:44:29 +08001178
Zhang Rui7a57e9f2020-10-13 15:42:40 +08001179 ra.mask = ENERGY_STATUS_MASK;
Zhang Rui1193b162019-07-10 21:44:29 +08001180 if (rp->priv->read_raw(cpu, &ra) || !ra.value)
Jacob Pan2d281d82013-10-17 10:28:35 -07001181 return -ENODEV;
1182
Jacob Pan9d31c672014-04-29 15:33:06 -07001183 return 0;
Jacob Pan2d281d82013-10-17 10:28:35 -07001184}
1185
Jacob Pane1399ba2016-05-31 13:41:29 -07001186/*
1187 * Check if power limits are available. Two cases when they are not available:
1188 * 1. Locked by BIOS, in this case we still provide read-only access so that
1189 * users can see what limit is set by the BIOS.
1190 * 2. Some CPUs make some domains monitoring only which means PLx MSRs may not
Zhang Rui33823882019-07-10 21:44:30 +08001191 * exist at all. In this case, we do not show the constraints in powercap.
Jacob Pane1399ba2016-05-31 13:41:29 -07001192 *
1193 * Called after domains are detected and initialized.
1194 */
1195static void rapl_detect_powerlimit(struct rapl_domain *rd)
1196{
1197 u64 val64;
1198 int i;
1199
1200 /* check if the domain is locked by BIOS, ignore if MSR doesn't exist */
1201 if (!rapl_read_data_raw(rd, FW_LOCK, false, &val64)) {
1202 if (val64) {
Zhang Rui9ea76122019-05-13 13:58:53 -04001203 pr_info("RAPL %s domain %s locked by BIOS\n",
1204 rd->rp->name, rd->name);
Jacob Pane1399ba2016-05-31 13:41:29 -07001205 rd->state |= DOMAIN_STATE_BIOS_LOCKED;
1206 }
1207 }
Zhang Rui33823882019-07-10 21:44:30 +08001208 /* check if power limit MSR exists, otherwise domain is monitoring only */
Jacob Pane1399ba2016-05-31 13:41:29 -07001209 for (i = 0; i < NR_POWER_LIMITS; i++) {
1210 int prim = rd->rpl[i].prim_id;
Zhang Rui33823882019-07-10 21:44:30 +08001211
Jacob Pane1399ba2016-05-31 13:41:29 -07001212 if (rapl_read_data_raw(rd, prim, false, &val64))
1213 rd->rpl[i].name = NULL;
1214 }
1215}
1216
Jacob Pan2d281d82013-10-17 10:28:35 -07001217/* Detect active and valid domains for the given CPU, caller must
1218 * ensure the CPU belongs to the targeted package and CPU hotlug is disabled.
1219 */
1220static int rapl_detect_domains(struct rapl_package *rp, int cpu)
1221{
Jacob Pan2d281d82013-10-17 10:28:35 -07001222 struct rapl_domain *rd;
Thomas Gleixner58705062016-11-22 21:16:02 +00001223 int i;
Jacob Pan2d281d82013-10-17 10:28:35 -07001224
1225 for (i = 0; i < RAPL_DOMAIN_MAX; i++) {
1226 /* use physical package id to read counters */
Zhang Rui7fde2712019-07-10 21:44:26 +08001227 if (!rapl_check_domain(cpu, i, rp)) {
Jacob Pan2d281d82013-10-17 10:28:35 -07001228 rp->domain_map |= 1 << i;
Jacob Panfcdf1792014-09-02 02:55:21 -07001229 pr_info("Found RAPL domain %s\n", rapl_domain_names[i]);
1230 }
Jacob Pan2d281d82013-10-17 10:28:35 -07001231 }
Zhang Rui33823882019-07-10 21:44:30 +08001232 rp->nr_domains = bitmap_weight(&rp->domain_map, RAPL_DOMAIN_MAX);
Jacob Pan2d281d82013-10-17 10:28:35 -07001233 if (!rp->nr_domains) {
Zhang Rui9ea76122019-05-13 13:58:53 -04001234 pr_debug("no valid rapl domains found in %s\n", rp->name);
Thomas Gleixner58705062016-11-22 21:16:02 +00001235 return -ENODEV;
Jacob Pan2d281d82013-10-17 10:28:35 -07001236 }
Zhang Rui9ea76122019-05-13 13:58:53 -04001237 pr_debug("found %d domains on %s\n", rp->nr_domains, rp->name);
Jacob Pan2d281d82013-10-17 10:28:35 -07001238
1239 rp->domains = kcalloc(rp->nr_domains + 1, sizeof(struct rapl_domain),
Zhang Rui33823882019-07-10 21:44:30 +08001240 GFP_KERNEL);
Thomas Gleixner58705062016-11-22 21:16:02 +00001241 if (!rp->domains)
1242 return -ENOMEM;
1243
Jacob Pan2d281d82013-10-17 10:28:35 -07001244 rapl_init_domains(rp);
1245
Jacob Pane1399ba2016-05-31 13:41:29 -07001246 for (rd = rp->domains; rd < rp->domains + rp->nr_domains; rd++)
1247 rapl_detect_powerlimit(rd);
1248
Jacob Pan2d281d82013-10-17 10:28:35 -07001249 return 0;
1250}
1251
1252/* called from CPU hotplug notifier, hotplug lock held */
Zhang Rui33823882019-07-10 21:44:30 +08001253void rapl_remove_package(struct rapl_package *rp)
Jacob Pan2d281d82013-10-17 10:28:35 -07001254{
1255 struct rapl_domain *rd, *rd_package = NULL;
1256
Thomas Gleixner58705062016-11-22 21:16:02 +00001257 package_power_limit_irq_restore(rp);
1258
Jacob Pan2d281d82013-10-17 10:28:35 -07001259 for (rd = rp->domains; rd < rp->domains + rp->nr_domains; rd++) {
Thomas Gleixner58705062016-11-22 21:16:02 +00001260 rapl_write_data_raw(rd, PL1_ENABLE, 0);
1261 rapl_write_data_raw(rd, PL1_CLAMP, 0);
1262 if (find_nr_power_limit(rd) > 1) {
1263 rapl_write_data_raw(rd, PL2_ENABLE, 0);
1264 rapl_write_data_raw(rd, PL2_CLAMP, 0);
Sumeet Pawnikar8365a892020-07-16 23:14:55 +05301265 rapl_write_data_raw(rd, PL4_ENABLE, 0);
Thomas Gleixner58705062016-11-22 21:16:02 +00001266 }
Jacob Pan2d281d82013-10-17 10:28:35 -07001267 if (rd->id == RAPL_DOMAIN_PACKAGE) {
1268 rd_package = rd;
1269 continue;
1270 }
Zhang Rui9ea76122019-05-13 13:58:53 -04001271 pr_debug("remove package, undo power limit on %s: %s\n",
1272 rp->name, rd->name);
Zhang Rui33823882019-07-10 21:44:30 +08001273 powercap_unregister_zone(rp->priv->control_type,
1274 &rd->power_zone);
Jacob Pan2d281d82013-10-17 10:28:35 -07001275 }
1276 /* do parent zone last */
Zhang Rui33823882019-07-10 21:44:30 +08001277 powercap_unregister_zone(rp->priv->control_type,
1278 &rd_package->power_zone);
Jacob Pan2d281d82013-10-17 10:28:35 -07001279 list_del(&rp->plist);
1280 kfree(rp);
1281}
Zhang Rui33823882019-07-10 21:44:30 +08001282EXPORT_SYMBOL_GPL(rapl_remove_package);
1283
1284/* caller to ensure CPU hotplug lock is held */
1285struct rapl_package *rapl_find_package_domain(int cpu, struct rapl_if_priv *priv)
1286{
1287 int id = topology_logical_die_id(cpu);
1288 struct rapl_package *rp;
1289
1290 list_for_each_entry(rp, &rapl_packages, plist) {
1291 if (rp->id == id
1292 && rp->priv->control_type == priv->control_type)
1293 return rp;
1294 }
1295
1296 return NULL;
1297}
1298EXPORT_SYMBOL_GPL(rapl_find_package_domain);
Jacob Pan2d281d82013-10-17 10:28:35 -07001299
1300/* called from CPU hotplug notifier, hotplug lock held */
Zhang Rui33823882019-07-10 21:44:30 +08001301struct rapl_package *rapl_add_package(int cpu, struct rapl_if_priv *priv)
Jacob Pan2d281d82013-10-17 10:28:35 -07001302{
Zhang Rui32fb4802019-05-13 13:58:51 -04001303 int id = topology_logical_die_id(cpu);
Jacob Pan2d281d82013-10-17 10:28:35 -07001304 struct rapl_package *rp;
Zhang Rui9ea76122019-05-13 13:58:53 -04001305 struct cpuinfo_x86 *c = &cpu_data(cpu);
Thomas Gleixnerb4005e92016-11-22 21:16:05 +00001306 int ret;
Jacob Pan2d281d82013-10-17 10:28:35 -07001307
Harry Pan3aa3c582019-12-30 22:36:56 +08001308 if (!rapl_defaults)
1309 return ERR_PTR(-ENODEV);
1310
Jacob Pan2d281d82013-10-17 10:28:35 -07001311 rp = kzalloc(sizeof(struct rapl_package), GFP_KERNEL);
1312 if (!rp)
Thomas Gleixnerb4005e92016-11-22 21:16:05 +00001313 return ERR_PTR(-ENOMEM);
Jacob Pan2d281d82013-10-17 10:28:35 -07001314
1315 /* add the new package to the list */
Zhang Ruiaadf7b32019-05-13 13:58:50 -04001316 rp->id = id;
Jacob Pan323ee642016-02-24 13:31:38 -08001317 rp->lead_cpu = cpu;
Zhang Rui7ebf8ef2019-07-10 21:44:25 +08001318 rp->priv = priv;
Jacob Pan323ee642016-02-24 13:31:38 -08001319
Zhang Rui9ea76122019-05-13 13:58:53 -04001320 if (topology_max_die_per_package() > 1)
1321 snprintf(rp->name, PACKAGE_DOMAIN_NAME_LENGTH,
Zhang Rui33823882019-07-10 21:44:30 +08001322 "package-%d-die-%d", c->phys_proc_id, c->cpu_die_id);
Zhang Rui9ea76122019-05-13 13:58:53 -04001323 else
1324 snprintf(rp->name, PACKAGE_DOMAIN_NAME_LENGTH, "package-%d",
Zhang Rui33823882019-07-10 21:44:30 +08001325 c->phys_proc_id);
Zhang Rui9ea76122019-05-13 13:58:53 -04001326
Jacob Pan2d281d82013-10-17 10:28:35 -07001327 /* check if the package contains valid domains */
Zhang Rui33823882019-07-10 21:44:30 +08001328 if (rapl_detect_domains(rp, cpu) || rapl_defaults->check_unit(rp, cpu)) {
Jacob Pan2d281d82013-10-17 10:28:35 -07001329 ret = -ENODEV;
1330 goto err_free_package;
1331 }
Thomas Gleixnera74f4362016-11-22 21:15:59 +00001332 ret = rapl_package_register_powercap(rp);
1333 if (!ret) {
Jacob Pan2d281d82013-10-17 10:28:35 -07001334 INIT_LIST_HEAD(&rp->plist);
1335 list_add(&rp->plist, &rapl_packages);
Thomas Gleixnerb4005e92016-11-22 21:16:05 +00001336 return rp;
Jacob Pan2d281d82013-10-17 10:28:35 -07001337 }
1338
1339err_free_package:
1340 kfree(rp->domains);
1341 kfree(rp);
Thomas Gleixnerb4005e92016-11-22 21:16:05 +00001342 return ERR_PTR(ret);
Jacob Pan2d281d82013-10-17 10:28:35 -07001343}
Zhang Rui33823882019-07-10 21:44:30 +08001344EXPORT_SYMBOL_GPL(rapl_add_package);
Jacob Pan2d281d82013-10-17 10:28:35 -07001345
Zhen Han52b36722018-01-10 08:38:23 +08001346static void power_limit_state_save(void)
1347{
1348 struct rapl_package *rp;
1349 struct rapl_domain *rd;
1350 int nr_pl, ret, i;
1351
1352 get_online_cpus();
1353 list_for_each_entry(rp, &rapl_packages, plist) {
1354 if (!rp->power_zone)
1355 continue;
1356 rd = power_zone_to_rapl_domain(rp->power_zone);
1357 nr_pl = find_nr_power_limit(rd);
1358 for (i = 0; i < nr_pl; i++) {
1359 switch (rd->rpl[i].prim_id) {
1360 case PL1_ENABLE:
1361 ret = rapl_read_data_raw(rd,
Zhang Rui33823882019-07-10 21:44:30 +08001362 POWER_LIMIT1, true,
1363 &rd->rpl[i].last_power_limit);
Zhen Han52b36722018-01-10 08:38:23 +08001364 if (ret)
1365 rd->rpl[i].last_power_limit = 0;
1366 break;
1367 case PL2_ENABLE:
1368 ret = rapl_read_data_raw(rd,
Zhang Rui33823882019-07-10 21:44:30 +08001369 POWER_LIMIT2, true,
1370 &rd->rpl[i].last_power_limit);
Zhen Han52b36722018-01-10 08:38:23 +08001371 if (ret)
1372 rd->rpl[i].last_power_limit = 0;
1373 break;
Sumeet Pawnikar8365a892020-07-16 23:14:55 +05301374 case PL4_ENABLE:
1375 ret = rapl_read_data_raw(rd,
1376 POWER_LIMIT4, true,
1377 &rd->rpl[i].last_power_limit);
1378 if (ret)
1379 rd->rpl[i].last_power_limit = 0;
1380 break;
Zhen Han52b36722018-01-10 08:38:23 +08001381 }
1382 }
1383 }
1384 put_online_cpus();
1385}
1386
1387static void power_limit_state_restore(void)
1388{
1389 struct rapl_package *rp;
1390 struct rapl_domain *rd;
1391 int nr_pl, i;
1392
1393 get_online_cpus();
1394 list_for_each_entry(rp, &rapl_packages, plist) {
1395 if (!rp->power_zone)
1396 continue;
1397 rd = power_zone_to_rapl_domain(rp->power_zone);
1398 nr_pl = find_nr_power_limit(rd);
1399 for (i = 0; i < nr_pl; i++) {
1400 switch (rd->rpl[i].prim_id) {
1401 case PL1_ENABLE:
1402 if (rd->rpl[i].last_power_limit)
Zhang Rui33823882019-07-10 21:44:30 +08001403 rapl_write_data_raw(rd, POWER_LIMIT1,
1404 rd->rpl[i].last_power_limit);
Zhen Han52b36722018-01-10 08:38:23 +08001405 break;
1406 case PL2_ENABLE:
1407 if (rd->rpl[i].last_power_limit)
Zhang Rui33823882019-07-10 21:44:30 +08001408 rapl_write_data_raw(rd, POWER_LIMIT2,
1409 rd->rpl[i].last_power_limit);
Zhen Han52b36722018-01-10 08:38:23 +08001410 break;
Sumeet Pawnikar8365a892020-07-16 23:14:55 +05301411 case PL4_ENABLE:
1412 if (rd->rpl[i].last_power_limit)
1413 rapl_write_data_raw(rd, POWER_LIMIT4,
1414 rd->rpl[i].last_power_limit);
1415 break;
Zhen Han52b36722018-01-10 08:38:23 +08001416 }
1417 }
1418 }
1419 put_online_cpus();
1420}
1421
1422static int rapl_pm_callback(struct notifier_block *nb,
Zhang Rui33823882019-07-10 21:44:30 +08001423 unsigned long mode, void *_unused)
Zhen Han52b36722018-01-10 08:38:23 +08001424{
1425 switch (mode) {
1426 case PM_SUSPEND_PREPARE:
1427 power_limit_state_save();
1428 break;
1429 case PM_POST_SUSPEND:
1430 power_limit_state_restore();
1431 break;
1432 }
1433 return NOTIFY_OK;
1434}
1435
1436static struct notifier_block rapl_pm_notifier = {
1437 .notifier_call = rapl_pm_callback,
1438};
1439
Zhang Ruiabcfaeb2019-07-10 21:44:34 +08001440static struct platform_device *rapl_msr_platdev;
1441
1442static int __init rapl_init(void)
Jacob Pan2d281d82013-10-17 10:28:35 -07001443{
Jacob Pan087e9cb2014-11-07 09:29:25 -08001444 const struct x86_cpu_id *id;
Thomas Gleixner58705062016-11-22 21:16:02 +00001445 int ret;
Jacob Pan2d281d82013-10-17 10:28:35 -07001446
Jacob Pan087e9cb2014-11-07 09:29:25 -08001447 id = x86_match_cpu(rapl_ids);
1448 if (!id) {
Jacob Pan2d281d82013-10-17 10:28:35 -07001449 pr_err("driver does not support CPU family %d model %d\n",
Zhang Rui33823882019-07-10 21:44:30 +08001450 boot_cpu_data.x86, boot_cpu_data.x86_model);
Jacob Pan2d281d82013-10-17 10:28:35 -07001451
1452 return -ENODEV;
1453 }
Srivatsa S. Bhat009f2252014-03-11 02:09:26 +05301454
Jacob Pan087e9cb2014-11-07 09:29:25 -08001455 rapl_defaults = (struct rapl_defaults *)id->driver_data;
1456
Zhen Han52b36722018-01-10 08:38:23 +08001457 ret = register_pm_notifier(&rapl_pm_notifier);
Zhang Ruiabcfaeb2019-07-10 21:44:34 +08001458 if (ret)
1459 return ret;
Zhen Han52b36722018-01-10 08:38:23 +08001460
Zhang Ruiabcfaeb2019-07-10 21:44:34 +08001461 rapl_msr_platdev = platform_device_alloc("intel_rapl_msr", 0);
1462 if (!rapl_msr_platdev) {
1463 ret = -ENOMEM;
1464 goto end;
1465 }
1466
1467 ret = platform_device_add(rapl_msr_platdev);
1468 if (ret)
1469 platform_device_put(rapl_msr_platdev);
1470
1471end:
1472 if (ret)
1473 unregister_pm_notifier(&rapl_pm_notifier);
1474
1475 return ret;
Jacob Pan2d281d82013-10-17 10:28:35 -07001476}
1477
Zhang Ruiabcfaeb2019-07-10 21:44:34 +08001478static void __exit rapl_exit(void)
Jacob Pan2d281d82013-10-17 10:28:35 -07001479{
Zhang Ruiabcfaeb2019-07-10 21:44:34 +08001480 platform_device_unregister(rapl_msr_platdev);
Zhen Han52b36722018-01-10 08:38:23 +08001481 unregister_pm_notifier(&rapl_pm_notifier);
Jacob Pan2d281d82013-10-17 10:28:35 -07001482}
1483
Zhang Ruif76cb062019-07-19 23:25:14 +08001484fs_initcall(rapl_init);
Zhang Ruiabcfaeb2019-07-10 21:44:34 +08001485module_exit(rapl_exit);
1486
Zhang Rui33823882019-07-10 21:44:30 +08001487MODULE_DESCRIPTION("Intel Runtime Average Power Limit (RAPL) common code");
Jacob Pan2d281d82013-10-17 10:28:35 -07001488MODULE_AUTHOR("Jacob Pan <jacob.jun.pan@intel.com>");
1489MODULE_LICENSE("GPL v2");