blob: b739ce4f390de61500c398944d771f1d4d28e7ac [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;
Zhang Rui2d798d92020-06-29 13:34:50 +080096 unsigned int psys_domain_energy_unit;
Jacob Pan087e9cb2014-11-07 09:29:25 -080097};
98static struct rapl_defaults *rapl_defaults;
99
Jacob Pan3c2c0842014-11-07 09:29:26 -0800100/* Sideband MBI registers */
Ajay Thomas51b63402015-04-30 01:43:23 +0530101#define IOSF_CPU_POWER_BUDGET_CTL_BYT (0x2)
102#define IOSF_CPU_POWER_BUDGET_CTL_TNG (0xdf)
Jacob Pan3c2c0842014-11-07 09:29:26 -0800103
Jacob Pan2d281d82013-10-17 10:28:35 -0700104#define PACKAGE_PLN_INT_SAVED BIT(0)
105#define MAX_PRIM_NAME (32)
106
107/* per domain data. used to describe individual knobs such that access function
108 * can be consolidated into one instead of many inline functions.
109 */
110struct rapl_primitive_info {
111 const char *name;
112 u64 mask;
113 int shift;
Zhang Ruif7c4e0c2019-07-10 21:44:22 +0800114 enum rapl_domain_reg_id id;
Jacob Pan2d281d82013-10-17 10:28:35 -0700115 enum unit_type unit;
116 u32 flag;
117};
118
119#define PRIMITIVE_INFO_INIT(p, m, s, i, u, f) { \
120 .name = #p, \
121 .mask = m, \
122 .shift = s, \
123 .id = i, \
124 .unit = u, \
125 .flag = f \
126 }
127
128static void rapl_init_domains(struct rapl_package *rp);
129static int rapl_read_data_raw(struct rapl_domain *rd,
Zhang Rui33823882019-07-10 21:44:30 +0800130 enum rapl_primitives prim,
131 bool xlate, u64 *data);
Jacob Pan2d281d82013-10-17 10:28:35 -0700132static int rapl_write_data_raw(struct rapl_domain *rd,
Zhang Rui33823882019-07-10 21:44:30 +0800133 enum rapl_primitives prim,
134 unsigned long long value);
Jacob Pan309557f2016-02-24 13:31:37 -0800135static u64 rapl_unit_xlate(struct rapl_domain *rd,
Zhang Rui33823882019-07-10 21:44:30 +0800136 enum unit_type type, u64 value, int to_raw);
Jacob Pan309557f2016-02-24 13:31:37 -0800137static void package_power_limit_irq_save(struct rapl_package *rp);
Jacob Pan2d281d82013-10-17 10:28:35 -0700138
Zhang Rui33823882019-07-10 21:44:30 +0800139static LIST_HEAD(rapl_packages); /* guarded by CPU hotplug lock */
Jacob Pan2d281d82013-10-17 10:28:35 -0700140
Zhang Rui33823882019-07-10 21:44:30 +0800141static const char *const rapl_domain_names[] = {
Jacob Pan2d281d82013-10-17 10:28:35 -0700142 "package",
143 "core",
144 "uncore",
145 "dram",
Srinivas Pandruvada3521ba12016-04-17 15:03:01 -0700146 "psys",
Jacob Pan2d281d82013-10-17 10:28:35 -0700147};
148
Zhang Rui33823882019-07-10 21:44:30 +0800149static int get_energy_counter(struct powercap_zone *power_zone,
150 u64 *energy_raw)
Jacob Pan2d281d82013-10-17 10:28:35 -0700151{
152 struct rapl_domain *rd;
153 u64 energy_now;
154
155 /* prevent CPU hotplug, make sure the RAPL domain does not go
156 * away while reading the counter.
157 */
158 get_online_cpus();
159 rd = power_zone_to_rapl_domain(power_zone);
160
161 if (!rapl_read_data_raw(rd, ENERGY_COUNTER, true, &energy_now)) {
162 *energy_raw = energy_now;
163 put_online_cpus();
164
165 return 0;
166 }
167 put_online_cpus();
168
169 return -EIO;
170}
171
172static int get_max_energy_counter(struct powercap_zone *pcd_dev, u64 *energy)
173{
Jacob Pand474a4d2015-03-13 03:48:56 -0700174 struct rapl_domain *rd = power_zone_to_rapl_domain(pcd_dev);
175
Jacob Pan309557f2016-02-24 13:31:37 -0800176 *energy = rapl_unit_xlate(rd, ENERGY_UNIT, ENERGY_STATUS_MASK, 0);
Jacob Pan2d281d82013-10-17 10:28:35 -0700177 return 0;
178}
179
180static int release_zone(struct powercap_zone *power_zone)
181{
182 struct rapl_domain *rd = power_zone_to_rapl_domain(power_zone);
Jacob Pan309557f2016-02-24 13:31:37 -0800183 struct rapl_package *rp = rd->rp;
Jacob Pan2d281d82013-10-17 10:28:35 -0700184
185 /* package zone is the last zone of a package, we can free
186 * memory here since all children has been unregistered.
187 */
188 if (rd->id == RAPL_DOMAIN_PACKAGE) {
Jacob Pan2d281d82013-10-17 10:28:35 -0700189 kfree(rd);
190 rp->domains = NULL;
191 }
192
193 return 0;
194
195}
196
197static int find_nr_power_limit(struct rapl_domain *rd)
198{
Jacob Pane1399ba2016-05-31 13:41:29 -0700199 int i, nr_pl = 0;
Jacob Pan2d281d82013-10-17 10:28:35 -0700200
201 for (i = 0; i < NR_POWER_LIMITS; i++) {
Jacob Pane1399ba2016-05-31 13:41:29 -0700202 if (rd->rpl[i].name)
203 nr_pl++;
Jacob Pan2d281d82013-10-17 10:28:35 -0700204 }
205
Jacob Pane1399ba2016-05-31 13:41:29 -0700206 return nr_pl;
Jacob Pan2d281d82013-10-17 10:28:35 -0700207}
208
209static int set_domain_enable(struct powercap_zone *power_zone, bool mode)
210{
211 struct rapl_domain *rd = power_zone_to_rapl_domain(power_zone);
Jacob Pan2d281d82013-10-17 10:28:35 -0700212
213 if (rd->state & DOMAIN_STATE_BIOS_LOCKED)
214 return -EACCES;
Jacob Pan3c2c0842014-11-07 09:29:26 -0800215
Jacob Pan2d281d82013-10-17 10:28:35 -0700216 get_online_cpus();
Jacob Pan2d281d82013-10-17 10:28:35 -0700217 rapl_write_data_raw(rd, PL1_ENABLE, mode);
Ajay Thomas51b63402015-04-30 01:43:23 +0530218 if (rapl_defaults->set_floor_freq)
219 rapl_defaults->set_floor_freq(rd, mode);
Jacob Pan2d281d82013-10-17 10:28:35 -0700220 put_online_cpus();
221
222 return 0;
223}
224
225static int get_domain_enable(struct powercap_zone *power_zone, bool *mode)
226{
227 struct rapl_domain *rd = power_zone_to_rapl_domain(power_zone);
228 u64 val;
229
230 if (rd->state & DOMAIN_STATE_BIOS_LOCKED) {
231 *mode = false;
232 return 0;
233 }
234 get_online_cpus();
235 if (rapl_read_data_raw(rd, PL1_ENABLE, true, &val)) {
236 put_online_cpus();
237 return -EIO;
238 }
239 *mode = val;
240 put_online_cpus();
241
242 return 0;
243}
244
245/* per RAPL domain ops, in the order of rapl_domain_type */
Julia Lawall600c3952015-12-23 22:59:55 +0100246static const struct powercap_zone_ops zone_ops[] = {
Jacob Pan2d281d82013-10-17 10:28:35 -0700247 /* RAPL_DOMAIN_PACKAGE */
248 {
Zhang Rui33823882019-07-10 21:44:30 +0800249 .get_energy_uj = get_energy_counter,
250 .get_max_energy_range_uj = get_max_energy_counter,
251 .release = release_zone,
252 .set_enable = set_domain_enable,
253 .get_enable = get_domain_enable,
254 },
Jacob Pan2d281d82013-10-17 10:28:35 -0700255 /* RAPL_DOMAIN_PP0 */
256 {
Zhang Rui33823882019-07-10 21:44:30 +0800257 .get_energy_uj = get_energy_counter,
258 .get_max_energy_range_uj = get_max_energy_counter,
259 .release = release_zone,
260 .set_enable = set_domain_enable,
261 .get_enable = get_domain_enable,
262 },
Jacob Pan2d281d82013-10-17 10:28:35 -0700263 /* RAPL_DOMAIN_PP1 */
264 {
Zhang Rui33823882019-07-10 21:44:30 +0800265 .get_energy_uj = get_energy_counter,
266 .get_max_energy_range_uj = get_max_energy_counter,
267 .release = release_zone,
268 .set_enable = set_domain_enable,
269 .get_enable = get_domain_enable,
270 },
Jacob Pan2d281d82013-10-17 10:28:35 -0700271 /* RAPL_DOMAIN_DRAM */
272 {
Zhang Rui33823882019-07-10 21:44:30 +0800273 .get_energy_uj = get_energy_counter,
274 .get_max_energy_range_uj = get_max_energy_counter,
275 .release = release_zone,
276 .set_enable = set_domain_enable,
277 .get_enable = get_domain_enable,
278 },
Srinivas Pandruvada3521ba12016-04-17 15:03:01 -0700279 /* RAPL_DOMAIN_PLATFORM */
280 {
Zhang Rui33823882019-07-10 21:44:30 +0800281 .get_energy_uj = get_energy_counter,
282 .get_max_energy_range_uj = get_max_energy_counter,
283 .release = release_zone,
284 .set_enable = set_domain_enable,
285 .get_enable = get_domain_enable,
286 },
Jacob Pan2d281d82013-10-17 10:28:35 -0700287};
288
Jacob Pane1399ba2016-05-31 13:41:29 -0700289/*
290 * Constraint index used by powercap can be different than power limit (PL)
Zhang Rui33823882019-07-10 21:44:30 +0800291 * index in that some PLs maybe missing due to non-existent MSRs. So we
Jacob Pane1399ba2016-05-31 13:41:29 -0700292 * need to convert here by finding the valid PLs only (name populated).
293 */
294static int contraint_to_pl(struct rapl_domain *rd, int cid)
295{
296 int i, j;
297
298 for (i = 0, j = 0; i < NR_POWER_LIMITS; i++) {
299 if ((rd->rpl[i].name) && j++ == cid) {
300 pr_debug("%s: index %d\n", __func__, i);
301 return i;
302 }
303 }
Jacob Pancb43f812016-11-28 13:53:11 -0800304 pr_err("Cannot find matching power limit for constraint %d\n", cid);
Jacob Pane1399ba2016-05-31 13:41:29 -0700305
306 return -EINVAL;
307}
308
309static int set_power_limit(struct powercap_zone *power_zone, int cid,
Zhang Rui33823882019-07-10 21:44:30 +0800310 u64 power_limit)
Jacob Pan2d281d82013-10-17 10:28:35 -0700311{
312 struct rapl_domain *rd;
313 struct rapl_package *rp;
314 int ret = 0;
Jacob Pane1399ba2016-05-31 13:41:29 -0700315 int id;
Jacob Pan2d281d82013-10-17 10:28:35 -0700316
317 get_online_cpus();
318 rd = power_zone_to_rapl_domain(power_zone);
Jacob Pane1399ba2016-05-31 13:41:29 -0700319 id = contraint_to_pl(rd, cid);
Jacob Pancb43f812016-11-28 13:53:11 -0800320 if (id < 0) {
321 ret = id;
322 goto set_exit;
323 }
Jacob Pane1399ba2016-05-31 13:41:29 -0700324
Jacob Pan309557f2016-02-24 13:31:37 -0800325 rp = rd->rp;
Jacob Pan2d281d82013-10-17 10:28:35 -0700326
327 if (rd->state & DOMAIN_STATE_BIOS_LOCKED) {
Zhang Rui33823882019-07-10 21:44:30 +0800328 dev_warn(&power_zone->dev,
329 "%s locked by BIOS, monitoring only\n", rd->name);
Jacob Pan2d281d82013-10-17 10:28:35 -0700330 ret = -EACCES;
331 goto set_exit;
332 }
333
334 switch (rd->rpl[id].prim_id) {
335 case PL1_ENABLE:
336 rapl_write_data_raw(rd, POWER_LIMIT1, power_limit);
337 break;
338 case PL2_ENABLE:
339 rapl_write_data_raw(rd, POWER_LIMIT2, power_limit);
340 break;
341 default:
342 ret = -EINVAL;
343 }
344 if (!ret)
Jacob Pan309557f2016-02-24 13:31:37 -0800345 package_power_limit_irq_save(rp);
Jacob Pan2d281d82013-10-17 10:28:35 -0700346set_exit:
347 put_online_cpus();
348 return ret;
349}
350
Jacob Pane1399ba2016-05-31 13:41:29 -0700351static int get_current_power_limit(struct powercap_zone *power_zone, int cid,
Zhang Rui33823882019-07-10 21:44:30 +0800352 u64 *data)
Jacob Pan2d281d82013-10-17 10:28:35 -0700353{
354 struct rapl_domain *rd;
355 u64 val;
356 int prim;
357 int ret = 0;
Jacob Pane1399ba2016-05-31 13:41:29 -0700358 int id;
Jacob Pan2d281d82013-10-17 10:28:35 -0700359
360 get_online_cpus();
361 rd = power_zone_to_rapl_domain(power_zone);
Jacob Pane1399ba2016-05-31 13:41:29 -0700362 id = contraint_to_pl(rd, cid);
Jacob Pancb43f812016-11-28 13:53:11 -0800363 if (id < 0) {
364 ret = id;
365 goto get_exit;
366 }
367
Jacob Pan2d281d82013-10-17 10:28:35 -0700368 switch (rd->rpl[id].prim_id) {
369 case PL1_ENABLE:
370 prim = POWER_LIMIT1;
371 break;
372 case PL2_ENABLE:
373 prim = POWER_LIMIT2;
374 break;
375 default:
376 put_online_cpus();
377 return -EINVAL;
378 }
379 if (rapl_read_data_raw(rd, prim, true, &val))
380 ret = -EIO;
381 else
382 *data = val;
383
Jacob Pancb43f812016-11-28 13:53:11 -0800384get_exit:
Jacob Pan2d281d82013-10-17 10:28:35 -0700385 put_online_cpus();
386
387 return ret;
388}
389
Jacob Pane1399ba2016-05-31 13:41:29 -0700390static int set_time_window(struct powercap_zone *power_zone, int cid,
Zhang Rui33823882019-07-10 21:44:30 +0800391 u64 window)
Jacob Pan2d281d82013-10-17 10:28:35 -0700392{
393 struct rapl_domain *rd;
394 int ret = 0;
Jacob Pane1399ba2016-05-31 13:41:29 -0700395 int id;
Jacob Pan2d281d82013-10-17 10:28:35 -0700396
397 get_online_cpus();
398 rd = power_zone_to_rapl_domain(power_zone);
Jacob Pane1399ba2016-05-31 13:41:29 -0700399 id = contraint_to_pl(rd, cid);
Jacob Pancb43f812016-11-28 13:53:11 -0800400 if (id < 0) {
401 ret = id;
402 goto set_time_exit;
403 }
Jacob Pane1399ba2016-05-31 13:41:29 -0700404
Jacob Pan2d281d82013-10-17 10:28:35 -0700405 switch (rd->rpl[id].prim_id) {
406 case PL1_ENABLE:
407 rapl_write_data_raw(rd, TIME_WINDOW1, window);
408 break;
409 case PL2_ENABLE:
410 rapl_write_data_raw(rd, TIME_WINDOW2, window);
411 break;
412 default:
413 ret = -EINVAL;
414 }
Jacob Pancb43f812016-11-28 13:53:11 -0800415
416set_time_exit:
Jacob Pan2d281d82013-10-17 10:28:35 -0700417 put_online_cpus();
418 return ret;
419}
420
Zhang Rui33823882019-07-10 21:44:30 +0800421static int get_time_window(struct powercap_zone *power_zone, int cid,
422 u64 *data)
Jacob Pan2d281d82013-10-17 10:28:35 -0700423{
424 struct rapl_domain *rd;
425 u64 val;
426 int ret = 0;
Jacob Pane1399ba2016-05-31 13:41:29 -0700427 int id;
Jacob Pan2d281d82013-10-17 10:28:35 -0700428
429 get_online_cpus();
430 rd = power_zone_to_rapl_domain(power_zone);
Jacob Pane1399ba2016-05-31 13:41:29 -0700431 id = contraint_to_pl(rd, cid);
Jacob Pancb43f812016-11-28 13:53:11 -0800432 if (id < 0) {
433 ret = id;
434 goto get_time_exit;
435 }
Jacob Pane1399ba2016-05-31 13:41:29 -0700436
Jacob Pan2d281d82013-10-17 10:28:35 -0700437 switch (rd->rpl[id].prim_id) {
438 case PL1_ENABLE:
439 ret = rapl_read_data_raw(rd, TIME_WINDOW1, true, &val);
440 break;
441 case PL2_ENABLE:
442 ret = rapl_read_data_raw(rd, TIME_WINDOW2, true, &val);
443 break;
444 default:
445 put_online_cpus();
446 return -EINVAL;
447 }
448 if (!ret)
449 *data = val;
Jacob Pancb43f812016-11-28 13:53:11 -0800450
451get_time_exit:
Jacob Pan2d281d82013-10-17 10:28:35 -0700452 put_online_cpus();
453
454 return ret;
455}
456
Zhang Rui33823882019-07-10 21:44:30 +0800457static const char *get_constraint_name(struct powercap_zone *power_zone,
458 int cid)
Jacob Pan2d281d82013-10-17 10:28:35 -0700459{
Jacob Pan2d281d82013-10-17 10:28:35 -0700460 struct rapl_domain *rd;
Jacob Pane1399ba2016-05-31 13:41:29 -0700461 int id;
Jacob Pan2d281d82013-10-17 10:28:35 -0700462
463 rd = power_zone_to_rapl_domain(power_zone);
Jacob Pane1399ba2016-05-31 13:41:29 -0700464 id = contraint_to_pl(rd, cid);
465 if (id >= 0)
466 return rd->rpl[id].name;
Jacob Pan2d281d82013-10-17 10:28:35 -0700467
Jacob Pane1399ba2016-05-31 13:41:29 -0700468 return NULL;
Jacob Pan2d281d82013-10-17 10:28:35 -0700469}
470
Zhang Rui33823882019-07-10 21:44:30 +0800471static int get_max_power(struct powercap_zone *power_zone, int id, u64 *data)
Jacob Pan2d281d82013-10-17 10:28:35 -0700472{
473 struct rapl_domain *rd;
474 u64 val;
475 int prim;
476 int ret = 0;
477
478 get_online_cpus();
479 rd = power_zone_to_rapl_domain(power_zone);
480 switch (rd->rpl[id].prim_id) {
481 case PL1_ENABLE:
482 prim = THERMAL_SPEC_POWER;
483 break;
484 case PL2_ENABLE:
485 prim = MAX_POWER;
486 break;
487 default:
488 put_online_cpus();
489 return -EINVAL;
490 }
491 if (rapl_read_data_raw(rd, prim, true, &val))
492 ret = -EIO;
493 else
494 *data = val;
495
496 put_online_cpus();
497
498 return ret;
499}
500
Julia Lawall600c3952015-12-23 22:59:55 +0100501static const struct powercap_zone_constraint_ops constraint_ops = {
Jacob Pan2d281d82013-10-17 10:28:35 -0700502 .set_power_limit_uw = set_power_limit,
503 .get_power_limit_uw = get_current_power_limit,
504 .set_time_window_us = set_time_window,
505 .get_time_window_us = get_time_window,
506 .get_max_power_uw = get_max_power,
507 .get_name = get_constraint_name,
508};
509
510/* called after domain detection and package level data are set */
511static void rapl_init_domains(struct rapl_package *rp)
512{
Zhang Rui0c2dded2019-07-10 21:44:32 +0800513 enum rapl_domain_type i;
514 enum rapl_domain_reg_id j;
Jacob Pan2d281d82013-10-17 10:28:35 -0700515 struct rapl_domain *rd = rp->domains;
516
517 for (i = 0; i < RAPL_DOMAIN_MAX; i++) {
518 unsigned int mask = rp->domain_map & (1 << i);
Zhang Rui7fde2712019-07-10 21:44:26 +0800519
Zhang Rui0c2dded2019-07-10 21:44:32 +0800520 if (!mask)
521 continue;
Zhang Rui7fde2712019-07-10 21:44:26 +0800522
Zhang Rui0c2dded2019-07-10 21:44:32 +0800523 rd->rp = rp;
524 rd->name = rapl_domain_names[i];
525 rd->id = i;
526 rd->rpl[0].prim_id = PL1_ENABLE;
527 rd->rpl[0].name = pl1_name;
528 /* some domain may support two power limits */
529 if (rp->priv->limits[i] == 2) {
Jacob Pan2d281d82013-10-17 10:28:35 -0700530 rd->rpl[1].prim_id = PL2_ENABLE;
531 rd->rpl[1].name = pl2_name;
Zhang Rui0c2dded2019-07-10 21:44:32 +0800532 }
533
534 for (j = 0; j < RAPL_DOMAIN_REG_MAX; j++)
535 rd->regs[j] = rp->priv->regs[i][j];
536
Zhang Rui2d798d92020-06-29 13:34:50 +0800537 switch (i) {
538 case RAPL_DOMAIN_DRAM:
Jacob Pand474a4d2015-03-13 03:48:56 -0700539 rd->domain_energy_unit =
Zhang Rui33823882019-07-10 21:44:30 +0800540 rapl_defaults->dram_domain_energy_unit;
Jacob Pand474a4d2015-03-13 03:48:56 -0700541 if (rd->domain_energy_unit)
542 pr_info("DRAM domain energy unit %dpj\n",
543 rd->domain_energy_unit);
Zhang Rui2d798d92020-06-29 13:34:50 +0800544 break;
545 case RAPL_DOMAIN_PLATFORM:
546 rd->domain_energy_unit =
547 rapl_defaults->psys_domain_energy_unit;
548 if (rd->domain_energy_unit)
549 pr_info("Platform domain energy unit %dpj\n",
550 rd->domain_energy_unit);
551 break;
552 default:
553 break;
Jacob Pan2d281d82013-10-17 10:28:35 -0700554 }
Zhang Rui0c2dded2019-07-10 21:44:32 +0800555 rd++;
Jacob Pan2d281d82013-10-17 10:28:35 -0700556 }
557}
558
Jacob Pan309557f2016-02-24 13:31:37 -0800559static u64 rapl_unit_xlate(struct rapl_domain *rd, enum unit_type type,
Zhang Rui33823882019-07-10 21:44:30 +0800560 u64 value, int to_raw)
Jacob Pan2d281d82013-10-17 10:28:35 -0700561{
Jacob Pan3c2c0842014-11-07 09:29:26 -0800562 u64 units = 1;
Jacob Pan309557f2016-02-24 13:31:37 -0800563 struct rapl_package *rp = rd->rp;
Jacob Pand474a4d2015-03-13 03:48:56 -0700564 u64 scale = 1;
Jacob Pan2d281d82013-10-17 10:28:35 -0700565
Jacob Pan2d281d82013-10-17 10:28:35 -0700566 switch (type) {
567 case POWER_UNIT:
Jacob Pan3c2c0842014-11-07 09:29:26 -0800568 units = rp->power_unit;
Jacob Pan2d281d82013-10-17 10:28:35 -0700569 break;
570 case ENERGY_UNIT:
Jacob Pand474a4d2015-03-13 03:48:56 -0700571 scale = ENERGY_UNIT_SCALE;
572 /* per domain unit takes precedence */
Jacob Pancb43f812016-11-28 13:53:11 -0800573 if (rd->domain_energy_unit)
Jacob Pand474a4d2015-03-13 03:48:56 -0700574 units = rd->domain_energy_unit;
575 else
576 units = rp->energy_unit;
Jacob Pan2d281d82013-10-17 10:28:35 -0700577 break;
578 case TIME_UNIT:
Jacob Pan3c2c0842014-11-07 09:29:26 -0800579 return rapl_defaults->compute_time_window(rp, value, to_raw);
Jacob Pan2d281d82013-10-17 10:28:35 -0700580 case ARBITRARY_UNIT:
581 default:
582 return value;
583 };
584
585 if (to_raw)
Jacob Pand474a4d2015-03-13 03:48:56 -0700586 return div64_u64(value, units) * scale;
Jacob Pan3c2c0842014-11-07 09:29:26 -0800587
588 value *= units;
589
Jacob Pand474a4d2015-03-13 03:48:56 -0700590 return div64_u64(value, scale);
Jacob Pan2d281d82013-10-17 10:28:35 -0700591}
592
593/* in the order of enum rapl_primitives */
594static struct rapl_primitive_info rpi[] = {
595 /* name, mask, shift, msr index, unit divisor */
596 PRIMITIVE_INFO_INIT(ENERGY_COUNTER, ENERGY_STATUS_MASK, 0,
Zhang Rui33823882019-07-10 21:44:30 +0800597 RAPL_DOMAIN_REG_STATUS, ENERGY_UNIT, 0),
Jacob Pan2d281d82013-10-17 10:28:35 -0700598 PRIMITIVE_INFO_INIT(POWER_LIMIT1, POWER_LIMIT1_MASK, 0,
Zhang Rui33823882019-07-10 21:44:30 +0800599 RAPL_DOMAIN_REG_LIMIT, POWER_UNIT, 0),
Jacob Pan2d281d82013-10-17 10:28:35 -0700600 PRIMITIVE_INFO_INIT(POWER_LIMIT2, POWER_LIMIT2_MASK, 32,
Zhang Rui33823882019-07-10 21:44:30 +0800601 RAPL_DOMAIN_REG_LIMIT, POWER_UNIT, 0),
Zhang Rui0c2dded2019-07-10 21:44:32 +0800602 PRIMITIVE_INFO_INIT(FW_LOCK, POWER_LOW_LOCK, 31,
Zhang Rui33823882019-07-10 21:44:30 +0800603 RAPL_DOMAIN_REG_LIMIT, ARBITRARY_UNIT, 0),
Jacob Pan2d281d82013-10-17 10:28:35 -0700604 PRIMITIVE_INFO_INIT(PL1_ENABLE, POWER_LIMIT1_ENABLE, 15,
Zhang Rui33823882019-07-10 21:44:30 +0800605 RAPL_DOMAIN_REG_LIMIT, ARBITRARY_UNIT, 0),
Jacob Pan2d281d82013-10-17 10:28:35 -0700606 PRIMITIVE_INFO_INIT(PL1_CLAMP, POWER_LIMIT1_CLAMP, 16,
Zhang Rui33823882019-07-10 21:44:30 +0800607 RAPL_DOMAIN_REG_LIMIT, ARBITRARY_UNIT, 0),
Jacob Pan2d281d82013-10-17 10:28:35 -0700608 PRIMITIVE_INFO_INIT(PL2_ENABLE, POWER_LIMIT2_ENABLE, 47,
Zhang Rui33823882019-07-10 21:44:30 +0800609 RAPL_DOMAIN_REG_LIMIT, ARBITRARY_UNIT, 0),
Jacob Pan2d281d82013-10-17 10:28:35 -0700610 PRIMITIVE_INFO_INIT(PL2_CLAMP, POWER_LIMIT2_CLAMP, 48,
Zhang Rui33823882019-07-10 21:44:30 +0800611 RAPL_DOMAIN_REG_LIMIT, ARBITRARY_UNIT, 0),
Jacob Pan2d281d82013-10-17 10:28:35 -0700612 PRIMITIVE_INFO_INIT(TIME_WINDOW1, TIME_WINDOW1_MASK, 17,
Zhang Rui33823882019-07-10 21:44:30 +0800613 RAPL_DOMAIN_REG_LIMIT, TIME_UNIT, 0),
Jacob Pan2d281d82013-10-17 10:28:35 -0700614 PRIMITIVE_INFO_INIT(TIME_WINDOW2, TIME_WINDOW2_MASK, 49,
Zhang Rui33823882019-07-10 21:44:30 +0800615 RAPL_DOMAIN_REG_LIMIT, TIME_UNIT, 0),
Jacob Pan2d281d82013-10-17 10:28:35 -0700616 PRIMITIVE_INFO_INIT(THERMAL_SPEC_POWER, POWER_INFO_THERMAL_SPEC_MASK,
Zhang Rui33823882019-07-10 21:44:30 +0800617 0, RAPL_DOMAIN_REG_INFO, POWER_UNIT, 0),
Jacob Pan2d281d82013-10-17 10:28:35 -0700618 PRIMITIVE_INFO_INIT(MAX_POWER, POWER_INFO_MAX_MASK, 32,
Zhang Rui33823882019-07-10 21:44:30 +0800619 RAPL_DOMAIN_REG_INFO, POWER_UNIT, 0),
Jacob Pan2d281d82013-10-17 10:28:35 -0700620 PRIMITIVE_INFO_INIT(MIN_POWER, POWER_INFO_MIN_MASK, 16,
Zhang Rui33823882019-07-10 21:44:30 +0800621 RAPL_DOMAIN_REG_INFO, POWER_UNIT, 0),
Jacob Pan2d281d82013-10-17 10:28:35 -0700622 PRIMITIVE_INFO_INIT(MAX_TIME_WINDOW, POWER_INFO_MAX_TIME_WIN_MASK, 48,
Zhang Rui33823882019-07-10 21:44:30 +0800623 RAPL_DOMAIN_REG_INFO, TIME_UNIT, 0),
Jacob Pan2d281d82013-10-17 10:28:35 -0700624 PRIMITIVE_INFO_INIT(THROTTLED_TIME, PERF_STATUS_THROTTLE_TIME_MASK, 0,
Zhang Rui33823882019-07-10 21:44:30 +0800625 RAPL_DOMAIN_REG_PERF, TIME_UNIT, 0),
Jacob Pan2d281d82013-10-17 10:28:35 -0700626 PRIMITIVE_INFO_INIT(PRIORITY_LEVEL, PP_POLICY_MASK, 0,
Zhang Rui33823882019-07-10 21:44:30 +0800627 RAPL_DOMAIN_REG_POLICY, ARBITRARY_UNIT, 0),
Jacob Pan2d281d82013-10-17 10:28:35 -0700628 /* non-hardware */
629 PRIMITIVE_INFO_INIT(AVERAGE_POWER, 0, 0, 0, POWER_UNIT,
Zhang Rui33823882019-07-10 21:44:30 +0800630 RAPL_PRIMITIVE_DERIVED),
Jacob Pan2d281d82013-10-17 10:28:35 -0700631 {NULL, 0, 0, 0},
632};
633
634/* Read primitive data based on its related struct rapl_primitive_info.
635 * if xlate flag is set, return translated data based on data units, i.e.
636 * time, energy, and power.
637 * RAPL MSRs are non-architectual and are laid out not consistently across
638 * domains. Here we use primitive info to allow writing consolidated access
639 * functions.
640 * For a given primitive, it is processed by MSR mask and shift. Unit conversion
641 * is pre-assigned based on RAPL unit MSRs read at init time.
642 * 63-------------------------- 31--------------------------- 0
643 * | xxxxx (mask) |
644 * | |<- shift ----------------|
645 * 63-------------------------- 31--------------------------- 0
646 */
647static int rapl_read_data_raw(struct rapl_domain *rd,
Zhang Rui33823882019-07-10 21:44:30 +0800648 enum rapl_primitives prim, bool xlate, u64 *data)
Jacob Pan2d281d82013-10-17 10:28:35 -0700649{
Zhang Ruibeea8df2019-07-10 21:44:27 +0800650 u64 value;
Jacob Pan2d281d82013-10-17 10:28:35 -0700651 struct rapl_primitive_info *rp = &rpi[prim];
Zhang Ruibeea8df2019-07-10 21:44:27 +0800652 struct reg_action ra;
Jacob Pan2d281d82013-10-17 10:28:35 -0700653 int cpu;
654
655 if (!rp->name || rp->flag & RAPL_PRIMITIVE_DUMMY)
656 return -EINVAL;
657
Zhang Ruibeea8df2019-07-10 21:44:27 +0800658 ra.reg = rd->regs[rp->id];
659 if (!ra.reg)
Jacob Pan2d281d82013-10-17 10:28:35 -0700660 return -EINVAL;
Jacob Pan323ee642016-02-24 13:31:38 -0800661
662 cpu = rd->rp->lead_cpu;
Jacob Pan2d281d82013-10-17 10:28:35 -0700663
Zhang Rui0c2dded2019-07-10 21:44:32 +0800664 /* domain with 2 limits has different bit */
665 if (prim == FW_LOCK && rd->rp->priv->limits[rd->id] == 2) {
666 rp->mask = POWER_HIGH_LOCK;
Jacob Pan2d281d82013-10-17 10:28:35 -0700667 rp->shift = 63;
668 }
669 /* non-hardware data are collected by the polling thread */
670 if (rp->flag & RAPL_PRIMITIVE_DERIVED) {
671 *data = rd->rdd.primitives[prim];
672 return 0;
673 }
674
Zhang Ruibeea8df2019-07-10 21:44:27 +0800675 ra.mask = rp->mask;
676
677 if (rd->rp->priv->read_raw(cpu, &ra)) {
Zhang Ruid978e752019-07-10 21:44:31 +0800678 pr_debug("failed to read reg 0x%llx on cpu %d\n", ra.reg, cpu);
Jacob Pan2d281d82013-10-17 10:28:35 -0700679 return -EIO;
680 }
681
Zhang Ruibeea8df2019-07-10 21:44:27 +0800682 value = ra.value >> rp->shift;
683
Jacob Pan2d281d82013-10-17 10:28:35 -0700684 if (xlate)
Zhang Ruibeea8df2019-07-10 21:44:27 +0800685 *data = rapl_unit_xlate(rd, rp->unit, value, 0);
Jacob Pan2d281d82013-10-17 10:28:35 -0700686 else
Zhang Ruibeea8df2019-07-10 21:44:27 +0800687 *data = value;
Jacob Pan2d281d82013-10-17 10:28:35 -0700688
689 return 0;
690}
691
692/* Similar use of primitive info in the read counterpart */
693static int rapl_write_data_raw(struct rapl_domain *rd,
Zhang Rui33823882019-07-10 21:44:30 +0800694 enum rapl_primitives prim,
695 unsigned long long value)
Jacob Pan2d281d82013-10-17 10:28:35 -0700696{
Jacob Pan2d281d82013-10-17 10:28:35 -0700697 struct rapl_primitive_info *rp = &rpi[prim];
698 int cpu;
Jacob Panf14a1392016-02-24 13:31:36 -0800699 u64 bits;
Zhang Ruibeea8df2019-07-10 21:44:27 +0800700 struct reg_action ra;
Jacob Panf14a1392016-02-24 13:31:36 -0800701 int ret;
Jacob Pan2d281d82013-10-17 10:28:35 -0700702
Jacob Pan323ee642016-02-24 13:31:38 -0800703 cpu = rd->rp->lead_cpu;
Jacob Pan309557f2016-02-24 13:31:37 -0800704 bits = rapl_unit_xlate(rd, rp->unit, value, 1);
Adam Lessnauedbdabc2017-06-01 11:21:50 +0200705 bits <<= rp->shift;
706 bits &= rp->mask;
707
Zhang Ruibeea8df2019-07-10 21:44:27 +0800708 memset(&ra, 0, sizeof(ra));
Jacob Panf14a1392016-02-24 13:31:36 -0800709
Zhang Ruibeea8df2019-07-10 21:44:27 +0800710 ra.reg = rd->regs[rp->id];
711 ra.mask = rp->mask;
712 ra.value = bits;
Jacob Panf14a1392016-02-24 13:31:36 -0800713
Zhang Ruibeea8df2019-07-10 21:44:27 +0800714 ret = rd->rp->priv->write_raw(cpu, &ra);
Jacob Panf14a1392016-02-24 13:31:36 -0800715
716 return ret;
Jacob Pan2d281d82013-10-17 10:28:35 -0700717}
718
Jacob Pan3c2c0842014-11-07 09:29:26 -0800719/*
720 * Raw RAPL data stored in MSRs are in certain scales. We need to
721 * convert them into standard units based on the units reported in
722 * the RAPL unit MSRs. This is specific to CPUs as the method to
723 * calculate units differ on different CPUs.
724 * We convert the units to below format based on CPUs.
725 * i.e.
Jacob Pand474a4d2015-03-13 03:48:56 -0700726 * energy unit: picoJoules : Represented in picoJoules by default
Jacob Pan3c2c0842014-11-07 09:29:26 -0800727 * power unit : microWatts : Represented in milliWatts by default
728 * time unit : microseconds: Represented in seconds by default
729 */
730static int rapl_check_unit_core(struct rapl_package *rp, int cpu)
Jacob Pan2d281d82013-10-17 10:28:35 -0700731{
Zhang Rui1193b162019-07-10 21:44:29 +0800732 struct reg_action ra;
Jacob Pan2d281d82013-10-17 10:28:35 -0700733 u32 value;
734
Zhang Rui1193b162019-07-10 21:44:29 +0800735 ra.reg = rp->priv->reg_unit;
736 ra.mask = ~0;
737 if (rp->priv->read_raw(cpu, &ra)) {
Zhang Ruid978e752019-07-10 21:44:31 +0800738 pr_err("Failed to read power unit REG 0x%llx on CPU %d, exit.\n",
Zhang Rui33823882019-07-10 21:44:30 +0800739 rp->priv->reg_unit, cpu);
Jacob Pan2d281d82013-10-17 10:28:35 -0700740 return -ENODEV;
741 }
742
Zhang Rui1193b162019-07-10 21:44:29 +0800743 value = (ra.value & ENERGY_UNIT_MASK) >> ENERGY_UNIT_OFFSET;
Jacob Pand474a4d2015-03-13 03:48:56 -0700744 rp->energy_unit = ENERGY_UNIT_SCALE * 1000000 / (1 << value);
Jacob Pan2d281d82013-10-17 10:28:35 -0700745
Zhang Rui1193b162019-07-10 21:44:29 +0800746 value = (ra.value & POWER_UNIT_MASK) >> POWER_UNIT_OFFSET;
Jacob Pan3c2c0842014-11-07 09:29:26 -0800747 rp->power_unit = 1000000 / (1 << value);
Jacob Pan2d281d82013-10-17 10:28:35 -0700748
Zhang Rui1193b162019-07-10 21:44:29 +0800749 value = (ra.value & TIME_UNIT_MASK) >> TIME_UNIT_OFFSET;
Jacob Pan3c2c0842014-11-07 09:29:26 -0800750 rp->time_unit = 1000000 / (1 << value);
Jacob Pan2d281d82013-10-17 10:28:35 -0700751
Zhang Rui9ea76122019-05-13 13:58:53 -0400752 pr_debug("Core CPU %s energy=%dpJ, time=%dus, power=%duW\n",
Zhang Rui33823882019-07-10 21:44:30 +0800753 rp->name, rp->energy_unit, rp->time_unit, rp->power_unit);
Jacob Pan2d281d82013-10-17 10:28:35 -0700754
755 return 0;
756}
757
Jacob Pan3c2c0842014-11-07 09:29:26 -0800758static int rapl_check_unit_atom(struct rapl_package *rp, int cpu)
759{
Zhang Rui1193b162019-07-10 21:44:29 +0800760 struct reg_action ra;
Jacob Pan3c2c0842014-11-07 09:29:26 -0800761 u32 value;
762
Zhang Rui1193b162019-07-10 21:44:29 +0800763 ra.reg = rp->priv->reg_unit;
764 ra.mask = ~0;
765 if (rp->priv->read_raw(cpu, &ra)) {
Zhang Ruid978e752019-07-10 21:44:31 +0800766 pr_err("Failed to read power unit REG 0x%llx on CPU %d, exit.\n",
Zhang Rui33823882019-07-10 21:44:30 +0800767 rp->priv->reg_unit, cpu);
Jacob Pan3c2c0842014-11-07 09:29:26 -0800768 return -ENODEV;
769 }
Zhang Rui1193b162019-07-10 21:44:29 +0800770
771 value = (ra.value & ENERGY_UNIT_MASK) >> ENERGY_UNIT_OFFSET;
Jacob Pand474a4d2015-03-13 03:48:56 -0700772 rp->energy_unit = ENERGY_UNIT_SCALE * 1 << value;
Jacob Pan3c2c0842014-11-07 09:29:26 -0800773
Zhang Rui1193b162019-07-10 21:44:29 +0800774 value = (ra.value & POWER_UNIT_MASK) >> POWER_UNIT_OFFSET;
Jacob Pan3c2c0842014-11-07 09:29:26 -0800775 rp->power_unit = (1 << value) * 1000;
776
Zhang Rui1193b162019-07-10 21:44:29 +0800777 value = (ra.value & TIME_UNIT_MASK) >> TIME_UNIT_OFFSET;
Jacob Pan3c2c0842014-11-07 09:29:26 -0800778 rp->time_unit = 1000000 / (1 << value);
779
Zhang Rui9ea76122019-05-13 13:58:53 -0400780 pr_debug("Atom %s energy=%dpJ, time=%dus, power=%duW\n",
Zhang Rui33823882019-07-10 21:44:30 +0800781 rp->name, rp->energy_unit, rp->time_unit, rp->power_unit);
Jacob Pan3c2c0842014-11-07 09:29:26 -0800782
783 return 0;
784}
785
Jacob Panf14a1392016-02-24 13:31:36 -0800786static void power_limit_irq_save_cpu(void *info)
787{
788 u32 l, h = 0;
789 struct rapl_package *rp = (struct rapl_package *)info;
790
791 /* save the state of PLN irq mask bit before disabling it */
792 rdmsr_safe(MSR_IA32_PACKAGE_THERM_INTERRUPT, &l, &h);
793 if (!(rp->power_limit_irq & PACKAGE_PLN_INT_SAVED)) {
794 rp->power_limit_irq = l & PACKAGE_THERM_INT_PLN_ENABLE;
795 rp->power_limit_irq |= PACKAGE_PLN_INT_SAVED;
796 }
797 l &= ~PACKAGE_THERM_INT_PLN_ENABLE;
798 wrmsr_safe(MSR_IA32_PACKAGE_THERM_INTERRUPT, l, h);
799}
800
Jacob Pan2d281d82013-10-17 10:28:35 -0700801/* REVISIT:
802 * When package power limit is set artificially low by RAPL, LVT
803 * thermal interrupt for package power limit should be ignored
804 * since we are not really exceeding the real limit. The intention
805 * is to avoid excessive interrupts while we are trying to save power.
806 * A useful feature might be routing the package_power_limit interrupt
807 * to userspace via eventfd. once we have a usecase, this is simple
808 * to do by adding an atomic notifier.
809 */
810
Jacob Pan309557f2016-02-24 13:31:37 -0800811static void package_power_limit_irq_save(struct rapl_package *rp)
Jacob Pan2d281d82013-10-17 10:28:35 -0700812{
Jacob Pan2d281d82013-10-17 10:28:35 -0700813 if (!boot_cpu_has(X86_FEATURE_PTS) || !boot_cpu_has(X86_FEATURE_PLN))
814 return;
815
Jacob Pan323ee642016-02-24 13:31:38 -0800816 smp_call_function_single(rp->lead_cpu, power_limit_irq_save_cpu, rp, 1);
Jacob Panf14a1392016-02-24 13:31:36 -0800817}
818
Thomas Gleixner58705062016-11-22 21:16:02 +0000819/*
820 * Restore per package power limit interrupt enable state. Called from cpu
821 * hotplug code on package removal.
822 */
823static void package_power_limit_irq_restore(struct rapl_package *rp)
Jacob Panf14a1392016-02-24 13:31:36 -0800824{
Thomas Gleixner58705062016-11-22 21:16:02 +0000825 u32 l, h;
826
827 if (!boot_cpu_has(X86_FEATURE_PTS) || !boot_cpu_has(X86_FEATURE_PLN))
828 return;
829
830 /* irq enable state not saved, nothing to restore */
831 if (!(rp->power_limit_irq & PACKAGE_PLN_INT_SAVED))
832 return;
Jacob Panf14a1392016-02-24 13:31:36 -0800833
834 rdmsr_safe(MSR_IA32_PACKAGE_THERM_INTERRUPT, &l, &h);
835
836 if (rp->power_limit_irq & PACKAGE_THERM_INT_PLN_ENABLE)
837 l |= PACKAGE_THERM_INT_PLN_ENABLE;
838 else
839 l &= ~PACKAGE_THERM_INT_PLN_ENABLE;
840
841 wrmsr_safe(MSR_IA32_PACKAGE_THERM_INTERRUPT, l, h);
Jacob Pan2d281d82013-10-17 10:28:35 -0700842}
843
Jacob Pan3c2c0842014-11-07 09:29:26 -0800844static void set_floor_freq_default(struct rapl_domain *rd, bool mode)
845{
846 int nr_powerlimit = find_nr_power_limit(rd);
847
848 /* always enable clamp such that p-state can go below OS requested
849 * range. power capping priority over guranteed frequency.
850 */
851 rapl_write_data_raw(rd, PL1_CLAMP, mode);
852
853 /* some domains have pl2 */
854 if (nr_powerlimit > 1) {
855 rapl_write_data_raw(rd, PL2_ENABLE, mode);
856 rapl_write_data_raw(rd, PL2_CLAMP, mode);
857 }
858}
859
860static void set_floor_freq_atom(struct rapl_domain *rd, bool enable)
861{
862 static u32 power_ctrl_orig_val;
863 u32 mdata;
864
Ajay Thomas51b63402015-04-30 01:43:23 +0530865 if (!rapl_defaults->floor_freq_reg_addr) {
866 pr_err("Invalid floor frequency config register\n");
867 return;
868 }
869
Jacob Pan3c2c0842014-11-07 09:29:26 -0800870 if (!power_ctrl_orig_val)
Andy Shevchenko4077a382015-11-11 19:59:29 +0200871 iosf_mbi_read(BT_MBI_UNIT_PMC, MBI_CR_READ,
872 rapl_defaults->floor_freq_reg_addr,
873 &power_ctrl_orig_val);
Jacob Pan3c2c0842014-11-07 09:29:26 -0800874 mdata = power_ctrl_orig_val;
875 if (enable) {
876 mdata &= ~(0x7f << 8);
877 mdata |= 1 << 8;
878 }
Andy Shevchenko4077a382015-11-11 19:59:29 +0200879 iosf_mbi_write(BT_MBI_UNIT_PMC, MBI_CR_WRITE,
880 rapl_defaults->floor_freq_reg_addr, mdata);
Jacob Pan3c2c0842014-11-07 09:29:26 -0800881}
882
883static u64 rapl_compute_time_window_core(struct rapl_package *rp, u64 value,
Zhang Rui33823882019-07-10 21:44:30 +0800884 bool to_raw)
Jacob Pan3c2c0842014-11-07 09:29:26 -0800885{
Zhang Rui33823882019-07-10 21:44:30 +0800886 u64 f, y; /* fraction and exp. used for time unit */
Jacob Pan3c2c0842014-11-07 09:29:26 -0800887
888 /*
889 * Special processing based on 2^Y*(1+F/4), refer
890 * to Intel Software Developer's manual Vol.3B: CH 14.9.3.
891 */
892 if (!to_raw) {
893 f = (value & 0x60) >> 5;
894 y = value & 0x1f;
895 value = (1 << y) * (4 + f) * rp->time_unit / 4;
896 } else {
897 do_div(value, rp->time_unit);
898 y = ilog2(value);
899 f = div64_u64(4 * (value - (1 << y)), 1 << y);
900 value = (y & 0x1f) | ((f & 0x3) << 5);
901 }
902 return value;
903}
904
905static u64 rapl_compute_time_window_atom(struct rapl_package *rp, u64 value,
Zhang Rui33823882019-07-10 21:44:30 +0800906 bool to_raw)
Jacob Pan3c2c0842014-11-07 09:29:26 -0800907{
908 /*
909 * Atom time unit encoding is straight forward val * time_unit,
910 * where time_unit is default to 1 sec. Never 0.
911 */
912 if (!to_raw)
913 return (value) ? value *= rp->time_unit : rp->time_unit;
Zhang Rui33823882019-07-10 21:44:30 +0800914
915 value = div64_u64(value, rp->time_unit);
Jacob Pan3c2c0842014-11-07 09:29:26 -0800916
917 return value;
918}
919
Jacob Pan087e9cb2014-11-07 09:29:25 -0800920static const struct rapl_defaults rapl_defaults_core = {
Ajay Thomas51b63402015-04-30 01:43:23 +0530921 .floor_freq_reg_addr = 0,
Jacob Pan3c2c0842014-11-07 09:29:26 -0800922 .check_unit = rapl_check_unit_core,
923 .set_floor_freq = set_floor_freq_default,
924 .compute_time_window = rapl_compute_time_window_core,
Jacob Pan087e9cb2014-11-07 09:29:25 -0800925};
926
Jacob Pand474a4d2015-03-13 03:48:56 -0700927static const struct rapl_defaults rapl_defaults_hsw_server = {
928 .check_unit = rapl_check_unit_core,
929 .set_floor_freq = set_floor_freq_default,
930 .compute_time_window = rapl_compute_time_window_core,
931 .dram_domain_energy_unit = 15300,
932};
933
Zhang Rui2d798d92020-06-29 13:34:50 +0800934static const struct rapl_defaults rapl_defaults_spr_server = {
935 .check_unit = rapl_check_unit_core,
936 .set_floor_freq = set_floor_freq_default,
937 .compute_time_window = rapl_compute_time_window_core,
938 .dram_domain_energy_unit = 15300,
939 .psys_domain_energy_unit = 1000000000,
940};
941
Ajay Thomas51b63402015-04-30 01:43:23 +0530942static const struct rapl_defaults rapl_defaults_byt = {
943 .floor_freq_reg_addr = IOSF_CPU_POWER_BUDGET_CTL_BYT,
Jacob Pan3c2c0842014-11-07 09:29:26 -0800944 .check_unit = rapl_check_unit_atom,
945 .set_floor_freq = set_floor_freq_atom,
946 .compute_time_window = rapl_compute_time_window_atom,
Jacob Pan087e9cb2014-11-07 09:29:25 -0800947};
948
Ajay Thomas51b63402015-04-30 01:43:23 +0530949static const struct rapl_defaults rapl_defaults_tng = {
950 .floor_freq_reg_addr = IOSF_CPU_POWER_BUDGET_CTL_TNG,
951 .check_unit = rapl_check_unit_atom,
952 .set_floor_freq = set_floor_freq_atom,
953 .compute_time_window = rapl_compute_time_window_atom,
954};
955
956static const struct rapl_defaults rapl_defaults_ann = {
957 .floor_freq_reg_addr = 0,
958 .check_unit = rapl_check_unit_atom,
959 .set_floor_freq = NULL,
960 .compute_time_window = rapl_compute_time_window_atom,
961};
962
963static const struct rapl_defaults rapl_defaults_cht = {
964 .floor_freq_reg_addr = 0,
965 .check_unit = rapl_check_unit_atom,
966 .set_floor_freq = NULL,
967 .compute_time_window = rapl_compute_time_window_atom,
968};
969
Mathias Krauseea85dbc2015-03-25 22:15:52 +0100970static const struct x86_cpu_id rapl_ids[] __initconst = {
Thomas Gleixnerf0722512020-03-20 14:14:03 +0100971 X86_MATCH_INTEL_FAM6_MODEL(SANDYBRIDGE, &rapl_defaults_core),
972 X86_MATCH_INTEL_FAM6_MODEL(SANDYBRIDGE_X, &rapl_defaults_core),
Dave Hansen0bb04b52016-06-02 17:19:37 -0700973
Thomas Gleixnerf0722512020-03-20 14:14:03 +0100974 X86_MATCH_INTEL_FAM6_MODEL(IVYBRIDGE, &rapl_defaults_core),
975 X86_MATCH_INTEL_FAM6_MODEL(IVYBRIDGE_X, &rapl_defaults_core),
Dave Hansen0bb04b52016-06-02 17:19:37 -0700976
Thomas Gleixnerf0722512020-03-20 14:14:03 +0100977 X86_MATCH_INTEL_FAM6_MODEL(HASWELL, &rapl_defaults_core),
978 X86_MATCH_INTEL_FAM6_MODEL(HASWELL_L, &rapl_defaults_core),
979 X86_MATCH_INTEL_FAM6_MODEL(HASWELL_G, &rapl_defaults_core),
980 X86_MATCH_INTEL_FAM6_MODEL(HASWELL_X, &rapl_defaults_hsw_server),
Dave Hansen0bb04b52016-06-02 17:19:37 -0700981
Thomas Gleixnerf0722512020-03-20 14:14:03 +0100982 X86_MATCH_INTEL_FAM6_MODEL(BROADWELL, &rapl_defaults_core),
983 X86_MATCH_INTEL_FAM6_MODEL(BROADWELL_G, &rapl_defaults_core),
984 X86_MATCH_INTEL_FAM6_MODEL(BROADWELL_D, &rapl_defaults_core),
985 X86_MATCH_INTEL_FAM6_MODEL(BROADWELL_X, &rapl_defaults_hsw_server),
Dave Hansen0bb04b52016-06-02 17:19:37 -0700986
Thomas Gleixnerf0722512020-03-20 14:14:03 +0100987 X86_MATCH_INTEL_FAM6_MODEL(SKYLAKE, &rapl_defaults_core),
988 X86_MATCH_INTEL_FAM6_MODEL(SKYLAKE_L, &rapl_defaults_core),
989 X86_MATCH_INTEL_FAM6_MODEL(SKYLAKE_X, &rapl_defaults_hsw_server),
990 X86_MATCH_INTEL_FAM6_MODEL(KABYLAKE_L, &rapl_defaults_core),
991 X86_MATCH_INTEL_FAM6_MODEL(KABYLAKE, &rapl_defaults_core),
992 X86_MATCH_INTEL_FAM6_MODEL(CANNONLAKE_L, &rapl_defaults_core),
993 X86_MATCH_INTEL_FAM6_MODEL(ICELAKE_L, &rapl_defaults_core),
994 X86_MATCH_INTEL_FAM6_MODEL(ICELAKE, &rapl_defaults_core),
995 X86_MATCH_INTEL_FAM6_MODEL(ICELAKE_NNPI, &rapl_defaults_core),
996 X86_MATCH_INTEL_FAM6_MODEL(ICELAKE_X, &rapl_defaults_hsw_server),
997 X86_MATCH_INTEL_FAM6_MODEL(ICELAKE_D, &rapl_defaults_hsw_server),
998 X86_MATCH_INTEL_FAM6_MODEL(COMETLAKE_L, &rapl_defaults_core),
999 X86_MATCH_INTEL_FAM6_MODEL(COMETLAKE, &rapl_defaults_core),
1000 X86_MATCH_INTEL_FAM6_MODEL(TIGERLAKE_L, &rapl_defaults_core),
Zhang Rui2d798d92020-06-29 13:34:50 +08001001 X86_MATCH_INTEL_FAM6_MODEL(SAPPHIRERAPIDS_X, &rapl_defaults_spr_server),
Dave Hansen0bb04b52016-06-02 17:19:37 -07001002
Thomas Gleixnerf0722512020-03-20 14:14:03 +01001003 X86_MATCH_INTEL_FAM6_MODEL(ATOM_SILVERMONT, &rapl_defaults_byt),
1004 X86_MATCH_INTEL_FAM6_MODEL(ATOM_AIRMONT, &rapl_defaults_cht),
1005 X86_MATCH_INTEL_FAM6_MODEL(ATOM_SILVERMONT_MID, &rapl_defaults_tng),
1006 X86_MATCH_INTEL_FAM6_MODEL(ATOM_AIRMONT_MID, &rapl_defaults_ann),
1007 X86_MATCH_INTEL_FAM6_MODEL(ATOM_GOLDMONT, &rapl_defaults_core),
1008 X86_MATCH_INTEL_FAM6_MODEL(ATOM_GOLDMONT_PLUS, &rapl_defaults_core),
1009 X86_MATCH_INTEL_FAM6_MODEL(ATOM_GOLDMONT_D, &rapl_defaults_core),
Jacob Pan33c98002020-05-15 15:30:41 +08001010 X86_MATCH_INTEL_FAM6_MODEL(ATOM_TREMONT, &rapl_defaults_core),
Thomas Gleixnerf0722512020-03-20 14:14:03 +01001011 X86_MATCH_INTEL_FAM6_MODEL(ATOM_TREMONT_D, &rapl_defaults_core),
1012 X86_MATCH_INTEL_FAM6_MODEL(ATOM_TREMONT_L, &rapl_defaults_core),
Dave Hansen0bb04b52016-06-02 17:19:37 -07001013
Thomas Gleixnerf0722512020-03-20 14:14:03 +01001014 X86_MATCH_INTEL_FAM6_MODEL(XEON_PHI_KNL, &rapl_defaults_hsw_server),
1015 X86_MATCH_INTEL_FAM6_MODEL(XEON_PHI_KNM, &rapl_defaults_hsw_server),
Jacob Pan2d281d82013-10-17 10:28:35 -07001016 {}
1017};
1018MODULE_DEVICE_TABLE(x86cpu, rapl_ids);
1019
Thomas Gleixnerbed5ab62016-11-22 21:15:58 +00001020/* Read once for all raw primitive data for domains */
1021static void rapl_update_domain_data(struct rapl_package *rp)
Jacob Pan2d281d82013-10-17 10:28:35 -07001022{
1023 int dmn, prim;
1024 u64 val;
Jacob Pan2d281d82013-10-17 10:28:35 -07001025
Thomas Gleixnerbed5ab62016-11-22 21:15:58 +00001026 for (dmn = 0; dmn < rp->nr_domains; dmn++) {
Zhang Rui9ea76122019-05-13 13:58:53 -04001027 pr_debug("update %s domain %s data\n", rp->name,
Thomas Gleixnerbed5ab62016-11-22 21:15:58 +00001028 rp->domains[dmn].name);
1029 /* exclude non-raw primitives */
1030 for (prim = 0; prim < NR_RAW_PRIMITIVES; prim++) {
1031 if (!rapl_read_data_raw(&rp->domains[dmn], prim,
1032 rpi[prim].unit, &val))
Zhang Rui33823882019-07-10 21:44:30 +08001033 rp->domains[dmn].rdd.primitives[prim] = val;
Jacob Pan2d281d82013-10-17 10:28:35 -07001034 }
1035 }
1036
1037}
1038
Jacob Pan2d281d82013-10-17 10:28:35 -07001039static int rapl_package_register_powercap(struct rapl_package *rp)
1040{
1041 struct rapl_domain *rd;
Jacob Pan2d281d82013-10-17 10:28:35 -07001042 struct powercap_zone *power_zone = NULL;
Luis de Bethencourt01857cf2018-01-17 10:30:34 +00001043 int nr_pl, ret;
Thomas Gleixnerbed5ab62016-11-22 21:15:58 +00001044
1045 /* Update the domain data of the new package */
1046 rapl_update_domain_data(rp);
Jacob Pan2d281d82013-10-17 10:28:35 -07001047
Zhang Rui33823882019-07-10 21:44:30 +08001048 /* first we register package domain as the parent zone */
Jacob Pan2d281d82013-10-17 10:28:35 -07001049 for (rd = rp->domains; rd < rp->domains + rp->nr_domains; rd++) {
1050 if (rd->id == RAPL_DOMAIN_PACKAGE) {
1051 nr_pl = find_nr_power_limit(rd);
Zhang Rui9ea76122019-05-13 13:58:53 -04001052 pr_debug("register package domain %s\n", rp->name);
Jacob Pan2d281d82013-10-17 10:28:35 -07001053 power_zone = powercap_register_zone(&rd->power_zone,
Zhang Rui33823882019-07-10 21:44:30 +08001054 rp->priv->control_type, rp->name,
1055 NULL, &zone_ops[rd->id], nr_pl,
1056 &constraint_ops);
Jacob Pan2d281d82013-10-17 10:28:35 -07001057 if (IS_ERR(power_zone)) {
Zhang Rui9ea76122019-05-13 13:58:53 -04001058 pr_debug("failed to register power zone %s\n",
Zhang Rui33823882019-07-10 21:44:30 +08001059 rp->name);
Thomas Gleixnerbed5ab62016-11-22 21:15:58 +00001060 return PTR_ERR(power_zone);
Jacob Pan2d281d82013-10-17 10:28:35 -07001061 }
1062 /* track parent zone in per package/socket data */
1063 rp->power_zone = power_zone;
1064 /* done, only one package domain per socket */
1065 break;
1066 }
1067 }
1068 if (!power_zone) {
1069 pr_err("no package domain found, unknown topology!\n");
Thomas Gleixnerbed5ab62016-11-22 21:15:58 +00001070 return -ENODEV;
Jacob Pan2d281d82013-10-17 10:28:35 -07001071 }
Zhang Rui33823882019-07-10 21:44:30 +08001072 /* now register domains as children of the socket/package */
Jacob Pan2d281d82013-10-17 10:28:35 -07001073 for (rd = rp->domains; rd < rp->domains + rp->nr_domains; rd++) {
1074 if (rd->id == RAPL_DOMAIN_PACKAGE)
1075 continue;
1076 /* number of power limits per domain varies */
1077 nr_pl = find_nr_power_limit(rd);
1078 power_zone = powercap_register_zone(&rd->power_zone,
Zhang Rui33823882019-07-10 21:44:30 +08001079 rp->priv->control_type,
1080 rd->name, rp->power_zone,
1081 &zone_ops[rd->id], nr_pl,
1082 &constraint_ops);
Jacob Pan2d281d82013-10-17 10:28:35 -07001083
1084 if (IS_ERR(power_zone)) {
Zhang Rui9ea76122019-05-13 13:58:53 -04001085 pr_debug("failed to register power_zone, %s:%s\n",
Zhang Rui33823882019-07-10 21:44:30 +08001086 rp->name, rd->name);
Jacob Pan2d281d82013-10-17 10:28:35 -07001087 ret = PTR_ERR(power_zone);
1088 goto err_cleanup;
1089 }
1090 }
Thomas Gleixnerbed5ab62016-11-22 21:15:58 +00001091 return 0;
Jacob Pan2d281d82013-10-17 10:28:35 -07001092
Jacob Pan2d281d82013-10-17 10:28:35 -07001093err_cleanup:
Thomas Gleixner58705062016-11-22 21:16:02 +00001094 /*
1095 * Clean up previously initialized domains within the package if we
Jacob Pan2d281d82013-10-17 10:28:35 -07001096 * failed after the first domain setup.
1097 */
1098 while (--rd >= rp->domains) {
Zhang Rui9ea76122019-05-13 13:58:53 -04001099 pr_debug("unregister %s domain %s\n", rp->name, rd->name);
Zhang Rui33823882019-07-10 21:44:30 +08001100 powercap_unregister_zone(rp->priv->control_type,
1101 &rd->power_zone);
Jacob Pan2d281d82013-10-17 10:28:35 -07001102 }
1103
1104 return ret;
1105}
1106
Zhang Rui33823882019-07-10 21:44:30 +08001107int rapl_add_platform_domain(struct rapl_if_priv *priv)
Srinivas Pandruvada3521ba12016-04-17 15:03:01 -07001108{
1109 struct rapl_domain *rd;
1110 struct powercap_zone *power_zone;
Zhang Rui8a006762019-07-10 21:44:28 +08001111 struct reg_action ra;
1112 int ret;
Srinivas Pandruvada3521ba12016-04-17 15:03:01 -07001113
Zhang Rui8a006762019-07-10 21:44:28 +08001114 ra.reg = priv->regs[RAPL_DOMAIN_PLATFORM][RAPL_DOMAIN_REG_STATUS];
1115 ra.mask = ~0;
1116 ret = priv->read_raw(0, &ra);
1117 if (ret || !ra.value)
Srinivas Pandruvada3521ba12016-04-17 15:03:01 -07001118 return -ENODEV;
1119
Zhang Rui8a006762019-07-10 21:44:28 +08001120 ra.reg = priv->regs[RAPL_DOMAIN_PLATFORM][RAPL_DOMAIN_REG_LIMIT];
1121 ra.mask = ~0;
1122 ret = priv->read_raw(0, &ra);
1123 if (ret || !ra.value)
Srinivas Pandruvada3521ba12016-04-17 15:03:01 -07001124 return -ENODEV;
1125
1126 rd = kzalloc(sizeof(*rd), GFP_KERNEL);
1127 if (!rd)
1128 return -ENOMEM;
1129
1130 rd->name = rapl_domain_names[RAPL_DOMAIN_PLATFORM];
1131 rd->id = RAPL_DOMAIN_PLATFORM;
Zhang Rui33823882019-07-10 21:44:30 +08001132 rd->regs[RAPL_DOMAIN_REG_LIMIT] =
1133 priv->regs[RAPL_DOMAIN_PLATFORM][RAPL_DOMAIN_REG_LIMIT];
1134 rd->regs[RAPL_DOMAIN_REG_STATUS] =
1135 priv->regs[RAPL_DOMAIN_PLATFORM][RAPL_DOMAIN_REG_STATUS];
Srinivas Pandruvada3521ba12016-04-17 15:03:01 -07001136 rd->rpl[0].prim_id = PL1_ENABLE;
1137 rd->rpl[0].name = pl1_name;
1138 rd->rpl[1].prim_id = PL2_ENABLE;
1139 rd->rpl[1].name = pl2_name;
Zhang Rui8a006762019-07-10 21:44:28 +08001140 rd->rp = rapl_find_package_domain(0, priv);
Srinivas Pandruvada3521ba12016-04-17 15:03:01 -07001141
Zhang Rui8a006762019-07-10 21:44:28 +08001142 power_zone = powercap_register_zone(&rd->power_zone, priv->control_type,
Srinivas Pandruvada3521ba12016-04-17 15:03:01 -07001143 "psys", NULL,
1144 &zone_ops[RAPL_DOMAIN_PLATFORM],
1145 2, &constraint_ops);
1146
1147 if (IS_ERR(power_zone)) {
1148 kfree(rd);
1149 return PTR_ERR(power_zone);
1150 }
1151
Zhang Rui8a006762019-07-10 21:44:28 +08001152 priv->platform_rapl_domain = rd;
Srinivas Pandruvada3521ba12016-04-17 15:03:01 -07001153
1154 return 0;
1155}
Zhang Rui33823882019-07-10 21:44:30 +08001156EXPORT_SYMBOL_GPL(rapl_add_platform_domain);
Srinivas Pandruvada3521ba12016-04-17 15:03:01 -07001157
Zhang Rui33823882019-07-10 21:44:30 +08001158void rapl_remove_platform_domain(struct rapl_if_priv *priv)
Jacob Pan2d281d82013-10-17 10:28:35 -07001159{
Zhang Rui8a006762019-07-10 21:44:28 +08001160 if (priv->platform_rapl_domain) {
1161 powercap_unregister_zone(priv->control_type,
Zhang Rui33823882019-07-10 21:44:30 +08001162 &priv->platform_rapl_domain->power_zone);
Zhang Rui8a006762019-07-10 21:44:28 +08001163 kfree(priv->platform_rapl_domain);
Jacob Pan2d281d82013-10-17 10:28:35 -07001164 }
Jacob Pan2d281d82013-10-17 10:28:35 -07001165}
Zhang Rui33823882019-07-10 21:44:30 +08001166EXPORT_SYMBOL_GPL(rapl_remove_platform_domain);
Jacob Pan2d281d82013-10-17 10:28:35 -07001167
Zhang Rui7fde2712019-07-10 21:44:26 +08001168static int rapl_check_domain(int cpu, int domain, struct rapl_package *rp)
Jacob Pan2d281d82013-10-17 10:28:35 -07001169{
Zhang Rui1193b162019-07-10 21:44:29 +08001170 struct reg_action ra;
Jacob Pan2d281d82013-10-17 10:28:35 -07001171
1172 switch (domain) {
1173 case RAPL_DOMAIN_PACKAGE:
Jacob Pan2d281d82013-10-17 10:28:35 -07001174 case RAPL_DOMAIN_PP0:
Jacob Pan2d281d82013-10-17 10:28:35 -07001175 case RAPL_DOMAIN_PP1:
Jacob Pan2d281d82013-10-17 10:28:35 -07001176 case RAPL_DOMAIN_DRAM:
Zhang Rui1193b162019-07-10 21:44:29 +08001177 ra.reg = rp->priv->regs[domain][RAPL_DOMAIN_REG_STATUS];
Jacob Pan2d281d82013-10-17 10:28:35 -07001178 break;
Srinivas Pandruvada3521ba12016-04-17 15:03:01 -07001179 case RAPL_DOMAIN_PLATFORM:
1180 /* PSYS(PLATFORM) is not a CPU domain, so avoid printng error */
1181 return -EINVAL;
Jacob Pan2d281d82013-10-17 10:28:35 -07001182 default:
1183 pr_err("invalid domain id %d\n", domain);
1184 return -EINVAL;
1185 }
Jacob Pan9d31c672014-04-29 15:33:06 -07001186 /* make sure domain counters are available and contains non-zero
1187 * values, otherwise skip it.
1188 */
Zhang Rui1193b162019-07-10 21:44:29 +08001189
1190 ra.mask = ~0;
1191 if (rp->priv->read_raw(cpu, &ra) || !ra.value)
Jacob Pan2d281d82013-10-17 10:28:35 -07001192 return -ENODEV;
1193
Jacob Pan9d31c672014-04-29 15:33:06 -07001194 return 0;
Jacob Pan2d281d82013-10-17 10:28:35 -07001195}
1196
Jacob Pane1399ba2016-05-31 13:41:29 -07001197/*
1198 * Check if power limits are available. Two cases when they are not available:
1199 * 1. Locked by BIOS, in this case we still provide read-only access so that
1200 * users can see what limit is set by the BIOS.
1201 * 2. Some CPUs make some domains monitoring only which means PLx MSRs may not
Zhang Rui33823882019-07-10 21:44:30 +08001202 * exist at all. In this case, we do not show the constraints in powercap.
Jacob Pane1399ba2016-05-31 13:41:29 -07001203 *
1204 * Called after domains are detected and initialized.
1205 */
1206static void rapl_detect_powerlimit(struct rapl_domain *rd)
1207{
1208 u64 val64;
1209 int i;
1210
1211 /* check if the domain is locked by BIOS, ignore if MSR doesn't exist */
1212 if (!rapl_read_data_raw(rd, FW_LOCK, false, &val64)) {
1213 if (val64) {
Zhang Rui9ea76122019-05-13 13:58:53 -04001214 pr_info("RAPL %s domain %s locked by BIOS\n",
1215 rd->rp->name, rd->name);
Jacob Pane1399ba2016-05-31 13:41:29 -07001216 rd->state |= DOMAIN_STATE_BIOS_LOCKED;
1217 }
1218 }
Zhang Rui33823882019-07-10 21:44:30 +08001219 /* check if power limit MSR exists, otherwise domain is monitoring only */
Jacob Pane1399ba2016-05-31 13:41:29 -07001220 for (i = 0; i < NR_POWER_LIMITS; i++) {
1221 int prim = rd->rpl[i].prim_id;
Zhang Rui33823882019-07-10 21:44:30 +08001222
Jacob Pane1399ba2016-05-31 13:41:29 -07001223 if (rapl_read_data_raw(rd, prim, false, &val64))
1224 rd->rpl[i].name = NULL;
1225 }
1226}
1227
Jacob Pan2d281d82013-10-17 10:28:35 -07001228/* Detect active and valid domains for the given CPU, caller must
1229 * ensure the CPU belongs to the targeted package and CPU hotlug is disabled.
1230 */
1231static int rapl_detect_domains(struct rapl_package *rp, int cpu)
1232{
Jacob Pan2d281d82013-10-17 10:28:35 -07001233 struct rapl_domain *rd;
Thomas Gleixner58705062016-11-22 21:16:02 +00001234 int i;
Jacob Pan2d281d82013-10-17 10:28:35 -07001235
1236 for (i = 0; i < RAPL_DOMAIN_MAX; i++) {
1237 /* use physical package id to read counters */
Zhang Rui7fde2712019-07-10 21:44:26 +08001238 if (!rapl_check_domain(cpu, i, rp)) {
Jacob Pan2d281d82013-10-17 10:28:35 -07001239 rp->domain_map |= 1 << i;
Jacob Panfcdf1792014-09-02 02:55:21 -07001240 pr_info("Found RAPL domain %s\n", rapl_domain_names[i]);
1241 }
Jacob Pan2d281d82013-10-17 10:28:35 -07001242 }
Zhang Rui33823882019-07-10 21:44:30 +08001243 rp->nr_domains = bitmap_weight(&rp->domain_map, RAPL_DOMAIN_MAX);
Jacob Pan2d281d82013-10-17 10:28:35 -07001244 if (!rp->nr_domains) {
Zhang Rui9ea76122019-05-13 13:58:53 -04001245 pr_debug("no valid rapl domains found in %s\n", rp->name);
Thomas Gleixner58705062016-11-22 21:16:02 +00001246 return -ENODEV;
Jacob Pan2d281d82013-10-17 10:28:35 -07001247 }
Zhang Rui9ea76122019-05-13 13:58:53 -04001248 pr_debug("found %d domains on %s\n", rp->nr_domains, rp->name);
Jacob Pan2d281d82013-10-17 10:28:35 -07001249
1250 rp->domains = kcalloc(rp->nr_domains + 1, sizeof(struct rapl_domain),
Zhang Rui33823882019-07-10 21:44:30 +08001251 GFP_KERNEL);
Thomas Gleixner58705062016-11-22 21:16:02 +00001252 if (!rp->domains)
1253 return -ENOMEM;
1254
Jacob Pan2d281d82013-10-17 10:28:35 -07001255 rapl_init_domains(rp);
1256
Jacob Pane1399ba2016-05-31 13:41:29 -07001257 for (rd = rp->domains; rd < rp->domains + rp->nr_domains; rd++)
1258 rapl_detect_powerlimit(rd);
1259
Jacob Pan2d281d82013-10-17 10:28:35 -07001260 return 0;
1261}
1262
1263/* called from CPU hotplug notifier, hotplug lock held */
Zhang Rui33823882019-07-10 21:44:30 +08001264void rapl_remove_package(struct rapl_package *rp)
Jacob Pan2d281d82013-10-17 10:28:35 -07001265{
1266 struct rapl_domain *rd, *rd_package = NULL;
1267
Thomas Gleixner58705062016-11-22 21:16:02 +00001268 package_power_limit_irq_restore(rp);
1269
Jacob Pan2d281d82013-10-17 10:28:35 -07001270 for (rd = rp->domains; rd < rp->domains + rp->nr_domains; rd++) {
Thomas Gleixner58705062016-11-22 21:16:02 +00001271 rapl_write_data_raw(rd, PL1_ENABLE, 0);
1272 rapl_write_data_raw(rd, PL1_CLAMP, 0);
1273 if (find_nr_power_limit(rd) > 1) {
1274 rapl_write_data_raw(rd, PL2_ENABLE, 0);
1275 rapl_write_data_raw(rd, PL2_CLAMP, 0);
1276 }
Jacob Pan2d281d82013-10-17 10:28:35 -07001277 if (rd->id == RAPL_DOMAIN_PACKAGE) {
1278 rd_package = rd;
1279 continue;
1280 }
Zhang Rui9ea76122019-05-13 13:58:53 -04001281 pr_debug("remove package, undo power limit on %s: %s\n",
1282 rp->name, rd->name);
Zhang Rui33823882019-07-10 21:44:30 +08001283 powercap_unregister_zone(rp->priv->control_type,
1284 &rd->power_zone);
Jacob Pan2d281d82013-10-17 10:28:35 -07001285 }
1286 /* do parent zone last */
Zhang Rui33823882019-07-10 21:44:30 +08001287 powercap_unregister_zone(rp->priv->control_type,
1288 &rd_package->power_zone);
Jacob Pan2d281d82013-10-17 10:28:35 -07001289 list_del(&rp->plist);
1290 kfree(rp);
1291}
Zhang Rui33823882019-07-10 21:44:30 +08001292EXPORT_SYMBOL_GPL(rapl_remove_package);
1293
1294/* caller to ensure CPU hotplug lock is held */
1295struct rapl_package *rapl_find_package_domain(int cpu, struct rapl_if_priv *priv)
1296{
1297 int id = topology_logical_die_id(cpu);
1298 struct rapl_package *rp;
1299
1300 list_for_each_entry(rp, &rapl_packages, plist) {
1301 if (rp->id == id
1302 && rp->priv->control_type == priv->control_type)
1303 return rp;
1304 }
1305
1306 return NULL;
1307}
1308EXPORT_SYMBOL_GPL(rapl_find_package_domain);
Jacob Pan2d281d82013-10-17 10:28:35 -07001309
1310/* called from CPU hotplug notifier, hotplug lock held */
Zhang Rui33823882019-07-10 21:44:30 +08001311struct rapl_package *rapl_add_package(int cpu, struct rapl_if_priv *priv)
Jacob Pan2d281d82013-10-17 10:28:35 -07001312{
Zhang Rui32fb4802019-05-13 13:58:51 -04001313 int id = topology_logical_die_id(cpu);
Jacob Pan2d281d82013-10-17 10:28:35 -07001314 struct rapl_package *rp;
Zhang Rui9ea76122019-05-13 13:58:53 -04001315 struct cpuinfo_x86 *c = &cpu_data(cpu);
Thomas Gleixnerb4005e92016-11-22 21:16:05 +00001316 int ret;
Jacob Pan2d281d82013-10-17 10:28:35 -07001317
Harry Pan3aa3c582019-12-30 22:36:56 +08001318 if (!rapl_defaults)
1319 return ERR_PTR(-ENODEV);
1320
Jacob Pan2d281d82013-10-17 10:28:35 -07001321 rp = kzalloc(sizeof(struct rapl_package), GFP_KERNEL);
1322 if (!rp)
Thomas Gleixnerb4005e92016-11-22 21:16:05 +00001323 return ERR_PTR(-ENOMEM);
Jacob Pan2d281d82013-10-17 10:28:35 -07001324
1325 /* add the new package to the list */
Zhang Ruiaadf7b32019-05-13 13:58:50 -04001326 rp->id = id;
Jacob Pan323ee642016-02-24 13:31:38 -08001327 rp->lead_cpu = cpu;
Zhang Rui7ebf8ef2019-07-10 21:44:25 +08001328 rp->priv = priv;
Jacob Pan323ee642016-02-24 13:31:38 -08001329
Zhang Rui9ea76122019-05-13 13:58:53 -04001330 if (topology_max_die_per_package() > 1)
1331 snprintf(rp->name, PACKAGE_DOMAIN_NAME_LENGTH,
Zhang Rui33823882019-07-10 21:44:30 +08001332 "package-%d-die-%d", c->phys_proc_id, c->cpu_die_id);
Zhang Rui9ea76122019-05-13 13:58:53 -04001333 else
1334 snprintf(rp->name, PACKAGE_DOMAIN_NAME_LENGTH, "package-%d",
Zhang Rui33823882019-07-10 21:44:30 +08001335 c->phys_proc_id);
Zhang Rui9ea76122019-05-13 13:58:53 -04001336
Jacob Pan2d281d82013-10-17 10:28:35 -07001337 /* check if the package contains valid domains */
Zhang Rui33823882019-07-10 21:44:30 +08001338 if (rapl_detect_domains(rp, cpu) || rapl_defaults->check_unit(rp, cpu)) {
Jacob Pan2d281d82013-10-17 10:28:35 -07001339 ret = -ENODEV;
1340 goto err_free_package;
1341 }
Thomas Gleixnera74f4362016-11-22 21:15:59 +00001342 ret = rapl_package_register_powercap(rp);
1343 if (!ret) {
Jacob Pan2d281d82013-10-17 10:28:35 -07001344 INIT_LIST_HEAD(&rp->plist);
1345 list_add(&rp->plist, &rapl_packages);
Thomas Gleixnerb4005e92016-11-22 21:16:05 +00001346 return rp;
Jacob Pan2d281d82013-10-17 10:28:35 -07001347 }
1348
1349err_free_package:
1350 kfree(rp->domains);
1351 kfree(rp);
Thomas Gleixnerb4005e92016-11-22 21:16:05 +00001352 return ERR_PTR(ret);
Jacob Pan2d281d82013-10-17 10:28:35 -07001353}
Zhang Rui33823882019-07-10 21:44:30 +08001354EXPORT_SYMBOL_GPL(rapl_add_package);
Jacob Pan2d281d82013-10-17 10:28:35 -07001355
Zhen Han52b36722018-01-10 08:38:23 +08001356static void power_limit_state_save(void)
1357{
1358 struct rapl_package *rp;
1359 struct rapl_domain *rd;
1360 int nr_pl, ret, i;
1361
1362 get_online_cpus();
1363 list_for_each_entry(rp, &rapl_packages, plist) {
1364 if (!rp->power_zone)
1365 continue;
1366 rd = power_zone_to_rapl_domain(rp->power_zone);
1367 nr_pl = find_nr_power_limit(rd);
1368 for (i = 0; i < nr_pl; i++) {
1369 switch (rd->rpl[i].prim_id) {
1370 case PL1_ENABLE:
1371 ret = rapl_read_data_raw(rd,
Zhang Rui33823882019-07-10 21:44:30 +08001372 POWER_LIMIT1, true,
1373 &rd->rpl[i].last_power_limit);
Zhen Han52b36722018-01-10 08:38:23 +08001374 if (ret)
1375 rd->rpl[i].last_power_limit = 0;
1376 break;
1377 case PL2_ENABLE:
1378 ret = rapl_read_data_raw(rd,
Zhang Rui33823882019-07-10 21:44:30 +08001379 POWER_LIMIT2, true,
1380 &rd->rpl[i].last_power_limit);
Zhen Han52b36722018-01-10 08:38:23 +08001381 if (ret)
1382 rd->rpl[i].last_power_limit = 0;
1383 break;
1384 }
1385 }
1386 }
1387 put_online_cpus();
1388}
1389
1390static void power_limit_state_restore(void)
1391{
1392 struct rapl_package *rp;
1393 struct rapl_domain *rd;
1394 int nr_pl, i;
1395
1396 get_online_cpus();
1397 list_for_each_entry(rp, &rapl_packages, plist) {
1398 if (!rp->power_zone)
1399 continue;
1400 rd = power_zone_to_rapl_domain(rp->power_zone);
1401 nr_pl = find_nr_power_limit(rd);
1402 for (i = 0; i < nr_pl; i++) {
1403 switch (rd->rpl[i].prim_id) {
1404 case PL1_ENABLE:
1405 if (rd->rpl[i].last_power_limit)
Zhang Rui33823882019-07-10 21:44:30 +08001406 rapl_write_data_raw(rd, POWER_LIMIT1,
1407 rd->rpl[i].last_power_limit);
Zhen Han52b36722018-01-10 08:38:23 +08001408 break;
1409 case PL2_ENABLE:
1410 if (rd->rpl[i].last_power_limit)
Zhang Rui33823882019-07-10 21:44:30 +08001411 rapl_write_data_raw(rd, POWER_LIMIT2,
1412 rd->rpl[i].last_power_limit);
Zhen Han52b36722018-01-10 08:38:23 +08001413 break;
1414 }
1415 }
1416 }
1417 put_online_cpus();
1418}
1419
1420static int rapl_pm_callback(struct notifier_block *nb,
Zhang Rui33823882019-07-10 21:44:30 +08001421 unsigned long mode, void *_unused)
Zhen Han52b36722018-01-10 08:38:23 +08001422{
1423 switch (mode) {
1424 case PM_SUSPEND_PREPARE:
1425 power_limit_state_save();
1426 break;
1427 case PM_POST_SUSPEND:
1428 power_limit_state_restore();
1429 break;
1430 }
1431 return NOTIFY_OK;
1432}
1433
1434static struct notifier_block rapl_pm_notifier = {
1435 .notifier_call = rapl_pm_callback,
1436};
1437
Zhang Ruiabcfaeb2019-07-10 21:44:34 +08001438static struct platform_device *rapl_msr_platdev;
1439
1440static int __init rapl_init(void)
Jacob Pan2d281d82013-10-17 10:28:35 -07001441{
Jacob Pan087e9cb2014-11-07 09:29:25 -08001442 const struct x86_cpu_id *id;
Thomas Gleixner58705062016-11-22 21:16:02 +00001443 int ret;
Jacob Pan2d281d82013-10-17 10:28:35 -07001444
Jacob Pan087e9cb2014-11-07 09:29:25 -08001445 id = x86_match_cpu(rapl_ids);
1446 if (!id) {
Jacob Pan2d281d82013-10-17 10:28:35 -07001447 pr_err("driver does not support CPU family %d model %d\n",
Zhang Rui33823882019-07-10 21:44:30 +08001448 boot_cpu_data.x86, boot_cpu_data.x86_model);
Jacob Pan2d281d82013-10-17 10:28:35 -07001449
1450 return -ENODEV;
1451 }
Srivatsa S. Bhat009f2252014-03-11 02:09:26 +05301452
Jacob Pan087e9cb2014-11-07 09:29:25 -08001453 rapl_defaults = (struct rapl_defaults *)id->driver_data;
1454
Zhen Han52b36722018-01-10 08:38:23 +08001455 ret = register_pm_notifier(&rapl_pm_notifier);
Zhang Ruiabcfaeb2019-07-10 21:44:34 +08001456 if (ret)
1457 return ret;
Zhen Han52b36722018-01-10 08:38:23 +08001458
Zhang Ruiabcfaeb2019-07-10 21:44:34 +08001459 rapl_msr_platdev = platform_device_alloc("intel_rapl_msr", 0);
1460 if (!rapl_msr_platdev) {
1461 ret = -ENOMEM;
1462 goto end;
1463 }
1464
1465 ret = platform_device_add(rapl_msr_platdev);
1466 if (ret)
1467 platform_device_put(rapl_msr_platdev);
1468
1469end:
1470 if (ret)
1471 unregister_pm_notifier(&rapl_pm_notifier);
1472
1473 return ret;
Jacob Pan2d281d82013-10-17 10:28:35 -07001474}
1475
Zhang Ruiabcfaeb2019-07-10 21:44:34 +08001476static void __exit rapl_exit(void)
Jacob Pan2d281d82013-10-17 10:28:35 -07001477{
Zhang Ruiabcfaeb2019-07-10 21:44:34 +08001478 platform_device_unregister(rapl_msr_platdev);
Zhen Han52b36722018-01-10 08:38:23 +08001479 unregister_pm_notifier(&rapl_pm_notifier);
Jacob Pan2d281d82013-10-17 10:28:35 -07001480}
1481
Zhang Ruif76cb062019-07-19 23:25:14 +08001482fs_initcall(rapl_init);
Zhang Ruiabcfaeb2019-07-10 21:44:34 +08001483module_exit(rapl_exit);
1484
Zhang Rui33823882019-07-10 21:44:30 +08001485MODULE_DESCRIPTION("Intel Runtime Average Power Limit (RAPL) common code");
Jacob Pan2d281d82013-10-17 10:28:35 -07001486MODULE_AUTHOR("Jacob Pan <jacob.jun.pan@intel.com>");
1487MODULE_LICENSE("GPL v2");