blob: 2651ea6cd6d37680f53ac80fff53fb962d928bac [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;
547 rd->name = rapl_domain_names[i];
548 rd->id = i;
549 rd->rpl[0].prim_id = PL1_ENABLE;
550 rd->rpl[0].name = pl1_name;
Sumeet Pawnikar8365a892020-07-16 23:14:55 +0530551
552 /*
553 * The PL2 power domain is applicable for limits two
554 * and limits three
555 */
556 if (rp->priv->limits[i] >= 2) {
Jacob Pan2d281d82013-10-17 10:28:35 -0700557 rd->rpl[1].prim_id = PL2_ENABLE;
558 rd->rpl[1].name = pl2_name;
Zhang Rui0c2dded2019-07-10 21:44:32 +0800559 }
560
Sumeet Pawnikar8365a892020-07-16 23:14:55 +0530561 /* Enable PL4 domain if the total power limits are three */
562 if (rp->priv->limits[i] == 3) {
563 rd->rpl[2].prim_id = PL4_ENABLE;
564 rd->rpl[2].name = pl4_name;
565 }
566
Zhang Rui0c2dded2019-07-10 21:44:32 +0800567 for (j = 0; j < RAPL_DOMAIN_REG_MAX; j++)
568 rd->regs[j] = rp->priv->regs[i][j];
569
Zhang Rui2d798d92020-06-29 13:34:50 +0800570 switch (i) {
571 case RAPL_DOMAIN_DRAM:
Jacob Pand474a4d2015-03-13 03:48:56 -0700572 rd->domain_energy_unit =
Zhang Rui33823882019-07-10 21:44:30 +0800573 rapl_defaults->dram_domain_energy_unit;
Jacob Pand474a4d2015-03-13 03:48:56 -0700574 if (rd->domain_energy_unit)
575 pr_info("DRAM domain energy unit %dpj\n",
576 rd->domain_energy_unit);
Zhang Rui2d798d92020-06-29 13:34:50 +0800577 break;
578 case RAPL_DOMAIN_PLATFORM:
579 rd->domain_energy_unit =
580 rapl_defaults->psys_domain_energy_unit;
581 if (rd->domain_energy_unit)
582 pr_info("Platform domain energy unit %dpj\n",
583 rd->domain_energy_unit);
584 break;
585 default:
586 break;
Jacob Pan2d281d82013-10-17 10:28:35 -0700587 }
Zhang Rui0c2dded2019-07-10 21:44:32 +0800588 rd++;
Jacob Pan2d281d82013-10-17 10:28:35 -0700589 }
590}
591
Jacob Pan309557f2016-02-24 13:31:37 -0800592static u64 rapl_unit_xlate(struct rapl_domain *rd, enum unit_type type,
Zhang Rui33823882019-07-10 21:44:30 +0800593 u64 value, int to_raw)
Jacob Pan2d281d82013-10-17 10:28:35 -0700594{
Jacob Pan3c2c0842014-11-07 09:29:26 -0800595 u64 units = 1;
Jacob Pan309557f2016-02-24 13:31:37 -0800596 struct rapl_package *rp = rd->rp;
Jacob Pand474a4d2015-03-13 03:48:56 -0700597 u64 scale = 1;
Jacob Pan2d281d82013-10-17 10:28:35 -0700598
Jacob Pan2d281d82013-10-17 10:28:35 -0700599 switch (type) {
600 case POWER_UNIT:
Jacob Pan3c2c0842014-11-07 09:29:26 -0800601 units = rp->power_unit;
Jacob Pan2d281d82013-10-17 10:28:35 -0700602 break;
603 case ENERGY_UNIT:
Jacob Pand474a4d2015-03-13 03:48:56 -0700604 scale = ENERGY_UNIT_SCALE;
605 /* per domain unit takes precedence */
Jacob Pancb43f812016-11-28 13:53:11 -0800606 if (rd->domain_energy_unit)
Jacob Pand474a4d2015-03-13 03:48:56 -0700607 units = rd->domain_energy_unit;
608 else
609 units = rp->energy_unit;
Jacob Pan2d281d82013-10-17 10:28:35 -0700610 break;
611 case TIME_UNIT:
Jacob Pan3c2c0842014-11-07 09:29:26 -0800612 return rapl_defaults->compute_time_window(rp, value, to_raw);
Jacob Pan2d281d82013-10-17 10:28:35 -0700613 case ARBITRARY_UNIT:
614 default:
615 return value;
616 };
617
618 if (to_raw)
Jacob Pand474a4d2015-03-13 03:48:56 -0700619 return div64_u64(value, units) * scale;
Jacob Pan3c2c0842014-11-07 09:29:26 -0800620
621 value *= units;
622
Jacob Pand474a4d2015-03-13 03:48:56 -0700623 return div64_u64(value, scale);
Jacob Pan2d281d82013-10-17 10:28:35 -0700624}
625
626/* in the order of enum rapl_primitives */
627static struct rapl_primitive_info rpi[] = {
628 /* name, mask, shift, msr index, unit divisor */
629 PRIMITIVE_INFO_INIT(ENERGY_COUNTER, ENERGY_STATUS_MASK, 0,
Zhang Rui33823882019-07-10 21:44:30 +0800630 RAPL_DOMAIN_REG_STATUS, ENERGY_UNIT, 0),
Jacob Pan2d281d82013-10-17 10:28:35 -0700631 PRIMITIVE_INFO_INIT(POWER_LIMIT1, POWER_LIMIT1_MASK, 0,
Zhang Rui33823882019-07-10 21:44:30 +0800632 RAPL_DOMAIN_REG_LIMIT, POWER_UNIT, 0),
Jacob Pan2d281d82013-10-17 10:28:35 -0700633 PRIMITIVE_INFO_INIT(POWER_LIMIT2, POWER_LIMIT2_MASK, 32,
Zhang Rui33823882019-07-10 21:44:30 +0800634 RAPL_DOMAIN_REG_LIMIT, POWER_UNIT, 0),
Sumeet Pawnikar8365a892020-07-16 23:14:55 +0530635 PRIMITIVE_INFO_INIT(POWER_LIMIT4, POWER_LIMIT4_MASK, 0,
636 RAPL_DOMAIN_REG_PL4, POWER_UNIT, 0),
Zhang Rui0c2dded2019-07-10 21:44:32 +0800637 PRIMITIVE_INFO_INIT(FW_LOCK, POWER_LOW_LOCK, 31,
Zhang Rui33823882019-07-10 21:44:30 +0800638 RAPL_DOMAIN_REG_LIMIT, ARBITRARY_UNIT, 0),
Jacob Pan2d281d82013-10-17 10:28:35 -0700639 PRIMITIVE_INFO_INIT(PL1_ENABLE, POWER_LIMIT1_ENABLE, 15,
Zhang Rui33823882019-07-10 21:44:30 +0800640 RAPL_DOMAIN_REG_LIMIT, ARBITRARY_UNIT, 0),
Jacob Pan2d281d82013-10-17 10:28:35 -0700641 PRIMITIVE_INFO_INIT(PL1_CLAMP, POWER_LIMIT1_CLAMP, 16,
Zhang Rui33823882019-07-10 21:44:30 +0800642 RAPL_DOMAIN_REG_LIMIT, ARBITRARY_UNIT, 0),
Jacob Pan2d281d82013-10-17 10:28:35 -0700643 PRIMITIVE_INFO_INIT(PL2_ENABLE, POWER_LIMIT2_ENABLE, 47,
Zhang Rui33823882019-07-10 21:44:30 +0800644 RAPL_DOMAIN_REG_LIMIT, ARBITRARY_UNIT, 0),
Jacob Pan2d281d82013-10-17 10:28:35 -0700645 PRIMITIVE_INFO_INIT(PL2_CLAMP, POWER_LIMIT2_CLAMP, 48,
Zhang Rui33823882019-07-10 21:44:30 +0800646 RAPL_DOMAIN_REG_LIMIT, ARBITRARY_UNIT, 0),
Sumeet Pawnikar8365a892020-07-16 23:14:55 +0530647 PRIMITIVE_INFO_INIT(PL4_ENABLE, POWER_LIMIT4_MASK, 0,
648 RAPL_DOMAIN_REG_PL4, ARBITRARY_UNIT, 0),
Jacob Pan2d281d82013-10-17 10:28:35 -0700649 PRIMITIVE_INFO_INIT(TIME_WINDOW1, TIME_WINDOW1_MASK, 17,
Zhang Rui33823882019-07-10 21:44:30 +0800650 RAPL_DOMAIN_REG_LIMIT, TIME_UNIT, 0),
Jacob Pan2d281d82013-10-17 10:28:35 -0700651 PRIMITIVE_INFO_INIT(TIME_WINDOW2, TIME_WINDOW2_MASK, 49,
Zhang Rui33823882019-07-10 21:44:30 +0800652 RAPL_DOMAIN_REG_LIMIT, TIME_UNIT, 0),
Jacob Pan2d281d82013-10-17 10:28:35 -0700653 PRIMITIVE_INFO_INIT(THERMAL_SPEC_POWER, POWER_INFO_THERMAL_SPEC_MASK,
Zhang Rui33823882019-07-10 21:44:30 +0800654 0, RAPL_DOMAIN_REG_INFO, POWER_UNIT, 0),
Jacob Pan2d281d82013-10-17 10:28:35 -0700655 PRIMITIVE_INFO_INIT(MAX_POWER, POWER_INFO_MAX_MASK, 32,
Zhang Rui33823882019-07-10 21:44:30 +0800656 RAPL_DOMAIN_REG_INFO, POWER_UNIT, 0),
Jacob Pan2d281d82013-10-17 10:28:35 -0700657 PRIMITIVE_INFO_INIT(MIN_POWER, POWER_INFO_MIN_MASK, 16,
Zhang Rui33823882019-07-10 21:44:30 +0800658 RAPL_DOMAIN_REG_INFO, POWER_UNIT, 0),
Jacob Pan2d281d82013-10-17 10:28:35 -0700659 PRIMITIVE_INFO_INIT(MAX_TIME_WINDOW, POWER_INFO_MAX_TIME_WIN_MASK, 48,
Zhang Rui33823882019-07-10 21:44:30 +0800660 RAPL_DOMAIN_REG_INFO, TIME_UNIT, 0),
Jacob Pan2d281d82013-10-17 10:28:35 -0700661 PRIMITIVE_INFO_INIT(THROTTLED_TIME, PERF_STATUS_THROTTLE_TIME_MASK, 0,
Zhang Rui33823882019-07-10 21:44:30 +0800662 RAPL_DOMAIN_REG_PERF, TIME_UNIT, 0),
Jacob Pan2d281d82013-10-17 10:28:35 -0700663 PRIMITIVE_INFO_INIT(PRIORITY_LEVEL, PP_POLICY_MASK, 0,
Zhang Rui33823882019-07-10 21:44:30 +0800664 RAPL_DOMAIN_REG_POLICY, ARBITRARY_UNIT, 0),
Jacob Pan2d281d82013-10-17 10:28:35 -0700665 /* non-hardware */
666 PRIMITIVE_INFO_INIT(AVERAGE_POWER, 0, 0, 0, POWER_UNIT,
Zhang Rui33823882019-07-10 21:44:30 +0800667 RAPL_PRIMITIVE_DERIVED),
Jacob Pan2d281d82013-10-17 10:28:35 -0700668 {NULL, 0, 0, 0},
669};
670
671/* Read primitive data based on its related struct rapl_primitive_info.
672 * if xlate flag is set, return translated data based on data units, i.e.
673 * time, energy, and power.
674 * RAPL MSRs are non-architectual and are laid out not consistently across
675 * domains. Here we use primitive info to allow writing consolidated access
676 * functions.
677 * For a given primitive, it is processed by MSR mask and shift. Unit conversion
678 * is pre-assigned based on RAPL unit MSRs read at init time.
679 * 63-------------------------- 31--------------------------- 0
680 * | xxxxx (mask) |
681 * | |<- shift ----------------|
682 * 63-------------------------- 31--------------------------- 0
683 */
684static int rapl_read_data_raw(struct rapl_domain *rd,
Zhang Rui33823882019-07-10 21:44:30 +0800685 enum rapl_primitives prim, bool xlate, u64 *data)
Jacob Pan2d281d82013-10-17 10:28:35 -0700686{
Zhang Ruibeea8df2019-07-10 21:44:27 +0800687 u64 value;
Jacob Pan2d281d82013-10-17 10:28:35 -0700688 struct rapl_primitive_info *rp = &rpi[prim];
Zhang Ruibeea8df2019-07-10 21:44:27 +0800689 struct reg_action ra;
Jacob Pan2d281d82013-10-17 10:28:35 -0700690 int cpu;
691
692 if (!rp->name || rp->flag & RAPL_PRIMITIVE_DUMMY)
693 return -EINVAL;
694
Zhang Ruibeea8df2019-07-10 21:44:27 +0800695 ra.reg = rd->regs[rp->id];
696 if (!ra.reg)
Jacob Pan2d281d82013-10-17 10:28:35 -0700697 return -EINVAL;
Jacob Pan323ee642016-02-24 13:31:38 -0800698
699 cpu = rd->rp->lead_cpu;
Jacob Pan2d281d82013-10-17 10:28:35 -0700700
Zhang Rui0c2dded2019-07-10 21:44:32 +0800701 /* domain with 2 limits has different bit */
702 if (prim == FW_LOCK && rd->rp->priv->limits[rd->id] == 2) {
703 rp->mask = POWER_HIGH_LOCK;
Jacob Pan2d281d82013-10-17 10:28:35 -0700704 rp->shift = 63;
705 }
706 /* non-hardware data are collected by the polling thread */
707 if (rp->flag & RAPL_PRIMITIVE_DERIVED) {
708 *data = rd->rdd.primitives[prim];
709 return 0;
710 }
711
Zhang Ruibeea8df2019-07-10 21:44:27 +0800712 ra.mask = rp->mask;
713
714 if (rd->rp->priv->read_raw(cpu, &ra)) {
Zhang Ruid978e752019-07-10 21:44:31 +0800715 pr_debug("failed to read reg 0x%llx on cpu %d\n", ra.reg, cpu);
Jacob Pan2d281d82013-10-17 10:28:35 -0700716 return -EIO;
717 }
718
Zhang Ruibeea8df2019-07-10 21:44:27 +0800719 value = ra.value >> rp->shift;
720
Jacob Pan2d281d82013-10-17 10:28:35 -0700721 if (xlate)
Zhang Ruibeea8df2019-07-10 21:44:27 +0800722 *data = rapl_unit_xlate(rd, rp->unit, value, 0);
Jacob Pan2d281d82013-10-17 10:28:35 -0700723 else
Zhang Ruibeea8df2019-07-10 21:44:27 +0800724 *data = value;
Jacob Pan2d281d82013-10-17 10:28:35 -0700725
726 return 0;
727}
728
729/* Similar use of primitive info in the read counterpart */
730static int rapl_write_data_raw(struct rapl_domain *rd,
Zhang Rui33823882019-07-10 21:44:30 +0800731 enum rapl_primitives prim,
732 unsigned long long value)
Jacob Pan2d281d82013-10-17 10:28:35 -0700733{
Jacob Pan2d281d82013-10-17 10:28:35 -0700734 struct rapl_primitive_info *rp = &rpi[prim];
735 int cpu;
Jacob Panf14a1392016-02-24 13:31:36 -0800736 u64 bits;
Zhang Ruibeea8df2019-07-10 21:44:27 +0800737 struct reg_action ra;
Jacob Panf14a1392016-02-24 13:31:36 -0800738 int ret;
Jacob Pan2d281d82013-10-17 10:28:35 -0700739
Jacob Pan323ee642016-02-24 13:31:38 -0800740 cpu = rd->rp->lead_cpu;
Jacob Pan309557f2016-02-24 13:31:37 -0800741 bits = rapl_unit_xlate(rd, rp->unit, value, 1);
Adam Lessnauedbdabc2017-06-01 11:21:50 +0200742 bits <<= rp->shift;
743 bits &= rp->mask;
744
Zhang Ruibeea8df2019-07-10 21:44:27 +0800745 memset(&ra, 0, sizeof(ra));
Jacob Panf14a1392016-02-24 13:31:36 -0800746
Zhang Ruibeea8df2019-07-10 21:44:27 +0800747 ra.reg = rd->regs[rp->id];
748 ra.mask = rp->mask;
749 ra.value = bits;
Jacob Panf14a1392016-02-24 13:31:36 -0800750
Zhang Ruibeea8df2019-07-10 21:44:27 +0800751 ret = rd->rp->priv->write_raw(cpu, &ra);
Jacob Panf14a1392016-02-24 13:31:36 -0800752
753 return ret;
Jacob Pan2d281d82013-10-17 10:28:35 -0700754}
755
Jacob Pan3c2c0842014-11-07 09:29:26 -0800756/*
757 * Raw RAPL data stored in MSRs are in certain scales. We need to
758 * convert them into standard units based on the units reported in
759 * the RAPL unit MSRs. This is specific to CPUs as the method to
760 * calculate units differ on different CPUs.
761 * We convert the units to below format based on CPUs.
762 * i.e.
Jacob Pand474a4d2015-03-13 03:48:56 -0700763 * energy unit: picoJoules : Represented in picoJoules by default
Jacob Pan3c2c0842014-11-07 09:29:26 -0800764 * power unit : microWatts : Represented in milliWatts by default
765 * time unit : microseconds: Represented in seconds by default
766 */
767static int rapl_check_unit_core(struct rapl_package *rp, int cpu)
Jacob Pan2d281d82013-10-17 10:28:35 -0700768{
Zhang Rui1193b162019-07-10 21:44:29 +0800769 struct reg_action ra;
Jacob Pan2d281d82013-10-17 10:28:35 -0700770 u32 value;
771
Zhang Rui1193b162019-07-10 21:44:29 +0800772 ra.reg = rp->priv->reg_unit;
773 ra.mask = ~0;
774 if (rp->priv->read_raw(cpu, &ra)) {
Zhang Ruid978e752019-07-10 21:44:31 +0800775 pr_err("Failed to read power unit REG 0x%llx on CPU %d, exit.\n",
Zhang Rui33823882019-07-10 21:44:30 +0800776 rp->priv->reg_unit, cpu);
Jacob Pan2d281d82013-10-17 10:28:35 -0700777 return -ENODEV;
778 }
779
Zhang Rui1193b162019-07-10 21:44:29 +0800780 value = (ra.value & ENERGY_UNIT_MASK) >> ENERGY_UNIT_OFFSET;
Jacob Pand474a4d2015-03-13 03:48:56 -0700781 rp->energy_unit = ENERGY_UNIT_SCALE * 1000000 / (1 << value);
Jacob Pan2d281d82013-10-17 10:28:35 -0700782
Zhang Rui1193b162019-07-10 21:44:29 +0800783 value = (ra.value & POWER_UNIT_MASK) >> POWER_UNIT_OFFSET;
Jacob Pan3c2c0842014-11-07 09:29:26 -0800784 rp->power_unit = 1000000 / (1 << value);
Jacob Pan2d281d82013-10-17 10:28:35 -0700785
Zhang Rui1193b162019-07-10 21:44:29 +0800786 value = (ra.value & TIME_UNIT_MASK) >> TIME_UNIT_OFFSET;
Jacob Pan3c2c0842014-11-07 09:29:26 -0800787 rp->time_unit = 1000000 / (1 << value);
Jacob Pan2d281d82013-10-17 10:28:35 -0700788
Zhang Rui9ea76122019-05-13 13:58:53 -0400789 pr_debug("Core CPU %s energy=%dpJ, time=%dus, power=%duW\n",
Zhang Rui33823882019-07-10 21:44:30 +0800790 rp->name, rp->energy_unit, rp->time_unit, rp->power_unit);
Jacob Pan2d281d82013-10-17 10:28:35 -0700791
792 return 0;
793}
794
Jacob Pan3c2c0842014-11-07 09:29:26 -0800795static int rapl_check_unit_atom(struct rapl_package *rp, int cpu)
796{
Zhang Rui1193b162019-07-10 21:44:29 +0800797 struct reg_action ra;
Jacob Pan3c2c0842014-11-07 09:29:26 -0800798 u32 value;
799
Zhang Rui1193b162019-07-10 21:44:29 +0800800 ra.reg = rp->priv->reg_unit;
801 ra.mask = ~0;
802 if (rp->priv->read_raw(cpu, &ra)) {
Zhang Ruid978e752019-07-10 21:44:31 +0800803 pr_err("Failed to read power unit REG 0x%llx on CPU %d, exit.\n",
Zhang Rui33823882019-07-10 21:44:30 +0800804 rp->priv->reg_unit, cpu);
Jacob Pan3c2c0842014-11-07 09:29:26 -0800805 return -ENODEV;
806 }
Zhang Rui1193b162019-07-10 21:44:29 +0800807
808 value = (ra.value & ENERGY_UNIT_MASK) >> ENERGY_UNIT_OFFSET;
Jacob Pand474a4d2015-03-13 03:48:56 -0700809 rp->energy_unit = ENERGY_UNIT_SCALE * 1 << value;
Jacob Pan3c2c0842014-11-07 09:29:26 -0800810
Zhang Rui1193b162019-07-10 21:44:29 +0800811 value = (ra.value & POWER_UNIT_MASK) >> POWER_UNIT_OFFSET;
Jacob Pan3c2c0842014-11-07 09:29:26 -0800812 rp->power_unit = (1 << value) * 1000;
813
Zhang Rui1193b162019-07-10 21:44:29 +0800814 value = (ra.value & TIME_UNIT_MASK) >> TIME_UNIT_OFFSET;
Jacob Pan3c2c0842014-11-07 09:29:26 -0800815 rp->time_unit = 1000000 / (1 << value);
816
Zhang Rui9ea76122019-05-13 13:58:53 -0400817 pr_debug("Atom %s energy=%dpJ, time=%dus, power=%duW\n",
Zhang Rui33823882019-07-10 21:44:30 +0800818 rp->name, rp->energy_unit, rp->time_unit, rp->power_unit);
Jacob Pan3c2c0842014-11-07 09:29:26 -0800819
820 return 0;
821}
822
Jacob Panf14a1392016-02-24 13:31:36 -0800823static void power_limit_irq_save_cpu(void *info)
824{
825 u32 l, h = 0;
826 struct rapl_package *rp = (struct rapl_package *)info;
827
828 /* save the state of PLN irq mask bit before disabling it */
829 rdmsr_safe(MSR_IA32_PACKAGE_THERM_INTERRUPT, &l, &h);
830 if (!(rp->power_limit_irq & PACKAGE_PLN_INT_SAVED)) {
831 rp->power_limit_irq = l & PACKAGE_THERM_INT_PLN_ENABLE;
832 rp->power_limit_irq |= PACKAGE_PLN_INT_SAVED;
833 }
834 l &= ~PACKAGE_THERM_INT_PLN_ENABLE;
835 wrmsr_safe(MSR_IA32_PACKAGE_THERM_INTERRUPT, l, h);
836}
837
Jacob Pan2d281d82013-10-17 10:28:35 -0700838/* REVISIT:
839 * When package power limit is set artificially low by RAPL, LVT
840 * thermal interrupt for package power limit should be ignored
841 * since we are not really exceeding the real limit. The intention
842 * is to avoid excessive interrupts while we are trying to save power.
843 * A useful feature might be routing the package_power_limit interrupt
844 * to userspace via eventfd. once we have a usecase, this is simple
845 * to do by adding an atomic notifier.
846 */
847
Jacob Pan309557f2016-02-24 13:31:37 -0800848static void package_power_limit_irq_save(struct rapl_package *rp)
Jacob Pan2d281d82013-10-17 10:28:35 -0700849{
Jacob Pan2d281d82013-10-17 10:28:35 -0700850 if (!boot_cpu_has(X86_FEATURE_PTS) || !boot_cpu_has(X86_FEATURE_PLN))
851 return;
852
Jacob Pan323ee642016-02-24 13:31:38 -0800853 smp_call_function_single(rp->lead_cpu, power_limit_irq_save_cpu, rp, 1);
Jacob Panf14a1392016-02-24 13:31:36 -0800854}
855
Thomas Gleixner58705062016-11-22 21:16:02 +0000856/*
857 * Restore per package power limit interrupt enable state. Called from cpu
858 * hotplug code on package removal.
859 */
860static void package_power_limit_irq_restore(struct rapl_package *rp)
Jacob Panf14a1392016-02-24 13:31:36 -0800861{
Thomas Gleixner58705062016-11-22 21:16:02 +0000862 u32 l, h;
863
864 if (!boot_cpu_has(X86_FEATURE_PTS) || !boot_cpu_has(X86_FEATURE_PLN))
865 return;
866
867 /* irq enable state not saved, nothing to restore */
868 if (!(rp->power_limit_irq & PACKAGE_PLN_INT_SAVED))
869 return;
Jacob Panf14a1392016-02-24 13:31:36 -0800870
871 rdmsr_safe(MSR_IA32_PACKAGE_THERM_INTERRUPT, &l, &h);
872
873 if (rp->power_limit_irq & PACKAGE_THERM_INT_PLN_ENABLE)
874 l |= PACKAGE_THERM_INT_PLN_ENABLE;
875 else
876 l &= ~PACKAGE_THERM_INT_PLN_ENABLE;
877
878 wrmsr_safe(MSR_IA32_PACKAGE_THERM_INTERRUPT, l, h);
Jacob Pan2d281d82013-10-17 10:28:35 -0700879}
880
Jacob Pan3c2c0842014-11-07 09:29:26 -0800881static void set_floor_freq_default(struct rapl_domain *rd, bool mode)
882{
883 int nr_powerlimit = find_nr_power_limit(rd);
884
885 /* always enable clamp such that p-state can go below OS requested
886 * range. power capping priority over guranteed frequency.
887 */
888 rapl_write_data_raw(rd, PL1_CLAMP, mode);
889
890 /* some domains have pl2 */
891 if (nr_powerlimit > 1) {
892 rapl_write_data_raw(rd, PL2_ENABLE, mode);
893 rapl_write_data_raw(rd, PL2_CLAMP, mode);
894 }
895}
896
897static void set_floor_freq_atom(struct rapl_domain *rd, bool enable)
898{
899 static u32 power_ctrl_orig_val;
900 u32 mdata;
901
Ajay Thomas51b63402015-04-30 01:43:23 +0530902 if (!rapl_defaults->floor_freq_reg_addr) {
903 pr_err("Invalid floor frequency config register\n");
904 return;
905 }
906
Jacob Pan3c2c0842014-11-07 09:29:26 -0800907 if (!power_ctrl_orig_val)
Andy Shevchenko4077a382015-11-11 19:59:29 +0200908 iosf_mbi_read(BT_MBI_UNIT_PMC, MBI_CR_READ,
909 rapl_defaults->floor_freq_reg_addr,
910 &power_ctrl_orig_val);
Jacob Pan3c2c0842014-11-07 09:29:26 -0800911 mdata = power_ctrl_orig_val;
912 if (enable) {
913 mdata &= ~(0x7f << 8);
914 mdata |= 1 << 8;
915 }
Andy Shevchenko4077a382015-11-11 19:59:29 +0200916 iosf_mbi_write(BT_MBI_UNIT_PMC, MBI_CR_WRITE,
917 rapl_defaults->floor_freq_reg_addr, mdata);
Jacob Pan3c2c0842014-11-07 09:29:26 -0800918}
919
920static u64 rapl_compute_time_window_core(struct rapl_package *rp, u64 value,
Zhang Rui33823882019-07-10 21:44:30 +0800921 bool to_raw)
Jacob Pan3c2c0842014-11-07 09:29:26 -0800922{
Zhang Rui33823882019-07-10 21:44:30 +0800923 u64 f, y; /* fraction and exp. used for time unit */
Jacob Pan3c2c0842014-11-07 09:29:26 -0800924
925 /*
926 * Special processing based on 2^Y*(1+F/4), refer
927 * to Intel Software Developer's manual Vol.3B: CH 14.9.3.
928 */
929 if (!to_raw) {
930 f = (value & 0x60) >> 5;
931 y = value & 0x1f;
932 value = (1 << y) * (4 + f) * rp->time_unit / 4;
933 } else {
934 do_div(value, rp->time_unit);
935 y = ilog2(value);
936 f = div64_u64(4 * (value - (1 << y)), 1 << y);
937 value = (y & 0x1f) | ((f & 0x3) << 5);
938 }
939 return value;
940}
941
942static u64 rapl_compute_time_window_atom(struct rapl_package *rp, u64 value,
Zhang Rui33823882019-07-10 21:44:30 +0800943 bool to_raw)
Jacob Pan3c2c0842014-11-07 09:29:26 -0800944{
945 /*
946 * Atom time unit encoding is straight forward val * time_unit,
947 * where time_unit is default to 1 sec. Never 0.
948 */
949 if (!to_raw)
950 return (value) ? value *= rp->time_unit : rp->time_unit;
Zhang Rui33823882019-07-10 21:44:30 +0800951
952 value = div64_u64(value, rp->time_unit);
Jacob Pan3c2c0842014-11-07 09:29:26 -0800953
954 return value;
955}
956
Jacob Pan087e9cb2014-11-07 09:29:25 -0800957static const struct rapl_defaults rapl_defaults_core = {
Ajay Thomas51b63402015-04-30 01:43:23 +0530958 .floor_freq_reg_addr = 0,
Jacob Pan3c2c0842014-11-07 09:29:26 -0800959 .check_unit = rapl_check_unit_core,
960 .set_floor_freq = set_floor_freq_default,
961 .compute_time_window = rapl_compute_time_window_core,
Jacob Pan087e9cb2014-11-07 09:29:25 -0800962};
963
Jacob Pand474a4d2015-03-13 03:48:56 -0700964static const struct rapl_defaults rapl_defaults_hsw_server = {
965 .check_unit = rapl_check_unit_core,
966 .set_floor_freq = set_floor_freq_default,
967 .compute_time_window = rapl_compute_time_window_core,
968 .dram_domain_energy_unit = 15300,
969};
970
Zhang Rui2d798d92020-06-29 13:34:50 +0800971static const struct rapl_defaults rapl_defaults_spr_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 .psys_domain_energy_unit = 1000000000,
977};
978
Ajay Thomas51b63402015-04-30 01:43:23 +0530979static const struct rapl_defaults rapl_defaults_byt = {
980 .floor_freq_reg_addr = IOSF_CPU_POWER_BUDGET_CTL_BYT,
Jacob Pan3c2c0842014-11-07 09:29:26 -0800981 .check_unit = rapl_check_unit_atom,
982 .set_floor_freq = set_floor_freq_atom,
983 .compute_time_window = rapl_compute_time_window_atom,
Jacob Pan087e9cb2014-11-07 09:29:25 -0800984};
985
Ajay Thomas51b63402015-04-30 01:43:23 +0530986static const struct rapl_defaults rapl_defaults_tng = {
987 .floor_freq_reg_addr = IOSF_CPU_POWER_BUDGET_CTL_TNG,
988 .check_unit = rapl_check_unit_atom,
989 .set_floor_freq = set_floor_freq_atom,
990 .compute_time_window = rapl_compute_time_window_atom,
991};
992
993static const struct rapl_defaults rapl_defaults_ann = {
994 .floor_freq_reg_addr = 0,
995 .check_unit = rapl_check_unit_atom,
996 .set_floor_freq = NULL,
997 .compute_time_window = rapl_compute_time_window_atom,
998};
999
1000static const struct rapl_defaults rapl_defaults_cht = {
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
Mathias Krauseea85dbc2015-03-25 22:15:52 +01001007static const struct x86_cpu_id rapl_ids[] __initconst = {
Thomas Gleixnerf0722512020-03-20 14:14:03 +01001008 X86_MATCH_INTEL_FAM6_MODEL(SANDYBRIDGE, &rapl_defaults_core),
1009 X86_MATCH_INTEL_FAM6_MODEL(SANDYBRIDGE_X, &rapl_defaults_core),
Dave Hansen0bb04b52016-06-02 17:19:37 -07001010
Thomas Gleixnerf0722512020-03-20 14:14:03 +01001011 X86_MATCH_INTEL_FAM6_MODEL(IVYBRIDGE, &rapl_defaults_core),
1012 X86_MATCH_INTEL_FAM6_MODEL(IVYBRIDGE_X, &rapl_defaults_core),
Dave Hansen0bb04b52016-06-02 17:19:37 -07001013
Thomas Gleixnerf0722512020-03-20 14:14:03 +01001014 X86_MATCH_INTEL_FAM6_MODEL(HASWELL, &rapl_defaults_core),
1015 X86_MATCH_INTEL_FAM6_MODEL(HASWELL_L, &rapl_defaults_core),
1016 X86_MATCH_INTEL_FAM6_MODEL(HASWELL_G, &rapl_defaults_core),
1017 X86_MATCH_INTEL_FAM6_MODEL(HASWELL_X, &rapl_defaults_hsw_server),
Dave Hansen0bb04b52016-06-02 17:19:37 -07001018
Thomas Gleixnerf0722512020-03-20 14:14:03 +01001019 X86_MATCH_INTEL_FAM6_MODEL(BROADWELL, &rapl_defaults_core),
1020 X86_MATCH_INTEL_FAM6_MODEL(BROADWELL_G, &rapl_defaults_core),
1021 X86_MATCH_INTEL_FAM6_MODEL(BROADWELL_D, &rapl_defaults_core),
1022 X86_MATCH_INTEL_FAM6_MODEL(BROADWELL_X, &rapl_defaults_hsw_server),
Dave Hansen0bb04b52016-06-02 17:19:37 -07001023
Thomas Gleixnerf0722512020-03-20 14:14:03 +01001024 X86_MATCH_INTEL_FAM6_MODEL(SKYLAKE, &rapl_defaults_core),
1025 X86_MATCH_INTEL_FAM6_MODEL(SKYLAKE_L, &rapl_defaults_core),
1026 X86_MATCH_INTEL_FAM6_MODEL(SKYLAKE_X, &rapl_defaults_hsw_server),
1027 X86_MATCH_INTEL_FAM6_MODEL(KABYLAKE_L, &rapl_defaults_core),
1028 X86_MATCH_INTEL_FAM6_MODEL(KABYLAKE, &rapl_defaults_core),
1029 X86_MATCH_INTEL_FAM6_MODEL(CANNONLAKE_L, &rapl_defaults_core),
1030 X86_MATCH_INTEL_FAM6_MODEL(ICELAKE_L, &rapl_defaults_core),
1031 X86_MATCH_INTEL_FAM6_MODEL(ICELAKE, &rapl_defaults_core),
1032 X86_MATCH_INTEL_FAM6_MODEL(ICELAKE_NNPI, &rapl_defaults_core),
1033 X86_MATCH_INTEL_FAM6_MODEL(ICELAKE_X, &rapl_defaults_hsw_server),
1034 X86_MATCH_INTEL_FAM6_MODEL(ICELAKE_D, &rapl_defaults_hsw_server),
1035 X86_MATCH_INTEL_FAM6_MODEL(COMETLAKE_L, &rapl_defaults_core),
1036 X86_MATCH_INTEL_FAM6_MODEL(COMETLAKE, &rapl_defaults_core),
1037 X86_MATCH_INTEL_FAM6_MODEL(TIGERLAKE_L, &rapl_defaults_core),
Zhang Rui57a2fb02020-09-10 15:48:58 +08001038 X86_MATCH_INTEL_FAM6_MODEL(TIGERLAKE, &rapl_defaults_core),
Zhang Rui64e5f362020-09-10 15:49:11 +08001039 X86_MATCH_INTEL_FAM6_MODEL(ROCKETLAKE, &rapl_defaults_core),
Zhang Ruiba92a422020-09-10 15:49:21 +08001040 X86_MATCH_INTEL_FAM6_MODEL(ALDERLAKE, &rapl_defaults_core),
Zhang Rui2d798d92020-06-29 13:34:50 +08001041 X86_MATCH_INTEL_FAM6_MODEL(SAPPHIRERAPIDS_X, &rapl_defaults_spr_server),
Ricardo Nerie1c2d962020-08-21 11:48:10 -07001042 X86_MATCH_INTEL_FAM6_MODEL(LAKEFIELD, &rapl_defaults_core),
Dave Hansen0bb04b52016-06-02 17:19:37 -07001043
Thomas Gleixnerf0722512020-03-20 14:14:03 +01001044 X86_MATCH_INTEL_FAM6_MODEL(ATOM_SILVERMONT, &rapl_defaults_byt),
1045 X86_MATCH_INTEL_FAM6_MODEL(ATOM_AIRMONT, &rapl_defaults_cht),
1046 X86_MATCH_INTEL_FAM6_MODEL(ATOM_SILVERMONT_MID, &rapl_defaults_tng),
1047 X86_MATCH_INTEL_FAM6_MODEL(ATOM_AIRMONT_MID, &rapl_defaults_ann),
1048 X86_MATCH_INTEL_FAM6_MODEL(ATOM_GOLDMONT, &rapl_defaults_core),
1049 X86_MATCH_INTEL_FAM6_MODEL(ATOM_GOLDMONT_PLUS, &rapl_defaults_core),
1050 X86_MATCH_INTEL_FAM6_MODEL(ATOM_GOLDMONT_D, &rapl_defaults_core),
Jacob Pan33c98002020-05-15 15:30:41 +08001051 X86_MATCH_INTEL_FAM6_MODEL(ATOM_TREMONT, &rapl_defaults_core),
Thomas Gleixnerf0722512020-03-20 14:14:03 +01001052 X86_MATCH_INTEL_FAM6_MODEL(ATOM_TREMONT_D, &rapl_defaults_core),
1053 X86_MATCH_INTEL_FAM6_MODEL(ATOM_TREMONT_L, &rapl_defaults_core),
Dave Hansen0bb04b52016-06-02 17:19:37 -07001054
Thomas Gleixnerf0722512020-03-20 14:14:03 +01001055 X86_MATCH_INTEL_FAM6_MODEL(XEON_PHI_KNL, &rapl_defaults_hsw_server),
1056 X86_MATCH_INTEL_FAM6_MODEL(XEON_PHI_KNM, &rapl_defaults_hsw_server),
Jacob Pan2d281d82013-10-17 10:28:35 -07001057 {}
1058};
1059MODULE_DEVICE_TABLE(x86cpu, rapl_ids);
1060
Thomas Gleixnerbed5ab62016-11-22 21:15:58 +00001061/* Read once for all raw primitive data for domains */
1062static void rapl_update_domain_data(struct rapl_package *rp)
Jacob Pan2d281d82013-10-17 10:28:35 -07001063{
1064 int dmn, prim;
1065 u64 val;
Jacob Pan2d281d82013-10-17 10:28:35 -07001066
Thomas Gleixnerbed5ab62016-11-22 21:15:58 +00001067 for (dmn = 0; dmn < rp->nr_domains; dmn++) {
Zhang Rui9ea76122019-05-13 13:58:53 -04001068 pr_debug("update %s domain %s data\n", rp->name,
Thomas Gleixnerbed5ab62016-11-22 21:15:58 +00001069 rp->domains[dmn].name);
1070 /* exclude non-raw primitives */
1071 for (prim = 0; prim < NR_RAW_PRIMITIVES; prim++) {
1072 if (!rapl_read_data_raw(&rp->domains[dmn], prim,
1073 rpi[prim].unit, &val))
Zhang Rui33823882019-07-10 21:44:30 +08001074 rp->domains[dmn].rdd.primitives[prim] = val;
Jacob Pan2d281d82013-10-17 10:28:35 -07001075 }
1076 }
1077
1078}
1079
Jacob Pan2d281d82013-10-17 10:28:35 -07001080static int rapl_package_register_powercap(struct rapl_package *rp)
1081{
1082 struct rapl_domain *rd;
Jacob Pan2d281d82013-10-17 10:28:35 -07001083 struct powercap_zone *power_zone = NULL;
Luis de Bethencourt01857cf2018-01-17 10:30:34 +00001084 int nr_pl, ret;
Thomas Gleixnerbed5ab62016-11-22 21:15:58 +00001085
1086 /* Update the domain data of the new package */
1087 rapl_update_domain_data(rp);
Jacob Pan2d281d82013-10-17 10:28:35 -07001088
Zhang Rui33823882019-07-10 21:44:30 +08001089 /* first we register package domain as the parent zone */
Jacob Pan2d281d82013-10-17 10:28:35 -07001090 for (rd = rp->domains; rd < rp->domains + rp->nr_domains; rd++) {
1091 if (rd->id == RAPL_DOMAIN_PACKAGE) {
1092 nr_pl = find_nr_power_limit(rd);
Zhang Rui9ea76122019-05-13 13:58:53 -04001093 pr_debug("register package domain %s\n", rp->name);
Jacob Pan2d281d82013-10-17 10:28:35 -07001094 power_zone = powercap_register_zone(&rd->power_zone,
Zhang Rui33823882019-07-10 21:44:30 +08001095 rp->priv->control_type, rp->name,
1096 NULL, &zone_ops[rd->id], nr_pl,
1097 &constraint_ops);
Jacob Pan2d281d82013-10-17 10:28:35 -07001098 if (IS_ERR(power_zone)) {
Zhang Rui9ea76122019-05-13 13:58:53 -04001099 pr_debug("failed to register power zone %s\n",
Zhang Rui33823882019-07-10 21:44:30 +08001100 rp->name);
Thomas Gleixnerbed5ab62016-11-22 21:15:58 +00001101 return PTR_ERR(power_zone);
Jacob Pan2d281d82013-10-17 10:28:35 -07001102 }
1103 /* track parent zone in per package/socket data */
1104 rp->power_zone = power_zone;
1105 /* done, only one package domain per socket */
1106 break;
1107 }
1108 }
1109 if (!power_zone) {
1110 pr_err("no package domain found, unknown topology!\n");
Thomas Gleixnerbed5ab62016-11-22 21:15:58 +00001111 return -ENODEV;
Jacob Pan2d281d82013-10-17 10:28:35 -07001112 }
Zhang Rui33823882019-07-10 21:44:30 +08001113 /* now register domains as children of the socket/package */
Jacob Pan2d281d82013-10-17 10:28:35 -07001114 for (rd = rp->domains; rd < rp->domains + rp->nr_domains; rd++) {
1115 if (rd->id == RAPL_DOMAIN_PACKAGE)
1116 continue;
1117 /* number of power limits per domain varies */
1118 nr_pl = find_nr_power_limit(rd);
1119 power_zone = powercap_register_zone(&rd->power_zone,
Zhang Rui33823882019-07-10 21:44:30 +08001120 rp->priv->control_type,
1121 rd->name, rp->power_zone,
1122 &zone_ops[rd->id], nr_pl,
1123 &constraint_ops);
Jacob Pan2d281d82013-10-17 10:28:35 -07001124
1125 if (IS_ERR(power_zone)) {
Zhang Rui9ea76122019-05-13 13:58:53 -04001126 pr_debug("failed to register power_zone, %s:%s\n",
Zhang Rui33823882019-07-10 21:44:30 +08001127 rp->name, rd->name);
Jacob Pan2d281d82013-10-17 10:28:35 -07001128 ret = PTR_ERR(power_zone);
1129 goto err_cleanup;
1130 }
1131 }
Thomas Gleixnerbed5ab62016-11-22 21:15:58 +00001132 return 0;
Jacob Pan2d281d82013-10-17 10:28:35 -07001133
Jacob Pan2d281d82013-10-17 10:28:35 -07001134err_cleanup:
Thomas Gleixner58705062016-11-22 21:16:02 +00001135 /*
1136 * Clean up previously initialized domains within the package if we
Jacob Pan2d281d82013-10-17 10:28:35 -07001137 * failed after the first domain setup.
1138 */
1139 while (--rd >= rp->domains) {
Zhang Rui9ea76122019-05-13 13:58:53 -04001140 pr_debug("unregister %s domain %s\n", rp->name, rd->name);
Zhang Rui33823882019-07-10 21:44:30 +08001141 powercap_unregister_zone(rp->priv->control_type,
1142 &rd->power_zone);
Jacob Pan2d281d82013-10-17 10:28:35 -07001143 }
1144
1145 return ret;
1146}
1147
Zhang Rui33823882019-07-10 21:44:30 +08001148int rapl_add_platform_domain(struct rapl_if_priv *priv)
Srinivas Pandruvada3521ba12016-04-17 15:03:01 -07001149{
1150 struct rapl_domain *rd;
1151 struct powercap_zone *power_zone;
Zhang Rui8a006762019-07-10 21:44:28 +08001152 struct reg_action ra;
1153 int ret;
Srinivas Pandruvada3521ba12016-04-17 15:03:01 -07001154
Zhang Rui8a006762019-07-10 21:44:28 +08001155 ra.reg = priv->regs[RAPL_DOMAIN_PLATFORM][RAPL_DOMAIN_REG_STATUS];
1156 ra.mask = ~0;
1157 ret = priv->read_raw(0, &ra);
1158 if (ret || !ra.value)
Srinivas Pandruvada3521ba12016-04-17 15:03:01 -07001159 return -ENODEV;
1160
Zhang Rui8a006762019-07-10 21:44:28 +08001161 ra.reg = priv->regs[RAPL_DOMAIN_PLATFORM][RAPL_DOMAIN_REG_LIMIT];
1162 ra.mask = ~0;
1163 ret = priv->read_raw(0, &ra);
1164 if (ret || !ra.value)
Srinivas Pandruvada3521ba12016-04-17 15:03:01 -07001165 return -ENODEV;
1166
1167 rd = kzalloc(sizeof(*rd), GFP_KERNEL);
1168 if (!rd)
1169 return -ENOMEM;
1170
1171 rd->name = rapl_domain_names[RAPL_DOMAIN_PLATFORM];
1172 rd->id = RAPL_DOMAIN_PLATFORM;
Zhang Rui33823882019-07-10 21:44:30 +08001173 rd->regs[RAPL_DOMAIN_REG_LIMIT] =
1174 priv->regs[RAPL_DOMAIN_PLATFORM][RAPL_DOMAIN_REG_LIMIT];
1175 rd->regs[RAPL_DOMAIN_REG_STATUS] =
1176 priv->regs[RAPL_DOMAIN_PLATFORM][RAPL_DOMAIN_REG_STATUS];
Srinivas Pandruvada3521ba12016-04-17 15:03:01 -07001177 rd->rpl[0].prim_id = PL1_ENABLE;
1178 rd->rpl[0].name = pl1_name;
1179 rd->rpl[1].prim_id = PL2_ENABLE;
1180 rd->rpl[1].name = pl2_name;
Zhang Rui8a006762019-07-10 21:44:28 +08001181 rd->rp = rapl_find_package_domain(0, priv);
Srinivas Pandruvada3521ba12016-04-17 15:03:01 -07001182
Zhang Rui8a006762019-07-10 21:44:28 +08001183 power_zone = powercap_register_zone(&rd->power_zone, priv->control_type,
Srinivas Pandruvada3521ba12016-04-17 15:03:01 -07001184 "psys", NULL,
1185 &zone_ops[RAPL_DOMAIN_PLATFORM],
1186 2, &constraint_ops);
1187
1188 if (IS_ERR(power_zone)) {
1189 kfree(rd);
1190 return PTR_ERR(power_zone);
1191 }
1192
Zhang Rui8a006762019-07-10 21:44:28 +08001193 priv->platform_rapl_domain = rd;
Srinivas Pandruvada3521ba12016-04-17 15:03:01 -07001194
1195 return 0;
1196}
Zhang Rui33823882019-07-10 21:44:30 +08001197EXPORT_SYMBOL_GPL(rapl_add_platform_domain);
Srinivas Pandruvada3521ba12016-04-17 15:03:01 -07001198
Zhang Rui33823882019-07-10 21:44:30 +08001199void rapl_remove_platform_domain(struct rapl_if_priv *priv)
Jacob Pan2d281d82013-10-17 10:28:35 -07001200{
Zhang Rui8a006762019-07-10 21:44:28 +08001201 if (priv->platform_rapl_domain) {
1202 powercap_unregister_zone(priv->control_type,
Zhang Rui33823882019-07-10 21:44:30 +08001203 &priv->platform_rapl_domain->power_zone);
Zhang Rui8a006762019-07-10 21:44:28 +08001204 kfree(priv->platform_rapl_domain);
Jacob Pan2d281d82013-10-17 10:28:35 -07001205 }
Jacob Pan2d281d82013-10-17 10:28:35 -07001206}
Zhang Rui33823882019-07-10 21:44:30 +08001207EXPORT_SYMBOL_GPL(rapl_remove_platform_domain);
Jacob Pan2d281d82013-10-17 10:28:35 -07001208
Zhang Rui7fde2712019-07-10 21:44:26 +08001209static int rapl_check_domain(int cpu, int domain, struct rapl_package *rp)
Jacob Pan2d281d82013-10-17 10:28:35 -07001210{
Zhang Rui1193b162019-07-10 21:44:29 +08001211 struct reg_action ra;
Jacob Pan2d281d82013-10-17 10:28:35 -07001212
1213 switch (domain) {
1214 case RAPL_DOMAIN_PACKAGE:
Jacob Pan2d281d82013-10-17 10:28:35 -07001215 case RAPL_DOMAIN_PP0:
Jacob Pan2d281d82013-10-17 10:28:35 -07001216 case RAPL_DOMAIN_PP1:
Jacob Pan2d281d82013-10-17 10:28:35 -07001217 case RAPL_DOMAIN_DRAM:
Zhang Rui1193b162019-07-10 21:44:29 +08001218 ra.reg = rp->priv->regs[domain][RAPL_DOMAIN_REG_STATUS];
Jacob Pan2d281d82013-10-17 10:28:35 -07001219 break;
Srinivas Pandruvada3521ba12016-04-17 15:03:01 -07001220 case RAPL_DOMAIN_PLATFORM:
1221 /* PSYS(PLATFORM) is not a CPU domain, so avoid printng error */
1222 return -EINVAL;
Jacob Pan2d281d82013-10-17 10:28:35 -07001223 default:
1224 pr_err("invalid domain id %d\n", domain);
1225 return -EINVAL;
1226 }
Jacob Pan9d31c672014-04-29 15:33:06 -07001227 /* make sure domain counters are available and contains non-zero
1228 * values, otherwise skip it.
1229 */
Zhang Rui1193b162019-07-10 21:44:29 +08001230
Zhang Rui7a57e9f2020-10-13 15:42:40 +08001231 ra.mask = ENERGY_STATUS_MASK;
Zhang Rui1193b162019-07-10 21:44:29 +08001232 if (rp->priv->read_raw(cpu, &ra) || !ra.value)
Jacob Pan2d281d82013-10-17 10:28:35 -07001233 return -ENODEV;
1234
Jacob Pan9d31c672014-04-29 15:33:06 -07001235 return 0;
Jacob Pan2d281d82013-10-17 10:28:35 -07001236}
1237
Jacob Pane1399ba2016-05-31 13:41:29 -07001238/*
1239 * Check if power limits are available. Two cases when they are not available:
1240 * 1. Locked by BIOS, in this case we still provide read-only access so that
1241 * users can see what limit is set by the BIOS.
1242 * 2. Some CPUs make some domains monitoring only which means PLx MSRs may not
Zhang Rui33823882019-07-10 21:44:30 +08001243 * exist at all. In this case, we do not show the constraints in powercap.
Jacob Pane1399ba2016-05-31 13:41:29 -07001244 *
1245 * Called after domains are detected and initialized.
1246 */
1247static void rapl_detect_powerlimit(struct rapl_domain *rd)
1248{
1249 u64 val64;
1250 int i;
1251
1252 /* check if the domain is locked by BIOS, ignore if MSR doesn't exist */
1253 if (!rapl_read_data_raw(rd, FW_LOCK, false, &val64)) {
1254 if (val64) {
Zhang Rui9ea76122019-05-13 13:58:53 -04001255 pr_info("RAPL %s domain %s locked by BIOS\n",
1256 rd->rp->name, rd->name);
Jacob Pane1399ba2016-05-31 13:41:29 -07001257 rd->state |= DOMAIN_STATE_BIOS_LOCKED;
1258 }
1259 }
Zhang Rui33823882019-07-10 21:44:30 +08001260 /* check if power limit MSR exists, otherwise domain is monitoring only */
Jacob Pane1399ba2016-05-31 13:41:29 -07001261 for (i = 0; i < NR_POWER_LIMITS; i++) {
1262 int prim = rd->rpl[i].prim_id;
Zhang Rui33823882019-07-10 21:44:30 +08001263
Jacob Pane1399ba2016-05-31 13:41:29 -07001264 if (rapl_read_data_raw(rd, prim, false, &val64))
1265 rd->rpl[i].name = NULL;
1266 }
1267}
1268
Jacob Pan2d281d82013-10-17 10:28:35 -07001269/* Detect active and valid domains for the given CPU, caller must
1270 * ensure the CPU belongs to the targeted package and CPU hotlug is disabled.
1271 */
1272static int rapl_detect_domains(struct rapl_package *rp, int cpu)
1273{
Jacob Pan2d281d82013-10-17 10:28:35 -07001274 struct rapl_domain *rd;
Thomas Gleixner58705062016-11-22 21:16:02 +00001275 int i;
Jacob Pan2d281d82013-10-17 10:28:35 -07001276
1277 for (i = 0; i < RAPL_DOMAIN_MAX; i++) {
1278 /* use physical package id to read counters */
Zhang Rui7fde2712019-07-10 21:44:26 +08001279 if (!rapl_check_domain(cpu, i, rp)) {
Jacob Pan2d281d82013-10-17 10:28:35 -07001280 rp->domain_map |= 1 << i;
Jacob Panfcdf1792014-09-02 02:55:21 -07001281 pr_info("Found RAPL domain %s\n", rapl_domain_names[i]);
1282 }
Jacob Pan2d281d82013-10-17 10:28:35 -07001283 }
Zhang Rui33823882019-07-10 21:44:30 +08001284 rp->nr_domains = bitmap_weight(&rp->domain_map, RAPL_DOMAIN_MAX);
Jacob Pan2d281d82013-10-17 10:28:35 -07001285 if (!rp->nr_domains) {
Zhang Rui9ea76122019-05-13 13:58:53 -04001286 pr_debug("no valid rapl domains found in %s\n", rp->name);
Thomas Gleixner58705062016-11-22 21:16:02 +00001287 return -ENODEV;
Jacob Pan2d281d82013-10-17 10:28:35 -07001288 }
Zhang Rui9ea76122019-05-13 13:58:53 -04001289 pr_debug("found %d domains on %s\n", rp->nr_domains, rp->name);
Jacob Pan2d281d82013-10-17 10:28:35 -07001290
1291 rp->domains = kcalloc(rp->nr_domains + 1, sizeof(struct rapl_domain),
Zhang Rui33823882019-07-10 21:44:30 +08001292 GFP_KERNEL);
Thomas Gleixner58705062016-11-22 21:16:02 +00001293 if (!rp->domains)
1294 return -ENOMEM;
1295
Jacob Pan2d281d82013-10-17 10:28:35 -07001296 rapl_init_domains(rp);
1297
Jacob Pane1399ba2016-05-31 13:41:29 -07001298 for (rd = rp->domains; rd < rp->domains + rp->nr_domains; rd++)
1299 rapl_detect_powerlimit(rd);
1300
Jacob Pan2d281d82013-10-17 10:28:35 -07001301 return 0;
1302}
1303
1304/* called from CPU hotplug notifier, hotplug lock held */
Zhang Rui33823882019-07-10 21:44:30 +08001305void rapl_remove_package(struct rapl_package *rp)
Jacob Pan2d281d82013-10-17 10:28:35 -07001306{
1307 struct rapl_domain *rd, *rd_package = NULL;
1308
Thomas Gleixner58705062016-11-22 21:16:02 +00001309 package_power_limit_irq_restore(rp);
1310
Jacob Pan2d281d82013-10-17 10:28:35 -07001311 for (rd = rp->domains; rd < rp->domains + rp->nr_domains; rd++) {
Thomas Gleixner58705062016-11-22 21:16:02 +00001312 rapl_write_data_raw(rd, PL1_ENABLE, 0);
1313 rapl_write_data_raw(rd, PL1_CLAMP, 0);
1314 if (find_nr_power_limit(rd) > 1) {
1315 rapl_write_data_raw(rd, PL2_ENABLE, 0);
1316 rapl_write_data_raw(rd, PL2_CLAMP, 0);
Sumeet Pawnikar8365a892020-07-16 23:14:55 +05301317 rapl_write_data_raw(rd, PL4_ENABLE, 0);
Thomas Gleixner58705062016-11-22 21:16:02 +00001318 }
Jacob Pan2d281d82013-10-17 10:28:35 -07001319 if (rd->id == RAPL_DOMAIN_PACKAGE) {
1320 rd_package = rd;
1321 continue;
1322 }
Zhang Rui9ea76122019-05-13 13:58:53 -04001323 pr_debug("remove package, undo power limit on %s: %s\n",
1324 rp->name, rd->name);
Zhang Rui33823882019-07-10 21:44:30 +08001325 powercap_unregister_zone(rp->priv->control_type,
1326 &rd->power_zone);
Jacob Pan2d281d82013-10-17 10:28:35 -07001327 }
1328 /* do parent zone last */
Zhang Rui33823882019-07-10 21:44:30 +08001329 powercap_unregister_zone(rp->priv->control_type,
1330 &rd_package->power_zone);
Jacob Pan2d281d82013-10-17 10:28:35 -07001331 list_del(&rp->plist);
1332 kfree(rp);
1333}
Zhang Rui33823882019-07-10 21:44:30 +08001334EXPORT_SYMBOL_GPL(rapl_remove_package);
1335
1336/* caller to ensure CPU hotplug lock is held */
1337struct rapl_package *rapl_find_package_domain(int cpu, struct rapl_if_priv *priv)
1338{
1339 int id = topology_logical_die_id(cpu);
1340 struct rapl_package *rp;
1341
1342 list_for_each_entry(rp, &rapl_packages, plist) {
1343 if (rp->id == id
1344 && rp->priv->control_type == priv->control_type)
1345 return rp;
1346 }
1347
1348 return NULL;
1349}
1350EXPORT_SYMBOL_GPL(rapl_find_package_domain);
Jacob Pan2d281d82013-10-17 10:28:35 -07001351
1352/* called from CPU hotplug notifier, hotplug lock held */
Zhang Rui33823882019-07-10 21:44:30 +08001353struct rapl_package *rapl_add_package(int cpu, struct rapl_if_priv *priv)
Jacob Pan2d281d82013-10-17 10:28:35 -07001354{
Zhang Rui32fb4802019-05-13 13:58:51 -04001355 int id = topology_logical_die_id(cpu);
Jacob Pan2d281d82013-10-17 10:28:35 -07001356 struct rapl_package *rp;
Zhang Rui9ea76122019-05-13 13:58:53 -04001357 struct cpuinfo_x86 *c = &cpu_data(cpu);
Thomas Gleixnerb4005e92016-11-22 21:16:05 +00001358 int ret;
Jacob Pan2d281d82013-10-17 10:28:35 -07001359
Harry Pan3aa3c582019-12-30 22:36:56 +08001360 if (!rapl_defaults)
1361 return ERR_PTR(-ENODEV);
1362
Jacob Pan2d281d82013-10-17 10:28:35 -07001363 rp = kzalloc(sizeof(struct rapl_package), GFP_KERNEL);
1364 if (!rp)
Thomas Gleixnerb4005e92016-11-22 21:16:05 +00001365 return ERR_PTR(-ENOMEM);
Jacob Pan2d281d82013-10-17 10:28:35 -07001366
1367 /* add the new package to the list */
Zhang Ruiaadf7b32019-05-13 13:58:50 -04001368 rp->id = id;
Jacob Pan323ee642016-02-24 13:31:38 -08001369 rp->lead_cpu = cpu;
Zhang Rui7ebf8ef2019-07-10 21:44:25 +08001370 rp->priv = priv;
Jacob Pan323ee642016-02-24 13:31:38 -08001371
Zhang Rui9ea76122019-05-13 13:58:53 -04001372 if (topology_max_die_per_package() > 1)
1373 snprintf(rp->name, PACKAGE_DOMAIN_NAME_LENGTH,
Zhang Rui33823882019-07-10 21:44:30 +08001374 "package-%d-die-%d", c->phys_proc_id, c->cpu_die_id);
Zhang Rui9ea76122019-05-13 13:58:53 -04001375 else
1376 snprintf(rp->name, PACKAGE_DOMAIN_NAME_LENGTH, "package-%d",
Zhang Rui33823882019-07-10 21:44:30 +08001377 c->phys_proc_id);
Zhang Rui9ea76122019-05-13 13:58:53 -04001378
Jacob Pan2d281d82013-10-17 10:28:35 -07001379 /* check if the package contains valid domains */
Zhang Rui33823882019-07-10 21:44:30 +08001380 if (rapl_detect_domains(rp, cpu) || rapl_defaults->check_unit(rp, cpu)) {
Jacob Pan2d281d82013-10-17 10:28:35 -07001381 ret = -ENODEV;
1382 goto err_free_package;
1383 }
Thomas Gleixnera74f4362016-11-22 21:15:59 +00001384 ret = rapl_package_register_powercap(rp);
1385 if (!ret) {
Jacob Pan2d281d82013-10-17 10:28:35 -07001386 INIT_LIST_HEAD(&rp->plist);
1387 list_add(&rp->plist, &rapl_packages);
Thomas Gleixnerb4005e92016-11-22 21:16:05 +00001388 return rp;
Jacob Pan2d281d82013-10-17 10:28:35 -07001389 }
1390
1391err_free_package:
1392 kfree(rp->domains);
1393 kfree(rp);
Thomas Gleixnerb4005e92016-11-22 21:16:05 +00001394 return ERR_PTR(ret);
Jacob Pan2d281d82013-10-17 10:28:35 -07001395}
Zhang Rui33823882019-07-10 21:44:30 +08001396EXPORT_SYMBOL_GPL(rapl_add_package);
Jacob Pan2d281d82013-10-17 10:28:35 -07001397
Zhen Han52b36722018-01-10 08:38:23 +08001398static void power_limit_state_save(void)
1399{
1400 struct rapl_package *rp;
1401 struct rapl_domain *rd;
1402 int nr_pl, ret, i;
1403
1404 get_online_cpus();
1405 list_for_each_entry(rp, &rapl_packages, plist) {
1406 if (!rp->power_zone)
1407 continue;
1408 rd = power_zone_to_rapl_domain(rp->power_zone);
1409 nr_pl = find_nr_power_limit(rd);
1410 for (i = 0; i < nr_pl; i++) {
1411 switch (rd->rpl[i].prim_id) {
1412 case PL1_ENABLE:
1413 ret = rapl_read_data_raw(rd,
Zhang Rui33823882019-07-10 21:44:30 +08001414 POWER_LIMIT1, true,
1415 &rd->rpl[i].last_power_limit);
Zhen Han52b36722018-01-10 08:38:23 +08001416 if (ret)
1417 rd->rpl[i].last_power_limit = 0;
1418 break;
1419 case PL2_ENABLE:
1420 ret = rapl_read_data_raw(rd,
Zhang Rui33823882019-07-10 21:44:30 +08001421 POWER_LIMIT2, true,
1422 &rd->rpl[i].last_power_limit);
Zhen Han52b36722018-01-10 08:38:23 +08001423 if (ret)
1424 rd->rpl[i].last_power_limit = 0;
1425 break;
Sumeet Pawnikar8365a892020-07-16 23:14:55 +05301426 case PL4_ENABLE:
1427 ret = rapl_read_data_raw(rd,
1428 POWER_LIMIT4, true,
1429 &rd->rpl[i].last_power_limit);
1430 if (ret)
1431 rd->rpl[i].last_power_limit = 0;
1432 break;
Zhen Han52b36722018-01-10 08:38:23 +08001433 }
1434 }
1435 }
1436 put_online_cpus();
1437}
1438
1439static void power_limit_state_restore(void)
1440{
1441 struct rapl_package *rp;
1442 struct rapl_domain *rd;
1443 int nr_pl, i;
1444
1445 get_online_cpus();
1446 list_for_each_entry(rp, &rapl_packages, plist) {
1447 if (!rp->power_zone)
1448 continue;
1449 rd = power_zone_to_rapl_domain(rp->power_zone);
1450 nr_pl = find_nr_power_limit(rd);
1451 for (i = 0; i < nr_pl; i++) {
1452 switch (rd->rpl[i].prim_id) {
1453 case PL1_ENABLE:
1454 if (rd->rpl[i].last_power_limit)
Zhang Rui33823882019-07-10 21:44:30 +08001455 rapl_write_data_raw(rd, POWER_LIMIT1,
1456 rd->rpl[i].last_power_limit);
Zhen Han52b36722018-01-10 08:38:23 +08001457 break;
1458 case PL2_ENABLE:
1459 if (rd->rpl[i].last_power_limit)
Zhang Rui33823882019-07-10 21:44:30 +08001460 rapl_write_data_raw(rd, POWER_LIMIT2,
1461 rd->rpl[i].last_power_limit);
Zhen Han52b36722018-01-10 08:38:23 +08001462 break;
Sumeet Pawnikar8365a892020-07-16 23:14:55 +05301463 case PL4_ENABLE:
1464 if (rd->rpl[i].last_power_limit)
1465 rapl_write_data_raw(rd, POWER_LIMIT4,
1466 rd->rpl[i].last_power_limit);
1467 break;
Zhen Han52b36722018-01-10 08:38:23 +08001468 }
1469 }
1470 }
1471 put_online_cpus();
1472}
1473
1474static int rapl_pm_callback(struct notifier_block *nb,
Zhang Rui33823882019-07-10 21:44:30 +08001475 unsigned long mode, void *_unused)
Zhen Han52b36722018-01-10 08:38:23 +08001476{
1477 switch (mode) {
1478 case PM_SUSPEND_PREPARE:
1479 power_limit_state_save();
1480 break;
1481 case PM_POST_SUSPEND:
1482 power_limit_state_restore();
1483 break;
1484 }
1485 return NOTIFY_OK;
1486}
1487
1488static struct notifier_block rapl_pm_notifier = {
1489 .notifier_call = rapl_pm_callback,
1490};
1491
Zhang Ruiabcfaeb2019-07-10 21:44:34 +08001492static struct platform_device *rapl_msr_platdev;
1493
1494static int __init rapl_init(void)
Jacob Pan2d281d82013-10-17 10:28:35 -07001495{
Jacob Pan087e9cb2014-11-07 09:29:25 -08001496 const struct x86_cpu_id *id;
Thomas Gleixner58705062016-11-22 21:16:02 +00001497 int ret;
Jacob Pan2d281d82013-10-17 10:28:35 -07001498
Jacob Pan087e9cb2014-11-07 09:29:25 -08001499 id = x86_match_cpu(rapl_ids);
1500 if (!id) {
Jacob Pan2d281d82013-10-17 10:28:35 -07001501 pr_err("driver does not support CPU family %d model %d\n",
Zhang Rui33823882019-07-10 21:44:30 +08001502 boot_cpu_data.x86, boot_cpu_data.x86_model);
Jacob Pan2d281d82013-10-17 10:28:35 -07001503
1504 return -ENODEV;
1505 }
Srivatsa S. Bhat009f2252014-03-11 02:09:26 +05301506
Jacob Pan087e9cb2014-11-07 09:29:25 -08001507 rapl_defaults = (struct rapl_defaults *)id->driver_data;
1508
Zhen Han52b36722018-01-10 08:38:23 +08001509 ret = register_pm_notifier(&rapl_pm_notifier);
Zhang Ruiabcfaeb2019-07-10 21:44:34 +08001510 if (ret)
1511 return ret;
Zhen Han52b36722018-01-10 08:38:23 +08001512
Zhang Ruiabcfaeb2019-07-10 21:44:34 +08001513 rapl_msr_platdev = platform_device_alloc("intel_rapl_msr", 0);
1514 if (!rapl_msr_platdev) {
1515 ret = -ENOMEM;
1516 goto end;
1517 }
1518
1519 ret = platform_device_add(rapl_msr_platdev);
1520 if (ret)
1521 platform_device_put(rapl_msr_platdev);
1522
1523end:
1524 if (ret)
1525 unregister_pm_notifier(&rapl_pm_notifier);
1526
1527 return ret;
Jacob Pan2d281d82013-10-17 10:28:35 -07001528}
1529
Zhang Ruiabcfaeb2019-07-10 21:44:34 +08001530static void __exit rapl_exit(void)
Jacob Pan2d281d82013-10-17 10:28:35 -07001531{
Zhang Ruiabcfaeb2019-07-10 21:44:34 +08001532 platform_device_unregister(rapl_msr_platdev);
Zhen Han52b36722018-01-10 08:38:23 +08001533 unregister_pm_notifier(&rapl_pm_notifier);
Jacob Pan2d281d82013-10-17 10:28:35 -07001534}
1535
Zhang Ruif76cb062019-07-19 23:25:14 +08001536fs_initcall(rapl_init);
Zhang Ruiabcfaeb2019-07-10 21:44:34 +08001537module_exit(rapl_exit);
1538
Zhang Rui33823882019-07-10 21:44:30 +08001539MODULE_DESCRIPTION("Intel Runtime Average Power Limit (RAPL) common code");
Jacob Pan2d281d82013-10-17 10:28:35 -07001540MODULE_AUTHOR("Jacob Pan <jacob.jun.pan@intel.com>");
1541MODULE_LICENSE("GPL v2");