blob: 61a63a16b5e7d068f375b7af6a3c3f46f0e61c2e [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
42#define TIME_WINDOW1_MASK (0x7FULL<<17)
43#define TIME_WINDOW2_MASK (0x7FULL<<49)
44
45#define POWER_UNIT_OFFSET 0
46#define POWER_UNIT_MASK 0x0F
47
48#define ENERGY_UNIT_OFFSET 0x08
49#define ENERGY_UNIT_MASK 0x1F00
50
51#define TIME_UNIT_OFFSET 0x10
52#define TIME_UNIT_MASK 0xF0000
53
54#define POWER_INFO_MAX_MASK (0x7fffULL<<32)
55#define POWER_INFO_MIN_MASK (0x7fffULL<<16)
56#define POWER_INFO_MAX_TIME_WIN_MASK (0x3fULL<<48)
57#define POWER_INFO_THERMAL_SPEC_MASK 0x7fff
58
59#define PERF_STATUS_THROTTLE_TIME_MASK 0xffffffff
60#define PP_POLICY_MASK 0x1F
61
62/* Non HW constants */
Zhang Rui33823882019-07-10 21:44:30 +080063#define RAPL_PRIMITIVE_DERIVED BIT(1) /* not from raw data */
Jacob Pan2d281d82013-10-17 10:28:35 -070064#define RAPL_PRIMITIVE_DUMMY BIT(2)
65
Jacob Pan2d281d82013-10-17 10:28:35 -070066#define TIME_WINDOW_MAX_MSEC 40000
67#define TIME_WINDOW_MIN_MSEC 250
Zhang Rui33823882019-07-10 21:44:30 +080068#define ENERGY_UNIT_SCALE 1000 /* scale from driver unit to powercap unit */
Jacob Pan2d281d82013-10-17 10:28:35 -070069enum unit_type {
Zhang Rui33823882019-07-10 21:44:30 +080070 ARBITRARY_UNIT, /* no translation */
Jacob Pan2d281d82013-10-17 10:28:35 -070071 POWER_UNIT,
72 ENERGY_UNIT,
73 TIME_UNIT,
74};
75
Jacob Pan2d281d82013-10-17 10:28:35 -070076/* per domain data, some are optional */
Jacob Pan2d281d82013-10-17 10:28:35 -070077#define NR_RAW_PRIMITIVES (NR_RAPL_PRIMITIVES - 2)
78
Jacob Pan2d281d82013-10-17 10:28:35 -070079#define DOMAIN_STATE_INACTIVE BIT(0)
80#define DOMAIN_STATE_POWER_LIMIT_SET BIT(1)
81#define DOMAIN_STATE_BIOS_LOCKED BIT(2)
82
Jacob Pan2d281d82013-10-17 10:28:35 -070083static const char pl1_name[] = "long_term";
84static const char pl2_name[] = "short_term";
85
Jacob Pan2d281d82013-10-17 10:28:35 -070086#define power_zone_to_rapl_domain(_zone) \
87 container_of(_zone, struct rapl_domain, power_zone)
88
Jacob Pan087e9cb2014-11-07 09:29:25 -080089struct rapl_defaults {
Ajay Thomas51b63402015-04-30 01:43:23 +053090 u8 floor_freq_reg_addr;
Jacob Pan087e9cb2014-11-07 09:29:25 -080091 int (*check_unit)(struct rapl_package *rp, int cpu);
92 void (*set_floor_freq)(struct rapl_domain *rd, bool mode);
93 u64 (*compute_time_window)(struct rapl_package *rp, u64 val,
Zhang Rui33823882019-07-10 21:44:30 +080094 bool to_raw);
Jacob Pand474a4d2015-03-13 03:48:56 -070095 unsigned int dram_domain_energy_unit;
Jacob Pan087e9cb2014-11-07 09:29:25 -080096};
97static struct rapl_defaults *rapl_defaults;
98
Jacob Pan3c2c0842014-11-07 09:29:26 -080099/* Sideband MBI registers */
Ajay Thomas51b63402015-04-30 01:43:23 +0530100#define IOSF_CPU_POWER_BUDGET_CTL_BYT (0x2)
101#define IOSF_CPU_POWER_BUDGET_CTL_TNG (0xdf)
Jacob Pan3c2c0842014-11-07 09:29:26 -0800102
Jacob Pan2d281d82013-10-17 10:28:35 -0700103#define PACKAGE_PLN_INT_SAVED BIT(0)
104#define MAX_PRIM_NAME (32)
105
106/* per domain data. used to describe individual knobs such that access function
107 * can be consolidated into one instead of many inline functions.
108 */
109struct rapl_primitive_info {
110 const char *name;
111 u64 mask;
112 int shift;
Zhang Ruif7c4e0c2019-07-10 21:44:22 +0800113 enum rapl_domain_reg_id id;
Jacob Pan2d281d82013-10-17 10:28:35 -0700114 enum unit_type unit;
115 u32 flag;
116};
117
118#define PRIMITIVE_INFO_INIT(p, m, s, i, u, f) { \
119 .name = #p, \
120 .mask = m, \
121 .shift = s, \
122 .id = i, \
123 .unit = u, \
124 .flag = f \
125 }
126
127static void rapl_init_domains(struct rapl_package *rp);
128static int rapl_read_data_raw(struct rapl_domain *rd,
Zhang Rui33823882019-07-10 21:44:30 +0800129 enum rapl_primitives prim,
130 bool xlate, u64 *data);
Jacob Pan2d281d82013-10-17 10:28:35 -0700131static int rapl_write_data_raw(struct rapl_domain *rd,
Zhang Rui33823882019-07-10 21:44:30 +0800132 enum rapl_primitives prim,
133 unsigned long long value);
Jacob Pan309557f2016-02-24 13:31:37 -0800134static u64 rapl_unit_xlate(struct rapl_domain *rd,
Zhang Rui33823882019-07-10 21:44:30 +0800135 enum unit_type type, u64 value, int to_raw);
Jacob Pan309557f2016-02-24 13:31:37 -0800136static void package_power_limit_irq_save(struct rapl_package *rp);
Jacob Pan2d281d82013-10-17 10:28:35 -0700137
Zhang Rui33823882019-07-10 21:44:30 +0800138static LIST_HEAD(rapl_packages); /* guarded by CPU hotplug lock */
Jacob Pan2d281d82013-10-17 10:28:35 -0700139
Zhang Rui33823882019-07-10 21:44:30 +0800140static const char *const rapl_domain_names[] = {
Jacob Pan2d281d82013-10-17 10:28:35 -0700141 "package",
142 "core",
143 "uncore",
144 "dram",
Srinivas Pandruvada3521ba12016-04-17 15:03:01 -0700145 "psys",
Jacob Pan2d281d82013-10-17 10:28:35 -0700146};
147
Zhang Rui33823882019-07-10 21:44:30 +0800148static int get_energy_counter(struct powercap_zone *power_zone,
149 u64 *energy_raw)
Jacob Pan2d281d82013-10-17 10:28:35 -0700150{
151 struct rapl_domain *rd;
152 u64 energy_now;
153
154 /* prevent CPU hotplug, make sure the RAPL domain does not go
155 * away while reading the counter.
156 */
157 get_online_cpus();
158 rd = power_zone_to_rapl_domain(power_zone);
159
160 if (!rapl_read_data_raw(rd, ENERGY_COUNTER, true, &energy_now)) {
161 *energy_raw = energy_now;
162 put_online_cpus();
163
164 return 0;
165 }
166 put_online_cpus();
167
168 return -EIO;
169}
170
171static int get_max_energy_counter(struct powercap_zone *pcd_dev, u64 *energy)
172{
Jacob Pand474a4d2015-03-13 03:48:56 -0700173 struct rapl_domain *rd = power_zone_to_rapl_domain(pcd_dev);
174
Jacob Pan309557f2016-02-24 13:31:37 -0800175 *energy = rapl_unit_xlate(rd, ENERGY_UNIT, ENERGY_STATUS_MASK, 0);
Jacob Pan2d281d82013-10-17 10:28:35 -0700176 return 0;
177}
178
179static int release_zone(struct powercap_zone *power_zone)
180{
181 struct rapl_domain *rd = power_zone_to_rapl_domain(power_zone);
Jacob Pan309557f2016-02-24 13:31:37 -0800182 struct rapl_package *rp = rd->rp;
Jacob Pan2d281d82013-10-17 10:28:35 -0700183
184 /* package zone is the last zone of a package, we can free
185 * memory here since all children has been unregistered.
186 */
187 if (rd->id == RAPL_DOMAIN_PACKAGE) {
Jacob Pan2d281d82013-10-17 10:28:35 -0700188 kfree(rd);
189 rp->domains = NULL;
190 }
191
192 return 0;
193
194}
195
196static int find_nr_power_limit(struct rapl_domain *rd)
197{
Jacob Pane1399ba2016-05-31 13:41:29 -0700198 int i, nr_pl = 0;
Jacob Pan2d281d82013-10-17 10:28:35 -0700199
200 for (i = 0; i < NR_POWER_LIMITS; i++) {
Jacob Pane1399ba2016-05-31 13:41:29 -0700201 if (rd->rpl[i].name)
202 nr_pl++;
Jacob Pan2d281d82013-10-17 10:28:35 -0700203 }
204
Jacob Pane1399ba2016-05-31 13:41:29 -0700205 return nr_pl;
Jacob Pan2d281d82013-10-17 10:28:35 -0700206}
207
208static int set_domain_enable(struct powercap_zone *power_zone, bool mode)
209{
210 struct rapl_domain *rd = power_zone_to_rapl_domain(power_zone);
Jacob Pan2d281d82013-10-17 10:28:35 -0700211
212 if (rd->state & DOMAIN_STATE_BIOS_LOCKED)
213 return -EACCES;
Jacob Pan3c2c0842014-11-07 09:29:26 -0800214
Jacob Pan2d281d82013-10-17 10:28:35 -0700215 get_online_cpus();
Jacob Pan2d281d82013-10-17 10:28:35 -0700216 rapl_write_data_raw(rd, PL1_ENABLE, mode);
Ajay Thomas51b63402015-04-30 01:43:23 +0530217 if (rapl_defaults->set_floor_freq)
218 rapl_defaults->set_floor_freq(rd, mode);
Jacob Pan2d281d82013-10-17 10:28:35 -0700219 put_online_cpus();
220
221 return 0;
222}
223
224static int get_domain_enable(struct powercap_zone *power_zone, bool *mode)
225{
226 struct rapl_domain *rd = power_zone_to_rapl_domain(power_zone);
227 u64 val;
228
229 if (rd->state & DOMAIN_STATE_BIOS_LOCKED) {
230 *mode = false;
231 return 0;
232 }
233 get_online_cpus();
234 if (rapl_read_data_raw(rd, PL1_ENABLE, true, &val)) {
235 put_online_cpus();
236 return -EIO;
237 }
238 *mode = val;
239 put_online_cpus();
240
241 return 0;
242}
243
244/* per RAPL domain ops, in the order of rapl_domain_type */
Julia Lawall600c3952015-12-23 22:59:55 +0100245static const struct powercap_zone_ops zone_ops[] = {
Jacob Pan2d281d82013-10-17 10:28:35 -0700246 /* RAPL_DOMAIN_PACKAGE */
247 {
Zhang Rui33823882019-07-10 21:44:30 +0800248 .get_energy_uj = get_energy_counter,
249 .get_max_energy_range_uj = get_max_energy_counter,
250 .release = release_zone,
251 .set_enable = set_domain_enable,
252 .get_enable = get_domain_enable,
253 },
Jacob Pan2d281d82013-10-17 10:28:35 -0700254 /* RAPL_DOMAIN_PP0 */
255 {
Zhang Rui33823882019-07-10 21:44:30 +0800256 .get_energy_uj = get_energy_counter,
257 .get_max_energy_range_uj = get_max_energy_counter,
258 .release = release_zone,
259 .set_enable = set_domain_enable,
260 .get_enable = get_domain_enable,
261 },
Jacob Pan2d281d82013-10-17 10:28:35 -0700262 /* RAPL_DOMAIN_PP1 */
263 {
Zhang Rui33823882019-07-10 21:44:30 +0800264 .get_energy_uj = get_energy_counter,
265 .get_max_energy_range_uj = get_max_energy_counter,
266 .release = release_zone,
267 .set_enable = set_domain_enable,
268 .get_enable = get_domain_enable,
269 },
Jacob Pan2d281d82013-10-17 10:28:35 -0700270 /* RAPL_DOMAIN_DRAM */
271 {
Zhang Rui33823882019-07-10 21:44:30 +0800272 .get_energy_uj = get_energy_counter,
273 .get_max_energy_range_uj = get_max_energy_counter,
274 .release = release_zone,
275 .set_enable = set_domain_enable,
276 .get_enable = get_domain_enable,
277 },
Srinivas Pandruvada3521ba12016-04-17 15:03:01 -0700278 /* RAPL_DOMAIN_PLATFORM */
279 {
Zhang Rui33823882019-07-10 21:44:30 +0800280 .get_energy_uj = get_energy_counter,
281 .get_max_energy_range_uj = get_max_energy_counter,
282 .release = release_zone,
283 .set_enable = set_domain_enable,
284 .get_enable = get_domain_enable,
285 },
Jacob Pan2d281d82013-10-17 10:28:35 -0700286};
287
Jacob Pane1399ba2016-05-31 13:41:29 -0700288/*
289 * Constraint index used by powercap can be different than power limit (PL)
Zhang Rui33823882019-07-10 21:44:30 +0800290 * index in that some PLs maybe missing due to non-existent MSRs. So we
Jacob Pane1399ba2016-05-31 13:41:29 -0700291 * need to convert here by finding the valid PLs only (name populated).
292 */
293static int contraint_to_pl(struct rapl_domain *rd, int cid)
294{
295 int i, j;
296
297 for (i = 0, j = 0; i < NR_POWER_LIMITS; i++) {
298 if ((rd->rpl[i].name) && j++ == cid) {
299 pr_debug("%s: index %d\n", __func__, i);
300 return i;
301 }
302 }
Jacob Pancb43f812016-11-28 13:53:11 -0800303 pr_err("Cannot find matching power limit for constraint %d\n", cid);
Jacob Pane1399ba2016-05-31 13:41:29 -0700304
305 return -EINVAL;
306}
307
308static int set_power_limit(struct powercap_zone *power_zone, int cid,
Zhang Rui33823882019-07-10 21:44:30 +0800309 u64 power_limit)
Jacob Pan2d281d82013-10-17 10:28:35 -0700310{
311 struct rapl_domain *rd;
312 struct rapl_package *rp;
313 int ret = 0;
Jacob Pane1399ba2016-05-31 13:41:29 -0700314 int id;
Jacob Pan2d281d82013-10-17 10:28:35 -0700315
316 get_online_cpus();
317 rd = power_zone_to_rapl_domain(power_zone);
Jacob Pane1399ba2016-05-31 13:41:29 -0700318 id = contraint_to_pl(rd, cid);
Jacob Pancb43f812016-11-28 13:53:11 -0800319 if (id < 0) {
320 ret = id;
321 goto set_exit;
322 }
Jacob Pane1399ba2016-05-31 13:41:29 -0700323
Jacob Pan309557f2016-02-24 13:31:37 -0800324 rp = rd->rp;
Jacob Pan2d281d82013-10-17 10:28:35 -0700325
326 if (rd->state & DOMAIN_STATE_BIOS_LOCKED) {
Zhang Rui33823882019-07-10 21:44:30 +0800327 dev_warn(&power_zone->dev,
328 "%s locked by BIOS, monitoring only\n", rd->name);
Jacob Pan2d281d82013-10-17 10:28:35 -0700329 ret = -EACCES;
330 goto set_exit;
331 }
332
333 switch (rd->rpl[id].prim_id) {
334 case PL1_ENABLE:
335 rapl_write_data_raw(rd, POWER_LIMIT1, power_limit);
336 break;
337 case PL2_ENABLE:
338 rapl_write_data_raw(rd, POWER_LIMIT2, power_limit);
339 break;
340 default:
341 ret = -EINVAL;
342 }
343 if (!ret)
Jacob Pan309557f2016-02-24 13:31:37 -0800344 package_power_limit_irq_save(rp);
Jacob Pan2d281d82013-10-17 10:28:35 -0700345set_exit:
346 put_online_cpus();
347 return ret;
348}
349
Jacob Pane1399ba2016-05-31 13:41:29 -0700350static int get_current_power_limit(struct powercap_zone *power_zone, int cid,
Zhang Rui33823882019-07-10 21:44:30 +0800351 u64 *data)
Jacob Pan2d281d82013-10-17 10:28:35 -0700352{
353 struct rapl_domain *rd;
354 u64 val;
355 int prim;
356 int ret = 0;
Jacob Pane1399ba2016-05-31 13:41:29 -0700357 int id;
Jacob Pan2d281d82013-10-17 10:28:35 -0700358
359 get_online_cpus();
360 rd = power_zone_to_rapl_domain(power_zone);
Jacob Pane1399ba2016-05-31 13:41:29 -0700361 id = contraint_to_pl(rd, cid);
Jacob Pancb43f812016-11-28 13:53:11 -0800362 if (id < 0) {
363 ret = id;
364 goto get_exit;
365 }
366
Jacob Pan2d281d82013-10-17 10:28:35 -0700367 switch (rd->rpl[id].prim_id) {
368 case PL1_ENABLE:
369 prim = POWER_LIMIT1;
370 break;
371 case PL2_ENABLE:
372 prim = POWER_LIMIT2;
373 break;
374 default:
375 put_online_cpus();
376 return -EINVAL;
377 }
378 if (rapl_read_data_raw(rd, prim, true, &val))
379 ret = -EIO;
380 else
381 *data = val;
382
Jacob Pancb43f812016-11-28 13:53:11 -0800383get_exit:
Jacob Pan2d281d82013-10-17 10:28:35 -0700384 put_online_cpus();
385
386 return ret;
387}
388
Jacob Pane1399ba2016-05-31 13:41:29 -0700389static int set_time_window(struct powercap_zone *power_zone, int cid,
Zhang Rui33823882019-07-10 21:44:30 +0800390 u64 window)
Jacob Pan2d281d82013-10-17 10:28:35 -0700391{
392 struct rapl_domain *rd;
393 int ret = 0;
Jacob Pane1399ba2016-05-31 13:41:29 -0700394 int id;
Jacob Pan2d281d82013-10-17 10:28:35 -0700395
396 get_online_cpus();
397 rd = power_zone_to_rapl_domain(power_zone);
Jacob Pane1399ba2016-05-31 13:41:29 -0700398 id = contraint_to_pl(rd, cid);
Jacob Pancb43f812016-11-28 13:53:11 -0800399 if (id < 0) {
400 ret = id;
401 goto set_time_exit;
402 }
Jacob Pane1399ba2016-05-31 13:41:29 -0700403
Jacob Pan2d281d82013-10-17 10:28:35 -0700404 switch (rd->rpl[id].prim_id) {
405 case PL1_ENABLE:
406 rapl_write_data_raw(rd, TIME_WINDOW1, window);
407 break;
408 case PL2_ENABLE:
409 rapl_write_data_raw(rd, TIME_WINDOW2, window);
410 break;
411 default:
412 ret = -EINVAL;
413 }
Jacob Pancb43f812016-11-28 13:53:11 -0800414
415set_time_exit:
Jacob Pan2d281d82013-10-17 10:28:35 -0700416 put_online_cpus();
417 return ret;
418}
419
Zhang Rui33823882019-07-10 21:44:30 +0800420static int get_time_window(struct powercap_zone *power_zone, int cid,
421 u64 *data)
Jacob Pan2d281d82013-10-17 10:28:35 -0700422{
423 struct rapl_domain *rd;
424 u64 val;
425 int ret = 0;
Jacob Pane1399ba2016-05-31 13:41:29 -0700426 int id;
Jacob Pan2d281d82013-10-17 10:28:35 -0700427
428 get_online_cpus();
429 rd = power_zone_to_rapl_domain(power_zone);
Jacob Pane1399ba2016-05-31 13:41:29 -0700430 id = contraint_to_pl(rd, cid);
Jacob Pancb43f812016-11-28 13:53:11 -0800431 if (id < 0) {
432 ret = id;
433 goto get_time_exit;
434 }
Jacob Pane1399ba2016-05-31 13:41:29 -0700435
Jacob Pan2d281d82013-10-17 10:28:35 -0700436 switch (rd->rpl[id].prim_id) {
437 case PL1_ENABLE:
438 ret = rapl_read_data_raw(rd, TIME_WINDOW1, true, &val);
439 break;
440 case PL2_ENABLE:
441 ret = rapl_read_data_raw(rd, TIME_WINDOW2, true, &val);
442 break;
443 default:
444 put_online_cpus();
445 return -EINVAL;
446 }
447 if (!ret)
448 *data = val;
Jacob Pancb43f812016-11-28 13:53:11 -0800449
450get_time_exit:
Jacob Pan2d281d82013-10-17 10:28:35 -0700451 put_online_cpus();
452
453 return ret;
454}
455
Zhang Rui33823882019-07-10 21:44:30 +0800456static const char *get_constraint_name(struct powercap_zone *power_zone,
457 int cid)
Jacob Pan2d281d82013-10-17 10:28:35 -0700458{
Jacob Pan2d281d82013-10-17 10:28:35 -0700459 struct rapl_domain *rd;
Jacob Pane1399ba2016-05-31 13:41:29 -0700460 int id;
Jacob Pan2d281d82013-10-17 10:28:35 -0700461
462 rd = power_zone_to_rapl_domain(power_zone);
Jacob Pane1399ba2016-05-31 13:41:29 -0700463 id = contraint_to_pl(rd, cid);
464 if (id >= 0)
465 return rd->rpl[id].name;
Jacob Pan2d281d82013-10-17 10:28:35 -0700466
Jacob Pane1399ba2016-05-31 13:41:29 -0700467 return NULL;
Jacob Pan2d281d82013-10-17 10:28:35 -0700468}
469
Zhang Rui33823882019-07-10 21:44:30 +0800470static int get_max_power(struct powercap_zone *power_zone, int id, u64 *data)
Jacob Pan2d281d82013-10-17 10:28:35 -0700471{
472 struct rapl_domain *rd;
473 u64 val;
474 int prim;
475 int ret = 0;
476
477 get_online_cpus();
478 rd = power_zone_to_rapl_domain(power_zone);
479 switch (rd->rpl[id].prim_id) {
480 case PL1_ENABLE:
481 prim = THERMAL_SPEC_POWER;
482 break;
483 case PL2_ENABLE:
484 prim = MAX_POWER;
485 break;
486 default:
487 put_online_cpus();
488 return -EINVAL;
489 }
490 if (rapl_read_data_raw(rd, prim, true, &val))
491 ret = -EIO;
492 else
493 *data = val;
494
495 put_online_cpus();
496
497 return ret;
498}
499
Julia Lawall600c3952015-12-23 22:59:55 +0100500static const struct powercap_zone_constraint_ops constraint_ops = {
Jacob Pan2d281d82013-10-17 10:28:35 -0700501 .set_power_limit_uw = set_power_limit,
502 .get_power_limit_uw = get_current_power_limit,
503 .set_time_window_us = set_time_window,
504 .get_time_window_us = get_time_window,
505 .get_max_power_uw = get_max_power,
506 .get_name = get_constraint_name,
507};
508
509/* called after domain detection and package level data are set */
510static void rapl_init_domains(struct rapl_package *rp)
511{
Zhang Rui0c2dded2019-07-10 21:44:32 +0800512 enum rapl_domain_type i;
513 enum rapl_domain_reg_id j;
Jacob Pan2d281d82013-10-17 10:28:35 -0700514 struct rapl_domain *rd = rp->domains;
515
516 for (i = 0; i < RAPL_DOMAIN_MAX; i++) {
517 unsigned int mask = rp->domain_map & (1 << i);
Zhang Rui7fde2712019-07-10 21:44:26 +0800518
Zhang Rui0c2dded2019-07-10 21:44:32 +0800519 if (!mask)
520 continue;
Zhang Rui7fde2712019-07-10 21:44:26 +0800521
Zhang Rui0c2dded2019-07-10 21:44:32 +0800522 rd->rp = rp;
523 rd->name = rapl_domain_names[i];
524 rd->id = i;
525 rd->rpl[0].prim_id = PL1_ENABLE;
526 rd->rpl[0].name = pl1_name;
527 /* some domain may support two power limits */
528 if (rp->priv->limits[i] == 2) {
Jacob Pan2d281d82013-10-17 10:28:35 -0700529 rd->rpl[1].prim_id = PL2_ENABLE;
530 rd->rpl[1].name = pl2_name;
Zhang Rui0c2dded2019-07-10 21:44:32 +0800531 }
532
533 for (j = 0; j < RAPL_DOMAIN_REG_MAX; j++)
534 rd->regs[j] = rp->priv->regs[i][j];
535
536 if (i == RAPL_DOMAIN_DRAM) {
Jacob Pand474a4d2015-03-13 03:48:56 -0700537 rd->domain_energy_unit =
Zhang Rui33823882019-07-10 21:44:30 +0800538 rapl_defaults->dram_domain_energy_unit;
Jacob Pand474a4d2015-03-13 03:48:56 -0700539 if (rd->domain_energy_unit)
540 pr_info("DRAM domain energy unit %dpj\n",
541 rd->domain_energy_unit);
Jacob Pan2d281d82013-10-17 10:28:35 -0700542 }
Zhang Rui0c2dded2019-07-10 21:44:32 +0800543 rd++;
Jacob Pan2d281d82013-10-17 10:28:35 -0700544 }
545}
546
Jacob Pan309557f2016-02-24 13:31:37 -0800547static u64 rapl_unit_xlate(struct rapl_domain *rd, enum unit_type type,
Zhang Rui33823882019-07-10 21:44:30 +0800548 u64 value, int to_raw)
Jacob Pan2d281d82013-10-17 10:28:35 -0700549{
Jacob Pan3c2c0842014-11-07 09:29:26 -0800550 u64 units = 1;
Jacob Pan309557f2016-02-24 13:31:37 -0800551 struct rapl_package *rp = rd->rp;
Jacob Pand474a4d2015-03-13 03:48:56 -0700552 u64 scale = 1;
Jacob Pan2d281d82013-10-17 10:28:35 -0700553
Jacob Pan2d281d82013-10-17 10:28:35 -0700554 switch (type) {
555 case POWER_UNIT:
Jacob Pan3c2c0842014-11-07 09:29:26 -0800556 units = rp->power_unit;
Jacob Pan2d281d82013-10-17 10:28:35 -0700557 break;
558 case ENERGY_UNIT:
Jacob Pand474a4d2015-03-13 03:48:56 -0700559 scale = ENERGY_UNIT_SCALE;
560 /* per domain unit takes precedence */
Jacob Pancb43f812016-11-28 13:53:11 -0800561 if (rd->domain_energy_unit)
Jacob Pand474a4d2015-03-13 03:48:56 -0700562 units = rd->domain_energy_unit;
563 else
564 units = rp->energy_unit;
Jacob Pan2d281d82013-10-17 10:28:35 -0700565 break;
566 case TIME_UNIT:
Jacob Pan3c2c0842014-11-07 09:29:26 -0800567 return rapl_defaults->compute_time_window(rp, value, to_raw);
Jacob Pan2d281d82013-10-17 10:28:35 -0700568 case ARBITRARY_UNIT:
569 default:
570 return value;
571 };
572
573 if (to_raw)
Jacob Pand474a4d2015-03-13 03:48:56 -0700574 return div64_u64(value, units) * scale;
Jacob Pan3c2c0842014-11-07 09:29:26 -0800575
576 value *= units;
577
Jacob Pand474a4d2015-03-13 03:48:56 -0700578 return div64_u64(value, scale);
Jacob Pan2d281d82013-10-17 10:28:35 -0700579}
580
581/* in the order of enum rapl_primitives */
582static struct rapl_primitive_info rpi[] = {
583 /* name, mask, shift, msr index, unit divisor */
584 PRIMITIVE_INFO_INIT(ENERGY_COUNTER, ENERGY_STATUS_MASK, 0,
Zhang Rui33823882019-07-10 21:44:30 +0800585 RAPL_DOMAIN_REG_STATUS, ENERGY_UNIT, 0),
Jacob Pan2d281d82013-10-17 10:28:35 -0700586 PRIMITIVE_INFO_INIT(POWER_LIMIT1, POWER_LIMIT1_MASK, 0,
Zhang Rui33823882019-07-10 21:44:30 +0800587 RAPL_DOMAIN_REG_LIMIT, POWER_UNIT, 0),
Jacob Pan2d281d82013-10-17 10:28:35 -0700588 PRIMITIVE_INFO_INIT(POWER_LIMIT2, POWER_LIMIT2_MASK, 32,
Zhang Rui33823882019-07-10 21:44:30 +0800589 RAPL_DOMAIN_REG_LIMIT, POWER_UNIT, 0),
Zhang Rui0c2dded2019-07-10 21:44:32 +0800590 PRIMITIVE_INFO_INIT(FW_LOCK, POWER_LOW_LOCK, 31,
Zhang Rui33823882019-07-10 21:44:30 +0800591 RAPL_DOMAIN_REG_LIMIT, ARBITRARY_UNIT, 0),
Jacob Pan2d281d82013-10-17 10:28:35 -0700592 PRIMITIVE_INFO_INIT(PL1_ENABLE, POWER_LIMIT1_ENABLE, 15,
Zhang Rui33823882019-07-10 21:44:30 +0800593 RAPL_DOMAIN_REG_LIMIT, ARBITRARY_UNIT, 0),
Jacob Pan2d281d82013-10-17 10:28:35 -0700594 PRIMITIVE_INFO_INIT(PL1_CLAMP, POWER_LIMIT1_CLAMP, 16,
Zhang Rui33823882019-07-10 21:44:30 +0800595 RAPL_DOMAIN_REG_LIMIT, ARBITRARY_UNIT, 0),
Jacob Pan2d281d82013-10-17 10:28:35 -0700596 PRIMITIVE_INFO_INIT(PL2_ENABLE, POWER_LIMIT2_ENABLE, 47,
Zhang Rui33823882019-07-10 21:44:30 +0800597 RAPL_DOMAIN_REG_LIMIT, ARBITRARY_UNIT, 0),
Jacob Pan2d281d82013-10-17 10:28:35 -0700598 PRIMITIVE_INFO_INIT(PL2_CLAMP, POWER_LIMIT2_CLAMP, 48,
Zhang Rui33823882019-07-10 21:44:30 +0800599 RAPL_DOMAIN_REG_LIMIT, ARBITRARY_UNIT, 0),
Jacob Pan2d281d82013-10-17 10:28:35 -0700600 PRIMITIVE_INFO_INIT(TIME_WINDOW1, TIME_WINDOW1_MASK, 17,
Zhang Rui33823882019-07-10 21:44:30 +0800601 RAPL_DOMAIN_REG_LIMIT, TIME_UNIT, 0),
Jacob Pan2d281d82013-10-17 10:28:35 -0700602 PRIMITIVE_INFO_INIT(TIME_WINDOW2, TIME_WINDOW2_MASK, 49,
Zhang Rui33823882019-07-10 21:44:30 +0800603 RAPL_DOMAIN_REG_LIMIT, TIME_UNIT, 0),
Jacob Pan2d281d82013-10-17 10:28:35 -0700604 PRIMITIVE_INFO_INIT(THERMAL_SPEC_POWER, POWER_INFO_THERMAL_SPEC_MASK,
Zhang Rui33823882019-07-10 21:44:30 +0800605 0, RAPL_DOMAIN_REG_INFO, POWER_UNIT, 0),
Jacob Pan2d281d82013-10-17 10:28:35 -0700606 PRIMITIVE_INFO_INIT(MAX_POWER, POWER_INFO_MAX_MASK, 32,
Zhang Rui33823882019-07-10 21:44:30 +0800607 RAPL_DOMAIN_REG_INFO, POWER_UNIT, 0),
Jacob Pan2d281d82013-10-17 10:28:35 -0700608 PRIMITIVE_INFO_INIT(MIN_POWER, POWER_INFO_MIN_MASK, 16,
Zhang Rui33823882019-07-10 21:44:30 +0800609 RAPL_DOMAIN_REG_INFO, POWER_UNIT, 0),
Jacob Pan2d281d82013-10-17 10:28:35 -0700610 PRIMITIVE_INFO_INIT(MAX_TIME_WINDOW, POWER_INFO_MAX_TIME_WIN_MASK, 48,
Zhang Rui33823882019-07-10 21:44:30 +0800611 RAPL_DOMAIN_REG_INFO, TIME_UNIT, 0),
Jacob Pan2d281d82013-10-17 10:28:35 -0700612 PRIMITIVE_INFO_INIT(THROTTLED_TIME, PERF_STATUS_THROTTLE_TIME_MASK, 0,
Zhang Rui33823882019-07-10 21:44:30 +0800613 RAPL_DOMAIN_REG_PERF, TIME_UNIT, 0),
Jacob Pan2d281d82013-10-17 10:28:35 -0700614 PRIMITIVE_INFO_INIT(PRIORITY_LEVEL, PP_POLICY_MASK, 0,
Zhang Rui33823882019-07-10 21:44:30 +0800615 RAPL_DOMAIN_REG_POLICY, ARBITRARY_UNIT, 0),
Jacob Pan2d281d82013-10-17 10:28:35 -0700616 /* non-hardware */
617 PRIMITIVE_INFO_INIT(AVERAGE_POWER, 0, 0, 0, POWER_UNIT,
Zhang Rui33823882019-07-10 21:44:30 +0800618 RAPL_PRIMITIVE_DERIVED),
Jacob Pan2d281d82013-10-17 10:28:35 -0700619 {NULL, 0, 0, 0},
620};
621
622/* Read primitive data based on its related struct rapl_primitive_info.
623 * if xlate flag is set, return translated data based on data units, i.e.
624 * time, energy, and power.
625 * RAPL MSRs are non-architectual and are laid out not consistently across
626 * domains. Here we use primitive info to allow writing consolidated access
627 * functions.
628 * For a given primitive, it is processed by MSR mask and shift. Unit conversion
629 * is pre-assigned based on RAPL unit MSRs read at init time.
630 * 63-------------------------- 31--------------------------- 0
631 * | xxxxx (mask) |
632 * | |<- shift ----------------|
633 * 63-------------------------- 31--------------------------- 0
634 */
635static int rapl_read_data_raw(struct rapl_domain *rd,
Zhang Rui33823882019-07-10 21:44:30 +0800636 enum rapl_primitives prim, bool xlate, u64 *data)
Jacob Pan2d281d82013-10-17 10:28:35 -0700637{
Zhang Ruibeea8df2019-07-10 21:44:27 +0800638 u64 value;
Jacob Pan2d281d82013-10-17 10:28:35 -0700639 struct rapl_primitive_info *rp = &rpi[prim];
Zhang Ruibeea8df2019-07-10 21:44:27 +0800640 struct reg_action ra;
Jacob Pan2d281d82013-10-17 10:28:35 -0700641 int cpu;
642
643 if (!rp->name || rp->flag & RAPL_PRIMITIVE_DUMMY)
644 return -EINVAL;
645
Zhang Ruibeea8df2019-07-10 21:44:27 +0800646 ra.reg = rd->regs[rp->id];
647 if (!ra.reg)
Jacob Pan2d281d82013-10-17 10:28:35 -0700648 return -EINVAL;
Jacob Pan323ee642016-02-24 13:31:38 -0800649
650 cpu = rd->rp->lead_cpu;
Jacob Pan2d281d82013-10-17 10:28:35 -0700651
Zhang Rui0c2dded2019-07-10 21:44:32 +0800652 /* domain with 2 limits has different bit */
653 if (prim == FW_LOCK && rd->rp->priv->limits[rd->id] == 2) {
654 rp->mask = POWER_HIGH_LOCK;
Jacob Pan2d281d82013-10-17 10:28:35 -0700655 rp->shift = 63;
656 }
657 /* non-hardware data are collected by the polling thread */
658 if (rp->flag & RAPL_PRIMITIVE_DERIVED) {
659 *data = rd->rdd.primitives[prim];
660 return 0;
661 }
662
Zhang Ruibeea8df2019-07-10 21:44:27 +0800663 ra.mask = rp->mask;
664
665 if (rd->rp->priv->read_raw(cpu, &ra)) {
Zhang Ruid978e752019-07-10 21:44:31 +0800666 pr_debug("failed to read reg 0x%llx on cpu %d\n", ra.reg, cpu);
Jacob Pan2d281d82013-10-17 10:28:35 -0700667 return -EIO;
668 }
669
Zhang Ruibeea8df2019-07-10 21:44:27 +0800670 value = ra.value >> rp->shift;
671
Jacob Pan2d281d82013-10-17 10:28:35 -0700672 if (xlate)
Zhang Ruibeea8df2019-07-10 21:44:27 +0800673 *data = rapl_unit_xlate(rd, rp->unit, value, 0);
Jacob Pan2d281d82013-10-17 10:28:35 -0700674 else
Zhang Ruibeea8df2019-07-10 21:44:27 +0800675 *data = value;
Jacob Pan2d281d82013-10-17 10:28:35 -0700676
677 return 0;
678}
679
680/* Similar use of primitive info in the read counterpart */
681static int rapl_write_data_raw(struct rapl_domain *rd,
Zhang Rui33823882019-07-10 21:44:30 +0800682 enum rapl_primitives prim,
683 unsigned long long value)
Jacob Pan2d281d82013-10-17 10:28:35 -0700684{
Jacob Pan2d281d82013-10-17 10:28:35 -0700685 struct rapl_primitive_info *rp = &rpi[prim];
686 int cpu;
Jacob Panf14a1392016-02-24 13:31:36 -0800687 u64 bits;
Zhang Ruibeea8df2019-07-10 21:44:27 +0800688 struct reg_action ra;
Jacob Panf14a1392016-02-24 13:31:36 -0800689 int ret;
Jacob Pan2d281d82013-10-17 10:28:35 -0700690
Jacob Pan323ee642016-02-24 13:31:38 -0800691 cpu = rd->rp->lead_cpu;
Jacob Pan309557f2016-02-24 13:31:37 -0800692 bits = rapl_unit_xlate(rd, rp->unit, value, 1);
Adam Lessnauedbdabc2017-06-01 11:21:50 +0200693 bits <<= rp->shift;
694 bits &= rp->mask;
695
Zhang Ruibeea8df2019-07-10 21:44:27 +0800696 memset(&ra, 0, sizeof(ra));
Jacob Panf14a1392016-02-24 13:31:36 -0800697
Zhang Ruibeea8df2019-07-10 21:44:27 +0800698 ra.reg = rd->regs[rp->id];
699 ra.mask = rp->mask;
700 ra.value = bits;
Jacob Panf14a1392016-02-24 13:31:36 -0800701
Zhang Ruibeea8df2019-07-10 21:44:27 +0800702 ret = rd->rp->priv->write_raw(cpu, &ra);
Jacob Panf14a1392016-02-24 13:31:36 -0800703
704 return ret;
Jacob Pan2d281d82013-10-17 10:28:35 -0700705}
706
Jacob Pan3c2c0842014-11-07 09:29:26 -0800707/*
708 * Raw RAPL data stored in MSRs are in certain scales. We need to
709 * convert them into standard units based on the units reported in
710 * the RAPL unit MSRs. This is specific to CPUs as the method to
711 * calculate units differ on different CPUs.
712 * We convert the units to below format based on CPUs.
713 * i.e.
Jacob Pand474a4d2015-03-13 03:48:56 -0700714 * energy unit: picoJoules : Represented in picoJoules by default
Jacob Pan3c2c0842014-11-07 09:29:26 -0800715 * power unit : microWatts : Represented in milliWatts by default
716 * time unit : microseconds: Represented in seconds by default
717 */
718static int rapl_check_unit_core(struct rapl_package *rp, int cpu)
Jacob Pan2d281d82013-10-17 10:28:35 -0700719{
Zhang Rui1193b162019-07-10 21:44:29 +0800720 struct reg_action ra;
Jacob Pan2d281d82013-10-17 10:28:35 -0700721 u32 value;
722
Zhang Rui1193b162019-07-10 21:44:29 +0800723 ra.reg = rp->priv->reg_unit;
724 ra.mask = ~0;
725 if (rp->priv->read_raw(cpu, &ra)) {
Zhang Ruid978e752019-07-10 21:44:31 +0800726 pr_err("Failed to read power unit REG 0x%llx on CPU %d, exit.\n",
Zhang Rui33823882019-07-10 21:44:30 +0800727 rp->priv->reg_unit, cpu);
Jacob Pan2d281d82013-10-17 10:28:35 -0700728 return -ENODEV;
729 }
730
Zhang Rui1193b162019-07-10 21:44:29 +0800731 value = (ra.value & ENERGY_UNIT_MASK) >> ENERGY_UNIT_OFFSET;
Jacob Pand474a4d2015-03-13 03:48:56 -0700732 rp->energy_unit = ENERGY_UNIT_SCALE * 1000000 / (1 << value);
Jacob Pan2d281d82013-10-17 10:28:35 -0700733
Zhang Rui1193b162019-07-10 21:44:29 +0800734 value = (ra.value & POWER_UNIT_MASK) >> POWER_UNIT_OFFSET;
Jacob Pan3c2c0842014-11-07 09:29:26 -0800735 rp->power_unit = 1000000 / (1 << value);
Jacob Pan2d281d82013-10-17 10:28:35 -0700736
Zhang Rui1193b162019-07-10 21:44:29 +0800737 value = (ra.value & TIME_UNIT_MASK) >> TIME_UNIT_OFFSET;
Jacob Pan3c2c0842014-11-07 09:29:26 -0800738 rp->time_unit = 1000000 / (1 << value);
Jacob Pan2d281d82013-10-17 10:28:35 -0700739
Zhang Rui9ea76122019-05-13 13:58:53 -0400740 pr_debug("Core CPU %s energy=%dpJ, time=%dus, power=%duW\n",
Zhang Rui33823882019-07-10 21:44:30 +0800741 rp->name, rp->energy_unit, rp->time_unit, rp->power_unit);
Jacob Pan2d281d82013-10-17 10:28:35 -0700742
743 return 0;
744}
745
Jacob Pan3c2c0842014-11-07 09:29:26 -0800746static int rapl_check_unit_atom(struct rapl_package *rp, int cpu)
747{
Zhang Rui1193b162019-07-10 21:44:29 +0800748 struct reg_action ra;
Jacob Pan3c2c0842014-11-07 09:29:26 -0800749 u32 value;
750
Zhang Rui1193b162019-07-10 21:44:29 +0800751 ra.reg = rp->priv->reg_unit;
752 ra.mask = ~0;
753 if (rp->priv->read_raw(cpu, &ra)) {
Zhang Ruid978e752019-07-10 21:44:31 +0800754 pr_err("Failed to read power unit REG 0x%llx on CPU %d, exit.\n",
Zhang Rui33823882019-07-10 21:44:30 +0800755 rp->priv->reg_unit, cpu);
Jacob Pan3c2c0842014-11-07 09:29:26 -0800756 return -ENODEV;
757 }
Zhang Rui1193b162019-07-10 21:44:29 +0800758
759 value = (ra.value & ENERGY_UNIT_MASK) >> ENERGY_UNIT_OFFSET;
Jacob Pand474a4d2015-03-13 03:48:56 -0700760 rp->energy_unit = ENERGY_UNIT_SCALE * 1 << value;
Jacob Pan3c2c0842014-11-07 09:29:26 -0800761
Zhang Rui1193b162019-07-10 21:44:29 +0800762 value = (ra.value & POWER_UNIT_MASK) >> POWER_UNIT_OFFSET;
Jacob Pan3c2c0842014-11-07 09:29:26 -0800763 rp->power_unit = (1 << value) * 1000;
764
Zhang Rui1193b162019-07-10 21:44:29 +0800765 value = (ra.value & TIME_UNIT_MASK) >> TIME_UNIT_OFFSET;
Jacob Pan3c2c0842014-11-07 09:29:26 -0800766 rp->time_unit = 1000000 / (1 << value);
767
Zhang Rui9ea76122019-05-13 13:58:53 -0400768 pr_debug("Atom %s energy=%dpJ, time=%dus, power=%duW\n",
Zhang Rui33823882019-07-10 21:44:30 +0800769 rp->name, rp->energy_unit, rp->time_unit, rp->power_unit);
Jacob Pan3c2c0842014-11-07 09:29:26 -0800770
771 return 0;
772}
773
Jacob Panf14a1392016-02-24 13:31:36 -0800774static void power_limit_irq_save_cpu(void *info)
775{
776 u32 l, h = 0;
777 struct rapl_package *rp = (struct rapl_package *)info;
778
779 /* save the state of PLN irq mask bit before disabling it */
780 rdmsr_safe(MSR_IA32_PACKAGE_THERM_INTERRUPT, &l, &h);
781 if (!(rp->power_limit_irq & PACKAGE_PLN_INT_SAVED)) {
782 rp->power_limit_irq = l & PACKAGE_THERM_INT_PLN_ENABLE;
783 rp->power_limit_irq |= PACKAGE_PLN_INT_SAVED;
784 }
785 l &= ~PACKAGE_THERM_INT_PLN_ENABLE;
786 wrmsr_safe(MSR_IA32_PACKAGE_THERM_INTERRUPT, l, h);
787}
788
Jacob Pan2d281d82013-10-17 10:28:35 -0700789/* REVISIT:
790 * When package power limit is set artificially low by RAPL, LVT
791 * thermal interrupt for package power limit should be ignored
792 * since we are not really exceeding the real limit. The intention
793 * is to avoid excessive interrupts while we are trying to save power.
794 * A useful feature might be routing the package_power_limit interrupt
795 * to userspace via eventfd. once we have a usecase, this is simple
796 * to do by adding an atomic notifier.
797 */
798
Jacob Pan309557f2016-02-24 13:31:37 -0800799static void package_power_limit_irq_save(struct rapl_package *rp)
Jacob Pan2d281d82013-10-17 10:28:35 -0700800{
Jacob Pan2d281d82013-10-17 10:28:35 -0700801 if (!boot_cpu_has(X86_FEATURE_PTS) || !boot_cpu_has(X86_FEATURE_PLN))
802 return;
803
Jacob Pan323ee642016-02-24 13:31:38 -0800804 smp_call_function_single(rp->lead_cpu, power_limit_irq_save_cpu, rp, 1);
Jacob Panf14a1392016-02-24 13:31:36 -0800805}
806
Thomas Gleixner58705062016-11-22 21:16:02 +0000807/*
808 * Restore per package power limit interrupt enable state. Called from cpu
809 * hotplug code on package removal.
810 */
811static void package_power_limit_irq_restore(struct rapl_package *rp)
Jacob Panf14a1392016-02-24 13:31:36 -0800812{
Thomas Gleixner58705062016-11-22 21:16:02 +0000813 u32 l, h;
814
815 if (!boot_cpu_has(X86_FEATURE_PTS) || !boot_cpu_has(X86_FEATURE_PLN))
816 return;
817
818 /* irq enable state not saved, nothing to restore */
819 if (!(rp->power_limit_irq & PACKAGE_PLN_INT_SAVED))
820 return;
Jacob Panf14a1392016-02-24 13:31:36 -0800821
822 rdmsr_safe(MSR_IA32_PACKAGE_THERM_INTERRUPT, &l, &h);
823
824 if (rp->power_limit_irq & PACKAGE_THERM_INT_PLN_ENABLE)
825 l |= PACKAGE_THERM_INT_PLN_ENABLE;
826 else
827 l &= ~PACKAGE_THERM_INT_PLN_ENABLE;
828
829 wrmsr_safe(MSR_IA32_PACKAGE_THERM_INTERRUPT, l, h);
Jacob Pan2d281d82013-10-17 10:28:35 -0700830}
831
Jacob Pan3c2c0842014-11-07 09:29:26 -0800832static void set_floor_freq_default(struct rapl_domain *rd, bool mode)
833{
834 int nr_powerlimit = find_nr_power_limit(rd);
835
836 /* always enable clamp such that p-state can go below OS requested
837 * range. power capping priority over guranteed frequency.
838 */
839 rapl_write_data_raw(rd, PL1_CLAMP, mode);
840
841 /* some domains have pl2 */
842 if (nr_powerlimit > 1) {
843 rapl_write_data_raw(rd, PL2_ENABLE, mode);
844 rapl_write_data_raw(rd, PL2_CLAMP, mode);
845 }
846}
847
848static void set_floor_freq_atom(struct rapl_domain *rd, bool enable)
849{
850 static u32 power_ctrl_orig_val;
851 u32 mdata;
852
Ajay Thomas51b63402015-04-30 01:43:23 +0530853 if (!rapl_defaults->floor_freq_reg_addr) {
854 pr_err("Invalid floor frequency config register\n");
855 return;
856 }
857
Jacob Pan3c2c0842014-11-07 09:29:26 -0800858 if (!power_ctrl_orig_val)
Andy Shevchenko4077a382015-11-11 19:59:29 +0200859 iosf_mbi_read(BT_MBI_UNIT_PMC, MBI_CR_READ,
860 rapl_defaults->floor_freq_reg_addr,
861 &power_ctrl_orig_val);
Jacob Pan3c2c0842014-11-07 09:29:26 -0800862 mdata = power_ctrl_orig_val;
863 if (enable) {
864 mdata &= ~(0x7f << 8);
865 mdata |= 1 << 8;
866 }
Andy Shevchenko4077a382015-11-11 19:59:29 +0200867 iosf_mbi_write(BT_MBI_UNIT_PMC, MBI_CR_WRITE,
868 rapl_defaults->floor_freq_reg_addr, mdata);
Jacob Pan3c2c0842014-11-07 09:29:26 -0800869}
870
871static u64 rapl_compute_time_window_core(struct rapl_package *rp, u64 value,
Zhang Rui33823882019-07-10 21:44:30 +0800872 bool to_raw)
Jacob Pan3c2c0842014-11-07 09:29:26 -0800873{
Zhang Rui33823882019-07-10 21:44:30 +0800874 u64 f, y; /* fraction and exp. used for time unit */
Jacob Pan3c2c0842014-11-07 09:29:26 -0800875
876 /*
877 * Special processing based on 2^Y*(1+F/4), refer
878 * to Intel Software Developer's manual Vol.3B: CH 14.9.3.
879 */
880 if (!to_raw) {
881 f = (value & 0x60) >> 5;
882 y = value & 0x1f;
883 value = (1 << y) * (4 + f) * rp->time_unit / 4;
884 } else {
885 do_div(value, rp->time_unit);
886 y = ilog2(value);
887 f = div64_u64(4 * (value - (1 << y)), 1 << y);
888 value = (y & 0x1f) | ((f & 0x3) << 5);
889 }
890 return value;
891}
892
893static u64 rapl_compute_time_window_atom(struct rapl_package *rp, u64 value,
Zhang Rui33823882019-07-10 21:44:30 +0800894 bool to_raw)
Jacob Pan3c2c0842014-11-07 09:29:26 -0800895{
896 /*
897 * Atom time unit encoding is straight forward val * time_unit,
898 * where time_unit is default to 1 sec. Never 0.
899 */
900 if (!to_raw)
901 return (value) ? value *= rp->time_unit : rp->time_unit;
Zhang Rui33823882019-07-10 21:44:30 +0800902
903 value = div64_u64(value, rp->time_unit);
Jacob Pan3c2c0842014-11-07 09:29:26 -0800904
905 return value;
906}
907
Jacob Pan087e9cb2014-11-07 09:29:25 -0800908static const struct rapl_defaults rapl_defaults_core = {
Ajay Thomas51b63402015-04-30 01:43:23 +0530909 .floor_freq_reg_addr = 0,
Jacob Pan3c2c0842014-11-07 09:29:26 -0800910 .check_unit = rapl_check_unit_core,
911 .set_floor_freq = set_floor_freq_default,
912 .compute_time_window = rapl_compute_time_window_core,
Jacob Pan087e9cb2014-11-07 09:29:25 -0800913};
914
Jacob Pand474a4d2015-03-13 03:48:56 -0700915static const struct rapl_defaults rapl_defaults_hsw_server = {
916 .check_unit = rapl_check_unit_core,
917 .set_floor_freq = set_floor_freq_default,
918 .compute_time_window = rapl_compute_time_window_core,
919 .dram_domain_energy_unit = 15300,
920};
921
Ajay Thomas51b63402015-04-30 01:43:23 +0530922static const struct rapl_defaults rapl_defaults_byt = {
923 .floor_freq_reg_addr = IOSF_CPU_POWER_BUDGET_CTL_BYT,
Jacob Pan3c2c0842014-11-07 09:29:26 -0800924 .check_unit = rapl_check_unit_atom,
925 .set_floor_freq = set_floor_freq_atom,
926 .compute_time_window = rapl_compute_time_window_atom,
Jacob Pan087e9cb2014-11-07 09:29:25 -0800927};
928
Ajay Thomas51b63402015-04-30 01:43:23 +0530929static const struct rapl_defaults rapl_defaults_tng = {
930 .floor_freq_reg_addr = IOSF_CPU_POWER_BUDGET_CTL_TNG,
931 .check_unit = rapl_check_unit_atom,
932 .set_floor_freq = set_floor_freq_atom,
933 .compute_time_window = rapl_compute_time_window_atom,
934};
935
936static const struct rapl_defaults rapl_defaults_ann = {
937 .floor_freq_reg_addr = 0,
938 .check_unit = rapl_check_unit_atom,
939 .set_floor_freq = NULL,
940 .compute_time_window = rapl_compute_time_window_atom,
941};
942
943static const struct rapl_defaults rapl_defaults_cht = {
944 .floor_freq_reg_addr = 0,
945 .check_unit = rapl_check_unit_atom,
946 .set_floor_freq = NULL,
947 .compute_time_window = rapl_compute_time_window_atom,
948};
949
Mathias Krauseea85dbc2015-03-25 22:15:52 +0100950static const struct x86_cpu_id rapl_ids[] __initconst = {
Thomas Gleixnerf0722512020-03-20 14:14:03 +0100951 X86_MATCH_INTEL_FAM6_MODEL(SANDYBRIDGE, &rapl_defaults_core),
952 X86_MATCH_INTEL_FAM6_MODEL(SANDYBRIDGE_X, &rapl_defaults_core),
Dave Hansen0bb04b52016-06-02 17:19:37 -0700953
Thomas Gleixnerf0722512020-03-20 14:14:03 +0100954 X86_MATCH_INTEL_FAM6_MODEL(IVYBRIDGE, &rapl_defaults_core),
955 X86_MATCH_INTEL_FAM6_MODEL(IVYBRIDGE_X, &rapl_defaults_core),
Dave Hansen0bb04b52016-06-02 17:19:37 -0700956
Thomas Gleixnerf0722512020-03-20 14:14:03 +0100957 X86_MATCH_INTEL_FAM6_MODEL(HASWELL, &rapl_defaults_core),
958 X86_MATCH_INTEL_FAM6_MODEL(HASWELL_L, &rapl_defaults_core),
959 X86_MATCH_INTEL_FAM6_MODEL(HASWELL_G, &rapl_defaults_core),
960 X86_MATCH_INTEL_FAM6_MODEL(HASWELL_X, &rapl_defaults_hsw_server),
Dave Hansen0bb04b52016-06-02 17:19:37 -0700961
Thomas Gleixnerf0722512020-03-20 14:14:03 +0100962 X86_MATCH_INTEL_FAM6_MODEL(BROADWELL, &rapl_defaults_core),
963 X86_MATCH_INTEL_FAM6_MODEL(BROADWELL_G, &rapl_defaults_core),
964 X86_MATCH_INTEL_FAM6_MODEL(BROADWELL_D, &rapl_defaults_core),
965 X86_MATCH_INTEL_FAM6_MODEL(BROADWELL_X, &rapl_defaults_hsw_server),
Dave Hansen0bb04b52016-06-02 17:19:37 -0700966
Thomas Gleixnerf0722512020-03-20 14:14:03 +0100967 X86_MATCH_INTEL_FAM6_MODEL(SKYLAKE, &rapl_defaults_core),
968 X86_MATCH_INTEL_FAM6_MODEL(SKYLAKE_L, &rapl_defaults_core),
969 X86_MATCH_INTEL_FAM6_MODEL(SKYLAKE_X, &rapl_defaults_hsw_server),
970 X86_MATCH_INTEL_FAM6_MODEL(KABYLAKE_L, &rapl_defaults_core),
971 X86_MATCH_INTEL_FAM6_MODEL(KABYLAKE, &rapl_defaults_core),
972 X86_MATCH_INTEL_FAM6_MODEL(CANNONLAKE_L, &rapl_defaults_core),
973 X86_MATCH_INTEL_FAM6_MODEL(ICELAKE_L, &rapl_defaults_core),
974 X86_MATCH_INTEL_FAM6_MODEL(ICELAKE, &rapl_defaults_core),
975 X86_MATCH_INTEL_FAM6_MODEL(ICELAKE_NNPI, &rapl_defaults_core),
976 X86_MATCH_INTEL_FAM6_MODEL(ICELAKE_X, &rapl_defaults_hsw_server),
977 X86_MATCH_INTEL_FAM6_MODEL(ICELAKE_D, &rapl_defaults_hsw_server),
978 X86_MATCH_INTEL_FAM6_MODEL(COMETLAKE_L, &rapl_defaults_core),
979 X86_MATCH_INTEL_FAM6_MODEL(COMETLAKE, &rapl_defaults_core),
980 X86_MATCH_INTEL_FAM6_MODEL(TIGERLAKE_L, &rapl_defaults_core),
Dave Hansen0bb04b52016-06-02 17:19:37 -0700981
Thomas Gleixnerf0722512020-03-20 14:14:03 +0100982 X86_MATCH_INTEL_FAM6_MODEL(ATOM_SILVERMONT, &rapl_defaults_byt),
983 X86_MATCH_INTEL_FAM6_MODEL(ATOM_AIRMONT, &rapl_defaults_cht),
984 X86_MATCH_INTEL_FAM6_MODEL(ATOM_SILVERMONT_MID, &rapl_defaults_tng),
985 X86_MATCH_INTEL_FAM6_MODEL(ATOM_AIRMONT_MID, &rapl_defaults_ann),
986 X86_MATCH_INTEL_FAM6_MODEL(ATOM_GOLDMONT, &rapl_defaults_core),
987 X86_MATCH_INTEL_FAM6_MODEL(ATOM_GOLDMONT_PLUS, &rapl_defaults_core),
988 X86_MATCH_INTEL_FAM6_MODEL(ATOM_GOLDMONT_D, &rapl_defaults_core),
Jacob Pan33c98002020-05-15 15:30:41 +0800989 X86_MATCH_INTEL_FAM6_MODEL(ATOM_TREMONT, &rapl_defaults_core),
Thomas Gleixnerf0722512020-03-20 14:14:03 +0100990 X86_MATCH_INTEL_FAM6_MODEL(ATOM_TREMONT_D, &rapl_defaults_core),
991 X86_MATCH_INTEL_FAM6_MODEL(ATOM_TREMONT_L, &rapl_defaults_core),
Dave Hansen0bb04b52016-06-02 17:19:37 -0700992
Thomas Gleixnerf0722512020-03-20 14:14:03 +0100993 X86_MATCH_INTEL_FAM6_MODEL(XEON_PHI_KNL, &rapl_defaults_hsw_server),
994 X86_MATCH_INTEL_FAM6_MODEL(XEON_PHI_KNM, &rapl_defaults_hsw_server),
Jacob Pan2d281d82013-10-17 10:28:35 -0700995 {}
996};
997MODULE_DEVICE_TABLE(x86cpu, rapl_ids);
998
Thomas Gleixnerbed5ab62016-11-22 21:15:58 +0000999/* Read once for all raw primitive data for domains */
1000static void rapl_update_domain_data(struct rapl_package *rp)
Jacob Pan2d281d82013-10-17 10:28:35 -07001001{
1002 int dmn, prim;
1003 u64 val;
Jacob Pan2d281d82013-10-17 10:28:35 -07001004
Thomas Gleixnerbed5ab62016-11-22 21:15:58 +00001005 for (dmn = 0; dmn < rp->nr_domains; dmn++) {
Zhang Rui9ea76122019-05-13 13:58:53 -04001006 pr_debug("update %s domain %s data\n", rp->name,
Thomas Gleixnerbed5ab62016-11-22 21:15:58 +00001007 rp->domains[dmn].name);
1008 /* exclude non-raw primitives */
1009 for (prim = 0; prim < NR_RAW_PRIMITIVES; prim++) {
1010 if (!rapl_read_data_raw(&rp->domains[dmn], prim,
1011 rpi[prim].unit, &val))
Zhang Rui33823882019-07-10 21:44:30 +08001012 rp->domains[dmn].rdd.primitives[prim] = val;
Jacob Pan2d281d82013-10-17 10:28:35 -07001013 }
1014 }
1015
1016}
1017
Jacob Pan2d281d82013-10-17 10:28:35 -07001018static int rapl_package_register_powercap(struct rapl_package *rp)
1019{
1020 struct rapl_domain *rd;
Jacob Pan2d281d82013-10-17 10:28:35 -07001021 struct powercap_zone *power_zone = NULL;
Luis de Bethencourt01857cf2018-01-17 10:30:34 +00001022 int nr_pl, ret;
Thomas Gleixnerbed5ab62016-11-22 21:15:58 +00001023
1024 /* Update the domain data of the new package */
1025 rapl_update_domain_data(rp);
Jacob Pan2d281d82013-10-17 10:28:35 -07001026
Zhang Rui33823882019-07-10 21:44:30 +08001027 /* first we register package domain as the parent zone */
Jacob Pan2d281d82013-10-17 10:28:35 -07001028 for (rd = rp->domains; rd < rp->domains + rp->nr_domains; rd++) {
1029 if (rd->id == RAPL_DOMAIN_PACKAGE) {
1030 nr_pl = find_nr_power_limit(rd);
Zhang Rui9ea76122019-05-13 13:58:53 -04001031 pr_debug("register package domain %s\n", rp->name);
Jacob Pan2d281d82013-10-17 10:28:35 -07001032 power_zone = powercap_register_zone(&rd->power_zone,
Zhang Rui33823882019-07-10 21:44:30 +08001033 rp->priv->control_type, rp->name,
1034 NULL, &zone_ops[rd->id], nr_pl,
1035 &constraint_ops);
Jacob Pan2d281d82013-10-17 10:28:35 -07001036 if (IS_ERR(power_zone)) {
Zhang Rui9ea76122019-05-13 13:58:53 -04001037 pr_debug("failed to register power zone %s\n",
Zhang Rui33823882019-07-10 21:44:30 +08001038 rp->name);
Thomas Gleixnerbed5ab62016-11-22 21:15:58 +00001039 return PTR_ERR(power_zone);
Jacob Pan2d281d82013-10-17 10:28:35 -07001040 }
1041 /* track parent zone in per package/socket data */
1042 rp->power_zone = power_zone;
1043 /* done, only one package domain per socket */
1044 break;
1045 }
1046 }
1047 if (!power_zone) {
1048 pr_err("no package domain found, unknown topology!\n");
Thomas Gleixnerbed5ab62016-11-22 21:15:58 +00001049 return -ENODEV;
Jacob Pan2d281d82013-10-17 10:28:35 -07001050 }
Zhang Rui33823882019-07-10 21:44:30 +08001051 /* now register domains as children of the socket/package */
Jacob Pan2d281d82013-10-17 10:28:35 -07001052 for (rd = rp->domains; rd < rp->domains + rp->nr_domains; rd++) {
1053 if (rd->id == RAPL_DOMAIN_PACKAGE)
1054 continue;
1055 /* number of power limits per domain varies */
1056 nr_pl = find_nr_power_limit(rd);
1057 power_zone = powercap_register_zone(&rd->power_zone,
Zhang Rui33823882019-07-10 21:44:30 +08001058 rp->priv->control_type,
1059 rd->name, rp->power_zone,
1060 &zone_ops[rd->id], nr_pl,
1061 &constraint_ops);
Jacob Pan2d281d82013-10-17 10:28:35 -07001062
1063 if (IS_ERR(power_zone)) {
Zhang Rui9ea76122019-05-13 13:58:53 -04001064 pr_debug("failed to register power_zone, %s:%s\n",
Zhang Rui33823882019-07-10 21:44:30 +08001065 rp->name, rd->name);
Jacob Pan2d281d82013-10-17 10:28:35 -07001066 ret = PTR_ERR(power_zone);
1067 goto err_cleanup;
1068 }
1069 }
Thomas Gleixnerbed5ab62016-11-22 21:15:58 +00001070 return 0;
Jacob Pan2d281d82013-10-17 10:28:35 -07001071
Jacob Pan2d281d82013-10-17 10:28:35 -07001072err_cleanup:
Thomas Gleixner58705062016-11-22 21:16:02 +00001073 /*
1074 * Clean up previously initialized domains within the package if we
Jacob Pan2d281d82013-10-17 10:28:35 -07001075 * failed after the first domain setup.
1076 */
1077 while (--rd >= rp->domains) {
Zhang Rui9ea76122019-05-13 13:58:53 -04001078 pr_debug("unregister %s domain %s\n", rp->name, rd->name);
Zhang Rui33823882019-07-10 21:44:30 +08001079 powercap_unregister_zone(rp->priv->control_type,
1080 &rd->power_zone);
Jacob Pan2d281d82013-10-17 10:28:35 -07001081 }
1082
1083 return ret;
1084}
1085
Zhang Rui33823882019-07-10 21:44:30 +08001086int rapl_add_platform_domain(struct rapl_if_priv *priv)
Srinivas Pandruvada3521ba12016-04-17 15:03:01 -07001087{
1088 struct rapl_domain *rd;
1089 struct powercap_zone *power_zone;
Zhang Rui8a006762019-07-10 21:44:28 +08001090 struct reg_action ra;
1091 int ret;
Srinivas Pandruvada3521ba12016-04-17 15:03:01 -07001092
Zhang Rui8a006762019-07-10 21:44:28 +08001093 ra.reg = priv->regs[RAPL_DOMAIN_PLATFORM][RAPL_DOMAIN_REG_STATUS];
1094 ra.mask = ~0;
1095 ret = priv->read_raw(0, &ra);
1096 if (ret || !ra.value)
Srinivas Pandruvada3521ba12016-04-17 15:03:01 -07001097 return -ENODEV;
1098
Zhang Rui8a006762019-07-10 21:44:28 +08001099 ra.reg = priv->regs[RAPL_DOMAIN_PLATFORM][RAPL_DOMAIN_REG_LIMIT];
1100 ra.mask = ~0;
1101 ret = priv->read_raw(0, &ra);
1102 if (ret || !ra.value)
Srinivas Pandruvada3521ba12016-04-17 15:03:01 -07001103 return -ENODEV;
1104
1105 rd = kzalloc(sizeof(*rd), GFP_KERNEL);
1106 if (!rd)
1107 return -ENOMEM;
1108
1109 rd->name = rapl_domain_names[RAPL_DOMAIN_PLATFORM];
1110 rd->id = RAPL_DOMAIN_PLATFORM;
Zhang Rui33823882019-07-10 21:44:30 +08001111 rd->regs[RAPL_DOMAIN_REG_LIMIT] =
1112 priv->regs[RAPL_DOMAIN_PLATFORM][RAPL_DOMAIN_REG_LIMIT];
1113 rd->regs[RAPL_DOMAIN_REG_STATUS] =
1114 priv->regs[RAPL_DOMAIN_PLATFORM][RAPL_DOMAIN_REG_STATUS];
Srinivas Pandruvada3521ba12016-04-17 15:03:01 -07001115 rd->rpl[0].prim_id = PL1_ENABLE;
1116 rd->rpl[0].name = pl1_name;
1117 rd->rpl[1].prim_id = PL2_ENABLE;
1118 rd->rpl[1].name = pl2_name;
Zhang Rui8a006762019-07-10 21:44:28 +08001119 rd->rp = rapl_find_package_domain(0, priv);
Srinivas Pandruvada3521ba12016-04-17 15:03:01 -07001120
Zhang Rui8a006762019-07-10 21:44:28 +08001121 power_zone = powercap_register_zone(&rd->power_zone, priv->control_type,
Srinivas Pandruvada3521ba12016-04-17 15:03:01 -07001122 "psys", NULL,
1123 &zone_ops[RAPL_DOMAIN_PLATFORM],
1124 2, &constraint_ops);
1125
1126 if (IS_ERR(power_zone)) {
1127 kfree(rd);
1128 return PTR_ERR(power_zone);
1129 }
1130
Zhang Rui8a006762019-07-10 21:44:28 +08001131 priv->platform_rapl_domain = rd;
Srinivas Pandruvada3521ba12016-04-17 15:03:01 -07001132
1133 return 0;
1134}
Zhang Rui33823882019-07-10 21:44:30 +08001135EXPORT_SYMBOL_GPL(rapl_add_platform_domain);
Srinivas Pandruvada3521ba12016-04-17 15:03:01 -07001136
Zhang Rui33823882019-07-10 21:44:30 +08001137void rapl_remove_platform_domain(struct rapl_if_priv *priv)
Jacob Pan2d281d82013-10-17 10:28:35 -07001138{
Zhang Rui8a006762019-07-10 21:44:28 +08001139 if (priv->platform_rapl_domain) {
1140 powercap_unregister_zone(priv->control_type,
Zhang Rui33823882019-07-10 21:44:30 +08001141 &priv->platform_rapl_domain->power_zone);
Zhang Rui8a006762019-07-10 21:44:28 +08001142 kfree(priv->platform_rapl_domain);
Jacob Pan2d281d82013-10-17 10:28:35 -07001143 }
Jacob Pan2d281d82013-10-17 10:28:35 -07001144}
Zhang Rui33823882019-07-10 21:44:30 +08001145EXPORT_SYMBOL_GPL(rapl_remove_platform_domain);
Jacob Pan2d281d82013-10-17 10:28:35 -07001146
Zhang Rui7fde2712019-07-10 21:44:26 +08001147static int rapl_check_domain(int cpu, int domain, struct rapl_package *rp)
Jacob Pan2d281d82013-10-17 10:28:35 -07001148{
Zhang Rui1193b162019-07-10 21:44:29 +08001149 struct reg_action ra;
Jacob Pan2d281d82013-10-17 10:28:35 -07001150
1151 switch (domain) {
1152 case RAPL_DOMAIN_PACKAGE:
Jacob Pan2d281d82013-10-17 10:28:35 -07001153 case RAPL_DOMAIN_PP0:
Jacob Pan2d281d82013-10-17 10:28:35 -07001154 case RAPL_DOMAIN_PP1:
Jacob Pan2d281d82013-10-17 10:28:35 -07001155 case RAPL_DOMAIN_DRAM:
Zhang Rui1193b162019-07-10 21:44:29 +08001156 ra.reg = rp->priv->regs[domain][RAPL_DOMAIN_REG_STATUS];
Jacob Pan2d281d82013-10-17 10:28:35 -07001157 break;
Srinivas Pandruvada3521ba12016-04-17 15:03:01 -07001158 case RAPL_DOMAIN_PLATFORM:
1159 /* PSYS(PLATFORM) is not a CPU domain, so avoid printng error */
1160 return -EINVAL;
Jacob Pan2d281d82013-10-17 10:28:35 -07001161 default:
1162 pr_err("invalid domain id %d\n", domain);
1163 return -EINVAL;
1164 }
Jacob Pan9d31c672014-04-29 15:33:06 -07001165 /* make sure domain counters are available and contains non-zero
1166 * values, otherwise skip it.
1167 */
Zhang Rui1193b162019-07-10 21:44:29 +08001168
1169 ra.mask = ~0;
1170 if (rp->priv->read_raw(cpu, &ra) || !ra.value)
Jacob Pan2d281d82013-10-17 10:28:35 -07001171 return -ENODEV;
1172
Jacob Pan9d31c672014-04-29 15:33:06 -07001173 return 0;
Jacob Pan2d281d82013-10-17 10:28:35 -07001174}
1175
Jacob Pane1399ba2016-05-31 13:41:29 -07001176/*
1177 * Check if power limits are available. Two cases when they are not available:
1178 * 1. Locked by BIOS, in this case we still provide read-only access so that
1179 * users can see what limit is set by the BIOS.
1180 * 2. Some CPUs make some domains monitoring only which means PLx MSRs may not
Zhang Rui33823882019-07-10 21:44:30 +08001181 * exist at all. In this case, we do not show the constraints in powercap.
Jacob Pane1399ba2016-05-31 13:41:29 -07001182 *
1183 * Called after domains are detected and initialized.
1184 */
1185static void rapl_detect_powerlimit(struct rapl_domain *rd)
1186{
1187 u64 val64;
1188 int i;
1189
1190 /* check if the domain is locked by BIOS, ignore if MSR doesn't exist */
1191 if (!rapl_read_data_raw(rd, FW_LOCK, false, &val64)) {
1192 if (val64) {
Zhang Rui9ea76122019-05-13 13:58:53 -04001193 pr_info("RAPL %s domain %s locked by BIOS\n",
1194 rd->rp->name, rd->name);
Jacob Pane1399ba2016-05-31 13:41:29 -07001195 rd->state |= DOMAIN_STATE_BIOS_LOCKED;
1196 }
1197 }
Zhang Rui33823882019-07-10 21:44:30 +08001198 /* check if power limit MSR exists, otherwise domain is monitoring only */
Jacob Pane1399ba2016-05-31 13:41:29 -07001199 for (i = 0; i < NR_POWER_LIMITS; i++) {
1200 int prim = rd->rpl[i].prim_id;
Zhang Rui33823882019-07-10 21:44:30 +08001201
Jacob Pane1399ba2016-05-31 13:41:29 -07001202 if (rapl_read_data_raw(rd, prim, false, &val64))
1203 rd->rpl[i].name = NULL;
1204 }
1205}
1206
Jacob Pan2d281d82013-10-17 10:28:35 -07001207/* Detect active and valid domains for the given CPU, caller must
1208 * ensure the CPU belongs to the targeted package and CPU hotlug is disabled.
1209 */
1210static int rapl_detect_domains(struct rapl_package *rp, int cpu)
1211{
Jacob Pan2d281d82013-10-17 10:28:35 -07001212 struct rapl_domain *rd;
Thomas Gleixner58705062016-11-22 21:16:02 +00001213 int i;
Jacob Pan2d281d82013-10-17 10:28:35 -07001214
1215 for (i = 0; i < RAPL_DOMAIN_MAX; i++) {
1216 /* use physical package id to read counters */
Zhang Rui7fde2712019-07-10 21:44:26 +08001217 if (!rapl_check_domain(cpu, i, rp)) {
Jacob Pan2d281d82013-10-17 10:28:35 -07001218 rp->domain_map |= 1 << i;
Jacob Panfcdf1792014-09-02 02:55:21 -07001219 pr_info("Found RAPL domain %s\n", rapl_domain_names[i]);
1220 }
Jacob Pan2d281d82013-10-17 10:28:35 -07001221 }
Zhang Rui33823882019-07-10 21:44:30 +08001222 rp->nr_domains = bitmap_weight(&rp->domain_map, RAPL_DOMAIN_MAX);
Jacob Pan2d281d82013-10-17 10:28:35 -07001223 if (!rp->nr_domains) {
Zhang Rui9ea76122019-05-13 13:58:53 -04001224 pr_debug("no valid rapl domains found in %s\n", rp->name);
Thomas Gleixner58705062016-11-22 21:16:02 +00001225 return -ENODEV;
Jacob Pan2d281d82013-10-17 10:28:35 -07001226 }
Zhang Rui9ea76122019-05-13 13:58:53 -04001227 pr_debug("found %d domains on %s\n", rp->nr_domains, rp->name);
Jacob Pan2d281d82013-10-17 10:28:35 -07001228
1229 rp->domains = kcalloc(rp->nr_domains + 1, sizeof(struct rapl_domain),
Zhang Rui33823882019-07-10 21:44:30 +08001230 GFP_KERNEL);
Thomas Gleixner58705062016-11-22 21:16:02 +00001231 if (!rp->domains)
1232 return -ENOMEM;
1233
Jacob Pan2d281d82013-10-17 10:28:35 -07001234 rapl_init_domains(rp);
1235
Jacob Pane1399ba2016-05-31 13:41:29 -07001236 for (rd = rp->domains; rd < rp->domains + rp->nr_domains; rd++)
1237 rapl_detect_powerlimit(rd);
1238
Jacob Pan2d281d82013-10-17 10:28:35 -07001239 return 0;
1240}
1241
1242/* called from CPU hotplug notifier, hotplug lock held */
Zhang Rui33823882019-07-10 21:44:30 +08001243void rapl_remove_package(struct rapl_package *rp)
Jacob Pan2d281d82013-10-17 10:28:35 -07001244{
1245 struct rapl_domain *rd, *rd_package = NULL;
1246
Thomas Gleixner58705062016-11-22 21:16:02 +00001247 package_power_limit_irq_restore(rp);
1248
Jacob Pan2d281d82013-10-17 10:28:35 -07001249 for (rd = rp->domains; rd < rp->domains + rp->nr_domains; rd++) {
Thomas Gleixner58705062016-11-22 21:16:02 +00001250 rapl_write_data_raw(rd, PL1_ENABLE, 0);
1251 rapl_write_data_raw(rd, PL1_CLAMP, 0);
1252 if (find_nr_power_limit(rd) > 1) {
1253 rapl_write_data_raw(rd, PL2_ENABLE, 0);
1254 rapl_write_data_raw(rd, PL2_CLAMP, 0);
1255 }
Jacob Pan2d281d82013-10-17 10:28:35 -07001256 if (rd->id == RAPL_DOMAIN_PACKAGE) {
1257 rd_package = rd;
1258 continue;
1259 }
Zhang Rui9ea76122019-05-13 13:58:53 -04001260 pr_debug("remove package, undo power limit on %s: %s\n",
1261 rp->name, rd->name);
Zhang Rui33823882019-07-10 21:44:30 +08001262 powercap_unregister_zone(rp->priv->control_type,
1263 &rd->power_zone);
Jacob Pan2d281d82013-10-17 10:28:35 -07001264 }
1265 /* do parent zone last */
Zhang Rui33823882019-07-10 21:44:30 +08001266 powercap_unregister_zone(rp->priv->control_type,
1267 &rd_package->power_zone);
Jacob Pan2d281d82013-10-17 10:28:35 -07001268 list_del(&rp->plist);
1269 kfree(rp);
1270}
Zhang Rui33823882019-07-10 21:44:30 +08001271EXPORT_SYMBOL_GPL(rapl_remove_package);
1272
1273/* caller to ensure CPU hotplug lock is held */
1274struct rapl_package *rapl_find_package_domain(int cpu, struct rapl_if_priv *priv)
1275{
1276 int id = topology_logical_die_id(cpu);
1277 struct rapl_package *rp;
1278
1279 list_for_each_entry(rp, &rapl_packages, plist) {
1280 if (rp->id == id
1281 && rp->priv->control_type == priv->control_type)
1282 return rp;
1283 }
1284
1285 return NULL;
1286}
1287EXPORT_SYMBOL_GPL(rapl_find_package_domain);
Jacob Pan2d281d82013-10-17 10:28:35 -07001288
1289/* called from CPU hotplug notifier, hotplug lock held */
Zhang Rui33823882019-07-10 21:44:30 +08001290struct rapl_package *rapl_add_package(int cpu, struct rapl_if_priv *priv)
Jacob Pan2d281d82013-10-17 10:28:35 -07001291{
Zhang Rui32fb4802019-05-13 13:58:51 -04001292 int id = topology_logical_die_id(cpu);
Jacob Pan2d281d82013-10-17 10:28:35 -07001293 struct rapl_package *rp;
Zhang Rui9ea76122019-05-13 13:58:53 -04001294 struct cpuinfo_x86 *c = &cpu_data(cpu);
Thomas Gleixnerb4005e92016-11-22 21:16:05 +00001295 int ret;
Jacob Pan2d281d82013-10-17 10:28:35 -07001296
Harry Pan3aa3c582019-12-30 22:36:56 +08001297 if (!rapl_defaults)
1298 return ERR_PTR(-ENODEV);
1299
Jacob Pan2d281d82013-10-17 10:28:35 -07001300 rp = kzalloc(sizeof(struct rapl_package), GFP_KERNEL);
1301 if (!rp)
Thomas Gleixnerb4005e92016-11-22 21:16:05 +00001302 return ERR_PTR(-ENOMEM);
Jacob Pan2d281d82013-10-17 10:28:35 -07001303
1304 /* add the new package to the list */
Zhang Ruiaadf7b32019-05-13 13:58:50 -04001305 rp->id = id;
Jacob Pan323ee642016-02-24 13:31:38 -08001306 rp->lead_cpu = cpu;
Zhang Rui7ebf8ef2019-07-10 21:44:25 +08001307 rp->priv = priv;
Jacob Pan323ee642016-02-24 13:31:38 -08001308
Zhang Rui9ea76122019-05-13 13:58:53 -04001309 if (topology_max_die_per_package() > 1)
1310 snprintf(rp->name, PACKAGE_DOMAIN_NAME_LENGTH,
Zhang Rui33823882019-07-10 21:44:30 +08001311 "package-%d-die-%d", c->phys_proc_id, c->cpu_die_id);
Zhang Rui9ea76122019-05-13 13:58:53 -04001312 else
1313 snprintf(rp->name, PACKAGE_DOMAIN_NAME_LENGTH, "package-%d",
Zhang Rui33823882019-07-10 21:44:30 +08001314 c->phys_proc_id);
Zhang Rui9ea76122019-05-13 13:58:53 -04001315
Jacob Pan2d281d82013-10-17 10:28:35 -07001316 /* check if the package contains valid domains */
Zhang Rui33823882019-07-10 21:44:30 +08001317 if (rapl_detect_domains(rp, cpu) || rapl_defaults->check_unit(rp, cpu)) {
Jacob Pan2d281d82013-10-17 10:28:35 -07001318 ret = -ENODEV;
1319 goto err_free_package;
1320 }
Thomas Gleixnera74f4362016-11-22 21:15:59 +00001321 ret = rapl_package_register_powercap(rp);
1322 if (!ret) {
Jacob Pan2d281d82013-10-17 10:28:35 -07001323 INIT_LIST_HEAD(&rp->plist);
1324 list_add(&rp->plist, &rapl_packages);
Thomas Gleixnerb4005e92016-11-22 21:16:05 +00001325 return rp;
Jacob Pan2d281d82013-10-17 10:28:35 -07001326 }
1327
1328err_free_package:
1329 kfree(rp->domains);
1330 kfree(rp);
Thomas Gleixnerb4005e92016-11-22 21:16:05 +00001331 return ERR_PTR(ret);
Jacob Pan2d281d82013-10-17 10:28:35 -07001332}
Zhang Rui33823882019-07-10 21:44:30 +08001333EXPORT_SYMBOL_GPL(rapl_add_package);
Jacob Pan2d281d82013-10-17 10:28:35 -07001334
Zhen Han52b36722018-01-10 08:38:23 +08001335static void power_limit_state_save(void)
1336{
1337 struct rapl_package *rp;
1338 struct rapl_domain *rd;
1339 int nr_pl, ret, i;
1340
1341 get_online_cpus();
1342 list_for_each_entry(rp, &rapl_packages, plist) {
1343 if (!rp->power_zone)
1344 continue;
1345 rd = power_zone_to_rapl_domain(rp->power_zone);
1346 nr_pl = find_nr_power_limit(rd);
1347 for (i = 0; i < nr_pl; i++) {
1348 switch (rd->rpl[i].prim_id) {
1349 case PL1_ENABLE:
1350 ret = rapl_read_data_raw(rd,
Zhang Rui33823882019-07-10 21:44:30 +08001351 POWER_LIMIT1, true,
1352 &rd->rpl[i].last_power_limit);
Zhen Han52b36722018-01-10 08:38:23 +08001353 if (ret)
1354 rd->rpl[i].last_power_limit = 0;
1355 break;
1356 case PL2_ENABLE:
1357 ret = rapl_read_data_raw(rd,
Zhang Rui33823882019-07-10 21:44:30 +08001358 POWER_LIMIT2, true,
1359 &rd->rpl[i].last_power_limit);
Zhen Han52b36722018-01-10 08:38:23 +08001360 if (ret)
1361 rd->rpl[i].last_power_limit = 0;
1362 break;
1363 }
1364 }
1365 }
1366 put_online_cpus();
1367}
1368
1369static void power_limit_state_restore(void)
1370{
1371 struct rapl_package *rp;
1372 struct rapl_domain *rd;
1373 int nr_pl, i;
1374
1375 get_online_cpus();
1376 list_for_each_entry(rp, &rapl_packages, plist) {
1377 if (!rp->power_zone)
1378 continue;
1379 rd = power_zone_to_rapl_domain(rp->power_zone);
1380 nr_pl = find_nr_power_limit(rd);
1381 for (i = 0; i < nr_pl; i++) {
1382 switch (rd->rpl[i].prim_id) {
1383 case PL1_ENABLE:
1384 if (rd->rpl[i].last_power_limit)
Zhang Rui33823882019-07-10 21:44:30 +08001385 rapl_write_data_raw(rd, POWER_LIMIT1,
1386 rd->rpl[i].last_power_limit);
Zhen Han52b36722018-01-10 08:38:23 +08001387 break;
1388 case PL2_ENABLE:
1389 if (rd->rpl[i].last_power_limit)
Zhang Rui33823882019-07-10 21:44:30 +08001390 rapl_write_data_raw(rd, POWER_LIMIT2,
1391 rd->rpl[i].last_power_limit);
Zhen Han52b36722018-01-10 08:38:23 +08001392 break;
1393 }
1394 }
1395 }
1396 put_online_cpus();
1397}
1398
1399static int rapl_pm_callback(struct notifier_block *nb,
Zhang Rui33823882019-07-10 21:44:30 +08001400 unsigned long mode, void *_unused)
Zhen Han52b36722018-01-10 08:38:23 +08001401{
1402 switch (mode) {
1403 case PM_SUSPEND_PREPARE:
1404 power_limit_state_save();
1405 break;
1406 case PM_POST_SUSPEND:
1407 power_limit_state_restore();
1408 break;
1409 }
1410 return NOTIFY_OK;
1411}
1412
1413static struct notifier_block rapl_pm_notifier = {
1414 .notifier_call = rapl_pm_callback,
1415};
1416
Zhang Ruiabcfaeb2019-07-10 21:44:34 +08001417static struct platform_device *rapl_msr_platdev;
1418
1419static int __init rapl_init(void)
Jacob Pan2d281d82013-10-17 10:28:35 -07001420{
Jacob Pan087e9cb2014-11-07 09:29:25 -08001421 const struct x86_cpu_id *id;
Thomas Gleixner58705062016-11-22 21:16:02 +00001422 int ret;
Jacob Pan2d281d82013-10-17 10:28:35 -07001423
Jacob Pan087e9cb2014-11-07 09:29:25 -08001424 id = x86_match_cpu(rapl_ids);
1425 if (!id) {
Jacob Pan2d281d82013-10-17 10:28:35 -07001426 pr_err("driver does not support CPU family %d model %d\n",
Zhang Rui33823882019-07-10 21:44:30 +08001427 boot_cpu_data.x86, boot_cpu_data.x86_model);
Jacob Pan2d281d82013-10-17 10:28:35 -07001428
1429 return -ENODEV;
1430 }
Srivatsa S. Bhat009f2252014-03-11 02:09:26 +05301431
Jacob Pan087e9cb2014-11-07 09:29:25 -08001432 rapl_defaults = (struct rapl_defaults *)id->driver_data;
1433
Zhen Han52b36722018-01-10 08:38:23 +08001434 ret = register_pm_notifier(&rapl_pm_notifier);
Zhang Ruiabcfaeb2019-07-10 21:44:34 +08001435 if (ret)
1436 return ret;
Zhen Han52b36722018-01-10 08:38:23 +08001437
Zhang Ruiabcfaeb2019-07-10 21:44:34 +08001438 rapl_msr_platdev = platform_device_alloc("intel_rapl_msr", 0);
1439 if (!rapl_msr_platdev) {
1440 ret = -ENOMEM;
1441 goto end;
1442 }
1443
1444 ret = platform_device_add(rapl_msr_platdev);
1445 if (ret)
1446 platform_device_put(rapl_msr_platdev);
1447
1448end:
1449 if (ret)
1450 unregister_pm_notifier(&rapl_pm_notifier);
1451
1452 return ret;
Jacob Pan2d281d82013-10-17 10:28:35 -07001453}
1454
Zhang Ruiabcfaeb2019-07-10 21:44:34 +08001455static void __exit rapl_exit(void)
Jacob Pan2d281d82013-10-17 10:28:35 -07001456{
Zhang Ruiabcfaeb2019-07-10 21:44:34 +08001457 platform_device_unregister(rapl_msr_platdev);
Zhen Han52b36722018-01-10 08:38:23 +08001458 unregister_pm_notifier(&rapl_pm_notifier);
Jacob Pan2d281d82013-10-17 10:28:35 -07001459}
1460
Zhang Ruif76cb062019-07-19 23:25:14 +08001461fs_initcall(rapl_init);
Zhang Ruiabcfaeb2019-07-10 21:44:34 +08001462module_exit(rapl_exit);
1463
Zhang Rui33823882019-07-10 21:44:30 +08001464MODULE_DESCRIPTION("Intel Runtime Average Power Limit (RAPL) common code");
Jacob Pan2d281d82013-10-17 10:28:35 -07001465MODULE_AUTHOR("Jacob Pan <jacob.jun.pan@intel.com>");
1466MODULE_LICENSE("GPL v2");