blob: 45d5f22d00e8779bcb0b24f69660cb9d831bf460 [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/*
3 * Intel Running Average Power Limit (RAPL) Driver
4 * Copyright (c) 2013, 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>
Jacob Pan3c2c0842014-11-07 09:29:26 -080021#include <asm/iosf_mbi.h>
Jacob Pan2d281d82013-10-17 10:28:35 -070022
23#include <asm/processor.h>
24#include <asm/cpu_device_id.h>
Dave Hansen62d16732016-06-02 17:19:36 -070025#include <asm/intel-family.h>
Jacob Pan2d281d82013-10-17 10:28:35 -070026
Srinivas Pandruvada3521ba12016-04-17 15:03:01 -070027/* Local defines */
28#define MSR_PLATFORM_POWER_LIMIT 0x0000065C
29
Jacob Pan2d281d82013-10-17 10:28:35 -070030/* bitmasks for RAPL MSRs, used by primitive access functions */
31#define ENERGY_STATUS_MASK 0xffffffff
32
33#define POWER_LIMIT1_MASK 0x7FFF
34#define POWER_LIMIT1_ENABLE BIT(15)
35#define POWER_LIMIT1_CLAMP BIT(16)
36
37#define POWER_LIMIT2_MASK (0x7FFFULL<<32)
38#define POWER_LIMIT2_ENABLE BIT_ULL(47)
39#define POWER_LIMIT2_CLAMP BIT_ULL(48)
40#define POWER_PACKAGE_LOCK BIT_ULL(63)
41#define POWER_PP_LOCK BIT(31)
42
43#define TIME_WINDOW1_MASK (0x7FULL<<17)
44#define TIME_WINDOW2_MASK (0x7FULL<<49)
45
46#define POWER_UNIT_OFFSET 0
47#define POWER_UNIT_MASK 0x0F
48
49#define ENERGY_UNIT_OFFSET 0x08
50#define ENERGY_UNIT_MASK 0x1F00
51
52#define TIME_UNIT_OFFSET 0x10
53#define TIME_UNIT_MASK 0xF0000
54
55#define POWER_INFO_MAX_MASK (0x7fffULL<<32)
56#define POWER_INFO_MIN_MASK (0x7fffULL<<16)
57#define POWER_INFO_MAX_TIME_WIN_MASK (0x3fULL<<48)
58#define POWER_INFO_THERMAL_SPEC_MASK 0x7fff
59
60#define PERF_STATUS_THROTTLE_TIME_MASK 0xffffffff
61#define PP_POLICY_MASK 0x1F
62
63/* Non HW constants */
64#define RAPL_PRIMITIVE_DERIVED BIT(1) /* not from raw data */
65#define RAPL_PRIMITIVE_DUMMY BIT(2)
66
Jacob Pan2d281d82013-10-17 10:28:35 -070067#define TIME_WINDOW_MAX_MSEC 40000
68#define TIME_WINDOW_MIN_MSEC 250
Jacob Pand474a4d2015-03-13 03:48:56 -070069#define ENERGY_UNIT_SCALE 1000 /* scale from driver unit to powercap unit */
Jacob Pan2d281d82013-10-17 10:28:35 -070070enum unit_type {
71 ARBITRARY_UNIT, /* no translation */
72 POWER_UNIT,
73 ENERGY_UNIT,
74 TIME_UNIT,
75};
76
77enum rapl_domain_type {
78 RAPL_DOMAIN_PACKAGE, /* entire package/socket */
79 RAPL_DOMAIN_PP0, /* core power plane */
80 RAPL_DOMAIN_PP1, /* graphics uncore */
81 RAPL_DOMAIN_DRAM,/* DRAM control_type */
Srinivas Pandruvada3521ba12016-04-17 15:03:01 -070082 RAPL_DOMAIN_PLATFORM, /* PSys control_type */
Jacob Pan2d281d82013-10-17 10:28:35 -070083 RAPL_DOMAIN_MAX,
84};
85
Zhang Ruif7c4e0c2019-07-10 21:44:22 +080086enum rapl_domain_reg_id {
87 RAPL_DOMAIN_REG_LIMIT,
88 RAPL_DOMAIN_REG_STATUS,
89 RAPL_DOMAIN_REG_PERF,
90 RAPL_DOMAIN_REG_POLICY,
91 RAPL_DOMAIN_REG_INFO,
92 RAPL_DOMAIN_REG_MAX,
Jacob Pan2d281d82013-10-17 10:28:35 -070093};
94
95/* per domain data, some are optional */
96enum rapl_primitives {
97 ENERGY_COUNTER,
98 POWER_LIMIT1,
99 POWER_LIMIT2,
100 FW_LOCK,
101
102 PL1_ENABLE, /* power limit 1, aka long term */
103 PL1_CLAMP, /* allow frequency to go below OS request */
104 PL2_ENABLE, /* power limit 2, aka short term, instantaneous */
105 PL2_CLAMP,
106
107 TIME_WINDOW1, /* long term */
108 TIME_WINDOW2, /* short term */
109 THERMAL_SPEC_POWER,
110 MAX_POWER,
111
112 MIN_POWER,
113 MAX_TIME_WINDOW,
114 THROTTLED_TIME,
115 PRIORITY_LEVEL,
116
117 /* below are not raw primitive data */
118 AVERAGE_POWER,
119 NR_RAPL_PRIMITIVES,
120};
121
122#define NR_RAW_PRIMITIVES (NR_RAPL_PRIMITIVES - 2)
123
124/* Can be expanded to include events, etc.*/
125struct rapl_domain_data {
126 u64 primitives[NR_RAPL_PRIMITIVES];
127 unsigned long timestamp;
128};
129
Jacob Panf14a1392016-02-24 13:31:36 -0800130struct msrl_action {
131 u32 msr_no;
132 u64 clear_mask;
133 u64 set_mask;
134 int err;
135};
Jacob Pan2d281d82013-10-17 10:28:35 -0700136
137#define DOMAIN_STATE_INACTIVE BIT(0)
138#define DOMAIN_STATE_POWER_LIMIT_SET BIT(1)
139#define DOMAIN_STATE_BIOS_LOCKED BIT(2)
140
141#define NR_POWER_LIMITS (2)
142struct rapl_power_limit {
143 struct powercap_zone_constraint *constraint;
144 int prim_id; /* primitive ID used to enable */
145 struct rapl_domain *domain;
146 const char *name;
Zhen Han52b36722018-01-10 08:38:23 +0800147 u64 last_power_limit;
Jacob Pan2d281d82013-10-17 10:28:35 -0700148};
149
150static const char pl1_name[] = "long_term";
151static const char pl2_name[] = "short_term";
152
Jacob Pan309557f2016-02-24 13:31:37 -0800153struct rapl_package;
Jacob Pan2d281d82013-10-17 10:28:35 -0700154struct rapl_domain {
155 const char *name;
156 enum rapl_domain_type id;
Zhang Ruif7c4e0c2019-07-10 21:44:22 +0800157 int regs[RAPL_DOMAIN_REG_MAX];
Jacob Pan2d281d82013-10-17 10:28:35 -0700158 struct powercap_zone power_zone;
159 struct rapl_domain_data rdd;
160 struct rapl_power_limit rpl[NR_POWER_LIMITS];
161 u64 attr_map; /* track capabilities */
162 unsigned int state;
Jacob Pand474a4d2015-03-13 03:48:56 -0700163 unsigned int domain_energy_unit;
Jacob Pan309557f2016-02-24 13:31:37 -0800164 struct rapl_package *rp;
Jacob Pan2d281d82013-10-17 10:28:35 -0700165};
166#define power_zone_to_rapl_domain(_zone) \
167 container_of(_zone, struct rapl_domain, power_zone)
168
Zhang Rui9ea76122019-05-13 13:58:53 -0400169/* maximum rapl package domain name: package-%d-die-%d */
170#define PACKAGE_DOMAIN_NAME_LENGTH 30
Jacob Pan2d281d82013-10-17 10:28:35 -0700171
Zhang Rui9ea76122019-05-13 13:58:53 -0400172
173/* Each rapl package contains multiple domains, these are the common
Jacob Pan2d281d82013-10-17 10:28:35 -0700174 * data across RAPL domains within a package.
175 */
176struct rapl_package {
Zhang Rui9ea76122019-05-13 13:58:53 -0400177 unsigned int id; /* logical die id, equals physical 1-die systems */
Jacob Pan2d281d82013-10-17 10:28:35 -0700178 unsigned int nr_domains;
179 unsigned long domain_map; /* bit map of active domains */
Jacob Pan3c2c0842014-11-07 09:29:26 -0800180 unsigned int power_unit;
181 unsigned int energy_unit;
182 unsigned int time_unit;
Jacob Pan2d281d82013-10-17 10:28:35 -0700183 struct rapl_domain *domains; /* array of domains, sized at runtime */
184 struct powercap_zone *power_zone; /* keep track of parent zone */
Jacob Pan2d281d82013-10-17 10:28:35 -0700185 unsigned long power_limit_irq; /* keep track of package power limit
186 * notify interrupt enable status.
187 */
188 struct list_head plist;
Jacob Pan323ee642016-02-24 13:31:38 -0800189 int lead_cpu; /* one active cpu per package for access */
Thomas Gleixnerb4005e92016-11-22 21:16:05 +0000190 /* Track active cpus */
191 struct cpumask cpumask;
Zhang Rui9ea76122019-05-13 13:58:53 -0400192 char name[PACKAGE_DOMAIN_NAME_LENGTH];
Jacob Pan2d281d82013-10-17 10:28:35 -0700193};
Jacob Pan087e9cb2014-11-07 09:29:25 -0800194
195struct rapl_defaults {
Ajay Thomas51b63402015-04-30 01:43:23 +0530196 u8 floor_freq_reg_addr;
Jacob Pan087e9cb2014-11-07 09:29:25 -0800197 int (*check_unit)(struct rapl_package *rp, int cpu);
198 void (*set_floor_freq)(struct rapl_domain *rd, bool mode);
199 u64 (*compute_time_window)(struct rapl_package *rp, u64 val,
200 bool to_raw);
Jacob Pand474a4d2015-03-13 03:48:56 -0700201 unsigned int dram_domain_energy_unit;
Jacob Pan087e9cb2014-11-07 09:29:25 -0800202};
203static struct rapl_defaults *rapl_defaults;
204
Jacob Pan3c2c0842014-11-07 09:29:26 -0800205/* Sideband MBI registers */
Ajay Thomas51b63402015-04-30 01:43:23 +0530206#define IOSF_CPU_POWER_BUDGET_CTL_BYT (0x2)
207#define IOSF_CPU_POWER_BUDGET_CTL_TNG (0xdf)
Jacob Pan3c2c0842014-11-07 09:29:26 -0800208
Jacob Pan2d281d82013-10-17 10:28:35 -0700209#define PACKAGE_PLN_INT_SAVED BIT(0)
210#define MAX_PRIM_NAME (32)
211
212/* per domain data. used to describe individual knobs such that access function
213 * can be consolidated into one instead of many inline functions.
214 */
215struct rapl_primitive_info {
216 const char *name;
217 u64 mask;
218 int shift;
Zhang Ruif7c4e0c2019-07-10 21:44:22 +0800219 enum rapl_domain_reg_id id;
Jacob Pan2d281d82013-10-17 10:28:35 -0700220 enum unit_type unit;
221 u32 flag;
222};
223
224#define PRIMITIVE_INFO_INIT(p, m, s, i, u, f) { \
225 .name = #p, \
226 .mask = m, \
227 .shift = s, \
228 .id = i, \
229 .unit = u, \
230 .flag = f \
231 }
232
233static void rapl_init_domains(struct rapl_package *rp);
234static int rapl_read_data_raw(struct rapl_domain *rd,
235 enum rapl_primitives prim,
236 bool xlate, u64 *data);
237static int rapl_write_data_raw(struct rapl_domain *rd,
238 enum rapl_primitives prim,
239 unsigned long long value);
Jacob Pan309557f2016-02-24 13:31:37 -0800240static u64 rapl_unit_xlate(struct rapl_domain *rd,
Jacob Pand474a4d2015-03-13 03:48:56 -0700241 enum unit_type type, u64 value,
Jacob Pan2d281d82013-10-17 10:28:35 -0700242 int to_raw);
Jacob Pan309557f2016-02-24 13:31:37 -0800243static void package_power_limit_irq_save(struct rapl_package *rp);
Jacob Pan2d281d82013-10-17 10:28:35 -0700244
245static LIST_HEAD(rapl_packages); /* guarded by CPU hotplug lock */
246
247static const char * const rapl_domain_names[] = {
248 "package",
249 "core",
250 "uncore",
251 "dram",
Srinivas Pandruvada3521ba12016-04-17 15:03:01 -0700252 "psys",
Jacob Pan2d281d82013-10-17 10:28:35 -0700253};
254
255static struct powercap_control_type *control_type; /* PowerCap Controller */
Srinivas Pandruvada3521ba12016-04-17 15:03:01 -0700256static struct rapl_domain *platform_rapl_domain; /* Platform (PSys) domain */
Jacob Pan2d281d82013-10-17 10:28:35 -0700257
258/* caller to ensure CPU hotplug lock is held */
Zhang Ruiaadf7b32019-05-13 13:58:50 -0400259static struct rapl_package *rapl_find_package_domain(int cpu)
Jacob Pan2d281d82013-10-17 10:28:35 -0700260{
Zhang Rui32fb4802019-05-13 13:58:51 -0400261 int id = topology_logical_die_id(cpu);
Jacob Pan2d281d82013-10-17 10:28:35 -0700262 struct rapl_package *rp;
263
264 list_for_each_entry(rp, &rapl_packages, plist) {
265 if (rp->id == id)
266 return rp;
267 }
268
269 return NULL;
270}
271
Jacob Pan2d281d82013-10-17 10:28:35 -0700272static int get_energy_counter(struct powercap_zone *power_zone, u64 *energy_raw)
273{
274 struct rapl_domain *rd;
275 u64 energy_now;
276
277 /* prevent CPU hotplug, make sure the RAPL domain does not go
278 * away while reading the counter.
279 */
280 get_online_cpus();
281 rd = power_zone_to_rapl_domain(power_zone);
282
283 if (!rapl_read_data_raw(rd, ENERGY_COUNTER, true, &energy_now)) {
284 *energy_raw = energy_now;
285 put_online_cpus();
286
287 return 0;
288 }
289 put_online_cpus();
290
291 return -EIO;
292}
293
294static int get_max_energy_counter(struct powercap_zone *pcd_dev, u64 *energy)
295{
Jacob Pand474a4d2015-03-13 03:48:56 -0700296 struct rapl_domain *rd = power_zone_to_rapl_domain(pcd_dev);
297
Jacob Pan309557f2016-02-24 13:31:37 -0800298 *energy = rapl_unit_xlate(rd, ENERGY_UNIT, ENERGY_STATUS_MASK, 0);
Jacob Pan2d281d82013-10-17 10:28:35 -0700299 return 0;
300}
301
302static int release_zone(struct powercap_zone *power_zone)
303{
304 struct rapl_domain *rd = power_zone_to_rapl_domain(power_zone);
Jacob Pan309557f2016-02-24 13:31:37 -0800305 struct rapl_package *rp = rd->rp;
Jacob Pan2d281d82013-10-17 10:28:35 -0700306
307 /* package zone is the last zone of a package, we can free
308 * memory here since all children has been unregistered.
309 */
310 if (rd->id == RAPL_DOMAIN_PACKAGE) {
Jacob Pan2d281d82013-10-17 10:28:35 -0700311 kfree(rd);
312 rp->domains = NULL;
313 }
314
315 return 0;
316
317}
318
319static int find_nr_power_limit(struct rapl_domain *rd)
320{
Jacob Pane1399ba2016-05-31 13:41:29 -0700321 int i, nr_pl = 0;
Jacob Pan2d281d82013-10-17 10:28:35 -0700322
323 for (i = 0; i < NR_POWER_LIMITS; i++) {
Jacob Pane1399ba2016-05-31 13:41:29 -0700324 if (rd->rpl[i].name)
325 nr_pl++;
Jacob Pan2d281d82013-10-17 10:28:35 -0700326 }
327
Jacob Pane1399ba2016-05-31 13:41:29 -0700328 return nr_pl;
Jacob Pan2d281d82013-10-17 10:28:35 -0700329}
330
331static int set_domain_enable(struct powercap_zone *power_zone, bool mode)
332{
333 struct rapl_domain *rd = power_zone_to_rapl_domain(power_zone);
Jacob Pan2d281d82013-10-17 10:28:35 -0700334
335 if (rd->state & DOMAIN_STATE_BIOS_LOCKED)
336 return -EACCES;
Jacob Pan3c2c0842014-11-07 09:29:26 -0800337
Jacob Pan2d281d82013-10-17 10:28:35 -0700338 get_online_cpus();
Jacob Pan2d281d82013-10-17 10:28:35 -0700339 rapl_write_data_raw(rd, PL1_ENABLE, mode);
Ajay Thomas51b63402015-04-30 01:43:23 +0530340 if (rapl_defaults->set_floor_freq)
341 rapl_defaults->set_floor_freq(rd, mode);
Jacob Pan2d281d82013-10-17 10:28:35 -0700342 put_online_cpus();
343
344 return 0;
345}
346
347static int get_domain_enable(struct powercap_zone *power_zone, bool *mode)
348{
349 struct rapl_domain *rd = power_zone_to_rapl_domain(power_zone);
350 u64 val;
351
352 if (rd->state & DOMAIN_STATE_BIOS_LOCKED) {
353 *mode = false;
354 return 0;
355 }
356 get_online_cpus();
357 if (rapl_read_data_raw(rd, PL1_ENABLE, true, &val)) {
358 put_online_cpus();
359 return -EIO;
360 }
361 *mode = val;
362 put_online_cpus();
363
364 return 0;
365}
366
367/* per RAPL domain ops, in the order of rapl_domain_type */
Julia Lawall600c3952015-12-23 22:59:55 +0100368static const struct powercap_zone_ops zone_ops[] = {
Jacob Pan2d281d82013-10-17 10:28:35 -0700369 /* RAPL_DOMAIN_PACKAGE */
370 {
371 .get_energy_uj = get_energy_counter,
372 .get_max_energy_range_uj = get_max_energy_counter,
373 .release = release_zone,
374 .set_enable = set_domain_enable,
375 .get_enable = get_domain_enable,
376 },
377 /* RAPL_DOMAIN_PP0 */
378 {
379 .get_energy_uj = get_energy_counter,
380 .get_max_energy_range_uj = get_max_energy_counter,
381 .release = release_zone,
382 .set_enable = set_domain_enable,
383 .get_enable = get_domain_enable,
384 },
385 /* RAPL_DOMAIN_PP1 */
386 {
387 .get_energy_uj = get_energy_counter,
388 .get_max_energy_range_uj = get_max_energy_counter,
389 .release = release_zone,
390 .set_enable = set_domain_enable,
391 .get_enable = get_domain_enable,
392 },
393 /* RAPL_DOMAIN_DRAM */
394 {
395 .get_energy_uj = get_energy_counter,
396 .get_max_energy_range_uj = get_max_energy_counter,
397 .release = release_zone,
398 .set_enable = set_domain_enable,
399 .get_enable = get_domain_enable,
400 },
Srinivas Pandruvada3521ba12016-04-17 15:03:01 -0700401 /* RAPL_DOMAIN_PLATFORM */
402 {
403 .get_energy_uj = get_energy_counter,
404 .get_max_energy_range_uj = get_max_energy_counter,
405 .release = release_zone,
406 .set_enable = set_domain_enable,
407 .get_enable = get_domain_enable,
408 },
Jacob Pan2d281d82013-10-17 10:28:35 -0700409};
410
Jacob Pane1399ba2016-05-31 13:41:29 -0700411
412/*
413 * Constraint index used by powercap can be different than power limit (PL)
414 * index in that some PLs maybe missing due to non-existant MSRs. So we
415 * need to convert here by finding the valid PLs only (name populated).
416 */
417static int contraint_to_pl(struct rapl_domain *rd, int cid)
418{
419 int i, j;
420
421 for (i = 0, j = 0; i < NR_POWER_LIMITS; i++) {
422 if ((rd->rpl[i].name) && j++ == cid) {
423 pr_debug("%s: index %d\n", __func__, i);
424 return i;
425 }
426 }
Jacob Pancb43f812016-11-28 13:53:11 -0800427 pr_err("Cannot find matching power limit for constraint %d\n", cid);
Jacob Pane1399ba2016-05-31 13:41:29 -0700428
429 return -EINVAL;
430}
431
432static int set_power_limit(struct powercap_zone *power_zone, int cid,
Jacob Pan2d281d82013-10-17 10:28:35 -0700433 u64 power_limit)
434{
435 struct rapl_domain *rd;
436 struct rapl_package *rp;
437 int ret = 0;
Jacob Pane1399ba2016-05-31 13:41:29 -0700438 int id;
Jacob Pan2d281d82013-10-17 10:28:35 -0700439
440 get_online_cpus();
441 rd = power_zone_to_rapl_domain(power_zone);
Jacob Pane1399ba2016-05-31 13:41:29 -0700442 id = contraint_to_pl(rd, cid);
Jacob Pancb43f812016-11-28 13:53:11 -0800443 if (id < 0) {
444 ret = id;
445 goto set_exit;
446 }
Jacob Pane1399ba2016-05-31 13:41:29 -0700447
Jacob Pan309557f2016-02-24 13:31:37 -0800448 rp = rd->rp;
Jacob Pan2d281d82013-10-17 10:28:35 -0700449
450 if (rd->state & DOMAIN_STATE_BIOS_LOCKED) {
451 dev_warn(&power_zone->dev, "%s locked by BIOS, monitoring only\n",
452 rd->name);
453 ret = -EACCES;
454 goto set_exit;
455 }
456
457 switch (rd->rpl[id].prim_id) {
458 case PL1_ENABLE:
459 rapl_write_data_raw(rd, POWER_LIMIT1, power_limit);
460 break;
461 case PL2_ENABLE:
462 rapl_write_data_raw(rd, POWER_LIMIT2, power_limit);
463 break;
464 default:
465 ret = -EINVAL;
466 }
467 if (!ret)
Jacob Pan309557f2016-02-24 13:31:37 -0800468 package_power_limit_irq_save(rp);
Jacob Pan2d281d82013-10-17 10:28:35 -0700469set_exit:
470 put_online_cpus();
471 return ret;
472}
473
Jacob Pane1399ba2016-05-31 13:41:29 -0700474static int get_current_power_limit(struct powercap_zone *power_zone, int cid,
Jacob Pan2d281d82013-10-17 10:28:35 -0700475 u64 *data)
476{
477 struct rapl_domain *rd;
478 u64 val;
479 int prim;
480 int ret = 0;
Jacob Pane1399ba2016-05-31 13:41:29 -0700481 int id;
Jacob Pan2d281d82013-10-17 10:28:35 -0700482
483 get_online_cpus();
484 rd = power_zone_to_rapl_domain(power_zone);
Jacob Pane1399ba2016-05-31 13:41:29 -0700485 id = contraint_to_pl(rd, cid);
Jacob Pancb43f812016-11-28 13:53:11 -0800486 if (id < 0) {
487 ret = id;
488 goto get_exit;
489 }
490
Jacob Pan2d281d82013-10-17 10:28:35 -0700491 switch (rd->rpl[id].prim_id) {
492 case PL1_ENABLE:
493 prim = POWER_LIMIT1;
494 break;
495 case PL2_ENABLE:
496 prim = POWER_LIMIT2;
497 break;
498 default:
499 put_online_cpus();
500 return -EINVAL;
501 }
502 if (rapl_read_data_raw(rd, prim, true, &val))
503 ret = -EIO;
504 else
505 *data = val;
506
Jacob Pancb43f812016-11-28 13:53:11 -0800507get_exit:
Jacob Pan2d281d82013-10-17 10:28:35 -0700508 put_online_cpus();
509
510 return ret;
511}
512
Jacob Pane1399ba2016-05-31 13:41:29 -0700513static int set_time_window(struct powercap_zone *power_zone, int cid,
Jacob Pan2d281d82013-10-17 10:28:35 -0700514 u64 window)
515{
516 struct rapl_domain *rd;
517 int ret = 0;
Jacob Pane1399ba2016-05-31 13:41:29 -0700518 int id;
Jacob Pan2d281d82013-10-17 10:28:35 -0700519
520 get_online_cpus();
521 rd = power_zone_to_rapl_domain(power_zone);
Jacob Pane1399ba2016-05-31 13:41:29 -0700522 id = contraint_to_pl(rd, cid);
Jacob Pancb43f812016-11-28 13:53:11 -0800523 if (id < 0) {
524 ret = id;
525 goto set_time_exit;
526 }
Jacob Pane1399ba2016-05-31 13:41:29 -0700527
Jacob Pan2d281d82013-10-17 10:28:35 -0700528 switch (rd->rpl[id].prim_id) {
529 case PL1_ENABLE:
530 rapl_write_data_raw(rd, TIME_WINDOW1, window);
531 break;
532 case PL2_ENABLE:
533 rapl_write_data_raw(rd, TIME_WINDOW2, window);
534 break;
535 default:
536 ret = -EINVAL;
537 }
Jacob Pancb43f812016-11-28 13:53:11 -0800538
539set_time_exit:
Jacob Pan2d281d82013-10-17 10:28:35 -0700540 put_online_cpus();
541 return ret;
542}
543
Jacob Pane1399ba2016-05-31 13:41:29 -0700544static int get_time_window(struct powercap_zone *power_zone, int cid, u64 *data)
Jacob Pan2d281d82013-10-17 10:28:35 -0700545{
546 struct rapl_domain *rd;
547 u64 val;
548 int ret = 0;
Jacob Pane1399ba2016-05-31 13:41:29 -0700549 int id;
Jacob Pan2d281d82013-10-17 10:28:35 -0700550
551 get_online_cpus();
552 rd = power_zone_to_rapl_domain(power_zone);
Jacob Pane1399ba2016-05-31 13:41:29 -0700553 id = contraint_to_pl(rd, cid);
Jacob Pancb43f812016-11-28 13:53:11 -0800554 if (id < 0) {
555 ret = id;
556 goto get_time_exit;
557 }
Jacob Pane1399ba2016-05-31 13:41:29 -0700558
Jacob Pan2d281d82013-10-17 10:28:35 -0700559 switch (rd->rpl[id].prim_id) {
560 case PL1_ENABLE:
561 ret = rapl_read_data_raw(rd, TIME_WINDOW1, true, &val);
562 break;
563 case PL2_ENABLE:
564 ret = rapl_read_data_raw(rd, TIME_WINDOW2, true, &val);
565 break;
566 default:
567 put_online_cpus();
568 return -EINVAL;
569 }
570 if (!ret)
571 *data = val;
Jacob Pancb43f812016-11-28 13:53:11 -0800572
573get_time_exit:
Jacob Pan2d281d82013-10-17 10:28:35 -0700574 put_online_cpus();
575
576 return ret;
577}
578
Jacob Pane1399ba2016-05-31 13:41:29 -0700579static const char *get_constraint_name(struct powercap_zone *power_zone, int cid)
Jacob Pan2d281d82013-10-17 10:28:35 -0700580{
Jacob Pan2d281d82013-10-17 10:28:35 -0700581 struct rapl_domain *rd;
Jacob Pane1399ba2016-05-31 13:41:29 -0700582 int id;
Jacob Pan2d281d82013-10-17 10:28:35 -0700583
584 rd = power_zone_to_rapl_domain(power_zone);
Jacob Pane1399ba2016-05-31 13:41:29 -0700585 id = contraint_to_pl(rd, cid);
586 if (id >= 0)
587 return rd->rpl[id].name;
Jacob Pan2d281d82013-10-17 10:28:35 -0700588
Jacob Pane1399ba2016-05-31 13:41:29 -0700589 return NULL;
Jacob Pan2d281d82013-10-17 10:28:35 -0700590}
591
592
593static int get_max_power(struct powercap_zone *power_zone, int id,
594 u64 *data)
595{
596 struct rapl_domain *rd;
597 u64 val;
598 int prim;
599 int ret = 0;
600
601 get_online_cpus();
602 rd = power_zone_to_rapl_domain(power_zone);
603 switch (rd->rpl[id].prim_id) {
604 case PL1_ENABLE:
605 prim = THERMAL_SPEC_POWER;
606 break;
607 case PL2_ENABLE:
608 prim = MAX_POWER;
609 break;
610 default:
611 put_online_cpus();
612 return -EINVAL;
613 }
614 if (rapl_read_data_raw(rd, prim, true, &val))
615 ret = -EIO;
616 else
617 *data = val;
618
619 put_online_cpus();
620
621 return ret;
622}
623
Julia Lawall600c3952015-12-23 22:59:55 +0100624static const struct powercap_zone_constraint_ops constraint_ops = {
Jacob Pan2d281d82013-10-17 10:28:35 -0700625 .set_power_limit_uw = set_power_limit,
626 .get_power_limit_uw = get_current_power_limit,
627 .set_time_window_us = set_time_window,
628 .get_time_window_us = get_time_window,
629 .get_max_power_uw = get_max_power,
630 .get_name = get_constraint_name,
631};
632
633/* called after domain detection and package level data are set */
634static void rapl_init_domains(struct rapl_package *rp)
635{
636 int i;
637 struct rapl_domain *rd = rp->domains;
638
639 for (i = 0; i < RAPL_DOMAIN_MAX; i++) {
640 unsigned int mask = rp->domain_map & (1 << i);
641 switch (mask) {
642 case BIT(RAPL_DOMAIN_PACKAGE):
643 rd->name = rapl_domain_names[RAPL_DOMAIN_PACKAGE];
644 rd->id = RAPL_DOMAIN_PACKAGE;
Zhang Ruif7c4e0c2019-07-10 21:44:22 +0800645 rd->regs[0] = MSR_PKG_POWER_LIMIT;
646 rd->regs[1] = MSR_PKG_ENERGY_STATUS;
647 rd->regs[2] = MSR_PKG_PERF_STATUS;
648 rd->regs[3] = 0;
649 rd->regs[4] = MSR_PKG_POWER_INFO;
Jacob Pan2d281d82013-10-17 10:28:35 -0700650 rd->rpl[0].prim_id = PL1_ENABLE;
651 rd->rpl[0].name = pl1_name;
652 rd->rpl[1].prim_id = PL2_ENABLE;
653 rd->rpl[1].name = pl2_name;
654 break;
655 case BIT(RAPL_DOMAIN_PP0):
656 rd->name = rapl_domain_names[RAPL_DOMAIN_PP0];
657 rd->id = RAPL_DOMAIN_PP0;
Zhang Ruif7c4e0c2019-07-10 21:44:22 +0800658 rd->regs[0] = MSR_PP0_POWER_LIMIT;
659 rd->regs[1] = MSR_PP0_ENERGY_STATUS;
660 rd->regs[2] = 0;
661 rd->regs[3] = MSR_PP0_POLICY;
662 rd->regs[4] = 0;
Jacob Pan2d281d82013-10-17 10:28:35 -0700663 rd->rpl[0].prim_id = PL1_ENABLE;
664 rd->rpl[0].name = pl1_name;
665 break;
666 case BIT(RAPL_DOMAIN_PP1):
667 rd->name = rapl_domain_names[RAPL_DOMAIN_PP1];
668 rd->id = RAPL_DOMAIN_PP1;
Zhang Ruif7c4e0c2019-07-10 21:44:22 +0800669 rd->regs[0] = MSR_PP1_POWER_LIMIT;
670 rd->regs[1] = MSR_PP1_ENERGY_STATUS;
671 rd->regs[2] = 0;
672 rd->regs[3] = MSR_PP1_POLICY;
673 rd->regs[4] = 0;
Jacob Pan2d281d82013-10-17 10:28:35 -0700674 rd->rpl[0].prim_id = PL1_ENABLE;
675 rd->rpl[0].name = pl1_name;
676 break;
677 case BIT(RAPL_DOMAIN_DRAM):
678 rd->name = rapl_domain_names[RAPL_DOMAIN_DRAM];
679 rd->id = RAPL_DOMAIN_DRAM;
Zhang Ruif7c4e0c2019-07-10 21:44:22 +0800680 rd->regs[0] = MSR_DRAM_POWER_LIMIT;
681 rd->regs[1] = MSR_DRAM_ENERGY_STATUS;
682 rd->regs[2] = MSR_DRAM_PERF_STATUS;
683 rd->regs[3] = 0;
684 rd->regs[4] = MSR_DRAM_POWER_INFO;
Jacob Pan2d281d82013-10-17 10:28:35 -0700685 rd->rpl[0].prim_id = PL1_ENABLE;
686 rd->rpl[0].name = pl1_name;
Jacob Pand474a4d2015-03-13 03:48:56 -0700687 rd->domain_energy_unit =
688 rapl_defaults->dram_domain_energy_unit;
689 if (rd->domain_energy_unit)
690 pr_info("DRAM domain energy unit %dpj\n",
691 rd->domain_energy_unit);
Jacob Pan2d281d82013-10-17 10:28:35 -0700692 break;
693 }
694 if (mask) {
Jacob Pan309557f2016-02-24 13:31:37 -0800695 rd->rp = rp;
Jacob Pan2d281d82013-10-17 10:28:35 -0700696 rd++;
697 }
698 }
699}
700
Jacob Pan309557f2016-02-24 13:31:37 -0800701static u64 rapl_unit_xlate(struct rapl_domain *rd, enum unit_type type,
702 u64 value, int to_raw)
Jacob Pan2d281d82013-10-17 10:28:35 -0700703{
Jacob Pan3c2c0842014-11-07 09:29:26 -0800704 u64 units = 1;
Jacob Pan309557f2016-02-24 13:31:37 -0800705 struct rapl_package *rp = rd->rp;
Jacob Pand474a4d2015-03-13 03:48:56 -0700706 u64 scale = 1;
Jacob Pan2d281d82013-10-17 10:28:35 -0700707
Jacob Pan2d281d82013-10-17 10:28:35 -0700708 switch (type) {
709 case POWER_UNIT:
Jacob Pan3c2c0842014-11-07 09:29:26 -0800710 units = rp->power_unit;
Jacob Pan2d281d82013-10-17 10:28:35 -0700711 break;
712 case ENERGY_UNIT:
Jacob Pand474a4d2015-03-13 03:48:56 -0700713 scale = ENERGY_UNIT_SCALE;
714 /* per domain unit takes precedence */
Jacob Pancb43f812016-11-28 13:53:11 -0800715 if (rd->domain_energy_unit)
Jacob Pand474a4d2015-03-13 03:48:56 -0700716 units = rd->domain_energy_unit;
717 else
718 units = rp->energy_unit;
Jacob Pan2d281d82013-10-17 10:28:35 -0700719 break;
720 case TIME_UNIT:
Jacob Pan3c2c0842014-11-07 09:29:26 -0800721 return rapl_defaults->compute_time_window(rp, value, to_raw);
Jacob Pan2d281d82013-10-17 10:28:35 -0700722 case ARBITRARY_UNIT:
723 default:
724 return value;
725 };
726
727 if (to_raw)
Jacob Pand474a4d2015-03-13 03:48:56 -0700728 return div64_u64(value, units) * scale;
Jacob Pan3c2c0842014-11-07 09:29:26 -0800729
730 value *= units;
731
Jacob Pand474a4d2015-03-13 03:48:56 -0700732 return div64_u64(value, scale);
Jacob Pan2d281d82013-10-17 10:28:35 -0700733}
734
735/* in the order of enum rapl_primitives */
736static struct rapl_primitive_info rpi[] = {
737 /* name, mask, shift, msr index, unit divisor */
738 PRIMITIVE_INFO_INIT(ENERGY_COUNTER, ENERGY_STATUS_MASK, 0,
Zhang Ruif7c4e0c2019-07-10 21:44:22 +0800739 RAPL_DOMAIN_REG_STATUS, ENERGY_UNIT, 0),
Jacob Pan2d281d82013-10-17 10:28:35 -0700740 PRIMITIVE_INFO_INIT(POWER_LIMIT1, POWER_LIMIT1_MASK, 0,
Zhang Ruif7c4e0c2019-07-10 21:44:22 +0800741 RAPL_DOMAIN_REG_LIMIT, POWER_UNIT, 0),
Jacob Pan2d281d82013-10-17 10:28:35 -0700742 PRIMITIVE_INFO_INIT(POWER_LIMIT2, POWER_LIMIT2_MASK, 32,
Zhang Ruif7c4e0c2019-07-10 21:44:22 +0800743 RAPL_DOMAIN_REG_LIMIT, POWER_UNIT, 0),
Jacob Pan2d281d82013-10-17 10:28:35 -0700744 PRIMITIVE_INFO_INIT(FW_LOCK, POWER_PP_LOCK, 31,
Zhang Ruif7c4e0c2019-07-10 21:44:22 +0800745 RAPL_DOMAIN_REG_LIMIT, ARBITRARY_UNIT, 0),
Jacob Pan2d281d82013-10-17 10:28:35 -0700746 PRIMITIVE_INFO_INIT(PL1_ENABLE, POWER_LIMIT1_ENABLE, 15,
Zhang Ruif7c4e0c2019-07-10 21:44:22 +0800747 RAPL_DOMAIN_REG_LIMIT, ARBITRARY_UNIT, 0),
Jacob Pan2d281d82013-10-17 10:28:35 -0700748 PRIMITIVE_INFO_INIT(PL1_CLAMP, POWER_LIMIT1_CLAMP, 16,
Zhang Ruif7c4e0c2019-07-10 21:44:22 +0800749 RAPL_DOMAIN_REG_LIMIT, ARBITRARY_UNIT, 0),
Jacob Pan2d281d82013-10-17 10:28:35 -0700750 PRIMITIVE_INFO_INIT(PL2_ENABLE, POWER_LIMIT2_ENABLE, 47,
Zhang Ruif7c4e0c2019-07-10 21:44:22 +0800751 RAPL_DOMAIN_REG_LIMIT, ARBITRARY_UNIT, 0),
Jacob Pan2d281d82013-10-17 10:28:35 -0700752 PRIMITIVE_INFO_INIT(PL2_CLAMP, POWER_LIMIT2_CLAMP, 48,
Zhang Ruif7c4e0c2019-07-10 21:44:22 +0800753 RAPL_DOMAIN_REG_LIMIT, ARBITRARY_UNIT, 0),
Jacob Pan2d281d82013-10-17 10:28:35 -0700754 PRIMITIVE_INFO_INIT(TIME_WINDOW1, TIME_WINDOW1_MASK, 17,
Zhang Ruif7c4e0c2019-07-10 21:44:22 +0800755 RAPL_DOMAIN_REG_LIMIT, TIME_UNIT, 0),
Jacob Pan2d281d82013-10-17 10:28:35 -0700756 PRIMITIVE_INFO_INIT(TIME_WINDOW2, TIME_WINDOW2_MASK, 49,
Zhang Ruif7c4e0c2019-07-10 21:44:22 +0800757 RAPL_DOMAIN_REG_LIMIT, TIME_UNIT, 0),
Jacob Pan2d281d82013-10-17 10:28:35 -0700758 PRIMITIVE_INFO_INIT(THERMAL_SPEC_POWER, POWER_INFO_THERMAL_SPEC_MASK,
Zhang Ruif7c4e0c2019-07-10 21:44:22 +0800759 0, RAPL_DOMAIN_REG_INFO, POWER_UNIT, 0),
Jacob Pan2d281d82013-10-17 10:28:35 -0700760 PRIMITIVE_INFO_INIT(MAX_POWER, POWER_INFO_MAX_MASK, 32,
Zhang Ruif7c4e0c2019-07-10 21:44:22 +0800761 RAPL_DOMAIN_REG_INFO, POWER_UNIT, 0),
Jacob Pan2d281d82013-10-17 10:28:35 -0700762 PRIMITIVE_INFO_INIT(MIN_POWER, POWER_INFO_MIN_MASK, 16,
Zhang Ruif7c4e0c2019-07-10 21:44:22 +0800763 RAPL_DOMAIN_REG_INFO, POWER_UNIT, 0),
Jacob Pan2d281d82013-10-17 10:28:35 -0700764 PRIMITIVE_INFO_INIT(MAX_TIME_WINDOW, POWER_INFO_MAX_TIME_WIN_MASK, 48,
Zhang Ruif7c4e0c2019-07-10 21:44:22 +0800765 RAPL_DOMAIN_REG_INFO, TIME_UNIT, 0),
Jacob Pan2d281d82013-10-17 10:28:35 -0700766 PRIMITIVE_INFO_INIT(THROTTLED_TIME, PERF_STATUS_THROTTLE_TIME_MASK, 0,
Zhang Ruif7c4e0c2019-07-10 21:44:22 +0800767 RAPL_DOMAIN_REG_PERF, TIME_UNIT, 0),
Jacob Pan2d281d82013-10-17 10:28:35 -0700768 PRIMITIVE_INFO_INIT(PRIORITY_LEVEL, PP_POLICY_MASK, 0,
Zhang Ruif7c4e0c2019-07-10 21:44:22 +0800769 RAPL_DOMAIN_REG_POLICY, ARBITRARY_UNIT, 0),
Jacob Pan2d281d82013-10-17 10:28:35 -0700770 /* non-hardware */
771 PRIMITIVE_INFO_INIT(AVERAGE_POWER, 0, 0, 0, POWER_UNIT,
772 RAPL_PRIMITIVE_DERIVED),
773 {NULL, 0, 0, 0},
774};
775
776/* Read primitive data based on its related struct rapl_primitive_info.
777 * if xlate flag is set, return translated data based on data units, i.e.
778 * time, energy, and power.
779 * RAPL MSRs are non-architectual and are laid out not consistently across
780 * domains. Here we use primitive info to allow writing consolidated access
781 * functions.
782 * For a given primitive, it is processed by MSR mask and shift. Unit conversion
783 * is pre-assigned based on RAPL unit MSRs read at init time.
784 * 63-------------------------- 31--------------------------- 0
785 * | xxxxx (mask) |
786 * | |<- shift ----------------|
787 * 63-------------------------- 31--------------------------- 0
788 */
789static int rapl_read_data_raw(struct rapl_domain *rd,
790 enum rapl_primitives prim,
791 bool xlate, u64 *data)
792{
793 u64 value, final;
794 u32 msr;
795 struct rapl_primitive_info *rp = &rpi[prim];
796 int cpu;
797
798 if (!rp->name || rp->flag & RAPL_PRIMITIVE_DUMMY)
799 return -EINVAL;
800
Zhang Ruif7c4e0c2019-07-10 21:44:22 +0800801 msr = rd->regs[rp->id];
Jacob Pan2d281d82013-10-17 10:28:35 -0700802 if (!msr)
803 return -EINVAL;
Jacob Pan323ee642016-02-24 13:31:38 -0800804
805 cpu = rd->rp->lead_cpu;
Jacob Pan2d281d82013-10-17 10:28:35 -0700806
807 /* special-case package domain, which uses a different bit*/
808 if (prim == FW_LOCK && rd->id == RAPL_DOMAIN_PACKAGE) {
809 rp->mask = POWER_PACKAGE_LOCK;
810 rp->shift = 63;
811 }
812 /* non-hardware data are collected by the polling thread */
813 if (rp->flag & RAPL_PRIMITIVE_DERIVED) {
814 *data = rd->rdd.primitives[prim];
815 return 0;
816 }
817
818 if (rdmsrl_safe_on_cpu(cpu, msr, &value)) {
819 pr_debug("failed to read msr 0x%x on cpu %d\n", msr, cpu);
820 return -EIO;
821 }
822
823 final = value & rp->mask;
824 final = final >> rp->shift;
825 if (xlate)
Jacob Pan309557f2016-02-24 13:31:37 -0800826 *data = rapl_unit_xlate(rd, rp->unit, final, 0);
Jacob Pan2d281d82013-10-17 10:28:35 -0700827 else
828 *data = final;
829
830 return 0;
831}
832
Jacob Panf14a1392016-02-24 13:31:36 -0800833
834static int msrl_update_safe(u32 msr_no, u64 clear_mask, u64 set_mask)
835{
836 int err;
837 u64 val;
838
839 err = rdmsrl_safe(msr_no, &val);
840 if (err)
841 goto out;
842
843 val &= ~clear_mask;
844 val |= set_mask;
845
846 err = wrmsrl_safe(msr_no, val);
847
848out:
849 return err;
850}
851
852static void msrl_update_func(void *info)
853{
854 struct msrl_action *ma = info;
855
856 ma->err = msrl_update_safe(ma->msr_no, ma->clear_mask, ma->set_mask);
857}
858
Jacob Pan2d281d82013-10-17 10:28:35 -0700859/* Similar use of primitive info in the read counterpart */
860static int rapl_write_data_raw(struct rapl_domain *rd,
861 enum rapl_primitives prim,
862 unsigned long long value)
863{
Jacob Pan2d281d82013-10-17 10:28:35 -0700864 struct rapl_primitive_info *rp = &rpi[prim];
865 int cpu;
Jacob Panf14a1392016-02-24 13:31:36 -0800866 u64 bits;
867 struct msrl_action ma;
868 int ret;
Jacob Pan2d281d82013-10-17 10:28:35 -0700869
Jacob Pan323ee642016-02-24 13:31:38 -0800870 cpu = rd->rp->lead_cpu;
Jacob Pan309557f2016-02-24 13:31:37 -0800871 bits = rapl_unit_xlate(rd, rp->unit, value, 1);
Adam Lessnauedbdabc2017-06-01 11:21:50 +0200872 bits <<= rp->shift;
873 bits &= rp->mask;
874
Jacob Panf14a1392016-02-24 13:31:36 -0800875 memset(&ma, 0, sizeof(ma));
876
Zhang Ruif7c4e0c2019-07-10 21:44:22 +0800877 ma.msr_no = rd->regs[rp->id];
Jacob Panf14a1392016-02-24 13:31:36 -0800878 ma.clear_mask = rp->mask;
879 ma.set_mask = bits;
880
881 ret = smp_call_function_single(cpu, msrl_update_func, &ma, 1);
882 if (ret)
883 WARN_ON_ONCE(ret);
884 else
885 ret = ma.err;
886
887 return ret;
Jacob Pan2d281d82013-10-17 10:28:35 -0700888}
889
Jacob Pan3c2c0842014-11-07 09:29:26 -0800890/*
891 * Raw RAPL data stored in MSRs are in certain scales. We need to
892 * convert them into standard units based on the units reported in
893 * the RAPL unit MSRs. This is specific to CPUs as the method to
894 * calculate units differ on different CPUs.
895 * We convert the units to below format based on CPUs.
896 * i.e.
Jacob Pand474a4d2015-03-13 03:48:56 -0700897 * energy unit: picoJoules : Represented in picoJoules by default
Jacob Pan3c2c0842014-11-07 09:29:26 -0800898 * power unit : microWatts : Represented in milliWatts by default
899 * time unit : microseconds: Represented in seconds by default
900 */
901static int rapl_check_unit_core(struct rapl_package *rp, int cpu)
Jacob Pan2d281d82013-10-17 10:28:35 -0700902{
903 u64 msr_val;
904 u32 value;
905
906 if (rdmsrl_safe_on_cpu(cpu, MSR_RAPL_POWER_UNIT, &msr_val)) {
907 pr_err("Failed to read power unit MSR 0x%x on CPU %d, exit.\n",
908 MSR_RAPL_POWER_UNIT, cpu);
909 return -ENODEV;
910 }
911
Jacob Pan2d281d82013-10-17 10:28:35 -0700912 value = (msr_val & ENERGY_UNIT_MASK) >> ENERGY_UNIT_OFFSET;
Jacob Pand474a4d2015-03-13 03:48:56 -0700913 rp->energy_unit = ENERGY_UNIT_SCALE * 1000000 / (1 << value);
Jacob Pan2d281d82013-10-17 10:28:35 -0700914
915 value = (msr_val & POWER_UNIT_MASK) >> POWER_UNIT_OFFSET;
Jacob Pan3c2c0842014-11-07 09:29:26 -0800916 rp->power_unit = 1000000 / (1 << value);
Jacob Pan2d281d82013-10-17 10:28:35 -0700917
918 value = (msr_val & TIME_UNIT_MASK) >> TIME_UNIT_OFFSET;
Jacob Pan3c2c0842014-11-07 09:29:26 -0800919 rp->time_unit = 1000000 / (1 << value);
Jacob Pan2d281d82013-10-17 10:28:35 -0700920
Zhang Rui9ea76122019-05-13 13:58:53 -0400921 pr_debug("Core CPU %s energy=%dpJ, time=%dus, power=%duW\n",
922 rp->name, rp->energy_unit, rp->time_unit, rp->power_unit);
Jacob Pan2d281d82013-10-17 10:28:35 -0700923
924 return 0;
925}
926
Jacob Pan3c2c0842014-11-07 09:29:26 -0800927static int rapl_check_unit_atom(struct rapl_package *rp, int cpu)
928{
929 u64 msr_val;
930 u32 value;
931
932 if (rdmsrl_safe_on_cpu(cpu, MSR_RAPL_POWER_UNIT, &msr_val)) {
933 pr_err("Failed to read power unit MSR 0x%x on CPU %d, exit.\n",
934 MSR_RAPL_POWER_UNIT, cpu);
935 return -ENODEV;
936 }
937 value = (msr_val & ENERGY_UNIT_MASK) >> ENERGY_UNIT_OFFSET;
Jacob Pand474a4d2015-03-13 03:48:56 -0700938 rp->energy_unit = ENERGY_UNIT_SCALE * 1 << value;
Jacob Pan3c2c0842014-11-07 09:29:26 -0800939
940 value = (msr_val & POWER_UNIT_MASK) >> POWER_UNIT_OFFSET;
941 rp->power_unit = (1 << value) * 1000;
942
943 value = (msr_val & TIME_UNIT_MASK) >> TIME_UNIT_OFFSET;
944 rp->time_unit = 1000000 / (1 << value);
945
Zhang Rui9ea76122019-05-13 13:58:53 -0400946 pr_debug("Atom %s energy=%dpJ, time=%dus, power=%duW\n",
947 rp->name, rp->energy_unit, rp->time_unit, rp->power_unit);
Jacob Pan3c2c0842014-11-07 09:29:26 -0800948
949 return 0;
950}
951
Jacob Panf14a1392016-02-24 13:31:36 -0800952static void power_limit_irq_save_cpu(void *info)
953{
954 u32 l, h = 0;
955 struct rapl_package *rp = (struct rapl_package *)info;
956
957 /* save the state of PLN irq mask bit before disabling it */
958 rdmsr_safe(MSR_IA32_PACKAGE_THERM_INTERRUPT, &l, &h);
959 if (!(rp->power_limit_irq & PACKAGE_PLN_INT_SAVED)) {
960 rp->power_limit_irq = l & PACKAGE_THERM_INT_PLN_ENABLE;
961 rp->power_limit_irq |= PACKAGE_PLN_INT_SAVED;
962 }
963 l &= ~PACKAGE_THERM_INT_PLN_ENABLE;
964 wrmsr_safe(MSR_IA32_PACKAGE_THERM_INTERRUPT, l, h);
965}
966
Jacob Pan3c2c0842014-11-07 09:29:26 -0800967
Jacob Pan2d281d82013-10-17 10:28:35 -0700968/* REVISIT:
969 * When package power limit is set artificially low by RAPL, LVT
970 * thermal interrupt for package power limit should be ignored
971 * since we are not really exceeding the real limit. The intention
972 * is to avoid excessive interrupts while we are trying to save power.
973 * A useful feature might be routing the package_power_limit interrupt
974 * to userspace via eventfd. once we have a usecase, this is simple
975 * to do by adding an atomic notifier.
976 */
977
Jacob Pan309557f2016-02-24 13:31:37 -0800978static void package_power_limit_irq_save(struct rapl_package *rp)
Jacob Pan2d281d82013-10-17 10:28:35 -0700979{
Jacob Pan2d281d82013-10-17 10:28:35 -0700980 if (!boot_cpu_has(X86_FEATURE_PTS) || !boot_cpu_has(X86_FEATURE_PLN))
981 return;
982
Jacob Pan323ee642016-02-24 13:31:38 -0800983 smp_call_function_single(rp->lead_cpu, power_limit_irq_save_cpu, rp, 1);
Jacob Panf14a1392016-02-24 13:31:36 -0800984}
985
Thomas Gleixner58705062016-11-22 21:16:02 +0000986/*
987 * Restore per package power limit interrupt enable state. Called from cpu
988 * hotplug code on package removal.
989 */
990static void package_power_limit_irq_restore(struct rapl_package *rp)
Jacob Panf14a1392016-02-24 13:31:36 -0800991{
Thomas Gleixner58705062016-11-22 21:16:02 +0000992 u32 l, h;
993
994 if (!boot_cpu_has(X86_FEATURE_PTS) || !boot_cpu_has(X86_FEATURE_PLN))
995 return;
996
997 /* irq enable state not saved, nothing to restore */
998 if (!(rp->power_limit_irq & PACKAGE_PLN_INT_SAVED))
999 return;
Jacob Panf14a1392016-02-24 13:31:36 -08001000
1001 rdmsr_safe(MSR_IA32_PACKAGE_THERM_INTERRUPT, &l, &h);
1002
1003 if (rp->power_limit_irq & PACKAGE_THERM_INT_PLN_ENABLE)
1004 l |= PACKAGE_THERM_INT_PLN_ENABLE;
1005 else
1006 l &= ~PACKAGE_THERM_INT_PLN_ENABLE;
1007
1008 wrmsr_safe(MSR_IA32_PACKAGE_THERM_INTERRUPT, l, h);
Jacob Pan2d281d82013-10-17 10:28:35 -07001009}
1010
Jacob Pan3c2c0842014-11-07 09:29:26 -08001011static void set_floor_freq_default(struct rapl_domain *rd, bool mode)
1012{
1013 int nr_powerlimit = find_nr_power_limit(rd);
1014
1015 /* always enable clamp such that p-state can go below OS requested
1016 * range. power capping priority over guranteed frequency.
1017 */
1018 rapl_write_data_raw(rd, PL1_CLAMP, mode);
1019
1020 /* some domains have pl2 */
1021 if (nr_powerlimit > 1) {
1022 rapl_write_data_raw(rd, PL2_ENABLE, mode);
1023 rapl_write_data_raw(rd, PL2_CLAMP, mode);
1024 }
1025}
1026
1027static void set_floor_freq_atom(struct rapl_domain *rd, bool enable)
1028{
1029 static u32 power_ctrl_orig_val;
1030 u32 mdata;
1031
Ajay Thomas51b63402015-04-30 01:43:23 +05301032 if (!rapl_defaults->floor_freq_reg_addr) {
1033 pr_err("Invalid floor frequency config register\n");
1034 return;
1035 }
1036
Jacob Pan3c2c0842014-11-07 09:29:26 -08001037 if (!power_ctrl_orig_val)
Andy Shevchenko4077a382015-11-11 19:59:29 +02001038 iosf_mbi_read(BT_MBI_UNIT_PMC, MBI_CR_READ,
1039 rapl_defaults->floor_freq_reg_addr,
1040 &power_ctrl_orig_val);
Jacob Pan3c2c0842014-11-07 09:29:26 -08001041 mdata = power_ctrl_orig_val;
1042 if (enable) {
1043 mdata &= ~(0x7f << 8);
1044 mdata |= 1 << 8;
1045 }
Andy Shevchenko4077a382015-11-11 19:59:29 +02001046 iosf_mbi_write(BT_MBI_UNIT_PMC, MBI_CR_WRITE,
1047 rapl_defaults->floor_freq_reg_addr, mdata);
Jacob Pan3c2c0842014-11-07 09:29:26 -08001048}
1049
1050static u64 rapl_compute_time_window_core(struct rapl_package *rp, u64 value,
1051 bool to_raw)
1052{
1053 u64 f, y; /* fraction and exp. used for time unit */
1054
1055 /*
1056 * Special processing based on 2^Y*(1+F/4), refer
1057 * to Intel Software Developer's manual Vol.3B: CH 14.9.3.
1058 */
1059 if (!to_raw) {
1060 f = (value & 0x60) >> 5;
1061 y = value & 0x1f;
1062 value = (1 << y) * (4 + f) * rp->time_unit / 4;
1063 } else {
1064 do_div(value, rp->time_unit);
1065 y = ilog2(value);
1066 f = div64_u64(4 * (value - (1 << y)), 1 << y);
1067 value = (y & 0x1f) | ((f & 0x3) << 5);
1068 }
1069 return value;
1070}
1071
1072static u64 rapl_compute_time_window_atom(struct rapl_package *rp, u64 value,
1073 bool to_raw)
1074{
1075 /*
1076 * Atom time unit encoding is straight forward val * time_unit,
1077 * where time_unit is default to 1 sec. Never 0.
1078 */
1079 if (!to_raw)
1080 return (value) ? value *= rp->time_unit : rp->time_unit;
1081 else
1082 value = div64_u64(value, rp->time_unit);
1083
1084 return value;
1085}
1086
Jacob Pan087e9cb2014-11-07 09:29:25 -08001087static const struct rapl_defaults rapl_defaults_core = {
Ajay Thomas51b63402015-04-30 01:43:23 +05301088 .floor_freq_reg_addr = 0,
Jacob Pan3c2c0842014-11-07 09:29:26 -08001089 .check_unit = rapl_check_unit_core,
1090 .set_floor_freq = set_floor_freq_default,
1091 .compute_time_window = rapl_compute_time_window_core,
Jacob Pan087e9cb2014-11-07 09:29:25 -08001092};
1093
Jacob Pand474a4d2015-03-13 03:48:56 -07001094static const struct rapl_defaults rapl_defaults_hsw_server = {
1095 .check_unit = rapl_check_unit_core,
1096 .set_floor_freq = set_floor_freq_default,
1097 .compute_time_window = rapl_compute_time_window_core,
1098 .dram_domain_energy_unit = 15300,
1099};
1100
Ajay Thomas51b63402015-04-30 01:43:23 +05301101static const struct rapl_defaults rapl_defaults_byt = {
1102 .floor_freq_reg_addr = IOSF_CPU_POWER_BUDGET_CTL_BYT,
Jacob Pan3c2c0842014-11-07 09:29:26 -08001103 .check_unit = rapl_check_unit_atom,
1104 .set_floor_freq = set_floor_freq_atom,
1105 .compute_time_window = rapl_compute_time_window_atom,
Jacob Pan087e9cb2014-11-07 09:29:25 -08001106};
1107
Ajay Thomas51b63402015-04-30 01:43:23 +05301108static const struct rapl_defaults rapl_defaults_tng = {
1109 .floor_freq_reg_addr = IOSF_CPU_POWER_BUDGET_CTL_TNG,
1110 .check_unit = rapl_check_unit_atom,
1111 .set_floor_freq = set_floor_freq_atom,
1112 .compute_time_window = rapl_compute_time_window_atom,
1113};
1114
1115static const struct rapl_defaults rapl_defaults_ann = {
1116 .floor_freq_reg_addr = 0,
1117 .check_unit = rapl_check_unit_atom,
1118 .set_floor_freq = NULL,
1119 .compute_time_window = rapl_compute_time_window_atom,
1120};
1121
1122static const struct rapl_defaults rapl_defaults_cht = {
1123 .floor_freq_reg_addr = 0,
1124 .check_unit = rapl_check_unit_atom,
1125 .set_floor_freq = NULL,
1126 .compute_time_window = rapl_compute_time_window_atom,
1127};
1128
Mathias Krauseea85dbc2015-03-25 22:15:52 +01001129static const struct x86_cpu_id rapl_ids[] __initconst = {
Andy Shevchenko17ed1512018-08-31 11:25:13 +03001130 INTEL_CPU_FAM6(SANDYBRIDGE, rapl_defaults_core),
1131 INTEL_CPU_FAM6(SANDYBRIDGE_X, rapl_defaults_core),
Dave Hansen0bb04b52016-06-02 17:19:37 -07001132
Andy Shevchenko17ed1512018-08-31 11:25:13 +03001133 INTEL_CPU_FAM6(IVYBRIDGE, rapl_defaults_core),
1134 INTEL_CPU_FAM6(IVYBRIDGE_X, rapl_defaults_core),
Dave Hansen0bb04b52016-06-02 17:19:37 -07001135
Andy Shevchenko17ed1512018-08-31 11:25:13 +03001136 INTEL_CPU_FAM6(HASWELL_CORE, rapl_defaults_core),
1137 INTEL_CPU_FAM6(HASWELL_ULT, rapl_defaults_core),
1138 INTEL_CPU_FAM6(HASWELL_GT3E, rapl_defaults_core),
1139 INTEL_CPU_FAM6(HASWELL_X, rapl_defaults_hsw_server),
Dave Hansen0bb04b52016-06-02 17:19:37 -07001140
Andy Shevchenko17ed1512018-08-31 11:25:13 +03001141 INTEL_CPU_FAM6(BROADWELL_CORE, rapl_defaults_core),
1142 INTEL_CPU_FAM6(BROADWELL_GT3E, rapl_defaults_core),
1143 INTEL_CPU_FAM6(BROADWELL_XEON_D, rapl_defaults_core),
1144 INTEL_CPU_FAM6(BROADWELL_X, rapl_defaults_hsw_server),
Dave Hansen0bb04b52016-06-02 17:19:37 -07001145
Andy Shevchenko17ed1512018-08-31 11:25:13 +03001146 INTEL_CPU_FAM6(SKYLAKE_DESKTOP, rapl_defaults_core),
1147 INTEL_CPU_FAM6(SKYLAKE_MOBILE, rapl_defaults_core),
1148 INTEL_CPU_FAM6(SKYLAKE_X, rapl_defaults_hsw_server),
1149 INTEL_CPU_FAM6(KABYLAKE_MOBILE, rapl_defaults_core),
1150 INTEL_CPU_FAM6(KABYLAKE_DESKTOP, rapl_defaults_core),
1151 INTEL_CPU_FAM6(CANNONLAKE_MOBILE, rapl_defaults_core),
Gayatri Kammelaba6f3ec2019-02-18 15:01:02 +08001152 INTEL_CPU_FAM6(ICELAKE_MOBILE, rapl_defaults_core),
Dave Hansen0bb04b52016-06-02 17:19:37 -07001153
Linus Torvaldsc05f3642018-10-23 13:32:18 +01001154 INTEL_CPU_FAM6(ATOM_SILVERMONT, rapl_defaults_byt),
Andy Shevchenko17ed1512018-08-31 11:25:13 +03001155 INTEL_CPU_FAM6(ATOM_AIRMONT, rapl_defaults_cht),
Linus Torvaldsc05f3642018-10-23 13:32:18 +01001156 INTEL_CPU_FAM6(ATOM_SILVERMONT_MID, rapl_defaults_tng),
1157 INTEL_CPU_FAM6(ATOM_AIRMONT_MID, rapl_defaults_ann),
Andy Shevchenko17ed1512018-08-31 11:25:13 +03001158 INTEL_CPU_FAM6(ATOM_GOLDMONT, rapl_defaults_core),
Linus Torvaldsc05f3642018-10-23 13:32:18 +01001159 INTEL_CPU_FAM6(ATOM_GOLDMONT_PLUS, rapl_defaults_core),
1160 INTEL_CPU_FAM6(ATOM_GOLDMONT_X, rapl_defaults_core),
Zhang Ruidf7f8e02019-02-12 10:47:10 +08001161 INTEL_CPU_FAM6(ATOM_TREMONT_X, rapl_defaults_core),
Dave Hansen0bb04b52016-06-02 17:19:37 -07001162
Andy Shevchenko17ed1512018-08-31 11:25:13 +03001163 INTEL_CPU_FAM6(XEON_PHI_KNL, rapl_defaults_hsw_server),
1164 INTEL_CPU_FAM6(XEON_PHI_KNM, rapl_defaults_hsw_server),
Jacob Pan2d281d82013-10-17 10:28:35 -07001165 {}
1166};
1167MODULE_DEVICE_TABLE(x86cpu, rapl_ids);
1168
Thomas Gleixnerbed5ab62016-11-22 21:15:58 +00001169/* Read once for all raw primitive data for domains */
1170static void rapl_update_domain_data(struct rapl_package *rp)
Jacob Pan2d281d82013-10-17 10:28:35 -07001171{
1172 int dmn, prim;
1173 u64 val;
Jacob Pan2d281d82013-10-17 10:28:35 -07001174
Thomas Gleixnerbed5ab62016-11-22 21:15:58 +00001175 for (dmn = 0; dmn < rp->nr_domains; dmn++) {
Zhang Rui9ea76122019-05-13 13:58:53 -04001176 pr_debug("update %s domain %s data\n", rp->name,
Thomas Gleixnerbed5ab62016-11-22 21:15:58 +00001177 rp->domains[dmn].name);
1178 /* exclude non-raw primitives */
1179 for (prim = 0; prim < NR_RAW_PRIMITIVES; prim++) {
1180 if (!rapl_read_data_raw(&rp->domains[dmn], prim,
1181 rpi[prim].unit, &val))
1182 rp->domains[dmn].rdd.primitives[prim] = val;
Jacob Pan2d281d82013-10-17 10:28:35 -07001183 }
1184 }
1185
1186}
1187
Thomas Gleixner58705062016-11-22 21:16:02 +00001188static void rapl_unregister_powercap(void)
Jacob Pan2d281d82013-10-17 10:28:35 -07001189{
Srinivas Pandruvada3521ba12016-04-17 15:03:01 -07001190 if (platform_rapl_domain) {
1191 powercap_unregister_zone(control_type,
1192 &platform_rapl_domain->power_zone);
1193 kfree(platform_rapl_domain);
1194 }
Jacob Pan2d281d82013-10-17 10:28:35 -07001195 powercap_unregister_control_type(control_type);
Jacob Pan2d281d82013-10-17 10:28:35 -07001196}
1197
1198static int rapl_package_register_powercap(struct rapl_package *rp)
1199{
1200 struct rapl_domain *rd;
Jacob Pan2d281d82013-10-17 10:28:35 -07001201 struct powercap_zone *power_zone = NULL;
Luis de Bethencourt01857cf2018-01-17 10:30:34 +00001202 int nr_pl, ret;
Thomas Gleixnerbed5ab62016-11-22 21:15:58 +00001203
1204 /* Update the domain data of the new package */
1205 rapl_update_domain_data(rp);
Jacob Pan2d281d82013-10-17 10:28:35 -07001206
1207 /* first we register package domain as the parent zone*/
1208 for (rd = rp->domains; rd < rp->domains + rp->nr_domains; rd++) {
1209 if (rd->id == RAPL_DOMAIN_PACKAGE) {
1210 nr_pl = find_nr_power_limit(rd);
Zhang Rui9ea76122019-05-13 13:58:53 -04001211 pr_debug("register package domain %s\n", rp->name);
Jacob Pan2d281d82013-10-17 10:28:35 -07001212 power_zone = powercap_register_zone(&rd->power_zone,
1213 control_type,
Zhang Rui9ea76122019-05-13 13:58:53 -04001214 rp->name, NULL,
Jacob Pan2d281d82013-10-17 10:28:35 -07001215 &zone_ops[rd->id],
1216 nr_pl,
1217 &constraint_ops);
1218 if (IS_ERR(power_zone)) {
Zhang Rui9ea76122019-05-13 13:58:53 -04001219 pr_debug("failed to register power zone %s\n",
1220 rp->name);
Thomas Gleixnerbed5ab62016-11-22 21:15:58 +00001221 return PTR_ERR(power_zone);
Jacob Pan2d281d82013-10-17 10:28:35 -07001222 }
1223 /* track parent zone in per package/socket data */
1224 rp->power_zone = power_zone;
1225 /* done, only one package domain per socket */
1226 break;
1227 }
1228 }
1229 if (!power_zone) {
1230 pr_err("no package domain found, unknown topology!\n");
Thomas Gleixnerbed5ab62016-11-22 21:15:58 +00001231 return -ENODEV;
Jacob Pan2d281d82013-10-17 10:28:35 -07001232 }
1233 /* now register domains as children of the socket/package*/
1234 for (rd = rp->domains; rd < rp->domains + rp->nr_domains; rd++) {
1235 if (rd->id == RAPL_DOMAIN_PACKAGE)
1236 continue;
1237 /* number of power limits per domain varies */
1238 nr_pl = find_nr_power_limit(rd);
1239 power_zone = powercap_register_zone(&rd->power_zone,
1240 control_type, rd->name,
1241 rp->power_zone,
1242 &zone_ops[rd->id], nr_pl,
1243 &constraint_ops);
1244
1245 if (IS_ERR(power_zone)) {
Zhang Rui9ea76122019-05-13 13:58:53 -04001246 pr_debug("failed to register power_zone, %s:%s\n",
1247 rp->name, rd->name);
Jacob Pan2d281d82013-10-17 10:28:35 -07001248 ret = PTR_ERR(power_zone);
1249 goto err_cleanup;
1250 }
1251 }
Thomas Gleixnerbed5ab62016-11-22 21:15:58 +00001252 return 0;
Jacob Pan2d281d82013-10-17 10:28:35 -07001253
Jacob Pan2d281d82013-10-17 10:28:35 -07001254err_cleanup:
Thomas Gleixner58705062016-11-22 21:16:02 +00001255 /*
1256 * Clean up previously initialized domains within the package if we
Jacob Pan2d281d82013-10-17 10:28:35 -07001257 * failed after the first domain setup.
1258 */
1259 while (--rd >= rp->domains) {
Zhang Rui9ea76122019-05-13 13:58:53 -04001260 pr_debug("unregister %s domain %s\n", rp->name, rd->name);
Jacob Pan2d281d82013-10-17 10:28:35 -07001261 powercap_unregister_zone(control_type, &rd->power_zone);
1262 }
1263
1264 return ret;
1265}
1266
Thomas Gleixner58705062016-11-22 21:16:02 +00001267static int __init rapl_register_psys(void)
Srinivas Pandruvada3521ba12016-04-17 15:03:01 -07001268{
1269 struct rapl_domain *rd;
1270 struct powercap_zone *power_zone;
1271 u64 val;
1272
1273 if (rdmsrl_safe_on_cpu(0, MSR_PLATFORM_ENERGY_STATUS, &val) || !val)
1274 return -ENODEV;
1275
1276 if (rdmsrl_safe_on_cpu(0, MSR_PLATFORM_POWER_LIMIT, &val) || !val)
1277 return -ENODEV;
1278
1279 rd = kzalloc(sizeof(*rd), GFP_KERNEL);
1280 if (!rd)
1281 return -ENOMEM;
1282
1283 rd->name = rapl_domain_names[RAPL_DOMAIN_PLATFORM];
1284 rd->id = RAPL_DOMAIN_PLATFORM;
Zhang Ruif7c4e0c2019-07-10 21:44:22 +08001285 rd->regs[0] = MSR_PLATFORM_POWER_LIMIT;
1286 rd->regs[1] = MSR_PLATFORM_ENERGY_STATUS;
Srinivas Pandruvada3521ba12016-04-17 15:03:01 -07001287 rd->rpl[0].prim_id = PL1_ENABLE;
1288 rd->rpl[0].name = pl1_name;
1289 rd->rpl[1].prim_id = PL2_ENABLE;
1290 rd->rpl[1].name = pl2_name;
Zhang Ruiaadf7b32019-05-13 13:58:50 -04001291 rd->rp = rapl_find_package_domain(0);
Srinivas Pandruvada3521ba12016-04-17 15:03:01 -07001292
1293 power_zone = powercap_register_zone(&rd->power_zone, control_type,
1294 "psys", NULL,
1295 &zone_ops[RAPL_DOMAIN_PLATFORM],
1296 2, &constraint_ops);
1297
1298 if (IS_ERR(power_zone)) {
1299 kfree(rd);
1300 return PTR_ERR(power_zone);
1301 }
1302
1303 platform_rapl_domain = rd;
1304
1305 return 0;
1306}
1307
Thomas Gleixner58705062016-11-22 21:16:02 +00001308static int __init rapl_register_powercap(void)
Jacob Pan2d281d82013-10-17 10:28:35 -07001309{
Jacob Pan2d281d82013-10-17 10:28:35 -07001310 control_type = powercap_register_control_type(NULL, "intel-rapl", NULL);
1311 if (IS_ERR(control_type)) {
1312 pr_debug("failed to register powercap control_type.\n");
1313 return PTR_ERR(control_type);
1314 }
Thomas Gleixner58705062016-11-22 21:16:02 +00001315 return 0;
Jacob Pan2d281d82013-10-17 10:28:35 -07001316}
1317
1318static int rapl_check_domain(int cpu, int domain)
1319{
1320 unsigned msr;
Jacob Pan9d31c672014-04-29 15:33:06 -07001321 u64 val = 0;
Jacob Pan2d281d82013-10-17 10:28:35 -07001322
1323 switch (domain) {
1324 case RAPL_DOMAIN_PACKAGE:
1325 msr = MSR_PKG_ENERGY_STATUS;
1326 break;
1327 case RAPL_DOMAIN_PP0:
1328 msr = MSR_PP0_ENERGY_STATUS;
1329 break;
1330 case RAPL_DOMAIN_PP1:
1331 msr = MSR_PP1_ENERGY_STATUS;
1332 break;
1333 case RAPL_DOMAIN_DRAM:
1334 msr = MSR_DRAM_ENERGY_STATUS;
1335 break;
Srinivas Pandruvada3521ba12016-04-17 15:03:01 -07001336 case RAPL_DOMAIN_PLATFORM:
1337 /* PSYS(PLATFORM) is not a CPU domain, so avoid printng error */
1338 return -EINVAL;
Jacob Pan2d281d82013-10-17 10:28:35 -07001339 default:
1340 pr_err("invalid domain id %d\n", domain);
1341 return -EINVAL;
1342 }
Jacob Pan9d31c672014-04-29 15:33:06 -07001343 /* make sure domain counters are available and contains non-zero
1344 * values, otherwise skip it.
1345 */
1346 if (rdmsrl_safe_on_cpu(cpu, msr, &val) || !val)
Jacob Pan2d281d82013-10-17 10:28:35 -07001347 return -ENODEV;
1348
Jacob Pan9d31c672014-04-29 15:33:06 -07001349 return 0;
Jacob Pan2d281d82013-10-17 10:28:35 -07001350}
1351
Jacob Pane1399ba2016-05-31 13:41:29 -07001352
1353/*
1354 * Check if power limits are available. Two cases when they are not available:
1355 * 1. Locked by BIOS, in this case we still provide read-only access so that
1356 * users can see what limit is set by the BIOS.
1357 * 2. Some CPUs make some domains monitoring only which means PLx MSRs may not
1358 * exist at all. In this case, we do not show the contraints in powercap.
1359 *
1360 * Called after domains are detected and initialized.
1361 */
1362static void rapl_detect_powerlimit(struct rapl_domain *rd)
1363{
1364 u64 val64;
1365 int i;
1366
1367 /* check if the domain is locked by BIOS, ignore if MSR doesn't exist */
1368 if (!rapl_read_data_raw(rd, FW_LOCK, false, &val64)) {
1369 if (val64) {
Zhang Rui9ea76122019-05-13 13:58:53 -04001370 pr_info("RAPL %s domain %s locked by BIOS\n",
1371 rd->rp->name, rd->name);
Jacob Pane1399ba2016-05-31 13:41:29 -07001372 rd->state |= DOMAIN_STATE_BIOS_LOCKED;
1373 }
1374 }
1375 /* check if power limit MSRs exists, otherwise domain is monitoring only */
1376 for (i = 0; i < NR_POWER_LIMITS; i++) {
1377 int prim = rd->rpl[i].prim_id;
1378 if (rapl_read_data_raw(rd, prim, false, &val64))
1379 rd->rpl[i].name = NULL;
1380 }
1381}
1382
Jacob Pan2d281d82013-10-17 10:28:35 -07001383/* Detect active and valid domains for the given CPU, caller must
1384 * ensure the CPU belongs to the targeted package and CPU hotlug is disabled.
1385 */
1386static int rapl_detect_domains(struct rapl_package *rp, int cpu)
1387{
Jacob Pan2d281d82013-10-17 10:28:35 -07001388 struct rapl_domain *rd;
Thomas Gleixner58705062016-11-22 21:16:02 +00001389 int i;
Jacob Pan2d281d82013-10-17 10:28:35 -07001390
1391 for (i = 0; i < RAPL_DOMAIN_MAX; i++) {
1392 /* use physical package id to read counters */
Jacob Panfcdf1792014-09-02 02:55:21 -07001393 if (!rapl_check_domain(cpu, i)) {
Jacob Pan2d281d82013-10-17 10:28:35 -07001394 rp->domain_map |= 1 << i;
Jacob Panfcdf1792014-09-02 02:55:21 -07001395 pr_info("Found RAPL domain %s\n", rapl_domain_names[i]);
1396 }
Jacob Pan2d281d82013-10-17 10:28:35 -07001397 }
1398 rp->nr_domains = bitmap_weight(&rp->domain_map, RAPL_DOMAIN_MAX);
1399 if (!rp->nr_domains) {
Zhang Rui9ea76122019-05-13 13:58:53 -04001400 pr_debug("no valid rapl domains found in %s\n", rp->name);
Thomas Gleixner58705062016-11-22 21:16:02 +00001401 return -ENODEV;
Jacob Pan2d281d82013-10-17 10:28:35 -07001402 }
Zhang Rui9ea76122019-05-13 13:58:53 -04001403 pr_debug("found %d domains on %s\n", rp->nr_domains, rp->name);
Jacob Pan2d281d82013-10-17 10:28:35 -07001404
1405 rp->domains = kcalloc(rp->nr_domains + 1, sizeof(struct rapl_domain),
1406 GFP_KERNEL);
Thomas Gleixner58705062016-11-22 21:16:02 +00001407 if (!rp->domains)
1408 return -ENOMEM;
1409
Jacob Pan2d281d82013-10-17 10:28:35 -07001410 rapl_init_domains(rp);
1411
Jacob Pane1399ba2016-05-31 13:41:29 -07001412 for (rd = rp->domains; rd < rp->domains + rp->nr_domains; rd++)
1413 rapl_detect_powerlimit(rd);
1414
Jacob Pan2d281d82013-10-17 10:28:35 -07001415 return 0;
1416}
1417
1418/* called from CPU hotplug notifier, hotplug lock held */
1419static void rapl_remove_package(struct rapl_package *rp)
1420{
1421 struct rapl_domain *rd, *rd_package = NULL;
1422
Thomas Gleixner58705062016-11-22 21:16:02 +00001423 package_power_limit_irq_restore(rp);
1424
Jacob Pan2d281d82013-10-17 10:28:35 -07001425 for (rd = rp->domains; rd < rp->domains + rp->nr_domains; rd++) {
Thomas Gleixner58705062016-11-22 21:16:02 +00001426 rapl_write_data_raw(rd, PL1_ENABLE, 0);
1427 rapl_write_data_raw(rd, PL1_CLAMP, 0);
1428 if (find_nr_power_limit(rd) > 1) {
1429 rapl_write_data_raw(rd, PL2_ENABLE, 0);
1430 rapl_write_data_raw(rd, PL2_CLAMP, 0);
1431 }
Jacob Pan2d281d82013-10-17 10:28:35 -07001432 if (rd->id == RAPL_DOMAIN_PACKAGE) {
1433 rd_package = rd;
1434 continue;
1435 }
Zhang Rui9ea76122019-05-13 13:58:53 -04001436 pr_debug("remove package, undo power limit on %s: %s\n",
1437 rp->name, rd->name);
Jacob Pan2d281d82013-10-17 10:28:35 -07001438 powercap_unregister_zone(control_type, &rd->power_zone);
1439 }
1440 /* do parent zone last */
1441 powercap_unregister_zone(control_type, &rd_package->power_zone);
1442 list_del(&rp->plist);
1443 kfree(rp);
1444}
1445
1446/* called from CPU hotplug notifier, hotplug lock held */
Zhang Ruiaadf7b32019-05-13 13:58:50 -04001447static struct rapl_package *rapl_add_package(int cpu)
Jacob Pan2d281d82013-10-17 10:28:35 -07001448{
Zhang Rui32fb4802019-05-13 13:58:51 -04001449 int id = topology_logical_die_id(cpu);
Jacob Pan2d281d82013-10-17 10:28:35 -07001450 struct rapl_package *rp;
Zhang Rui9ea76122019-05-13 13:58:53 -04001451 struct cpuinfo_x86 *c = &cpu_data(cpu);
Thomas Gleixnerb4005e92016-11-22 21:16:05 +00001452 int ret;
Jacob Pan2d281d82013-10-17 10:28:35 -07001453
Jacob Pan2d281d82013-10-17 10:28:35 -07001454 rp = kzalloc(sizeof(struct rapl_package), GFP_KERNEL);
1455 if (!rp)
Thomas Gleixnerb4005e92016-11-22 21:16:05 +00001456 return ERR_PTR(-ENOMEM);
Jacob Pan2d281d82013-10-17 10:28:35 -07001457
1458 /* add the new package to the list */
Zhang Ruiaadf7b32019-05-13 13:58:50 -04001459 rp->id = id;
Jacob Pan323ee642016-02-24 13:31:38 -08001460 rp->lead_cpu = cpu;
1461
Zhang Rui9ea76122019-05-13 13:58:53 -04001462 if (topology_max_die_per_package() > 1)
1463 snprintf(rp->name, PACKAGE_DOMAIN_NAME_LENGTH,
1464 "package-%d-die-%d", c->phys_proc_id, c->cpu_die_id);
1465 else
1466 snprintf(rp->name, PACKAGE_DOMAIN_NAME_LENGTH, "package-%d",
1467 c->phys_proc_id);
1468
Jacob Pan2d281d82013-10-17 10:28:35 -07001469 /* check if the package contains valid domains */
1470 if (rapl_detect_domains(rp, cpu) ||
Jacob Pan3c2c0842014-11-07 09:29:26 -08001471 rapl_defaults->check_unit(rp, cpu)) {
Jacob Pan2d281d82013-10-17 10:28:35 -07001472 ret = -ENODEV;
1473 goto err_free_package;
1474 }
Thomas Gleixnera74f4362016-11-22 21:15:59 +00001475 ret = rapl_package_register_powercap(rp);
1476 if (!ret) {
Jacob Pan2d281d82013-10-17 10:28:35 -07001477 INIT_LIST_HEAD(&rp->plist);
1478 list_add(&rp->plist, &rapl_packages);
Thomas Gleixnerb4005e92016-11-22 21:16:05 +00001479 return rp;
Jacob Pan2d281d82013-10-17 10:28:35 -07001480 }
1481
1482err_free_package:
1483 kfree(rp->domains);
1484 kfree(rp);
Thomas Gleixnerb4005e92016-11-22 21:16:05 +00001485 return ERR_PTR(ret);
Jacob Pan2d281d82013-10-17 10:28:35 -07001486}
1487
1488/* Handles CPU hotplug on multi-socket systems.
1489 * If a CPU goes online as the first CPU of the physical package
1490 * we add the RAPL package to the system. Similarly, when the last
1491 * CPU of the package is removed, we remove the RAPL package and its
1492 * associated domains. Cooling devices are handled accordingly at
1493 * per-domain level.
1494 */
Sebastian Andrzej Siewior5e4dc792016-11-22 21:16:00 +00001495static int rapl_cpu_online(unsigned int cpu)
Jacob Pan2d281d82013-10-17 10:28:35 -07001496{
Sebastian Andrzej Siewior5e4dc792016-11-22 21:16:00 +00001497 struct rapl_package *rp;
Sebastian Andrzej Siewior5e4dc792016-11-22 21:16:00 +00001498
Zhang Ruiaadf7b32019-05-13 13:58:50 -04001499 rp = rapl_find_package_domain(cpu);
Thomas Gleixnerb4005e92016-11-22 21:16:05 +00001500 if (!rp) {
Zhang Ruiaadf7b32019-05-13 13:58:50 -04001501 rp = rapl_add_package(cpu);
Thomas Gleixnerb4005e92016-11-22 21:16:05 +00001502 if (IS_ERR(rp))
1503 return PTR_ERR(rp);
Thomas Gleixner58705062016-11-22 21:16:02 +00001504 }
Thomas Gleixnerb4005e92016-11-22 21:16:05 +00001505 cpumask_set_cpu(cpu, &rp->cpumask);
1506 return 0;
Sebastian Andrzej Siewior5e4dc792016-11-22 21:16:00 +00001507}
1508
1509static int rapl_cpu_down_prep(unsigned int cpu)
1510{
Jacob Pan2d281d82013-10-17 10:28:35 -07001511 struct rapl_package *rp;
Jacob Pan323ee642016-02-24 13:31:38 -08001512 int lead_cpu;
Jacob Pan2d281d82013-10-17 10:28:35 -07001513
Zhang Ruiaadf7b32019-05-13 13:58:50 -04001514 rp = rapl_find_package_domain(cpu);
Sebastian Andrzej Siewior5e4dc792016-11-22 21:16:00 +00001515 if (!rp)
1516 return 0;
Thomas Gleixnerb4005e92016-11-22 21:16:05 +00001517
1518 cpumask_clear_cpu(cpu, &rp->cpumask);
1519 lead_cpu = cpumask_first(&rp->cpumask);
1520 if (lead_cpu >= nr_cpu_ids)
Sebastian Andrzej Siewior5e4dc792016-11-22 21:16:00 +00001521 rapl_remove_package(rp);
Thomas Gleixnerb4005e92016-11-22 21:16:05 +00001522 else if (rp->lead_cpu == cpu)
1523 rp->lead_cpu = lead_cpu;
Sebastian Andrzej Siewior5e4dc792016-11-22 21:16:00 +00001524 return 0;
Jacob Pan2d281d82013-10-17 10:28:35 -07001525}
1526
Sebastian Andrzej Siewior5e4dc792016-11-22 21:16:00 +00001527static enum cpuhp_state pcap_rapl_online;
Jacob Pan2d281d82013-10-17 10:28:35 -07001528
Zhen Han52b36722018-01-10 08:38:23 +08001529static void power_limit_state_save(void)
1530{
1531 struct rapl_package *rp;
1532 struct rapl_domain *rd;
1533 int nr_pl, ret, i;
1534
1535 get_online_cpus();
1536 list_for_each_entry(rp, &rapl_packages, plist) {
1537 if (!rp->power_zone)
1538 continue;
1539 rd = power_zone_to_rapl_domain(rp->power_zone);
1540 nr_pl = find_nr_power_limit(rd);
1541 for (i = 0; i < nr_pl; i++) {
1542 switch (rd->rpl[i].prim_id) {
1543 case PL1_ENABLE:
1544 ret = rapl_read_data_raw(rd,
1545 POWER_LIMIT1,
1546 true,
1547 &rd->rpl[i].last_power_limit);
1548 if (ret)
1549 rd->rpl[i].last_power_limit = 0;
1550 break;
1551 case PL2_ENABLE:
1552 ret = rapl_read_data_raw(rd,
1553 POWER_LIMIT2,
1554 true,
1555 &rd->rpl[i].last_power_limit);
1556 if (ret)
1557 rd->rpl[i].last_power_limit = 0;
1558 break;
1559 }
1560 }
1561 }
1562 put_online_cpus();
1563}
1564
1565static void power_limit_state_restore(void)
1566{
1567 struct rapl_package *rp;
1568 struct rapl_domain *rd;
1569 int nr_pl, i;
1570
1571 get_online_cpus();
1572 list_for_each_entry(rp, &rapl_packages, plist) {
1573 if (!rp->power_zone)
1574 continue;
1575 rd = power_zone_to_rapl_domain(rp->power_zone);
1576 nr_pl = find_nr_power_limit(rd);
1577 for (i = 0; i < nr_pl; i++) {
1578 switch (rd->rpl[i].prim_id) {
1579 case PL1_ENABLE:
1580 if (rd->rpl[i].last_power_limit)
1581 rapl_write_data_raw(rd,
1582 POWER_LIMIT1,
1583 rd->rpl[i].last_power_limit);
1584 break;
1585 case PL2_ENABLE:
1586 if (rd->rpl[i].last_power_limit)
1587 rapl_write_data_raw(rd,
1588 POWER_LIMIT2,
1589 rd->rpl[i].last_power_limit);
1590 break;
1591 }
1592 }
1593 }
1594 put_online_cpus();
1595}
1596
1597static int rapl_pm_callback(struct notifier_block *nb,
1598 unsigned long mode, void *_unused)
1599{
1600 switch (mode) {
1601 case PM_SUSPEND_PREPARE:
1602 power_limit_state_save();
1603 break;
1604 case PM_POST_SUSPEND:
1605 power_limit_state_restore();
1606 break;
1607 }
1608 return NOTIFY_OK;
1609}
1610
1611static struct notifier_block rapl_pm_notifier = {
1612 .notifier_call = rapl_pm_callback,
1613};
1614
Jacob Pan2d281d82013-10-17 10:28:35 -07001615static int __init rapl_init(void)
1616{
Jacob Pan087e9cb2014-11-07 09:29:25 -08001617 const struct x86_cpu_id *id;
Thomas Gleixner58705062016-11-22 21:16:02 +00001618 int ret;
Jacob Pan2d281d82013-10-17 10:28:35 -07001619
Jacob Pan087e9cb2014-11-07 09:29:25 -08001620 id = x86_match_cpu(rapl_ids);
1621 if (!id) {
Jacob Pan2d281d82013-10-17 10:28:35 -07001622 pr_err("driver does not support CPU family %d model %d\n",
1623 boot_cpu_data.x86, boot_cpu_data.x86_model);
1624
1625 return -ENODEV;
1626 }
Srivatsa S. Bhat009f2252014-03-11 02:09:26 +05301627
Jacob Pan087e9cb2014-11-07 09:29:25 -08001628 rapl_defaults = (struct rapl_defaults *)id->driver_data;
1629
Thomas Gleixner58705062016-11-22 21:16:02 +00001630 ret = rapl_register_powercap();
Jacob Pan2d281d82013-10-17 10:28:35 -07001631 if (ret)
Thomas Gleixner58705062016-11-22 21:16:02 +00001632 return ret;
Jacob Pan2d281d82013-10-17 10:28:35 -07001633
Thomas Gleixner58705062016-11-22 21:16:02 +00001634 ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "powercap/rapl:online",
1635 rapl_cpu_online, rapl_cpu_down_prep);
Sebastian Andrzej Siewior5e4dc792016-11-22 21:16:00 +00001636 if (ret < 0)
1637 goto err_unreg;
1638 pcap_rapl_online = ret;
Thomas Gleixner58705062016-11-22 21:16:02 +00001639
1640 /* Don't bail out if PSys is not supported */
1641 rapl_register_psys();
Zhen Han52b36722018-01-10 08:38:23 +08001642
1643 ret = register_pm_notifier(&rapl_pm_notifier);
1644 if (ret)
1645 goto err_unreg_all;
1646
Sebastian Andrzej Siewior5e4dc792016-11-22 21:16:00 +00001647 return 0;
Jacob Pan2d281d82013-10-17 10:28:35 -07001648
Zhen Han52b36722018-01-10 08:38:23 +08001649err_unreg_all:
1650 cpuhp_remove_state(pcap_rapl_online);
1651
Sebastian Andrzej Siewior5e4dc792016-11-22 21:16:00 +00001652err_unreg:
1653 rapl_unregister_powercap();
Jacob Pan2d281d82013-10-17 10:28:35 -07001654 return ret;
1655}
1656
1657static void __exit rapl_exit(void)
1658{
Zhen Han52b36722018-01-10 08:38:23 +08001659 unregister_pm_notifier(&rapl_pm_notifier);
Sebastian Andrzej Siewior5e4dc792016-11-22 21:16:00 +00001660 cpuhp_remove_state(pcap_rapl_online);
Jacob Pan2d281d82013-10-17 10:28:35 -07001661 rapl_unregister_powercap();
Jacob Pan2d281d82013-10-17 10:28:35 -07001662}
1663
1664module_init(rapl_init);
1665module_exit(rapl_exit);
1666
1667MODULE_DESCRIPTION("Driver for Intel RAPL (Running Average Power Limit)");
1668MODULE_AUTHOR("Jacob Pan <jacob.jun.pan@intel.com>");
1669MODULE_LICENSE("GPL v2");