blob: d3b9d1cf4d48f0829e9a104b84fd441bd9383d77 [file] [log] [blame]
Thomas Gleixnerf6cc69f2019-05-29 16:57:24 -07001// SPDX-License-Identifier: GPL-2.0-only
Jacob Pan2d281d82013-10-17 10:28:35 -07002/*
3 * Intel Running Average Power Limit (RAPL) Driver
4 * Copyright (c) 2013, Intel Corporation.
Jacob Pan2d281d82013-10-17 10:28:35 -07005 */
6#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
7
8#include <linux/kernel.h>
9#include <linux/module.h>
10#include <linux/list.h>
11#include <linux/types.h>
12#include <linux/device.h>
13#include <linux/slab.h>
14#include <linux/log2.h>
15#include <linux/bitmap.h>
16#include <linux/delay.h>
17#include <linux/sysfs.h>
18#include <linux/cpu.h>
19#include <linux/powercap.h>
Zhen Han52b36722018-01-10 08:38:23 +080020#include <linux/suspend.h>
Zhang Ruiff956822019-07-10 21:44:24 +080021#include <linux/intel_rapl.h>
Jacob Pan2d281d82013-10-17 10:28:35 -070022
Zhang Ruiff956822019-07-10 21:44:24 +080023#include <asm/iosf_mbi.h>
Jacob Pan2d281d82013-10-17 10:28:35 -070024#include <asm/processor.h>
25#include <asm/cpu_device_id.h>
Dave Hansen62d16732016-06-02 17:19:36 -070026#include <asm/intel-family.h>
Jacob Pan2d281d82013-10-17 10:28:35 -070027
Srinivas Pandruvada3521ba12016-04-17 15:03:01 -070028/* Local defines */
29#define MSR_PLATFORM_POWER_LIMIT 0x0000065C
30
Jacob Pan2d281d82013-10-17 10:28:35 -070031/* bitmasks for RAPL MSRs, used by primitive access functions */
32#define ENERGY_STATUS_MASK 0xffffffff
33
34#define POWER_LIMIT1_MASK 0x7FFF
35#define POWER_LIMIT1_ENABLE BIT(15)
36#define POWER_LIMIT1_CLAMP BIT(16)
37
38#define POWER_LIMIT2_MASK (0x7FFFULL<<32)
39#define POWER_LIMIT2_ENABLE BIT_ULL(47)
40#define POWER_LIMIT2_CLAMP BIT_ULL(48)
41#define POWER_PACKAGE_LOCK BIT_ULL(63)
42#define POWER_PP_LOCK BIT(31)
43
44#define TIME_WINDOW1_MASK (0x7FULL<<17)
45#define TIME_WINDOW2_MASK (0x7FULL<<49)
46
47#define POWER_UNIT_OFFSET 0
48#define POWER_UNIT_MASK 0x0F
49
50#define ENERGY_UNIT_OFFSET 0x08
51#define ENERGY_UNIT_MASK 0x1F00
52
53#define TIME_UNIT_OFFSET 0x10
54#define TIME_UNIT_MASK 0xF0000
55
56#define POWER_INFO_MAX_MASK (0x7fffULL<<32)
57#define POWER_INFO_MIN_MASK (0x7fffULL<<16)
58#define POWER_INFO_MAX_TIME_WIN_MASK (0x3fULL<<48)
59#define POWER_INFO_THERMAL_SPEC_MASK 0x7fff
60
61#define PERF_STATUS_THROTTLE_TIME_MASK 0xffffffff
62#define PP_POLICY_MASK 0x1F
63
64/* Non HW constants */
65#define RAPL_PRIMITIVE_DERIVED BIT(1) /* not from raw data */
66#define RAPL_PRIMITIVE_DUMMY BIT(2)
67
Jacob Pan2d281d82013-10-17 10:28:35 -070068#define TIME_WINDOW_MAX_MSEC 40000
69#define TIME_WINDOW_MIN_MSEC 250
Jacob Pand474a4d2015-03-13 03:48:56 -070070#define ENERGY_UNIT_SCALE 1000 /* scale from driver unit to powercap unit */
Jacob Pan2d281d82013-10-17 10:28:35 -070071enum unit_type {
72 ARBITRARY_UNIT, /* no translation */
73 POWER_UNIT,
74 ENERGY_UNIT,
75 TIME_UNIT,
76};
77
Zhang Rui7ebf8ef2019-07-10 21:44:25 +080078/* private data for RAPL MSR Interface */
Zhang Rui7fde2712019-07-10 21:44:26 +080079static struct rapl_if_priv rapl_msr_priv = {
80 .reg_unit = MSR_RAPL_POWER_UNIT,
81 .regs[RAPL_DOMAIN_PACKAGE] = {
82 MSR_PKG_POWER_LIMIT, MSR_PKG_ENERGY_STATUS, MSR_PKG_PERF_STATUS, 0, MSR_PKG_POWER_INFO },
83 .regs[RAPL_DOMAIN_PP0] = {
84 MSR_PP0_POWER_LIMIT, MSR_PP0_ENERGY_STATUS, 0, MSR_PP0_POLICY, 0 },
85 .regs[RAPL_DOMAIN_PP1] = {
86 MSR_PP1_POWER_LIMIT, MSR_PP1_ENERGY_STATUS, 0, MSR_PP1_POLICY, 0 },
87 .regs[RAPL_DOMAIN_DRAM] = {
88 MSR_DRAM_POWER_LIMIT, MSR_DRAM_ENERGY_STATUS, MSR_DRAM_PERF_STATUS, 0, MSR_DRAM_POWER_INFO },
89 .regs[RAPL_DOMAIN_PLATFORM] = {
90 MSR_PLATFORM_POWER_LIMIT, MSR_PLATFORM_ENERGY_STATUS, 0, 0, 0},
91};
Zhang Rui7ebf8ef2019-07-10 21:44:25 +080092
Jacob Pan2d281d82013-10-17 10:28:35 -070093/* per domain data, some are optional */
Jacob Pan2d281d82013-10-17 10:28:35 -070094#define NR_RAW_PRIMITIVES (NR_RAPL_PRIMITIVES - 2)
95
Jacob Pan2d281d82013-10-17 10:28:35 -070096#define DOMAIN_STATE_INACTIVE BIT(0)
97#define DOMAIN_STATE_POWER_LIMIT_SET BIT(1)
98#define DOMAIN_STATE_BIOS_LOCKED BIT(2)
99
Jacob Pan2d281d82013-10-17 10:28:35 -0700100static const char pl1_name[] = "long_term";
101static const char pl2_name[] = "short_term";
102
Jacob Pan2d281d82013-10-17 10:28:35 -0700103#define power_zone_to_rapl_domain(_zone) \
104 container_of(_zone, struct rapl_domain, power_zone)
105
Jacob Pan087e9cb2014-11-07 09:29:25 -0800106struct rapl_defaults {
Ajay Thomas51b63402015-04-30 01:43:23 +0530107 u8 floor_freq_reg_addr;
Jacob Pan087e9cb2014-11-07 09:29:25 -0800108 int (*check_unit)(struct rapl_package *rp, int cpu);
109 void (*set_floor_freq)(struct rapl_domain *rd, bool mode);
110 u64 (*compute_time_window)(struct rapl_package *rp, u64 val,
111 bool to_raw);
Jacob Pand474a4d2015-03-13 03:48:56 -0700112 unsigned int dram_domain_energy_unit;
Jacob Pan087e9cb2014-11-07 09:29:25 -0800113};
114static struct rapl_defaults *rapl_defaults;
115
Jacob Pan3c2c0842014-11-07 09:29:26 -0800116/* Sideband MBI registers */
Ajay Thomas51b63402015-04-30 01:43:23 +0530117#define IOSF_CPU_POWER_BUDGET_CTL_BYT (0x2)
118#define IOSF_CPU_POWER_BUDGET_CTL_TNG (0xdf)
Jacob Pan3c2c0842014-11-07 09:29:26 -0800119
Jacob Pan2d281d82013-10-17 10:28:35 -0700120#define PACKAGE_PLN_INT_SAVED BIT(0)
121#define MAX_PRIM_NAME (32)
122
123/* per domain data. used to describe individual knobs such that access function
124 * can be consolidated into one instead of many inline functions.
125 */
126struct rapl_primitive_info {
127 const char *name;
128 u64 mask;
129 int shift;
Zhang Ruif7c4e0c2019-07-10 21:44:22 +0800130 enum rapl_domain_reg_id id;
Jacob Pan2d281d82013-10-17 10:28:35 -0700131 enum unit_type unit;
132 u32 flag;
133};
134
135#define PRIMITIVE_INFO_INIT(p, m, s, i, u, f) { \
136 .name = #p, \
137 .mask = m, \
138 .shift = s, \
139 .id = i, \
140 .unit = u, \
141 .flag = f \
142 }
143
144static void rapl_init_domains(struct rapl_package *rp);
145static int rapl_read_data_raw(struct rapl_domain *rd,
146 enum rapl_primitives prim,
147 bool xlate, u64 *data);
148static int rapl_write_data_raw(struct rapl_domain *rd,
149 enum rapl_primitives prim,
150 unsigned long long value);
Jacob Pan309557f2016-02-24 13:31:37 -0800151static u64 rapl_unit_xlate(struct rapl_domain *rd,
Jacob Pand474a4d2015-03-13 03:48:56 -0700152 enum unit_type type, u64 value,
Jacob Pan2d281d82013-10-17 10:28:35 -0700153 int to_raw);
Jacob Pan309557f2016-02-24 13:31:37 -0800154static void package_power_limit_irq_save(struct rapl_package *rp);
Jacob Pan2d281d82013-10-17 10:28:35 -0700155
156static LIST_HEAD(rapl_packages); /* guarded by CPU hotplug lock */
157
158static const char * const rapl_domain_names[] = {
159 "package",
160 "core",
161 "uncore",
162 "dram",
Srinivas Pandruvada3521ba12016-04-17 15:03:01 -0700163 "psys",
Jacob Pan2d281d82013-10-17 10:28:35 -0700164};
165
Jacob Pan2d281d82013-10-17 10:28:35 -0700166/* caller to ensure CPU hotplug lock is held */
Zhang Rui7ebf8ef2019-07-10 21:44:25 +0800167static struct rapl_package *rapl_find_package_domain(int cpu, struct rapl_if_priv *priv)
Jacob Pan2d281d82013-10-17 10:28:35 -0700168{
Zhang Rui32fb4802019-05-13 13:58:51 -0400169 int id = topology_logical_die_id(cpu);
Jacob Pan2d281d82013-10-17 10:28:35 -0700170 struct rapl_package *rp;
171
172 list_for_each_entry(rp, &rapl_packages, plist) {
Zhang Rui7ebf8ef2019-07-10 21:44:25 +0800173 if (rp->id == id && rp->priv->control_type == priv->control_type)
Jacob Pan2d281d82013-10-17 10:28:35 -0700174 return rp;
175 }
176
177 return NULL;
178}
179
Jacob Pan2d281d82013-10-17 10:28:35 -0700180static int get_energy_counter(struct powercap_zone *power_zone, u64 *energy_raw)
181{
182 struct rapl_domain *rd;
183 u64 energy_now;
184
185 /* prevent CPU hotplug, make sure the RAPL domain does not go
186 * away while reading the counter.
187 */
188 get_online_cpus();
189 rd = power_zone_to_rapl_domain(power_zone);
190
191 if (!rapl_read_data_raw(rd, ENERGY_COUNTER, true, &energy_now)) {
192 *energy_raw = energy_now;
193 put_online_cpus();
194
195 return 0;
196 }
197 put_online_cpus();
198
199 return -EIO;
200}
201
202static int get_max_energy_counter(struct powercap_zone *pcd_dev, u64 *energy)
203{
Jacob Pand474a4d2015-03-13 03:48:56 -0700204 struct rapl_domain *rd = power_zone_to_rapl_domain(pcd_dev);
205
Jacob Pan309557f2016-02-24 13:31:37 -0800206 *energy = rapl_unit_xlate(rd, ENERGY_UNIT, ENERGY_STATUS_MASK, 0);
Jacob Pan2d281d82013-10-17 10:28:35 -0700207 return 0;
208}
209
210static int release_zone(struct powercap_zone *power_zone)
211{
212 struct rapl_domain *rd = power_zone_to_rapl_domain(power_zone);
Jacob Pan309557f2016-02-24 13:31:37 -0800213 struct rapl_package *rp = rd->rp;
Jacob Pan2d281d82013-10-17 10:28:35 -0700214
215 /* package zone is the last zone of a package, we can free
216 * memory here since all children has been unregistered.
217 */
218 if (rd->id == RAPL_DOMAIN_PACKAGE) {
Jacob Pan2d281d82013-10-17 10:28:35 -0700219 kfree(rd);
220 rp->domains = NULL;
221 }
222
223 return 0;
224
225}
226
227static int find_nr_power_limit(struct rapl_domain *rd)
228{
Jacob Pane1399ba2016-05-31 13:41:29 -0700229 int i, nr_pl = 0;
Jacob Pan2d281d82013-10-17 10:28:35 -0700230
231 for (i = 0; i < NR_POWER_LIMITS; i++) {
Jacob Pane1399ba2016-05-31 13:41:29 -0700232 if (rd->rpl[i].name)
233 nr_pl++;
Jacob Pan2d281d82013-10-17 10:28:35 -0700234 }
235
Jacob Pane1399ba2016-05-31 13:41:29 -0700236 return nr_pl;
Jacob Pan2d281d82013-10-17 10:28:35 -0700237}
238
239static int set_domain_enable(struct powercap_zone *power_zone, bool mode)
240{
241 struct rapl_domain *rd = power_zone_to_rapl_domain(power_zone);
Jacob Pan2d281d82013-10-17 10:28:35 -0700242
243 if (rd->state & DOMAIN_STATE_BIOS_LOCKED)
244 return -EACCES;
Jacob Pan3c2c0842014-11-07 09:29:26 -0800245
Jacob Pan2d281d82013-10-17 10:28:35 -0700246 get_online_cpus();
Jacob Pan2d281d82013-10-17 10:28:35 -0700247 rapl_write_data_raw(rd, PL1_ENABLE, mode);
Ajay Thomas51b63402015-04-30 01:43:23 +0530248 if (rapl_defaults->set_floor_freq)
249 rapl_defaults->set_floor_freq(rd, mode);
Jacob Pan2d281d82013-10-17 10:28:35 -0700250 put_online_cpus();
251
252 return 0;
253}
254
255static int get_domain_enable(struct powercap_zone *power_zone, bool *mode)
256{
257 struct rapl_domain *rd = power_zone_to_rapl_domain(power_zone);
258 u64 val;
259
260 if (rd->state & DOMAIN_STATE_BIOS_LOCKED) {
261 *mode = false;
262 return 0;
263 }
264 get_online_cpus();
265 if (rapl_read_data_raw(rd, PL1_ENABLE, true, &val)) {
266 put_online_cpus();
267 return -EIO;
268 }
269 *mode = val;
270 put_online_cpus();
271
272 return 0;
273}
274
275/* per RAPL domain ops, in the order of rapl_domain_type */
Julia Lawall600c3952015-12-23 22:59:55 +0100276static const struct powercap_zone_ops zone_ops[] = {
Jacob Pan2d281d82013-10-17 10:28:35 -0700277 /* RAPL_DOMAIN_PACKAGE */
278 {
279 .get_energy_uj = get_energy_counter,
280 .get_max_energy_range_uj = get_max_energy_counter,
281 .release = release_zone,
282 .set_enable = set_domain_enable,
283 .get_enable = get_domain_enable,
284 },
285 /* RAPL_DOMAIN_PP0 */
286 {
287 .get_energy_uj = get_energy_counter,
288 .get_max_energy_range_uj = get_max_energy_counter,
289 .release = release_zone,
290 .set_enable = set_domain_enable,
291 .get_enable = get_domain_enable,
292 },
293 /* RAPL_DOMAIN_PP1 */
294 {
295 .get_energy_uj = get_energy_counter,
296 .get_max_energy_range_uj = get_max_energy_counter,
297 .release = release_zone,
298 .set_enable = set_domain_enable,
299 .get_enable = get_domain_enable,
300 },
301 /* RAPL_DOMAIN_DRAM */
302 {
303 .get_energy_uj = get_energy_counter,
304 .get_max_energy_range_uj = get_max_energy_counter,
305 .release = release_zone,
306 .set_enable = set_domain_enable,
307 .get_enable = get_domain_enable,
308 },
Srinivas Pandruvada3521ba12016-04-17 15:03:01 -0700309 /* RAPL_DOMAIN_PLATFORM */
310 {
311 .get_energy_uj = get_energy_counter,
312 .get_max_energy_range_uj = get_max_energy_counter,
313 .release = release_zone,
314 .set_enable = set_domain_enable,
315 .get_enable = get_domain_enable,
316 },
Jacob Pan2d281d82013-10-17 10:28:35 -0700317};
318
Jacob Pane1399ba2016-05-31 13:41:29 -0700319
320/*
321 * Constraint index used by powercap can be different than power limit (PL)
322 * index in that some PLs maybe missing due to non-existant MSRs. So we
323 * need to convert here by finding the valid PLs only (name populated).
324 */
325static int contraint_to_pl(struct rapl_domain *rd, int cid)
326{
327 int i, j;
328
329 for (i = 0, j = 0; i < NR_POWER_LIMITS; i++) {
330 if ((rd->rpl[i].name) && j++ == cid) {
331 pr_debug("%s: index %d\n", __func__, i);
332 return i;
333 }
334 }
Jacob Pancb43f812016-11-28 13:53:11 -0800335 pr_err("Cannot find matching power limit for constraint %d\n", cid);
Jacob Pane1399ba2016-05-31 13:41:29 -0700336
337 return -EINVAL;
338}
339
340static int set_power_limit(struct powercap_zone *power_zone, int cid,
Jacob Pan2d281d82013-10-17 10:28:35 -0700341 u64 power_limit)
342{
343 struct rapl_domain *rd;
344 struct rapl_package *rp;
345 int ret = 0;
Jacob Pane1399ba2016-05-31 13:41:29 -0700346 int id;
Jacob Pan2d281d82013-10-17 10:28:35 -0700347
348 get_online_cpus();
349 rd = power_zone_to_rapl_domain(power_zone);
Jacob Pane1399ba2016-05-31 13:41:29 -0700350 id = contraint_to_pl(rd, cid);
Jacob Pancb43f812016-11-28 13:53:11 -0800351 if (id < 0) {
352 ret = id;
353 goto set_exit;
354 }
Jacob Pane1399ba2016-05-31 13:41:29 -0700355
Jacob Pan309557f2016-02-24 13:31:37 -0800356 rp = rd->rp;
Jacob Pan2d281d82013-10-17 10:28:35 -0700357
358 if (rd->state & DOMAIN_STATE_BIOS_LOCKED) {
359 dev_warn(&power_zone->dev, "%s locked by BIOS, monitoring only\n",
360 rd->name);
361 ret = -EACCES;
362 goto set_exit;
363 }
364
365 switch (rd->rpl[id].prim_id) {
366 case PL1_ENABLE:
367 rapl_write_data_raw(rd, POWER_LIMIT1, power_limit);
368 break;
369 case PL2_ENABLE:
370 rapl_write_data_raw(rd, POWER_LIMIT2, power_limit);
371 break;
372 default:
373 ret = -EINVAL;
374 }
375 if (!ret)
Jacob Pan309557f2016-02-24 13:31:37 -0800376 package_power_limit_irq_save(rp);
Jacob Pan2d281d82013-10-17 10:28:35 -0700377set_exit:
378 put_online_cpus();
379 return ret;
380}
381
Jacob Pane1399ba2016-05-31 13:41:29 -0700382static int get_current_power_limit(struct powercap_zone *power_zone, int cid,
Jacob Pan2d281d82013-10-17 10:28:35 -0700383 u64 *data)
384{
385 struct rapl_domain *rd;
386 u64 val;
387 int prim;
388 int ret = 0;
Jacob Pane1399ba2016-05-31 13:41:29 -0700389 int id;
Jacob Pan2d281d82013-10-17 10:28:35 -0700390
391 get_online_cpus();
392 rd = power_zone_to_rapl_domain(power_zone);
Jacob Pane1399ba2016-05-31 13:41:29 -0700393 id = contraint_to_pl(rd, cid);
Jacob Pancb43f812016-11-28 13:53:11 -0800394 if (id < 0) {
395 ret = id;
396 goto get_exit;
397 }
398
Jacob Pan2d281d82013-10-17 10:28:35 -0700399 switch (rd->rpl[id].prim_id) {
400 case PL1_ENABLE:
401 prim = POWER_LIMIT1;
402 break;
403 case PL2_ENABLE:
404 prim = POWER_LIMIT2;
405 break;
406 default:
407 put_online_cpus();
408 return -EINVAL;
409 }
410 if (rapl_read_data_raw(rd, prim, true, &val))
411 ret = -EIO;
412 else
413 *data = val;
414
Jacob Pancb43f812016-11-28 13:53:11 -0800415get_exit:
Jacob Pan2d281d82013-10-17 10:28:35 -0700416 put_online_cpus();
417
418 return ret;
419}
420
Jacob Pane1399ba2016-05-31 13:41:29 -0700421static int set_time_window(struct powercap_zone *power_zone, int cid,
Jacob Pan2d281d82013-10-17 10:28:35 -0700422 u64 window)
423{
424 struct rapl_domain *rd;
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 set_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 rapl_write_data_raw(rd, TIME_WINDOW1, window);
439 break;
440 case PL2_ENABLE:
441 rapl_write_data_raw(rd, TIME_WINDOW2, window);
442 break;
443 default:
444 ret = -EINVAL;
445 }
Jacob Pancb43f812016-11-28 13:53:11 -0800446
447set_time_exit:
Jacob Pan2d281d82013-10-17 10:28:35 -0700448 put_online_cpus();
449 return ret;
450}
451
Jacob Pane1399ba2016-05-31 13:41:29 -0700452static int get_time_window(struct powercap_zone *power_zone, int cid, u64 *data)
Jacob Pan2d281d82013-10-17 10:28:35 -0700453{
454 struct rapl_domain *rd;
455 u64 val;
456 int ret = 0;
Jacob Pane1399ba2016-05-31 13:41:29 -0700457 int id;
Jacob Pan2d281d82013-10-17 10:28:35 -0700458
459 get_online_cpus();
460 rd = power_zone_to_rapl_domain(power_zone);
Jacob Pane1399ba2016-05-31 13:41:29 -0700461 id = contraint_to_pl(rd, cid);
Jacob Pancb43f812016-11-28 13:53:11 -0800462 if (id < 0) {
463 ret = id;
464 goto get_time_exit;
465 }
Jacob Pane1399ba2016-05-31 13:41:29 -0700466
Jacob Pan2d281d82013-10-17 10:28:35 -0700467 switch (rd->rpl[id].prim_id) {
468 case PL1_ENABLE:
469 ret = rapl_read_data_raw(rd, TIME_WINDOW1, true, &val);
470 break;
471 case PL2_ENABLE:
472 ret = rapl_read_data_raw(rd, TIME_WINDOW2, true, &val);
473 break;
474 default:
475 put_online_cpus();
476 return -EINVAL;
477 }
478 if (!ret)
479 *data = val;
Jacob Pancb43f812016-11-28 13:53:11 -0800480
481get_time_exit:
Jacob Pan2d281d82013-10-17 10:28:35 -0700482 put_online_cpus();
483
484 return ret;
485}
486
Jacob Pane1399ba2016-05-31 13:41:29 -0700487static const char *get_constraint_name(struct powercap_zone *power_zone, int cid)
Jacob Pan2d281d82013-10-17 10:28:35 -0700488{
Jacob Pan2d281d82013-10-17 10:28:35 -0700489 struct rapl_domain *rd;
Jacob Pane1399ba2016-05-31 13:41:29 -0700490 int id;
Jacob Pan2d281d82013-10-17 10:28:35 -0700491
492 rd = power_zone_to_rapl_domain(power_zone);
Jacob Pane1399ba2016-05-31 13:41:29 -0700493 id = contraint_to_pl(rd, cid);
494 if (id >= 0)
495 return rd->rpl[id].name;
Jacob Pan2d281d82013-10-17 10:28:35 -0700496
Jacob Pane1399ba2016-05-31 13:41:29 -0700497 return NULL;
Jacob Pan2d281d82013-10-17 10:28:35 -0700498}
499
500
501static int get_max_power(struct powercap_zone *power_zone, int id,
502 u64 *data)
503{
504 struct rapl_domain *rd;
505 u64 val;
506 int prim;
507 int ret = 0;
508
509 get_online_cpus();
510 rd = power_zone_to_rapl_domain(power_zone);
511 switch (rd->rpl[id].prim_id) {
512 case PL1_ENABLE:
513 prim = THERMAL_SPEC_POWER;
514 break;
515 case PL2_ENABLE:
516 prim = MAX_POWER;
517 break;
518 default:
519 put_online_cpus();
520 return -EINVAL;
521 }
522 if (rapl_read_data_raw(rd, prim, true, &val))
523 ret = -EIO;
524 else
525 *data = val;
526
527 put_online_cpus();
528
529 return ret;
530}
531
Julia Lawall600c3952015-12-23 22:59:55 +0100532static const struct powercap_zone_constraint_ops constraint_ops = {
Jacob Pan2d281d82013-10-17 10:28:35 -0700533 .set_power_limit_uw = set_power_limit,
534 .get_power_limit_uw = get_current_power_limit,
535 .set_time_window_us = set_time_window,
536 .get_time_window_us = get_time_window,
537 .get_max_power_uw = get_max_power,
538 .get_name = get_constraint_name,
539};
540
541/* called after domain detection and package level data are set */
542static void rapl_init_domains(struct rapl_package *rp)
543{
544 int i;
545 struct rapl_domain *rd = rp->domains;
546
547 for (i = 0; i < RAPL_DOMAIN_MAX; i++) {
548 unsigned int mask = rp->domain_map & (1 << i);
Zhang Rui7fde2712019-07-10 21:44:26 +0800549
550 rd->regs[RAPL_DOMAIN_REG_LIMIT] = rp->priv->regs[i][RAPL_DOMAIN_REG_LIMIT];
551 rd->regs[RAPL_DOMAIN_REG_STATUS] = rp->priv->regs[i][RAPL_DOMAIN_REG_STATUS];
552 rd->regs[RAPL_DOMAIN_REG_PERF] = rp->priv->regs[i][RAPL_DOMAIN_REG_PERF];
553 rd->regs[RAPL_DOMAIN_REG_POLICY] = rp->priv->regs[i][RAPL_DOMAIN_REG_POLICY];
554 rd->regs[RAPL_DOMAIN_REG_INFO] = rp->priv->regs[i][RAPL_DOMAIN_REG_INFO];
555
Jacob Pan2d281d82013-10-17 10:28:35 -0700556 switch (mask) {
557 case BIT(RAPL_DOMAIN_PACKAGE):
558 rd->name = rapl_domain_names[RAPL_DOMAIN_PACKAGE];
559 rd->id = RAPL_DOMAIN_PACKAGE;
Jacob Pan2d281d82013-10-17 10:28:35 -0700560 rd->rpl[0].prim_id = PL1_ENABLE;
561 rd->rpl[0].name = pl1_name;
562 rd->rpl[1].prim_id = PL2_ENABLE;
563 rd->rpl[1].name = pl2_name;
564 break;
565 case BIT(RAPL_DOMAIN_PP0):
566 rd->name = rapl_domain_names[RAPL_DOMAIN_PP0];
567 rd->id = RAPL_DOMAIN_PP0;
Jacob Pan2d281d82013-10-17 10:28:35 -0700568 rd->rpl[0].prim_id = PL1_ENABLE;
569 rd->rpl[0].name = pl1_name;
570 break;
571 case BIT(RAPL_DOMAIN_PP1):
572 rd->name = rapl_domain_names[RAPL_DOMAIN_PP1];
573 rd->id = RAPL_DOMAIN_PP1;
Jacob Pan2d281d82013-10-17 10:28:35 -0700574 rd->rpl[0].prim_id = PL1_ENABLE;
575 rd->rpl[0].name = pl1_name;
576 break;
577 case BIT(RAPL_DOMAIN_DRAM):
578 rd->name = rapl_domain_names[RAPL_DOMAIN_DRAM];
579 rd->id = RAPL_DOMAIN_DRAM;
Jacob Pan2d281d82013-10-17 10:28:35 -0700580 rd->rpl[0].prim_id = PL1_ENABLE;
581 rd->rpl[0].name = pl1_name;
Jacob Pand474a4d2015-03-13 03:48:56 -0700582 rd->domain_energy_unit =
583 rapl_defaults->dram_domain_energy_unit;
584 if (rd->domain_energy_unit)
585 pr_info("DRAM domain energy unit %dpj\n",
586 rd->domain_energy_unit);
Jacob Pan2d281d82013-10-17 10:28:35 -0700587 break;
588 }
589 if (mask) {
Jacob Pan309557f2016-02-24 13:31:37 -0800590 rd->rp = rp;
Jacob Pan2d281d82013-10-17 10:28:35 -0700591 rd++;
592 }
593 }
594}
595
Jacob Pan309557f2016-02-24 13:31:37 -0800596static u64 rapl_unit_xlate(struct rapl_domain *rd, enum unit_type type,
597 u64 value, int to_raw)
Jacob Pan2d281d82013-10-17 10:28:35 -0700598{
Jacob Pan3c2c0842014-11-07 09:29:26 -0800599 u64 units = 1;
Jacob Pan309557f2016-02-24 13:31:37 -0800600 struct rapl_package *rp = rd->rp;
Jacob Pand474a4d2015-03-13 03:48:56 -0700601 u64 scale = 1;
Jacob Pan2d281d82013-10-17 10:28:35 -0700602
Jacob Pan2d281d82013-10-17 10:28:35 -0700603 switch (type) {
604 case POWER_UNIT:
Jacob Pan3c2c0842014-11-07 09:29:26 -0800605 units = rp->power_unit;
Jacob Pan2d281d82013-10-17 10:28:35 -0700606 break;
607 case ENERGY_UNIT:
Jacob Pand474a4d2015-03-13 03:48:56 -0700608 scale = ENERGY_UNIT_SCALE;
609 /* per domain unit takes precedence */
Jacob Pancb43f812016-11-28 13:53:11 -0800610 if (rd->domain_energy_unit)
Jacob Pand474a4d2015-03-13 03:48:56 -0700611 units = rd->domain_energy_unit;
612 else
613 units = rp->energy_unit;
Jacob Pan2d281d82013-10-17 10:28:35 -0700614 break;
615 case TIME_UNIT:
Jacob Pan3c2c0842014-11-07 09:29:26 -0800616 return rapl_defaults->compute_time_window(rp, value, to_raw);
Jacob Pan2d281d82013-10-17 10:28:35 -0700617 case ARBITRARY_UNIT:
618 default:
619 return value;
620 };
621
622 if (to_raw)
Jacob Pand474a4d2015-03-13 03:48:56 -0700623 return div64_u64(value, units) * scale;
Jacob Pan3c2c0842014-11-07 09:29:26 -0800624
625 value *= units;
626
Jacob Pand474a4d2015-03-13 03:48:56 -0700627 return div64_u64(value, scale);
Jacob Pan2d281d82013-10-17 10:28:35 -0700628}
629
630/* in the order of enum rapl_primitives */
631static struct rapl_primitive_info rpi[] = {
632 /* name, mask, shift, msr index, unit divisor */
633 PRIMITIVE_INFO_INIT(ENERGY_COUNTER, ENERGY_STATUS_MASK, 0,
Zhang Ruif7c4e0c2019-07-10 21:44:22 +0800634 RAPL_DOMAIN_REG_STATUS, ENERGY_UNIT, 0),
Jacob Pan2d281d82013-10-17 10:28:35 -0700635 PRIMITIVE_INFO_INIT(POWER_LIMIT1, POWER_LIMIT1_MASK, 0,
Zhang Ruif7c4e0c2019-07-10 21:44:22 +0800636 RAPL_DOMAIN_REG_LIMIT, POWER_UNIT, 0),
Jacob Pan2d281d82013-10-17 10:28:35 -0700637 PRIMITIVE_INFO_INIT(POWER_LIMIT2, POWER_LIMIT2_MASK, 32,
Zhang Ruif7c4e0c2019-07-10 21:44:22 +0800638 RAPL_DOMAIN_REG_LIMIT, POWER_UNIT, 0),
Jacob Pan2d281d82013-10-17 10:28:35 -0700639 PRIMITIVE_INFO_INIT(FW_LOCK, POWER_PP_LOCK, 31,
Zhang Ruif7c4e0c2019-07-10 21:44:22 +0800640 RAPL_DOMAIN_REG_LIMIT, ARBITRARY_UNIT, 0),
Jacob Pan2d281d82013-10-17 10:28:35 -0700641 PRIMITIVE_INFO_INIT(PL1_ENABLE, POWER_LIMIT1_ENABLE, 15,
Zhang Ruif7c4e0c2019-07-10 21:44:22 +0800642 RAPL_DOMAIN_REG_LIMIT, ARBITRARY_UNIT, 0),
Jacob Pan2d281d82013-10-17 10:28:35 -0700643 PRIMITIVE_INFO_INIT(PL1_CLAMP, POWER_LIMIT1_CLAMP, 16,
Zhang Ruif7c4e0c2019-07-10 21:44:22 +0800644 RAPL_DOMAIN_REG_LIMIT, ARBITRARY_UNIT, 0),
Jacob Pan2d281d82013-10-17 10:28:35 -0700645 PRIMITIVE_INFO_INIT(PL2_ENABLE, POWER_LIMIT2_ENABLE, 47,
Zhang Ruif7c4e0c2019-07-10 21:44:22 +0800646 RAPL_DOMAIN_REG_LIMIT, ARBITRARY_UNIT, 0),
Jacob Pan2d281d82013-10-17 10:28:35 -0700647 PRIMITIVE_INFO_INIT(PL2_CLAMP, POWER_LIMIT2_CLAMP, 48,
Zhang Ruif7c4e0c2019-07-10 21:44:22 +0800648 RAPL_DOMAIN_REG_LIMIT, ARBITRARY_UNIT, 0),
Jacob Pan2d281d82013-10-17 10:28:35 -0700649 PRIMITIVE_INFO_INIT(TIME_WINDOW1, TIME_WINDOW1_MASK, 17,
Zhang Ruif7c4e0c2019-07-10 21:44:22 +0800650 RAPL_DOMAIN_REG_LIMIT, TIME_UNIT, 0),
Jacob Pan2d281d82013-10-17 10:28:35 -0700651 PRIMITIVE_INFO_INIT(TIME_WINDOW2, TIME_WINDOW2_MASK, 49,
Zhang Ruif7c4e0c2019-07-10 21:44:22 +0800652 RAPL_DOMAIN_REG_LIMIT, TIME_UNIT, 0),
Jacob Pan2d281d82013-10-17 10:28:35 -0700653 PRIMITIVE_INFO_INIT(THERMAL_SPEC_POWER, POWER_INFO_THERMAL_SPEC_MASK,
Zhang Ruif7c4e0c2019-07-10 21:44:22 +0800654 0, RAPL_DOMAIN_REG_INFO, POWER_UNIT, 0),
Jacob Pan2d281d82013-10-17 10:28:35 -0700655 PRIMITIVE_INFO_INIT(MAX_POWER, POWER_INFO_MAX_MASK, 32,
Zhang Ruif7c4e0c2019-07-10 21:44:22 +0800656 RAPL_DOMAIN_REG_INFO, POWER_UNIT, 0),
Jacob Pan2d281d82013-10-17 10:28:35 -0700657 PRIMITIVE_INFO_INIT(MIN_POWER, POWER_INFO_MIN_MASK, 16,
Zhang Ruif7c4e0c2019-07-10 21:44:22 +0800658 RAPL_DOMAIN_REG_INFO, POWER_UNIT, 0),
Jacob Pan2d281d82013-10-17 10:28:35 -0700659 PRIMITIVE_INFO_INIT(MAX_TIME_WINDOW, POWER_INFO_MAX_TIME_WIN_MASK, 48,
Zhang Ruif7c4e0c2019-07-10 21:44:22 +0800660 RAPL_DOMAIN_REG_INFO, TIME_UNIT, 0),
Jacob Pan2d281d82013-10-17 10:28:35 -0700661 PRIMITIVE_INFO_INIT(THROTTLED_TIME, PERF_STATUS_THROTTLE_TIME_MASK, 0,
Zhang Ruif7c4e0c2019-07-10 21:44:22 +0800662 RAPL_DOMAIN_REG_PERF, TIME_UNIT, 0),
Jacob Pan2d281d82013-10-17 10:28:35 -0700663 PRIMITIVE_INFO_INIT(PRIORITY_LEVEL, PP_POLICY_MASK, 0,
Zhang Ruif7c4e0c2019-07-10 21:44:22 +0800664 RAPL_DOMAIN_REG_POLICY, ARBITRARY_UNIT, 0),
Jacob Pan2d281d82013-10-17 10:28:35 -0700665 /* non-hardware */
666 PRIMITIVE_INFO_INIT(AVERAGE_POWER, 0, 0, 0, POWER_UNIT,
667 RAPL_PRIMITIVE_DERIVED),
668 {NULL, 0, 0, 0},
669};
670
671/* Read primitive data based on its related struct rapl_primitive_info.
672 * if xlate flag is set, return translated data based on data units, i.e.
673 * time, energy, and power.
674 * RAPL MSRs are non-architectual and are laid out not consistently across
675 * domains. Here we use primitive info to allow writing consolidated access
676 * functions.
677 * For a given primitive, it is processed by MSR mask and shift. Unit conversion
678 * is pre-assigned based on RAPL unit MSRs read at init time.
679 * 63-------------------------- 31--------------------------- 0
680 * | xxxxx (mask) |
681 * | |<- shift ----------------|
682 * 63-------------------------- 31--------------------------- 0
683 */
684static int rapl_read_data_raw(struct rapl_domain *rd,
685 enum rapl_primitives prim,
686 bool xlate, u64 *data)
687{
Zhang Ruibeea8df2019-07-10 21:44:27 +0800688 u64 value;
Jacob Pan2d281d82013-10-17 10:28:35 -0700689 struct rapl_primitive_info *rp = &rpi[prim];
Zhang Ruibeea8df2019-07-10 21:44:27 +0800690 struct reg_action ra;
Jacob Pan2d281d82013-10-17 10:28:35 -0700691 int cpu;
692
693 if (!rp->name || rp->flag & RAPL_PRIMITIVE_DUMMY)
694 return -EINVAL;
695
Zhang Ruibeea8df2019-07-10 21:44:27 +0800696 ra.reg = rd->regs[rp->id];
697 if (!ra.reg)
Jacob Pan2d281d82013-10-17 10:28:35 -0700698 return -EINVAL;
Jacob Pan323ee642016-02-24 13:31:38 -0800699
700 cpu = rd->rp->lead_cpu;
Jacob Pan2d281d82013-10-17 10:28:35 -0700701
702 /* special-case package domain, which uses a different bit*/
703 if (prim == FW_LOCK && rd->id == RAPL_DOMAIN_PACKAGE) {
704 rp->mask = POWER_PACKAGE_LOCK;
705 rp->shift = 63;
706 }
707 /* non-hardware data are collected by the polling thread */
708 if (rp->flag & RAPL_PRIMITIVE_DERIVED) {
709 *data = rd->rdd.primitives[prim];
710 return 0;
711 }
712
Zhang Ruibeea8df2019-07-10 21:44:27 +0800713 ra.mask = rp->mask;
714
715 if (rd->rp->priv->read_raw(cpu, &ra)) {
716 pr_debug("failed to read reg 0x%x on cpu %d\n", ra.reg, cpu);
Jacob Pan2d281d82013-10-17 10:28:35 -0700717 return -EIO;
718 }
719
Zhang Ruibeea8df2019-07-10 21:44:27 +0800720 value = ra.value >> rp->shift;
721
Jacob Pan2d281d82013-10-17 10:28:35 -0700722 if (xlate)
Zhang Ruibeea8df2019-07-10 21:44:27 +0800723 *data = rapl_unit_xlate(rd, rp->unit, value, 0);
Jacob Pan2d281d82013-10-17 10:28:35 -0700724 else
Zhang Ruibeea8df2019-07-10 21:44:27 +0800725 *data = value;
Jacob Pan2d281d82013-10-17 10:28:35 -0700726
727 return 0;
728}
729
730/* Similar use of primitive info in the read counterpart */
731static int rapl_write_data_raw(struct rapl_domain *rd,
732 enum rapl_primitives prim,
733 unsigned long long value)
734{
Jacob Pan2d281d82013-10-17 10:28:35 -0700735 struct rapl_primitive_info *rp = &rpi[prim];
736 int cpu;
Jacob Panf14a1392016-02-24 13:31:36 -0800737 u64 bits;
Zhang Ruibeea8df2019-07-10 21:44:27 +0800738 struct reg_action ra;
Jacob Panf14a1392016-02-24 13:31:36 -0800739 int ret;
Jacob Pan2d281d82013-10-17 10:28:35 -0700740
Jacob Pan323ee642016-02-24 13:31:38 -0800741 cpu = rd->rp->lead_cpu;
Jacob Pan309557f2016-02-24 13:31:37 -0800742 bits = rapl_unit_xlate(rd, rp->unit, value, 1);
Adam Lessnauedbdabc2017-06-01 11:21:50 +0200743 bits <<= rp->shift;
744 bits &= rp->mask;
745
Zhang Ruibeea8df2019-07-10 21:44:27 +0800746 memset(&ra, 0, sizeof(ra));
Jacob Panf14a1392016-02-24 13:31:36 -0800747
Zhang Ruibeea8df2019-07-10 21:44:27 +0800748 ra.reg = rd->regs[rp->id];
749 ra.mask = rp->mask;
750 ra.value = bits;
Jacob Panf14a1392016-02-24 13:31:36 -0800751
Zhang Ruibeea8df2019-07-10 21:44:27 +0800752 ret = rd->rp->priv->write_raw(cpu, &ra);
Jacob Panf14a1392016-02-24 13:31:36 -0800753
754 return ret;
Jacob Pan2d281d82013-10-17 10:28:35 -0700755}
756
Jacob Pan3c2c0842014-11-07 09:29:26 -0800757/*
758 * Raw RAPL data stored in MSRs are in certain scales. We need to
759 * convert them into standard units based on the units reported in
760 * the RAPL unit MSRs. This is specific to CPUs as the method to
761 * calculate units differ on different CPUs.
762 * We convert the units to below format based on CPUs.
763 * i.e.
Jacob Pand474a4d2015-03-13 03:48:56 -0700764 * energy unit: picoJoules : Represented in picoJoules by default
Jacob Pan3c2c0842014-11-07 09:29:26 -0800765 * power unit : microWatts : Represented in milliWatts by default
766 * time unit : microseconds: Represented in seconds by default
767 */
768static int rapl_check_unit_core(struct rapl_package *rp, int cpu)
Jacob Pan2d281d82013-10-17 10:28:35 -0700769{
770 u64 msr_val;
771 u32 value;
772
Zhang Rui7fde2712019-07-10 21:44:26 +0800773 if (rdmsrl_safe_on_cpu(cpu, rp->priv->reg_unit, &msr_val)) {
Jacob Pan2d281d82013-10-17 10:28:35 -0700774 pr_err("Failed to read power unit MSR 0x%x on CPU %d, exit.\n",
Zhang Rui7fde2712019-07-10 21:44:26 +0800775 rp->priv->reg_unit, cpu);
Jacob Pan2d281d82013-10-17 10:28:35 -0700776 return -ENODEV;
777 }
778
Jacob Pan2d281d82013-10-17 10:28:35 -0700779 value = (msr_val & ENERGY_UNIT_MASK) >> ENERGY_UNIT_OFFSET;
Jacob Pand474a4d2015-03-13 03:48:56 -0700780 rp->energy_unit = ENERGY_UNIT_SCALE * 1000000 / (1 << value);
Jacob Pan2d281d82013-10-17 10:28:35 -0700781
782 value = (msr_val & POWER_UNIT_MASK) >> POWER_UNIT_OFFSET;
Jacob Pan3c2c0842014-11-07 09:29:26 -0800783 rp->power_unit = 1000000 / (1 << value);
Jacob Pan2d281d82013-10-17 10:28:35 -0700784
785 value = (msr_val & TIME_UNIT_MASK) >> TIME_UNIT_OFFSET;
Jacob Pan3c2c0842014-11-07 09:29:26 -0800786 rp->time_unit = 1000000 / (1 << value);
Jacob Pan2d281d82013-10-17 10:28:35 -0700787
Zhang Rui9ea76122019-05-13 13:58:53 -0400788 pr_debug("Core CPU %s energy=%dpJ, time=%dus, power=%duW\n",
789 rp->name, rp->energy_unit, rp->time_unit, rp->power_unit);
Jacob Pan2d281d82013-10-17 10:28:35 -0700790
791 return 0;
792}
793
Jacob Pan3c2c0842014-11-07 09:29:26 -0800794static int rapl_check_unit_atom(struct rapl_package *rp, int cpu)
795{
796 u64 msr_val;
797 u32 value;
798
Zhang Rui7fde2712019-07-10 21:44:26 +0800799 if (rdmsrl_safe_on_cpu(cpu, rp->priv->reg_unit, &msr_val)) {
Jacob Pan3c2c0842014-11-07 09:29:26 -0800800 pr_err("Failed to read power unit MSR 0x%x on CPU %d, exit.\n",
Zhang Rui7fde2712019-07-10 21:44:26 +0800801 rp->priv->reg_unit, cpu);
Jacob Pan3c2c0842014-11-07 09:29:26 -0800802 return -ENODEV;
803 }
804 value = (msr_val & ENERGY_UNIT_MASK) >> ENERGY_UNIT_OFFSET;
Jacob Pand474a4d2015-03-13 03:48:56 -0700805 rp->energy_unit = ENERGY_UNIT_SCALE * 1 << value;
Jacob Pan3c2c0842014-11-07 09:29:26 -0800806
807 value = (msr_val & POWER_UNIT_MASK) >> POWER_UNIT_OFFSET;
808 rp->power_unit = (1 << value) * 1000;
809
810 value = (msr_val & TIME_UNIT_MASK) >> TIME_UNIT_OFFSET;
811 rp->time_unit = 1000000 / (1 << value);
812
Zhang Rui9ea76122019-05-13 13:58:53 -0400813 pr_debug("Atom %s energy=%dpJ, time=%dus, power=%duW\n",
814 rp->name, rp->energy_unit, rp->time_unit, rp->power_unit);
Jacob Pan3c2c0842014-11-07 09:29:26 -0800815
816 return 0;
817}
818
Jacob Panf14a1392016-02-24 13:31:36 -0800819static void power_limit_irq_save_cpu(void *info)
820{
821 u32 l, h = 0;
822 struct rapl_package *rp = (struct rapl_package *)info;
823
824 /* save the state of PLN irq mask bit before disabling it */
825 rdmsr_safe(MSR_IA32_PACKAGE_THERM_INTERRUPT, &l, &h);
826 if (!(rp->power_limit_irq & PACKAGE_PLN_INT_SAVED)) {
827 rp->power_limit_irq = l & PACKAGE_THERM_INT_PLN_ENABLE;
828 rp->power_limit_irq |= PACKAGE_PLN_INT_SAVED;
829 }
830 l &= ~PACKAGE_THERM_INT_PLN_ENABLE;
831 wrmsr_safe(MSR_IA32_PACKAGE_THERM_INTERRUPT, l, h);
832}
833
Jacob Pan3c2c0842014-11-07 09:29:26 -0800834
Jacob Pan2d281d82013-10-17 10:28:35 -0700835/* REVISIT:
836 * When package power limit is set artificially low by RAPL, LVT
837 * thermal interrupt for package power limit should be ignored
838 * since we are not really exceeding the real limit. The intention
839 * is to avoid excessive interrupts while we are trying to save power.
840 * A useful feature might be routing the package_power_limit interrupt
841 * to userspace via eventfd. once we have a usecase, this is simple
842 * to do by adding an atomic notifier.
843 */
844
Jacob Pan309557f2016-02-24 13:31:37 -0800845static void package_power_limit_irq_save(struct rapl_package *rp)
Jacob Pan2d281d82013-10-17 10:28:35 -0700846{
Jacob Pan2d281d82013-10-17 10:28:35 -0700847 if (!boot_cpu_has(X86_FEATURE_PTS) || !boot_cpu_has(X86_FEATURE_PLN))
848 return;
849
Jacob Pan323ee642016-02-24 13:31:38 -0800850 smp_call_function_single(rp->lead_cpu, power_limit_irq_save_cpu, rp, 1);
Jacob Panf14a1392016-02-24 13:31:36 -0800851}
852
Thomas Gleixner58705062016-11-22 21:16:02 +0000853/*
854 * Restore per package power limit interrupt enable state. Called from cpu
855 * hotplug code on package removal.
856 */
857static void package_power_limit_irq_restore(struct rapl_package *rp)
Jacob Panf14a1392016-02-24 13:31:36 -0800858{
Thomas Gleixner58705062016-11-22 21:16:02 +0000859 u32 l, h;
860
861 if (!boot_cpu_has(X86_FEATURE_PTS) || !boot_cpu_has(X86_FEATURE_PLN))
862 return;
863
864 /* irq enable state not saved, nothing to restore */
865 if (!(rp->power_limit_irq & PACKAGE_PLN_INT_SAVED))
866 return;
Jacob Panf14a1392016-02-24 13:31:36 -0800867
868 rdmsr_safe(MSR_IA32_PACKAGE_THERM_INTERRUPT, &l, &h);
869
870 if (rp->power_limit_irq & PACKAGE_THERM_INT_PLN_ENABLE)
871 l |= PACKAGE_THERM_INT_PLN_ENABLE;
872 else
873 l &= ~PACKAGE_THERM_INT_PLN_ENABLE;
874
875 wrmsr_safe(MSR_IA32_PACKAGE_THERM_INTERRUPT, l, h);
Jacob Pan2d281d82013-10-17 10:28:35 -0700876}
877
Jacob Pan3c2c0842014-11-07 09:29:26 -0800878static void set_floor_freq_default(struct rapl_domain *rd, bool mode)
879{
880 int nr_powerlimit = find_nr_power_limit(rd);
881
882 /* always enable clamp such that p-state can go below OS requested
883 * range. power capping priority over guranteed frequency.
884 */
885 rapl_write_data_raw(rd, PL1_CLAMP, mode);
886
887 /* some domains have pl2 */
888 if (nr_powerlimit > 1) {
889 rapl_write_data_raw(rd, PL2_ENABLE, mode);
890 rapl_write_data_raw(rd, PL2_CLAMP, mode);
891 }
892}
893
894static void set_floor_freq_atom(struct rapl_domain *rd, bool enable)
895{
896 static u32 power_ctrl_orig_val;
897 u32 mdata;
898
Ajay Thomas51b63402015-04-30 01:43:23 +0530899 if (!rapl_defaults->floor_freq_reg_addr) {
900 pr_err("Invalid floor frequency config register\n");
901 return;
902 }
903
Jacob Pan3c2c0842014-11-07 09:29:26 -0800904 if (!power_ctrl_orig_val)
Andy Shevchenko4077a382015-11-11 19:59:29 +0200905 iosf_mbi_read(BT_MBI_UNIT_PMC, MBI_CR_READ,
906 rapl_defaults->floor_freq_reg_addr,
907 &power_ctrl_orig_val);
Jacob Pan3c2c0842014-11-07 09:29:26 -0800908 mdata = power_ctrl_orig_val;
909 if (enable) {
910 mdata &= ~(0x7f << 8);
911 mdata |= 1 << 8;
912 }
Andy Shevchenko4077a382015-11-11 19:59:29 +0200913 iosf_mbi_write(BT_MBI_UNIT_PMC, MBI_CR_WRITE,
914 rapl_defaults->floor_freq_reg_addr, mdata);
Jacob Pan3c2c0842014-11-07 09:29:26 -0800915}
916
917static u64 rapl_compute_time_window_core(struct rapl_package *rp, u64 value,
918 bool to_raw)
919{
920 u64 f, y; /* fraction and exp. used for time unit */
921
922 /*
923 * Special processing based on 2^Y*(1+F/4), refer
924 * to Intel Software Developer's manual Vol.3B: CH 14.9.3.
925 */
926 if (!to_raw) {
927 f = (value & 0x60) >> 5;
928 y = value & 0x1f;
929 value = (1 << y) * (4 + f) * rp->time_unit / 4;
930 } else {
931 do_div(value, rp->time_unit);
932 y = ilog2(value);
933 f = div64_u64(4 * (value - (1 << y)), 1 << y);
934 value = (y & 0x1f) | ((f & 0x3) << 5);
935 }
936 return value;
937}
938
939static u64 rapl_compute_time_window_atom(struct rapl_package *rp, u64 value,
940 bool to_raw)
941{
942 /*
943 * Atom time unit encoding is straight forward val * time_unit,
944 * where time_unit is default to 1 sec. Never 0.
945 */
946 if (!to_raw)
947 return (value) ? value *= rp->time_unit : rp->time_unit;
948 else
949 value = div64_u64(value, rp->time_unit);
950
951 return value;
952}
953
Jacob Pan087e9cb2014-11-07 09:29:25 -0800954static const struct rapl_defaults rapl_defaults_core = {
Ajay Thomas51b63402015-04-30 01:43:23 +0530955 .floor_freq_reg_addr = 0,
Jacob Pan3c2c0842014-11-07 09:29:26 -0800956 .check_unit = rapl_check_unit_core,
957 .set_floor_freq = set_floor_freq_default,
958 .compute_time_window = rapl_compute_time_window_core,
Jacob Pan087e9cb2014-11-07 09:29:25 -0800959};
960
Jacob Pand474a4d2015-03-13 03:48:56 -0700961static const struct rapl_defaults rapl_defaults_hsw_server = {
962 .check_unit = rapl_check_unit_core,
963 .set_floor_freq = set_floor_freq_default,
964 .compute_time_window = rapl_compute_time_window_core,
965 .dram_domain_energy_unit = 15300,
966};
967
Ajay Thomas51b63402015-04-30 01:43:23 +0530968static const struct rapl_defaults rapl_defaults_byt = {
969 .floor_freq_reg_addr = IOSF_CPU_POWER_BUDGET_CTL_BYT,
Jacob Pan3c2c0842014-11-07 09:29:26 -0800970 .check_unit = rapl_check_unit_atom,
971 .set_floor_freq = set_floor_freq_atom,
972 .compute_time_window = rapl_compute_time_window_atom,
Jacob Pan087e9cb2014-11-07 09:29:25 -0800973};
974
Ajay Thomas51b63402015-04-30 01:43:23 +0530975static const struct rapl_defaults rapl_defaults_tng = {
976 .floor_freq_reg_addr = IOSF_CPU_POWER_BUDGET_CTL_TNG,
977 .check_unit = rapl_check_unit_atom,
978 .set_floor_freq = set_floor_freq_atom,
979 .compute_time_window = rapl_compute_time_window_atom,
980};
981
982static const struct rapl_defaults rapl_defaults_ann = {
983 .floor_freq_reg_addr = 0,
984 .check_unit = rapl_check_unit_atom,
985 .set_floor_freq = NULL,
986 .compute_time_window = rapl_compute_time_window_atom,
987};
988
989static const struct rapl_defaults rapl_defaults_cht = {
990 .floor_freq_reg_addr = 0,
991 .check_unit = rapl_check_unit_atom,
992 .set_floor_freq = NULL,
993 .compute_time_window = rapl_compute_time_window_atom,
994};
995
Mathias Krauseea85dbc2015-03-25 22:15:52 +0100996static const struct x86_cpu_id rapl_ids[] __initconst = {
Andy Shevchenko17ed1512018-08-31 11:25:13 +0300997 INTEL_CPU_FAM6(SANDYBRIDGE, rapl_defaults_core),
998 INTEL_CPU_FAM6(SANDYBRIDGE_X, rapl_defaults_core),
Dave Hansen0bb04b52016-06-02 17:19:37 -0700999
Andy Shevchenko17ed1512018-08-31 11:25:13 +03001000 INTEL_CPU_FAM6(IVYBRIDGE, rapl_defaults_core),
1001 INTEL_CPU_FAM6(IVYBRIDGE_X, rapl_defaults_core),
Dave Hansen0bb04b52016-06-02 17:19:37 -07001002
Andy Shevchenko17ed1512018-08-31 11:25:13 +03001003 INTEL_CPU_FAM6(HASWELL_CORE, rapl_defaults_core),
1004 INTEL_CPU_FAM6(HASWELL_ULT, rapl_defaults_core),
1005 INTEL_CPU_FAM6(HASWELL_GT3E, rapl_defaults_core),
1006 INTEL_CPU_FAM6(HASWELL_X, rapl_defaults_hsw_server),
Dave Hansen0bb04b52016-06-02 17:19:37 -07001007
Andy Shevchenko17ed1512018-08-31 11:25:13 +03001008 INTEL_CPU_FAM6(BROADWELL_CORE, rapl_defaults_core),
1009 INTEL_CPU_FAM6(BROADWELL_GT3E, rapl_defaults_core),
1010 INTEL_CPU_FAM6(BROADWELL_XEON_D, rapl_defaults_core),
1011 INTEL_CPU_FAM6(BROADWELL_X, rapl_defaults_hsw_server),
Dave Hansen0bb04b52016-06-02 17:19:37 -07001012
Andy Shevchenko17ed1512018-08-31 11:25:13 +03001013 INTEL_CPU_FAM6(SKYLAKE_DESKTOP, rapl_defaults_core),
1014 INTEL_CPU_FAM6(SKYLAKE_MOBILE, rapl_defaults_core),
1015 INTEL_CPU_FAM6(SKYLAKE_X, rapl_defaults_hsw_server),
1016 INTEL_CPU_FAM6(KABYLAKE_MOBILE, rapl_defaults_core),
1017 INTEL_CPU_FAM6(KABYLAKE_DESKTOP, rapl_defaults_core),
1018 INTEL_CPU_FAM6(CANNONLAKE_MOBILE, rapl_defaults_core),
Gayatri Kammelaba6f3ec2019-02-18 15:01:02 +08001019 INTEL_CPU_FAM6(ICELAKE_MOBILE, rapl_defaults_core),
Dave Hansen0bb04b52016-06-02 17:19:37 -07001020
Linus Torvaldsc05f3642018-10-23 13:32:18 +01001021 INTEL_CPU_FAM6(ATOM_SILVERMONT, rapl_defaults_byt),
Andy Shevchenko17ed1512018-08-31 11:25:13 +03001022 INTEL_CPU_FAM6(ATOM_AIRMONT, rapl_defaults_cht),
Linus Torvaldsc05f3642018-10-23 13:32:18 +01001023 INTEL_CPU_FAM6(ATOM_SILVERMONT_MID, rapl_defaults_tng),
1024 INTEL_CPU_FAM6(ATOM_AIRMONT_MID, rapl_defaults_ann),
Andy Shevchenko17ed1512018-08-31 11:25:13 +03001025 INTEL_CPU_FAM6(ATOM_GOLDMONT, rapl_defaults_core),
Linus Torvaldsc05f3642018-10-23 13:32:18 +01001026 INTEL_CPU_FAM6(ATOM_GOLDMONT_PLUS, rapl_defaults_core),
1027 INTEL_CPU_FAM6(ATOM_GOLDMONT_X, rapl_defaults_core),
Zhang Ruidf7f8e02019-02-12 10:47:10 +08001028 INTEL_CPU_FAM6(ATOM_TREMONT_X, rapl_defaults_core),
Dave Hansen0bb04b52016-06-02 17:19:37 -07001029
Andy Shevchenko17ed1512018-08-31 11:25:13 +03001030 INTEL_CPU_FAM6(XEON_PHI_KNL, rapl_defaults_hsw_server),
1031 INTEL_CPU_FAM6(XEON_PHI_KNM, rapl_defaults_hsw_server),
Jacob Pan2d281d82013-10-17 10:28:35 -07001032 {}
1033};
1034MODULE_DEVICE_TABLE(x86cpu, rapl_ids);
1035
Thomas Gleixnerbed5ab62016-11-22 21:15:58 +00001036/* Read once for all raw primitive data for domains */
1037static void rapl_update_domain_data(struct rapl_package *rp)
Jacob Pan2d281d82013-10-17 10:28:35 -07001038{
1039 int dmn, prim;
1040 u64 val;
Jacob Pan2d281d82013-10-17 10:28:35 -07001041
Thomas Gleixnerbed5ab62016-11-22 21:15:58 +00001042 for (dmn = 0; dmn < rp->nr_domains; dmn++) {
Zhang Rui9ea76122019-05-13 13:58:53 -04001043 pr_debug("update %s domain %s data\n", rp->name,
Thomas Gleixnerbed5ab62016-11-22 21:15:58 +00001044 rp->domains[dmn].name);
1045 /* exclude non-raw primitives */
1046 for (prim = 0; prim < NR_RAW_PRIMITIVES; prim++) {
1047 if (!rapl_read_data_raw(&rp->domains[dmn], prim,
1048 rpi[prim].unit, &val))
1049 rp->domains[dmn].rdd.primitives[prim] = val;
Jacob Pan2d281d82013-10-17 10:28:35 -07001050 }
1051 }
1052
1053}
1054
Thomas Gleixner58705062016-11-22 21:16:02 +00001055static void rapl_unregister_powercap(void)
Jacob Pan2d281d82013-10-17 10:28:35 -07001056{
Zhang Rui7ebf8ef2019-07-10 21:44:25 +08001057 if (&rapl_msr_priv.platform_rapl_domain) {
1058 powercap_unregister_zone(rapl_msr_priv.control_type,
1059 &rapl_msr_priv.platform_rapl_domain->power_zone);
1060 kfree(rapl_msr_priv.platform_rapl_domain);
Srinivas Pandruvada3521ba12016-04-17 15:03:01 -07001061 }
Zhang Rui7ebf8ef2019-07-10 21:44:25 +08001062 powercap_unregister_control_type(rapl_msr_priv.control_type);
Jacob Pan2d281d82013-10-17 10:28:35 -07001063}
1064
1065static int rapl_package_register_powercap(struct rapl_package *rp)
1066{
1067 struct rapl_domain *rd;
Jacob Pan2d281d82013-10-17 10:28:35 -07001068 struct powercap_zone *power_zone = NULL;
Luis de Bethencourt01857cf2018-01-17 10:30:34 +00001069 int nr_pl, ret;
Thomas Gleixnerbed5ab62016-11-22 21:15:58 +00001070
1071 /* Update the domain data of the new package */
1072 rapl_update_domain_data(rp);
Jacob Pan2d281d82013-10-17 10:28:35 -07001073
1074 /* first we register package domain as the parent zone*/
1075 for (rd = rp->domains; rd < rp->domains + rp->nr_domains; rd++) {
1076 if (rd->id == RAPL_DOMAIN_PACKAGE) {
1077 nr_pl = find_nr_power_limit(rd);
Zhang Rui9ea76122019-05-13 13:58:53 -04001078 pr_debug("register package domain %s\n", rp->name);
Jacob Pan2d281d82013-10-17 10:28:35 -07001079 power_zone = powercap_register_zone(&rd->power_zone,
Zhang Rui7ebf8ef2019-07-10 21:44:25 +08001080 rp->priv->control_type,
Zhang Rui9ea76122019-05-13 13:58:53 -04001081 rp->name, NULL,
Jacob Pan2d281d82013-10-17 10:28:35 -07001082 &zone_ops[rd->id],
1083 nr_pl,
1084 &constraint_ops);
1085 if (IS_ERR(power_zone)) {
Zhang Rui9ea76122019-05-13 13:58:53 -04001086 pr_debug("failed to register power zone %s\n",
1087 rp->name);
Thomas Gleixnerbed5ab62016-11-22 21:15:58 +00001088 return PTR_ERR(power_zone);
Jacob Pan2d281d82013-10-17 10:28:35 -07001089 }
1090 /* track parent zone in per package/socket data */
1091 rp->power_zone = power_zone;
1092 /* done, only one package domain per socket */
1093 break;
1094 }
1095 }
1096 if (!power_zone) {
1097 pr_err("no package domain found, unknown topology!\n");
Thomas Gleixnerbed5ab62016-11-22 21:15:58 +00001098 return -ENODEV;
Jacob Pan2d281d82013-10-17 10:28:35 -07001099 }
1100 /* now register domains as children of the socket/package*/
1101 for (rd = rp->domains; rd < rp->domains + rp->nr_domains; rd++) {
1102 if (rd->id == RAPL_DOMAIN_PACKAGE)
1103 continue;
1104 /* number of power limits per domain varies */
1105 nr_pl = find_nr_power_limit(rd);
1106 power_zone = powercap_register_zone(&rd->power_zone,
Zhang Rui7ebf8ef2019-07-10 21:44:25 +08001107 rp->priv->control_type, rd->name,
Jacob Pan2d281d82013-10-17 10:28:35 -07001108 rp->power_zone,
1109 &zone_ops[rd->id], nr_pl,
1110 &constraint_ops);
1111
1112 if (IS_ERR(power_zone)) {
Zhang Rui9ea76122019-05-13 13:58:53 -04001113 pr_debug("failed to register power_zone, %s:%s\n",
1114 rp->name, rd->name);
Jacob Pan2d281d82013-10-17 10:28:35 -07001115 ret = PTR_ERR(power_zone);
1116 goto err_cleanup;
1117 }
1118 }
Thomas Gleixnerbed5ab62016-11-22 21:15:58 +00001119 return 0;
Jacob Pan2d281d82013-10-17 10:28:35 -07001120
Jacob Pan2d281d82013-10-17 10:28:35 -07001121err_cleanup:
Thomas Gleixner58705062016-11-22 21:16:02 +00001122 /*
1123 * Clean up previously initialized domains within the package if we
Jacob Pan2d281d82013-10-17 10:28:35 -07001124 * failed after the first domain setup.
1125 */
1126 while (--rd >= rp->domains) {
Zhang Rui9ea76122019-05-13 13:58:53 -04001127 pr_debug("unregister %s domain %s\n", rp->name, rd->name);
Zhang Rui7ebf8ef2019-07-10 21:44:25 +08001128 powercap_unregister_zone(rp->priv->control_type, &rd->power_zone);
Jacob Pan2d281d82013-10-17 10:28:35 -07001129 }
1130
1131 return ret;
1132}
1133
Thomas Gleixner58705062016-11-22 21:16:02 +00001134static int __init rapl_register_psys(void)
Srinivas Pandruvada3521ba12016-04-17 15:03:01 -07001135{
1136 struct rapl_domain *rd;
1137 struct powercap_zone *power_zone;
1138 u64 val;
1139
Zhang Rui7fde2712019-07-10 21:44:26 +08001140 if (rdmsrl_safe_on_cpu(0, rapl_msr_priv.regs[RAPL_DOMAIN_PLATFORM][RAPL_DOMAIN_REG_STATUS], &val) || !val)
Srinivas Pandruvada3521ba12016-04-17 15:03:01 -07001141 return -ENODEV;
1142
Zhang Rui7fde2712019-07-10 21:44:26 +08001143 if (rdmsrl_safe_on_cpu(0, rapl_msr_priv.regs[RAPL_DOMAIN_PLATFORM][RAPL_DOMAIN_REG_LIMIT], &val) || !val)
Srinivas Pandruvada3521ba12016-04-17 15:03:01 -07001144 return -ENODEV;
1145
1146 rd = kzalloc(sizeof(*rd), GFP_KERNEL);
1147 if (!rd)
1148 return -ENOMEM;
1149
1150 rd->name = rapl_domain_names[RAPL_DOMAIN_PLATFORM];
1151 rd->id = RAPL_DOMAIN_PLATFORM;
Zhang Rui7fde2712019-07-10 21:44:26 +08001152 rd->regs[RAPL_DOMAIN_REG_LIMIT] = rapl_msr_priv.regs[RAPL_DOMAIN_PLATFORM][RAPL_DOMAIN_REG_LIMIT];
1153 rd->regs[RAPL_DOMAIN_REG_STATUS] = rapl_msr_priv.regs[RAPL_DOMAIN_PLATFORM][RAPL_DOMAIN_REG_STATUS];
Srinivas Pandruvada3521ba12016-04-17 15:03:01 -07001154 rd->rpl[0].prim_id = PL1_ENABLE;
1155 rd->rpl[0].name = pl1_name;
1156 rd->rpl[1].prim_id = PL2_ENABLE;
1157 rd->rpl[1].name = pl2_name;
Zhang Rui7ebf8ef2019-07-10 21:44:25 +08001158 rd->rp = rapl_find_package_domain(0, &rapl_msr_priv);
Srinivas Pandruvada3521ba12016-04-17 15:03:01 -07001159
Zhang Rui7ebf8ef2019-07-10 21:44:25 +08001160 power_zone = powercap_register_zone(&rd->power_zone, rapl_msr_priv.control_type,
Srinivas Pandruvada3521ba12016-04-17 15:03:01 -07001161 "psys", NULL,
1162 &zone_ops[RAPL_DOMAIN_PLATFORM],
1163 2, &constraint_ops);
1164
1165 if (IS_ERR(power_zone)) {
1166 kfree(rd);
1167 return PTR_ERR(power_zone);
1168 }
1169
Zhang Rui7ebf8ef2019-07-10 21:44:25 +08001170 rapl_msr_priv.platform_rapl_domain = rd;
Srinivas Pandruvada3521ba12016-04-17 15:03:01 -07001171
1172 return 0;
1173}
1174
Thomas Gleixner58705062016-11-22 21:16:02 +00001175static int __init rapl_register_powercap(void)
Jacob Pan2d281d82013-10-17 10:28:35 -07001176{
Zhang Rui7ebf8ef2019-07-10 21:44:25 +08001177 rapl_msr_priv.control_type = powercap_register_control_type(NULL, "intel-rapl", NULL);
1178 if (IS_ERR(rapl_msr_priv.control_type)) {
Jacob Pan2d281d82013-10-17 10:28:35 -07001179 pr_debug("failed to register powercap control_type.\n");
Zhang Rui7ebf8ef2019-07-10 21:44:25 +08001180 return PTR_ERR(rapl_msr_priv.control_type);
Jacob Pan2d281d82013-10-17 10:28:35 -07001181 }
Thomas Gleixner58705062016-11-22 21:16:02 +00001182 return 0;
Jacob Pan2d281d82013-10-17 10:28:35 -07001183}
1184
Zhang Rui7fde2712019-07-10 21:44:26 +08001185static int rapl_check_domain(int cpu, int domain, struct rapl_package *rp)
Jacob Pan2d281d82013-10-17 10:28:35 -07001186{
Zhang Rui7fde2712019-07-10 21:44:26 +08001187 u32 reg;
Jacob Pan9d31c672014-04-29 15:33:06 -07001188 u64 val = 0;
Jacob Pan2d281d82013-10-17 10:28:35 -07001189
1190 switch (domain) {
1191 case RAPL_DOMAIN_PACKAGE:
Jacob Pan2d281d82013-10-17 10:28:35 -07001192 case RAPL_DOMAIN_PP0:
Jacob Pan2d281d82013-10-17 10:28:35 -07001193 case RAPL_DOMAIN_PP1:
Jacob Pan2d281d82013-10-17 10:28:35 -07001194 case RAPL_DOMAIN_DRAM:
Zhang Rui7fde2712019-07-10 21:44:26 +08001195 reg = rp->priv->regs[domain][RAPL_DOMAIN_REG_STATUS];
Jacob Pan2d281d82013-10-17 10:28:35 -07001196 break;
Srinivas Pandruvada3521ba12016-04-17 15:03:01 -07001197 case RAPL_DOMAIN_PLATFORM:
1198 /* PSYS(PLATFORM) is not a CPU domain, so avoid printng error */
1199 return -EINVAL;
Jacob Pan2d281d82013-10-17 10:28:35 -07001200 default:
1201 pr_err("invalid domain id %d\n", domain);
1202 return -EINVAL;
1203 }
Jacob Pan9d31c672014-04-29 15:33:06 -07001204 /* make sure domain counters are available and contains non-zero
1205 * values, otherwise skip it.
1206 */
Zhang Rui7fde2712019-07-10 21:44:26 +08001207 if (rdmsrl_safe_on_cpu(cpu, reg, &val) || !val)
Jacob Pan2d281d82013-10-17 10:28:35 -07001208 return -ENODEV;
1209
Jacob Pan9d31c672014-04-29 15:33:06 -07001210 return 0;
Jacob Pan2d281d82013-10-17 10:28:35 -07001211}
1212
Jacob Pane1399ba2016-05-31 13:41:29 -07001213
1214/*
1215 * Check if power limits are available. Two cases when they are not available:
1216 * 1. Locked by BIOS, in this case we still provide read-only access so that
1217 * users can see what limit is set by the BIOS.
1218 * 2. Some CPUs make some domains monitoring only which means PLx MSRs may not
1219 * exist at all. In this case, we do not show the contraints in powercap.
1220 *
1221 * Called after domains are detected and initialized.
1222 */
1223static void rapl_detect_powerlimit(struct rapl_domain *rd)
1224{
1225 u64 val64;
1226 int i;
1227
1228 /* check if the domain is locked by BIOS, ignore if MSR doesn't exist */
1229 if (!rapl_read_data_raw(rd, FW_LOCK, false, &val64)) {
1230 if (val64) {
Zhang Rui9ea76122019-05-13 13:58:53 -04001231 pr_info("RAPL %s domain %s locked by BIOS\n",
1232 rd->rp->name, rd->name);
Jacob Pane1399ba2016-05-31 13:41:29 -07001233 rd->state |= DOMAIN_STATE_BIOS_LOCKED;
1234 }
1235 }
1236 /* check if power limit MSRs exists, otherwise domain is monitoring only */
1237 for (i = 0; i < NR_POWER_LIMITS; i++) {
1238 int prim = rd->rpl[i].prim_id;
1239 if (rapl_read_data_raw(rd, prim, false, &val64))
1240 rd->rpl[i].name = NULL;
1241 }
1242}
1243
Jacob Pan2d281d82013-10-17 10:28:35 -07001244/* Detect active and valid domains for the given CPU, caller must
1245 * ensure the CPU belongs to the targeted package and CPU hotlug is disabled.
1246 */
1247static int rapl_detect_domains(struct rapl_package *rp, int cpu)
1248{
Jacob Pan2d281d82013-10-17 10:28:35 -07001249 struct rapl_domain *rd;
Thomas Gleixner58705062016-11-22 21:16:02 +00001250 int i;
Jacob Pan2d281d82013-10-17 10:28:35 -07001251
1252 for (i = 0; i < RAPL_DOMAIN_MAX; i++) {
1253 /* use physical package id to read counters */
Zhang Rui7fde2712019-07-10 21:44:26 +08001254 if (!rapl_check_domain(cpu, i, rp)) {
Jacob Pan2d281d82013-10-17 10:28:35 -07001255 rp->domain_map |= 1 << i;
Jacob Panfcdf1792014-09-02 02:55:21 -07001256 pr_info("Found RAPL domain %s\n", rapl_domain_names[i]);
1257 }
Jacob Pan2d281d82013-10-17 10:28:35 -07001258 }
1259 rp->nr_domains = bitmap_weight(&rp->domain_map, RAPL_DOMAIN_MAX);
1260 if (!rp->nr_domains) {
Zhang Rui9ea76122019-05-13 13:58:53 -04001261 pr_debug("no valid rapl domains found in %s\n", rp->name);
Thomas Gleixner58705062016-11-22 21:16:02 +00001262 return -ENODEV;
Jacob Pan2d281d82013-10-17 10:28:35 -07001263 }
Zhang Rui9ea76122019-05-13 13:58:53 -04001264 pr_debug("found %d domains on %s\n", rp->nr_domains, rp->name);
Jacob Pan2d281d82013-10-17 10:28:35 -07001265
1266 rp->domains = kcalloc(rp->nr_domains + 1, sizeof(struct rapl_domain),
1267 GFP_KERNEL);
Thomas Gleixner58705062016-11-22 21:16:02 +00001268 if (!rp->domains)
1269 return -ENOMEM;
1270
Jacob Pan2d281d82013-10-17 10:28:35 -07001271 rapl_init_domains(rp);
1272
Jacob Pane1399ba2016-05-31 13:41:29 -07001273 for (rd = rp->domains; rd < rp->domains + rp->nr_domains; rd++)
1274 rapl_detect_powerlimit(rd);
1275
Jacob Pan2d281d82013-10-17 10:28:35 -07001276 return 0;
1277}
1278
1279/* called from CPU hotplug notifier, hotplug lock held */
1280static void rapl_remove_package(struct rapl_package *rp)
1281{
1282 struct rapl_domain *rd, *rd_package = NULL;
1283
Thomas Gleixner58705062016-11-22 21:16:02 +00001284 package_power_limit_irq_restore(rp);
1285
Jacob Pan2d281d82013-10-17 10:28:35 -07001286 for (rd = rp->domains; rd < rp->domains + rp->nr_domains; rd++) {
Thomas Gleixner58705062016-11-22 21:16:02 +00001287 rapl_write_data_raw(rd, PL1_ENABLE, 0);
1288 rapl_write_data_raw(rd, PL1_CLAMP, 0);
1289 if (find_nr_power_limit(rd) > 1) {
1290 rapl_write_data_raw(rd, PL2_ENABLE, 0);
1291 rapl_write_data_raw(rd, PL2_CLAMP, 0);
1292 }
Jacob Pan2d281d82013-10-17 10:28:35 -07001293 if (rd->id == RAPL_DOMAIN_PACKAGE) {
1294 rd_package = rd;
1295 continue;
1296 }
Zhang Rui9ea76122019-05-13 13:58:53 -04001297 pr_debug("remove package, undo power limit on %s: %s\n",
1298 rp->name, rd->name);
Zhang Rui7ebf8ef2019-07-10 21:44:25 +08001299 powercap_unregister_zone(rp->priv->control_type, &rd->power_zone);
Jacob Pan2d281d82013-10-17 10:28:35 -07001300 }
1301 /* do parent zone last */
Zhang Rui7ebf8ef2019-07-10 21:44:25 +08001302 powercap_unregister_zone(rp->priv->control_type, &rd_package->power_zone);
Jacob Pan2d281d82013-10-17 10:28:35 -07001303 list_del(&rp->plist);
1304 kfree(rp);
1305}
1306
1307/* called from CPU hotplug notifier, hotplug lock held */
Zhang Rui7ebf8ef2019-07-10 21:44:25 +08001308static struct rapl_package *rapl_add_package(int cpu, struct rapl_if_priv *priv)
Jacob Pan2d281d82013-10-17 10:28:35 -07001309{
Zhang Rui32fb4802019-05-13 13:58:51 -04001310 int id = topology_logical_die_id(cpu);
Jacob Pan2d281d82013-10-17 10:28:35 -07001311 struct rapl_package *rp;
Zhang Rui9ea76122019-05-13 13:58:53 -04001312 struct cpuinfo_x86 *c = &cpu_data(cpu);
Thomas Gleixnerb4005e92016-11-22 21:16:05 +00001313 int ret;
Jacob Pan2d281d82013-10-17 10:28:35 -07001314
Jacob Pan2d281d82013-10-17 10:28:35 -07001315 rp = kzalloc(sizeof(struct rapl_package), GFP_KERNEL);
1316 if (!rp)
Thomas Gleixnerb4005e92016-11-22 21:16:05 +00001317 return ERR_PTR(-ENOMEM);
Jacob Pan2d281d82013-10-17 10:28:35 -07001318
1319 /* add the new package to the list */
Zhang Ruiaadf7b32019-05-13 13:58:50 -04001320 rp->id = id;
Jacob Pan323ee642016-02-24 13:31:38 -08001321 rp->lead_cpu = cpu;
Zhang Rui7ebf8ef2019-07-10 21:44:25 +08001322 rp->priv = priv;
Jacob Pan323ee642016-02-24 13:31:38 -08001323
Zhang Rui9ea76122019-05-13 13:58:53 -04001324 if (topology_max_die_per_package() > 1)
1325 snprintf(rp->name, PACKAGE_DOMAIN_NAME_LENGTH,
1326 "package-%d-die-%d", c->phys_proc_id, c->cpu_die_id);
1327 else
1328 snprintf(rp->name, PACKAGE_DOMAIN_NAME_LENGTH, "package-%d",
1329 c->phys_proc_id);
1330
Jacob Pan2d281d82013-10-17 10:28:35 -07001331 /* check if the package contains valid domains */
1332 if (rapl_detect_domains(rp, cpu) ||
Jacob Pan3c2c0842014-11-07 09:29:26 -08001333 rapl_defaults->check_unit(rp, cpu)) {
Jacob Pan2d281d82013-10-17 10:28:35 -07001334 ret = -ENODEV;
1335 goto err_free_package;
1336 }
Thomas Gleixnera74f4362016-11-22 21:15:59 +00001337 ret = rapl_package_register_powercap(rp);
1338 if (!ret) {
Jacob Pan2d281d82013-10-17 10:28:35 -07001339 INIT_LIST_HEAD(&rp->plist);
1340 list_add(&rp->plist, &rapl_packages);
Thomas Gleixnerb4005e92016-11-22 21:16:05 +00001341 return rp;
Jacob Pan2d281d82013-10-17 10:28:35 -07001342 }
1343
1344err_free_package:
1345 kfree(rp->domains);
1346 kfree(rp);
Thomas Gleixnerb4005e92016-11-22 21:16:05 +00001347 return ERR_PTR(ret);
Jacob Pan2d281d82013-10-17 10:28:35 -07001348}
1349
1350/* Handles CPU hotplug on multi-socket systems.
1351 * If a CPU goes online as the first CPU of the physical package
1352 * we add the RAPL package to the system. Similarly, when the last
1353 * CPU of the package is removed, we remove the RAPL package and its
1354 * associated domains. Cooling devices are handled accordingly at
1355 * per-domain level.
1356 */
Sebastian Andrzej Siewior5e4dc792016-11-22 21:16:00 +00001357static int rapl_cpu_online(unsigned int cpu)
Jacob Pan2d281d82013-10-17 10:28:35 -07001358{
Sebastian Andrzej Siewior5e4dc792016-11-22 21:16:00 +00001359 struct rapl_package *rp;
Sebastian Andrzej Siewior5e4dc792016-11-22 21:16:00 +00001360
Zhang Rui7ebf8ef2019-07-10 21:44:25 +08001361 rp = rapl_find_package_domain(cpu, &rapl_msr_priv);
Thomas Gleixnerb4005e92016-11-22 21:16:05 +00001362 if (!rp) {
Zhang Rui7ebf8ef2019-07-10 21:44:25 +08001363 rp = rapl_add_package(cpu, &rapl_msr_priv);
Thomas Gleixnerb4005e92016-11-22 21:16:05 +00001364 if (IS_ERR(rp))
1365 return PTR_ERR(rp);
Thomas Gleixner58705062016-11-22 21:16:02 +00001366 }
Thomas Gleixnerb4005e92016-11-22 21:16:05 +00001367 cpumask_set_cpu(cpu, &rp->cpumask);
1368 return 0;
Sebastian Andrzej Siewior5e4dc792016-11-22 21:16:00 +00001369}
1370
1371static int rapl_cpu_down_prep(unsigned int cpu)
1372{
Jacob Pan2d281d82013-10-17 10:28:35 -07001373 struct rapl_package *rp;
Jacob Pan323ee642016-02-24 13:31:38 -08001374 int lead_cpu;
Jacob Pan2d281d82013-10-17 10:28:35 -07001375
Zhang Rui7ebf8ef2019-07-10 21:44:25 +08001376 rp = rapl_find_package_domain(cpu, &rapl_msr_priv);
Sebastian Andrzej Siewior5e4dc792016-11-22 21:16:00 +00001377 if (!rp)
1378 return 0;
Thomas Gleixnerb4005e92016-11-22 21:16:05 +00001379
1380 cpumask_clear_cpu(cpu, &rp->cpumask);
1381 lead_cpu = cpumask_first(&rp->cpumask);
1382 if (lead_cpu >= nr_cpu_ids)
Sebastian Andrzej Siewior5e4dc792016-11-22 21:16:00 +00001383 rapl_remove_package(rp);
Thomas Gleixnerb4005e92016-11-22 21:16:05 +00001384 else if (rp->lead_cpu == cpu)
1385 rp->lead_cpu = lead_cpu;
Sebastian Andrzej Siewior5e4dc792016-11-22 21:16:00 +00001386 return 0;
Jacob Pan2d281d82013-10-17 10:28:35 -07001387}
1388
Zhen Han52b36722018-01-10 08:38:23 +08001389static void power_limit_state_save(void)
1390{
1391 struct rapl_package *rp;
1392 struct rapl_domain *rd;
1393 int nr_pl, ret, i;
1394
1395 get_online_cpus();
1396 list_for_each_entry(rp, &rapl_packages, plist) {
1397 if (!rp->power_zone)
1398 continue;
1399 rd = power_zone_to_rapl_domain(rp->power_zone);
1400 nr_pl = find_nr_power_limit(rd);
1401 for (i = 0; i < nr_pl; i++) {
1402 switch (rd->rpl[i].prim_id) {
1403 case PL1_ENABLE:
1404 ret = rapl_read_data_raw(rd,
1405 POWER_LIMIT1,
1406 true,
1407 &rd->rpl[i].last_power_limit);
1408 if (ret)
1409 rd->rpl[i].last_power_limit = 0;
1410 break;
1411 case PL2_ENABLE:
1412 ret = rapl_read_data_raw(rd,
1413 POWER_LIMIT2,
1414 true,
1415 &rd->rpl[i].last_power_limit);
1416 if (ret)
1417 rd->rpl[i].last_power_limit = 0;
1418 break;
1419 }
1420 }
1421 }
1422 put_online_cpus();
1423}
1424
1425static void power_limit_state_restore(void)
1426{
1427 struct rapl_package *rp;
1428 struct rapl_domain *rd;
1429 int nr_pl, i;
1430
1431 get_online_cpus();
1432 list_for_each_entry(rp, &rapl_packages, plist) {
1433 if (!rp->power_zone)
1434 continue;
1435 rd = power_zone_to_rapl_domain(rp->power_zone);
1436 nr_pl = find_nr_power_limit(rd);
1437 for (i = 0; i < nr_pl; i++) {
1438 switch (rd->rpl[i].prim_id) {
1439 case PL1_ENABLE:
1440 if (rd->rpl[i].last_power_limit)
1441 rapl_write_data_raw(rd,
1442 POWER_LIMIT1,
1443 rd->rpl[i].last_power_limit);
1444 break;
1445 case PL2_ENABLE:
1446 if (rd->rpl[i].last_power_limit)
1447 rapl_write_data_raw(rd,
1448 POWER_LIMIT2,
1449 rd->rpl[i].last_power_limit);
1450 break;
1451 }
1452 }
1453 }
1454 put_online_cpus();
1455}
1456
1457static int rapl_pm_callback(struct notifier_block *nb,
1458 unsigned long mode, void *_unused)
1459{
1460 switch (mode) {
1461 case PM_SUSPEND_PREPARE:
1462 power_limit_state_save();
1463 break;
1464 case PM_POST_SUSPEND:
1465 power_limit_state_restore();
1466 break;
1467 }
1468 return NOTIFY_OK;
1469}
1470
1471static struct notifier_block rapl_pm_notifier = {
1472 .notifier_call = rapl_pm_callback,
1473};
1474
Zhang Ruibeea8df2019-07-10 21:44:27 +08001475static int rapl_msr_read_raw(int cpu, struct reg_action *ra)
1476{
1477 if (rdmsrl_safe_on_cpu(cpu, ra->reg, &ra->value)) {
1478 pr_debug("failed to read msr 0x%x on cpu %d\n", ra->reg, cpu);
1479 return -EIO;
1480 }
1481 ra->value &= ra->mask;
1482 return 0;
1483}
1484
1485static void rapl_msr_update_func(void *info)
1486{
1487 struct reg_action *ra = info;
1488 u64 val;
1489
1490 ra->err = rdmsrl_safe(ra->reg, &val);
1491 if (ra->err)
1492 return;
1493
1494 val &= ~ra->mask;
1495 val |= ra->value;
1496
1497 ra->err = wrmsrl_safe(ra->reg, val);
1498}
1499
1500
1501static int rapl_msr_write_raw(int cpu, struct reg_action *ra)
1502{
1503 int ret;
1504
1505 ret = smp_call_function_single(cpu, rapl_msr_update_func, ra, 1);
1506 if (WARN_ON_ONCE(ret))
1507 return ret;
1508
1509 return ra->err;
1510}
1511
Jacob Pan2d281d82013-10-17 10:28:35 -07001512static int __init rapl_init(void)
1513{
Jacob Pan087e9cb2014-11-07 09:29:25 -08001514 const struct x86_cpu_id *id;
Thomas Gleixner58705062016-11-22 21:16:02 +00001515 int ret;
Jacob Pan2d281d82013-10-17 10:28:35 -07001516
Jacob Pan087e9cb2014-11-07 09:29:25 -08001517 id = x86_match_cpu(rapl_ids);
1518 if (!id) {
Jacob Pan2d281d82013-10-17 10:28:35 -07001519 pr_err("driver does not support CPU family %d model %d\n",
1520 boot_cpu_data.x86, boot_cpu_data.x86_model);
1521
1522 return -ENODEV;
1523 }
Srivatsa S. Bhat009f2252014-03-11 02:09:26 +05301524
Jacob Pan087e9cb2014-11-07 09:29:25 -08001525 rapl_defaults = (struct rapl_defaults *)id->driver_data;
1526
Zhang Ruibeea8df2019-07-10 21:44:27 +08001527 rapl_msr_priv.read_raw = rapl_msr_read_raw;
1528 rapl_msr_priv.write_raw = rapl_msr_write_raw;
Thomas Gleixner58705062016-11-22 21:16:02 +00001529 ret = rapl_register_powercap();
Jacob Pan2d281d82013-10-17 10:28:35 -07001530 if (ret)
Thomas Gleixner58705062016-11-22 21:16:02 +00001531 return ret;
Jacob Pan2d281d82013-10-17 10:28:35 -07001532
Thomas Gleixner58705062016-11-22 21:16:02 +00001533 ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "powercap/rapl:online",
1534 rapl_cpu_online, rapl_cpu_down_prep);
Sebastian Andrzej Siewior5e4dc792016-11-22 21:16:00 +00001535 if (ret < 0)
1536 goto err_unreg;
Zhang Rui7ebf8ef2019-07-10 21:44:25 +08001537 rapl_msr_priv.pcap_rapl_online = ret;
Thomas Gleixner58705062016-11-22 21:16:02 +00001538
1539 /* Don't bail out if PSys is not supported */
1540 rapl_register_psys();
Zhen Han52b36722018-01-10 08:38:23 +08001541
1542 ret = register_pm_notifier(&rapl_pm_notifier);
1543 if (ret)
1544 goto err_unreg_all;
1545
Sebastian Andrzej Siewior5e4dc792016-11-22 21:16:00 +00001546 return 0;
Jacob Pan2d281d82013-10-17 10:28:35 -07001547
Zhen Han52b36722018-01-10 08:38:23 +08001548err_unreg_all:
Zhang Rui7ebf8ef2019-07-10 21:44:25 +08001549 cpuhp_remove_state(rapl_msr_priv.pcap_rapl_online);
Zhen Han52b36722018-01-10 08:38:23 +08001550
Sebastian Andrzej Siewior5e4dc792016-11-22 21:16:00 +00001551err_unreg:
1552 rapl_unregister_powercap();
Jacob Pan2d281d82013-10-17 10:28:35 -07001553 return ret;
1554}
1555
1556static void __exit rapl_exit(void)
1557{
Zhen Han52b36722018-01-10 08:38:23 +08001558 unregister_pm_notifier(&rapl_pm_notifier);
Zhang Rui7ebf8ef2019-07-10 21:44:25 +08001559 cpuhp_remove_state(rapl_msr_priv.pcap_rapl_online);
Jacob Pan2d281d82013-10-17 10:28:35 -07001560 rapl_unregister_powercap();
Jacob Pan2d281d82013-10-17 10:28:35 -07001561}
1562
1563module_init(rapl_init);
1564module_exit(rapl_exit);
1565
1566MODULE_DESCRIPTION("Driver for Intel RAPL (Running Average Power Limit)");
1567MODULE_AUTHOR("Jacob Pan <jacob.jun.pan@intel.com>");
1568MODULE_LICENSE("GPL v2");