blob: 3c3c0c23180b1d96cfe95299d895bcb5811651af [file] [log] [blame]
Jacob Pan2d281d82013-10-17 10:28:35 -07001/*
2 * Intel Running Average Power Limit (RAPL) Driver
3 * Copyright (c) 2013, Intel Corporation.
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along with
15 * this program; if not, write to the Free Software Foundation, Inc.
16 *
17 */
18#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
19
20#include <linux/kernel.h>
21#include <linux/module.h>
22#include <linux/list.h>
23#include <linux/types.h>
24#include <linux/device.h>
25#include <linux/slab.h>
26#include <linux/log2.h>
27#include <linux/bitmap.h>
28#include <linux/delay.h>
29#include <linux/sysfs.h>
30#include <linux/cpu.h>
31#include <linux/powercap.h>
Zhen Han52b36722018-01-10 08:38:23 +080032#include <linux/suspend.h>
Jacob Pan3c2c0842014-11-07 09:29:26 -080033#include <asm/iosf_mbi.h>
Jacob Pan2d281d82013-10-17 10:28:35 -070034
35#include <asm/processor.h>
36#include <asm/cpu_device_id.h>
Dave Hansen62d16732016-06-02 17:19:36 -070037#include <asm/intel-family.h>
Jacob Pan2d281d82013-10-17 10:28:35 -070038
Srinivas Pandruvada3521ba12016-04-17 15:03:01 -070039/* Local defines */
40#define MSR_PLATFORM_POWER_LIMIT 0x0000065C
41
Jacob Pan2d281d82013-10-17 10:28:35 -070042/* bitmasks for RAPL MSRs, used by primitive access functions */
43#define ENERGY_STATUS_MASK 0xffffffff
44
45#define POWER_LIMIT1_MASK 0x7FFF
46#define POWER_LIMIT1_ENABLE BIT(15)
47#define POWER_LIMIT1_CLAMP BIT(16)
48
49#define POWER_LIMIT2_MASK (0x7FFFULL<<32)
50#define POWER_LIMIT2_ENABLE BIT_ULL(47)
51#define POWER_LIMIT2_CLAMP BIT_ULL(48)
52#define POWER_PACKAGE_LOCK BIT_ULL(63)
53#define POWER_PP_LOCK BIT(31)
54
55#define TIME_WINDOW1_MASK (0x7FULL<<17)
56#define TIME_WINDOW2_MASK (0x7FULL<<49)
57
58#define POWER_UNIT_OFFSET 0
59#define POWER_UNIT_MASK 0x0F
60
61#define ENERGY_UNIT_OFFSET 0x08
62#define ENERGY_UNIT_MASK 0x1F00
63
64#define TIME_UNIT_OFFSET 0x10
65#define TIME_UNIT_MASK 0xF0000
66
67#define POWER_INFO_MAX_MASK (0x7fffULL<<32)
68#define POWER_INFO_MIN_MASK (0x7fffULL<<16)
69#define POWER_INFO_MAX_TIME_WIN_MASK (0x3fULL<<48)
70#define POWER_INFO_THERMAL_SPEC_MASK 0x7fff
71
72#define PERF_STATUS_THROTTLE_TIME_MASK 0xffffffff
73#define PP_POLICY_MASK 0x1F
74
75/* Non HW constants */
76#define RAPL_PRIMITIVE_DERIVED BIT(1) /* not from raw data */
77#define RAPL_PRIMITIVE_DUMMY BIT(2)
78
Jacob Pan2d281d82013-10-17 10:28:35 -070079#define TIME_WINDOW_MAX_MSEC 40000
80#define TIME_WINDOW_MIN_MSEC 250
Jacob Pand474a4d2015-03-13 03:48:56 -070081#define ENERGY_UNIT_SCALE 1000 /* scale from driver unit to powercap unit */
Jacob Pan2d281d82013-10-17 10:28:35 -070082enum unit_type {
83 ARBITRARY_UNIT, /* no translation */
84 POWER_UNIT,
85 ENERGY_UNIT,
86 TIME_UNIT,
87};
88
89enum rapl_domain_type {
90 RAPL_DOMAIN_PACKAGE, /* entire package/socket */
91 RAPL_DOMAIN_PP0, /* core power plane */
92 RAPL_DOMAIN_PP1, /* graphics uncore */
93 RAPL_DOMAIN_DRAM,/* DRAM control_type */
Srinivas Pandruvada3521ba12016-04-17 15:03:01 -070094 RAPL_DOMAIN_PLATFORM, /* PSys control_type */
Jacob Pan2d281d82013-10-17 10:28:35 -070095 RAPL_DOMAIN_MAX,
96};
97
98enum rapl_domain_msr_id {
99 RAPL_DOMAIN_MSR_LIMIT,
100 RAPL_DOMAIN_MSR_STATUS,
101 RAPL_DOMAIN_MSR_PERF,
102 RAPL_DOMAIN_MSR_POLICY,
103 RAPL_DOMAIN_MSR_INFO,
104 RAPL_DOMAIN_MSR_MAX,
105};
106
107/* per domain data, some are optional */
108enum rapl_primitives {
109 ENERGY_COUNTER,
110 POWER_LIMIT1,
111 POWER_LIMIT2,
112 FW_LOCK,
113
114 PL1_ENABLE, /* power limit 1, aka long term */
115 PL1_CLAMP, /* allow frequency to go below OS request */
116 PL2_ENABLE, /* power limit 2, aka short term, instantaneous */
117 PL2_CLAMP,
118
119 TIME_WINDOW1, /* long term */
120 TIME_WINDOW2, /* short term */
121 THERMAL_SPEC_POWER,
122 MAX_POWER,
123
124 MIN_POWER,
125 MAX_TIME_WINDOW,
126 THROTTLED_TIME,
127 PRIORITY_LEVEL,
128
129 /* below are not raw primitive data */
130 AVERAGE_POWER,
131 NR_RAPL_PRIMITIVES,
132};
133
134#define NR_RAW_PRIMITIVES (NR_RAPL_PRIMITIVES - 2)
135
136/* Can be expanded to include events, etc.*/
137struct rapl_domain_data {
138 u64 primitives[NR_RAPL_PRIMITIVES];
139 unsigned long timestamp;
140};
141
Jacob Panf14a1392016-02-24 13:31:36 -0800142struct msrl_action {
143 u32 msr_no;
144 u64 clear_mask;
145 u64 set_mask;
146 int err;
147};
Jacob Pan2d281d82013-10-17 10:28:35 -0700148
149#define DOMAIN_STATE_INACTIVE BIT(0)
150#define DOMAIN_STATE_POWER_LIMIT_SET BIT(1)
151#define DOMAIN_STATE_BIOS_LOCKED BIT(2)
152
153#define NR_POWER_LIMITS (2)
154struct rapl_power_limit {
155 struct powercap_zone_constraint *constraint;
156 int prim_id; /* primitive ID used to enable */
157 struct rapl_domain *domain;
158 const char *name;
Zhen Han52b36722018-01-10 08:38:23 +0800159 u64 last_power_limit;
Jacob Pan2d281d82013-10-17 10:28:35 -0700160};
161
162static const char pl1_name[] = "long_term";
163static const char pl2_name[] = "short_term";
164
Jacob Pan309557f2016-02-24 13:31:37 -0800165struct rapl_package;
Jacob Pan2d281d82013-10-17 10:28:35 -0700166struct rapl_domain {
167 const char *name;
168 enum rapl_domain_type id;
169 int msrs[RAPL_DOMAIN_MSR_MAX];
170 struct powercap_zone power_zone;
171 struct rapl_domain_data rdd;
172 struct rapl_power_limit rpl[NR_POWER_LIMITS];
173 u64 attr_map; /* track capabilities */
174 unsigned int state;
Jacob Pand474a4d2015-03-13 03:48:56 -0700175 unsigned int domain_energy_unit;
Jacob Pan309557f2016-02-24 13:31:37 -0800176 struct rapl_package *rp;
Jacob Pan2d281d82013-10-17 10:28:35 -0700177};
178#define power_zone_to_rapl_domain(_zone) \
179 container_of(_zone, struct rapl_domain, power_zone)
180
181
182/* Each physical package contains multiple domains, these are the common
183 * data across RAPL domains within a package.
184 */
185struct rapl_package {
186 unsigned int id; /* physical package/socket id */
187 unsigned int nr_domains;
188 unsigned long domain_map; /* bit map of active domains */
Jacob Pan3c2c0842014-11-07 09:29:26 -0800189 unsigned int power_unit;
190 unsigned int energy_unit;
191 unsigned int time_unit;
Jacob Pan2d281d82013-10-17 10:28:35 -0700192 struct rapl_domain *domains; /* array of domains, sized at runtime */
193 struct powercap_zone *power_zone; /* keep track of parent zone */
Jacob Pan2d281d82013-10-17 10:28:35 -0700194 unsigned long power_limit_irq; /* keep track of package power limit
195 * notify interrupt enable status.
196 */
197 struct list_head plist;
Jacob Pan323ee642016-02-24 13:31:38 -0800198 int lead_cpu; /* one active cpu per package for access */
Thomas Gleixnerb4005e92016-11-22 21:16:05 +0000199 /* Track active cpus */
200 struct cpumask cpumask;
Jacob Pan2d281d82013-10-17 10:28:35 -0700201};
Jacob Pan087e9cb2014-11-07 09:29:25 -0800202
203struct rapl_defaults {
Ajay Thomas51b63402015-04-30 01:43:23 +0530204 u8 floor_freq_reg_addr;
Jacob Pan087e9cb2014-11-07 09:29:25 -0800205 int (*check_unit)(struct rapl_package *rp, int cpu);
206 void (*set_floor_freq)(struct rapl_domain *rd, bool mode);
207 u64 (*compute_time_window)(struct rapl_package *rp, u64 val,
208 bool to_raw);
Jacob Pand474a4d2015-03-13 03:48:56 -0700209 unsigned int dram_domain_energy_unit;
Jacob Pan087e9cb2014-11-07 09:29:25 -0800210};
211static struct rapl_defaults *rapl_defaults;
212
Jacob Pan3c2c0842014-11-07 09:29:26 -0800213/* Sideband MBI registers */
Ajay Thomas51b63402015-04-30 01:43:23 +0530214#define IOSF_CPU_POWER_BUDGET_CTL_BYT (0x2)
215#define IOSF_CPU_POWER_BUDGET_CTL_TNG (0xdf)
Jacob Pan3c2c0842014-11-07 09:29:26 -0800216
Jacob Pan2d281d82013-10-17 10:28:35 -0700217#define PACKAGE_PLN_INT_SAVED BIT(0)
218#define MAX_PRIM_NAME (32)
219
220/* per domain data. used to describe individual knobs such that access function
221 * can be consolidated into one instead of many inline functions.
222 */
223struct rapl_primitive_info {
224 const char *name;
225 u64 mask;
226 int shift;
227 enum rapl_domain_msr_id id;
228 enum unit_type unit;
229 u32 flag;
230};
231
232#define PRIMITIVE_INFO_INIT(p, m, s, i, u, f) { \
233 .name = #p, \
234 .mask = m, \
235 .shift = s, \
236 .id = i, \
237 .unit = u, \
238 .flag = f \
239 }
240
241static void rapl_init_domains(struct rapl_package *rp);
242static int rapl_read_data_raw(struct rapl_domain *rd,
243 enum rapl_primitives prim,
244 bool xlate, u64 *data);
245static int rapl_write_data_raw(struct rapl_domain *rd,
246 enum rapl_primitives prim,
247 unsigned long long value);
Jacob Pan309557f2016-02-24 13:31:37 -0800248static u64 rapl_unit_xlate(struct rapl_domain *rd,
Jacob Pand474a4d2015-03-13 03:48:56 -0700249 enum unit_type type, u64 value,
Jacob Pan2d281d82013-10-17 10:28:35 -0700250 int to_raw);
Jacob Pan309557f2016-02-24 13:31:37 -0800251static void package_power_limit_irq_save(struct rapl_package *rp);
Jacob Pan2d281d82013-10-17 10:28:35 -0700252
253static LIST_HEAD(rapl_packages); /* guarded by CPU hotplug lock */
254
255static const char * const rapl_domain_names[] = {
256 "package",
257 "core",
258 "uncore",
259 "dram",
Srinivas Pandruvada3521ba12016-04-17 15:03:01 -0700260 "psys",
Jacob Pan2d281d82013-10-17 10:28:35 -0700261};
262
263static struct powercap_control_type *control_type; /* PowerCap Controller */
Srinivas Pandruvada3521ba12016-04-17 15:03:01 -0700264static struct rapl_domain *platform_rapl_domain; /* Platform (PSys) domain */
Jacob Pan2d281d82013-10-17 10:28:35 -0700265
266/* caller to ensure CPU hotplug lock is held */
Zhang Ruiaadf7b32019-05-13 13:58:50 -0400267static struct rapl_package *rapl_find_package_domain(int cpu)
Jacob Pan2d281d82013-10-17 10:28:35 -0700268{
Zhang Ruiaadf7b32019-05-13 13:58:50 -0400269 int id = topology_physical_package_id(cpu);
Jacob Pan2d281d82013-10-17 10:28:35 -0700270 struct rapl_package *rp;
271
272 list_for_each_entry(rp, &rapl_packages, plist) {
273 if (rp->id == id)
274 return rp;
275 }
276
277 return NULL;
278}
279
Jacob Pan2d281d82013-10-17 10:28:35 -0700280static int get_energy_counter(struct powercap_zone *power_zone, u64 *energy_raw)
281{
282 struct rapl_domain *rd;
283 u64 energy_now;
284
285 /* prevent CPU hotplug, make sure the RAPL domain does not go
286 * away while reading the counter.
287 */
288 get_online_cpus();
289 rd = power_zone_to_rapl_domain(power_zone);
290
291 if (!rapl_read_data_raw(rd, ENERGY_COUNTER, true, &energy_now)) {
292 *energy_raw = energy_now;
293 put_online_cpus();
294
295 return 0;
296 }
297 put_online_cpus();
298
299 return -EIO;
300}
301
302static int get_max_energy_counter(struct powercap_zone *pcd_dev, u64 *energy)
303{
Jacob Pand474a4d2015-03-13 03:48:56 -0700304 struct rapl_domain *rd = power_zone_to_rapl_domain(pcd_dev);
305
Jacob Pan309557f2016-02-24 13:31:37 -0800306 *energy = rapl_unit_xlate(rd, ENERGY_UNIT, ENERGY_STATUS_MASK, 0);
Jacob Pan2d281d82013-10-17 10:28:35 -0700307 return 0;
308}
309
310static int release_zone(struct powercap_zone *power_zone)
311{
312 struct rapl_domain *rd = power_zone_to_rapl_domain(power_zone);
Jacob Pan309557f2016-02-24 13:31:37 -0800313 struct rapl_package *rp = rd->rp;
Jacob Pan2d281d82013-10-17 10:28:35 -0700314
315 /* package zone is the last zone of a package, we can free
316 * memory here since all children has been unregistered.
317 */
318 if (rd->id == RAPL_DOMAIN_PACKAGE) {
Jacob Pan2d281d82013-10-17 10:28:35 -0700319 kfree(rd);
320 rp->domains = NULL;
321 }
322
323 return 0;
324
325}
326
327static int find_nr_power_limit(struct rapl_domain *rd)
328{
Jacob Pane1399ba2016-05-31 13:41:29 -0700329 int i, nr_pl = 0;
Jacob Pan2d281d82013-10-17 10:28:35 -0700330
331 for (i = 0; i < NR_POWER_LIMITS; i++) {
Jacob Pane1399ba2016-05-31 13:41:29 -0700332 if (rd->rpl[i].name)
333 nr_pl++;
Jacob Pan2d281d82013-10-17 10:28:35 -0700334 }
335
Jacob Pane1399ba2016-05-31 13:41:29 -0700336 return nr_pl;
Jacob Pan2d281d82013-10-17 10:28:35 -0700337}
338
339static int set_domain_enable(struct powercap_zone *power_zone, bool mode)
340{
341 struct rapl_domain *rd = power_zone_to_rapl_domain(power_zone);
Jacob Pan2d281d82013-10-17 10:28:35 -0700342
343 if (rd->state & DOMAIN_STATE_BIOS_LOCKED)
344 return -EACCES;
Jacob Pan3c2c0842014-11-07 09:29:26 -0800345
Jacob Pan2d281d82013-10-17 10:28:35 -0700346 get_online_cpus();
Jacob Pan2d281d82013-10-17 10:28:35 -0700347 rapl_write_data_raw(rd, PL1_ENABLE, mode);
Ajay Thomas51b63402015-04-30 01:43:23 +0530348 if (rapl_defaults->set_floor_freq)
349 rapl_defaults->set_floor_freq(rd, mode);
Jacob Pan2d281d82013-10-17 10:28:35 -0700350 put_online_cpus();
351
352 return 0;
353}
354
355static int get_domain_enable(struct powercap_zone *power_zone, bool *mode)
356{
357 struct rapl_domain *rd = power_zone_to_rapl_domain(power_zone);
358 u64 val;
359
360 if (rd->state & DOMAIN_STATE_BIOS_LOCKED) {
361 *mode = false;
362 return 0;
363 }
364 get_online_cpus();
365 if (rapl_read_data_raw(rd, PL1_ENABLE, true, &val)) {
366 put_online_cpus();
367 return -EIO;
368 }
369 *mode = val;
370 put_online_cpus();
371
372 return 0;
373}
374
375/* per RAPL domain ops, in the order of rapl_domain_type */
Julia Lawall600c3952015-12-23 22:59:55 +0100376static const struct powercap_zone_ops zone_ops[] = {
Jacob Pan2d281d82013-10-17 10:28:35 -0700377 /* RAPL_DOMAIN_PACKAGE */
378 {
379 .get_energy_uj = get_energy_counter,
380 .get_max_energy_range_uj = get_max_energy_counter,
381 .release = release_zone,
382 .set_enable = set_domain_enable,
383 .get_enable = get_domain_enable,
384 },
385 /* RAPL_DOMAIN_PP0 */
386 {
387 .get_energy_uj = get_energy_counter,
388 .get_max_energy_range_uj = get_max_energy_counter,
389 .release = release_zone,
390 .set_enable = set_domain_enable,
391 .get_enable = get_domain_enable,
392 },
393 /* RAPL_DOMAIN_PP1 */
394 {
395 .get_energy_uj = get_energy_counter,
396 .get_max_energy_range_uj = get_max_energy_counter,
397 .release = release_zone,
398 .set_enable = set_domain_enable,
399 .get_enable = get_domain_enable,
400 },
401 /* RAPL_DOMAIN_DRAM */
402 {
403 .get_energy_uj = get_energy_counter,
404 .get_max_energy_range_uj = get_max_energy_counter,
405 .release = release_zone,
406 .set_enable = set_domain_enable,
407 .get_enable = get_domain_enable,
408 },
Srinivas Pandruvada3521ba12016-04-17 15:03:01 -0700409 /* RAPL_DOMAIN_PLATFORM */
410 {
411 .get_energy_uj = get_energy_counter,
412 .get_max_energy_range_uj = get_max_energy_counter,
413 .release = release_zone,
414 .set_enable = set_domain_enable,
415 .get_enable = get_domain_enable,
416 },
Jacob Pan2d281d82013-10-17 10:28:35 -0700417};
418
Jacob Pane1399ba2016-05-31 13:41:29 -0700419
420/*
421 * Constraint index used by powercap can be different than power limit (PL)
422 * index in that some PLs maybe missing due to non-existant MSRs. So we
423 * need to convert here by finding the valid PLs only (name populated).
424 */
425static int contraint_to_pl(struct rapl_domain *rd, int cid)
426{
427 int i, j;
428
429 for (i = 0, j = 0; i < NR_POWER_LIMITS; i++) {
430 if ((rd->rpl[i].name) && j++ == cid) {
431 pr_debug("%s: index %d\n", __func__, i);
432 return i;
433 }
434 }
Jacob Pancb43f812016-11-28 13:53:11 -0800435 pr_err("Cannot find matching power limit for constraint %d\n", cid);
Jacob Pane1399ba2016-05-31 13:41:29 -0700436
437 return -EINVAL;
438}
439
440static int set_power_limit(struct powercap_zone *power_zone, int cid,
Jacob Pan2d281d82013-10-17 10:28:35 -0700441 u64 power_limit)
442{
443 struct rapl_domain *rd;
444 struct rapl_package *rp;
445 int ret = 0;
Jacob Pane1399ba2016-05-31 13:41:29 -0700446 int id;
Jacob Pan2d281d82013-10-17 10:28:35 -0700447
448 get_online_cpus();
449 rd = power_zone_to_rapl_domain(power_zone);
Jacob Pane1399ba2016-05-31 13:41:29 -0700450 id = contraint_to_pl(rd, cid);
Jacob Pancb43f812016-11-28 13:53:11 -0800451 if (id < 0) {
452 ret = id;
453 goto set_exit;
454 }
Jacob Pane1399ba2016-05-31 13:41:29 -0700455
Jacob Pan309557f2016-02-24 13:31:37 -0800456 rp = rd->rp;
Jacob Pan2d281d82013-10-17 10:28:35 -0700457
458 if (rd->state & DOMAIN_STATE_BIOS_LOCKED) {
459 dev_warn(&power_zone->dev, "%s locked by BIOS, monitoring only\n",
460 rd->name);
461 ret = -EACCES;
462 goto set_exit;
463 }
464
465 switch (rd->rpl[id].prim_id) {
466 case PL1_ENABLE:
467 rapl_write_data_raw(rd, POWER_LIMIT1, power_limit);
468 break;
469 case PL2_ENABLE:
470 rapl_write_data_raw(rd, POWER_LIMIT2, power_limit);
471 break;
472 default:
473 ret = -EINVAL;
474 }
475 if (!ret)
Jacob Pan309557f2016-02-24 13:31:37 -0800476 package_power_limit_irq_save(rp);
Jacob Pan2d281d82013-10-17 10:28:35 -0700477set_exit:
478 put_online_cpus();
479 return ret;
480}
481
Jacob Pane1399ba2016-05-31 13:41:29 -0700482static int get_current_power_limit(struct powercap_zone *power_zone, int cid,
Jacob Pan2d281d82013-10-17 10:28:35 -0700483 u64 *data)
484{
485 struct rapl_domain *rd;
486 u64 val;
487 int prim;
488 int ret = 0;
Jacob Pane1399ba2016-05-31 13:41:29 -0700489 int id;
Jacob Pan2d281d82013-10-17 10:28:35 -0700490
491 get_online_cpus();
492 rd = power_zone_to_rapl_domain(power_zone);
Jacob Pane1399ba2016-05-31 13:41:29 -0700493 id = contraint_to_pl(rd, cid);
Jacob Pancb43f812016-11-28 13:53:11 -0800494 if (id < 0) {
495 ret = id;
496 goto get_exit;
497 }
498
Jacob Pan2d281d82013-10-17 10:28:35 -0700499 switch (rd->rpl[id].prim_id) {
500 case PL1_ENABLE:
501 prim = POWER_LIMIT1;
502 break;
503 case PL2_ENABLE:
504 prim = POWER_LIMIT2;
505 break;
506 default:
507 put_online_cpus();
508 return -EINVAL;
509 }
510 if (rapl_read_data_raw(rd, prim, true, &val))
511 ret = -EIO;
512 else
513 *data = val;
514
Jacob Pancb43f812016-11-28 13:53:11 -0800515get_exit:
Jacob Pan2d281d82013-10-17 10:28:35 -0700516 put_online_cpus();
517
518 return ret;
519}
520
Jacob Pane1399ba2016-05-31 13:41:29 -0700521static int set_time_window(struct powercap_zone *power_zone, int cid,
Jacob Pan2d281d82013-10-17 10:28:35 -0700522 u64 window)
523{
524 struct rapl_domain *rd;
525 int ret = 0;
Jacob Pane1399ba2016-05-31 13:41:29 -0700526 int id;
Jacob Pan2d281d82013-10-17 10:28:35 -0700527
528 get_online_cpus();
529 rd = power_zone_to_rapl_domain(power_zone);
Jacob Pane1399ba2016-05-31 13:41:29 -0700530 id = contraint_to_pl(rd, cid);
Jacob Pancb43f812016-11-28 13:53:11 -0800531 if (id < 0) {
532 ret = id;
533 goto set_time_exit;
534 }
Jacob Pane1399ba2016-05-31 13:41:29 -0700535
Jacob Pan2d281d82013-10-17 10:28:35 -0700536 switch (rd->rpl[id].prim_id) {
537 case PL1_ENABLE:
538 rapl_write_data_raw(rd, TIME_WINDOW1, window);
539 break;
540 case PL2_ENABLE:
541 rapl_write_data_raw(rd, TIME_WINDOW2, window);
542 break;
543 default:
544 ret = -EINVAL;
545 }
Jacob Pancb43f812016-11-28 13:53:11 -0800546
547set_time_exit:
Jacob Pan2d281d82013-10-17 10:28:35 -0700548 put_online_cpus();
549 return ret;
550}
551
Jacob Pane1399ba2016-05-31 13:41:29 -0700552static int get_time_window(struct powercap_zone *power_zone, int cid, u64 *data)
Jacob Pan2d281d82013-10-17 10:28:35 -0700553{
554 struct rapl_domain *rd;
555 u64 val;
556 int ret = 0;
Jacob Pane1399ba2016-05-31 13:41:29 -0700557 int id;
Jacob Pan2d281d82013-10-17 10:28:35 -0700558
559 get_online_cpus();
560 rd = power_zone_to_rapl_domain(power_zone);
Jacob Pane1399ba2016-05-31 13:41:29 -0700561 id = contraint_to_pl(rd, cid);
Jacob Pancb43f812016-11-28 13:53:11 -0800562 if (id < 0) {
563 ret = id;
564 goto get_time_exit;
565 }
Jacob Pane1399ba2016-05-31 13:41:29 -0700566
Jacob Pan2d281d82013-10-17 10:28:35 -0700567 switch (rd->rpl[id].prim_id) {
568 case PL1_ENABLE:
569 ret = rapl_read_data_raw(rd, TIME_WINDOW1, true, &val);
570 break;
571 case PL2_ENABLE:
572 ret = rapl_read_data_raw(rd, TIME_WINDOW2, true, &val);
573 break;
574 default:
575 put_online_cpus();
576 return -EINVAL;
577 }
578 if (!ret)
579 *data = val;
Jacob Pancb43f812016-11-28 13:53:11 -0800580
581get_time_exit:
Jacob Pan2d281d82013-10-17 10:28:35 -0700582 put_online_cpus();
583
584 return ret;
585}
586
Jacob Pane1399ba2016-05-31 13:41:29 -0700587static const char *get_constraint_name(struct powercap_zone *power_zone, int cid)
Jacob Pan2d281d82013-10-17 10:28:35 -0700588{
Jacob Pan2d281d82013-10-17 10:28:35 -0700589 struct rapl_domain *rd;
Jacob Pane1399ba2016-05-31 13:41:29 -0700590 int id;
Jacob Pan2d281d82013-10-17 10:28:35 -0700591
592 rd = power_zone_to_rapl_domain(power_zone);
Jacob Pane1399ba2016-05-31 13:41:29 -0700593 id = contraint_to_pl(rd, cid);
594 if (id >= 0)
595 return rd->rpl[id].name;
Jacob Pan2d281d82013-10-17 10:28:35 -0700596
Jacob Pane1399ba2016-05-31 13:41:29 -0700597 return NULL;
Jacob Pan2d281d82013-10-17 10:28:35 -0700598}
599
600
601static int get_max_power(struct powercap_zone *power_zone, int id,
602 u64 *data)
603{
604 struct rapl_domain *rd;
605 u64 val;
606 int prim;
607 int ret = 0;
608
609 get_online_cpus();
610 rd = power_zone_to_rapl_domain(power_zone);
611 switch (rd->rpl[id].prim_id) {
612 case PL1_ENABLE:
613 prim = THERMAL_SPEC_POWER;
614 break;
615 case PL2_ENABLE:
616 prim = MAX_POWER;
617 break;
618 default:
619 put_online_cpus();
620 return -EINVAL;
621 }
622 if (rapl_read_data_raw(rd, prim, true, &val))
623 ret = -EIO;
624 else
625 *data = val;
626
627 put_online_cpus();
628
629 return ret;
630}
631
Julia Lawall600c3952015-12-23 22:59:55 +0100632static const struct powercap_zone_constraint_ops constraint_ops = {
Jacob Pan2d281d82013-10-17 10:28:35 -0700633 .set_power_limit_uw = set_power_limit,
634 .get_power_limit_uw = get_current_power_limit,
635 .set_time_window_us = set_time_window,
636 .get_time_window_us = get_time_window,
637 .get_max_power_uw = get_max_power,
638 .get_name = get_constraint_name,
639};
640
641/* called after domain detection and package level data are set */
642static void rapl_init_domains(struct rapl_package *rp)
643{
644 int i;
645 struct rapl_domain *rd = rp->domains;
646
647 for (i = 0; i < RAPL_DOMAIN_MAX; i++) {
648 unsigned int mask = rp->domain_map & (1 << i);
649 switch (mask) {
650 case BIT(RAPL_DOMAIN_PACKAGE):
651 rd->name = rapl_domain_names[RAPL_DOMAIN_PACKAGE];
652 rd->id = RAPL_DOMAIN_PACKAGE;
653 rd->msrs[0] = MSR_PKG_POWER_LIMIT;
654 rd->msrs[1] = MSR_PKG_ENERGY_STATUS;
655 rd->msrs[2] = MSR_PKG_PERF_STATUS;
656 rd->msrs[3] = 0;
657 rd->msrs[4] = MSR_PKG_POWER_INFO;
658 rd->rpl[0].prim_id = PL1_ENABLE;
659 rd->rpl[0].name = pl1_name;
660 rd->rpl[1].prim_id = PL2_ENABLE;
661 rd->rpl[1].name = pl2_name;
662 break;
663 case BIT(RAPL_DOMAIN_PP0):
664 rd->name = rapl_domain_names[RAPL_DOMAIN_PP0];
665 rd->id = RAPL_DOMAIN_PP0;
666 rd->msrs[0] = MSR_PP0_POWER_LIMIT;
667 rd->msrs[1] = MSR_PP0_ENERGY_STATUS;
668 rd->msrs[2] = 0;
669 rd->msrs[3] = MSR_PP0_POLICY;
670 rd->msrs[4] = 0;
671 rd->rpl[0].prim_id = PL1_ENABLE;
672 rd->rpl[0].name = pl1_name;
673 break;
674 case BIT(RAPL_DOMAIN_PP1):
675 rd->name = rapl_domain_names[RAPL_DOMAIN_PP1];
676 rd->id = RAPL_DOMAIN_PP1;
677 rd->msrs[0] = MSR_PP1_POWER_LIMIT;
678 rd->msrs[1] = MSR_PP1_ENERGY_STATUS;
679 rd->msrs[2] = 0;
680 rd->msrs[3] = MSR_PP1_POLICY;
681 rd->msrs[4] = 0;
682 rd->rpl[0].prim_id = PL1_ENABLE;
683 rd->rpl[0].name = pl1_name;
684 break;
685 case BIT(RAPL_DOMAIN_DRAM):
686 rd->name = rapl_domain_names[RAPL_DOMAIN_DRAM];
687 rd->id = RAPL_DOMAIN_DRAM;
688 rd->msrs[0] = MSR_DRAM_POWER_LIMIT;
689 rd->msrs[1] = MSR_DRAM_ENERGY_STATUS;
690 rd->msrs[2] = MSR_DRAM_PERF_STATUS;
691 rd->msrs[3] = 0;
692 rd->msrs[4] = MSR_DRAM_POWER_INFO;
693 rd->rpl[0].prim_id = PL1_ENABLE;
694 rd->rpl[0].name = pl1_name;
Jacob Pand474a4d2015-03-13 03:48:56 -0700695 rd->domain_energy_unit =
696 rapl_defaults->dram_domain_energy_unit;
697 if (rd->domain_energy_unit)
698 pr_info("DRAM domain energy unit %dpj\n",
699 rd->domain_energy_unit);
Jacob Pan2d281d82013-10-17 10:28:35 -0700700 break;
701 }
702 if (mask) {
Jacob Pan309557f2016-02-24 13:31:37 -0800703 rd->rp = rp;
Jacob Pan2d281d82013-10-17 10:28:35 -0700704 rd++;
705 }
706 }
707}
708
Jacob Pan309557f2016-02-24 13:31:37 -0800709static u64 rapl_unit_xlate(struct rapl_domain *rd, enum unit_type type,
710 u64 value, int to_raw)
Jacob Pan2d281d82013-10-17 10:28:35 -0700711{
Jacob Pan3c2c0842014-11-07 09:29:26 -0800712 u64 units = 1;
Jacob Pan309557f2016-02-24 13:31:37 -0800713 struct rapl_package *rp = rd->rp;
Jacob Pand474a4d2015-03-13 03:48:56 -0700714 u64 scale = 1;
Jacob Pan2d281d82013-10-17 10:28:35 -0700715
Jacob Pan2d281d82013-10-17 10:28:35 -0700716 switch (type) {
717 case POWER_UNIT:
Jacob Pan3c2c0842014-11-07 09:29:26 -0800718 units = rp->power_unit;
Jacob Pan2d281d82013-10-17 10:28:35 -0700719 break;
720 case ENERGY_UNIT:
Jacob Pand474a4d2015-03-13 03:48:56 -0700721 scale = ENERGY_UNIT_SCALE;
722 /* per domain unit takes precedence */
Jacob Pancb43f812016-11-28 13:53:11 -0800723 if (rd->domain_energy_unit)
Jacob Pand474a4d2015-03-13 03:48:56 -0700724 units = rd->domain_energy_unit;
725 else
726 units = rp->energy_unit;
Jacob Pan2d281d82013-10-17 10:28:35 -0700727 break;
728 case TIME_UNIT:
Jacob Pan3c2c0842014-11-07 09:29:26 -0800729 return rapl_defaults->compute_time_window(rp, value, to_raw);
Jacob Pan2d281d82013-10-17 10:28:35 -0700730 case ARBITRARY_UNIT:
731 default:
732 return value;
733 };
734
735 if (to_raw)
Jacob Pand474a4d2015-03-13 03:48:56 -0700736 return div64_u64(value, units) * scale;
Jacob Pan3c2c0842014-11-07 09:29:26 -0800737
738 value *= units;
739
Jacob Pand474a4d2015-03-13 03:48:56 -0700740 return div64_u64(value, scale);
Jacob Pan2d281d82013-10-17 10:28:35 -0700741}
742
743/* in the order of enum rapl_primitives */
744static struct rapl_primitive_info rpi[] = {
745 /* name, mask, shift, msr index, unit divisor */
746 PRIMITIVE_INFO_INIT(ENERGY_COUNTER, ENERGY_STATUS_MASK, 0,
747 RAPL_DOMAIN_MSR_STATUS, ENERGY_UNIT, 0),
748 PRIMITIVE_INFO_INIT(POWER_LIMIT1, POWER_LIMIT1_MASK, 0,
749 RAPL_DOMAIN_MSR_LIMIT, POWER_UNIT, 0),
750 PRIMITIVE_INFO_INIT(POWER_LIMIT2, POWER_LIMIT2_MASK, 32,
751 RAPL_DOMAIN_MSR_LIMIT, POWER_UNIT, 0),
752 PRIMITIVE_INFO_INIT(FW_LOCK, POWER_PP_LOCK, 31,
753 RAPL_DOMAIN_MSR_LIMIT, ARBITRARY_UNIT, 0),
754 PRIMITIVE_INFO_INIT(PL1_ENABLE, POWER_LIMIT1_ENABLE, 15,
755 RAPL_DOMAIN_MSR_LIMIT, ARBITRARY_UNIT, 0),
756 PRIMITIVE_INFO_INIT(PL1_CLAMP, POWER_LIMIT1_CLAMP, 16,
757 RAPL_DOMAIN_MSR_LIMIT, ARBITRARY_UNIT, 0),
758 PRIMITIVE_INFO_INIT(PL2_ENABLE, POWER_LIMIT2_ENABLE, 47,
759 RAPL_DOMAIN_MSR_LIMIT, ARBITRARY_UNIT, 0),
760 PRIMITIVE_INFO_INIT(PL2_CLAMP, POWER_LIMIT2_CLAMP, 48,
761 RAPL_DOMAIN_MSR_LIMIT, ARBITRARY_UNIT, 0),
762 PRIMITIVE_INFO_INIT(TIME_WINDOW1, TIME_WINDOW1_MASK, 17,
763 RAPL_DOMAIN_MSR_LIMIT, TIME_UNIT, 0),
764 PRIMITIVE_INFO_INIT(TIME_WINDOW2, TIME_WINDOW2_MASK, 49,
765 RAPL_DOMAIN_MSR_LIMIT, TIME_UNIT, 0),
766 PRIMITIVE_INFO_INIT(THERMAL_SPEC_POWER, POWER_INFO_THERMAL_SPEC_MASK,
767 0, RAPL_DOMAIN_MSR_INFO, POWER_UNIT, 0),
768 PRIMITIVE_INFO_INIT(MAX_POWER, POWER_INFO_MAX_MASK, 32,
769 RAPL_DOMAIN_MSR_INFO, POWER_UNIT, 0),
770 PRIMITIVE_INFO_INIT(MIN_POWER, POWER_INFO_MIN_MASK, 16,
771 RAPL_DOMAIN_MSR_INFO, POWER_UNIT, 0),
772 PRIMITIVE_INFO_INIT(MAX_TIME_WINDOW, POWER_INFO_MAX_TIME_WIN_MASK, 48,
773 RAPL_DOMAIN_MSR_INFO, TIME_UNIT, 0),
774 PRIMITIVE_INFO_INIT(THROTTLED_TIME, PERF_STATUS_THROTTLE_TIME_MASK, 0,
775 RAPL_DOMAIN_MSR_PERF, TIME_UNIT, 0),
776 PRIMITIVE_INFO_INIT(PRIORITY_LEVEL, PP_POLICY_MASK, 0,
777 RAPL_DOMAIN_MSR_POLICY, ARBITRARY_UNIT, 0),
778 /* non-hardware */
779 PRIMITIVE_INFO_INIT(AVERAGE_POWER, 0, 0, 0, POWER_UNIT,
780 RAPL_PRIMITIVE_DERIVED),
781 {NULL, 0, 0, 0},
782};
783
784/* Read primitive data based on its related struct rapl_primitive_info.
785 * if xlate flag is set, return translated data based on data units, i.e.
786 * time, energy, and power.
787 * RAPL MSRs are non-architectual and are laid out not consistently across
788 * domains. Here we use primitive info to allow writing consolidated access
789 * functions.
790 * For a given primitive, it is processed by MSR mask and shift. Unit conversion
791 * is pre-assigned based on RAPL unit MSRs read at init time.
792 * 63-------------------------- 31--------------------------- 0
793 * | xxxxx (mask) |
794 * | |<- shift ----------------|
795 * 63-------------------------- 31--------------------------- 0
796 */
797static int rapl_read_data_raw(struct rapl_domain *rd,
798 enum rapl_primitives prim,
799 bool xlate, u64 *data)
800{
801 u64 value, final;
802 u32 msr;
803 struct rapl_primitive_info *rp = &rpi[prim];
804 int cpu;
805
806 if (!rp->name || rp->flag & RAPL_PRIMITIVE_DUMMY)
807 return -EINVAL;
808
809 msr = rd->msrs[rp->id];
810 if (!msr)
811 return -EINVAL;
Jacob Pan323ee642016-02-24 13:31:38 -0800812
813 cpu = rd->rp->lead_cpu;
Jacob Pan2d281d82013-10-17 10:28:35 -0700814
815 /* special-case package domain, which uses a different bit*/
816 if (prim == FW_LOCK && rd->id == RAPL_DOMAIN_PACKAGE) {
817 rp->mask = POWER_PACKAGE_LOCK;
818 rp->shift = 63;
819 }
820 /* non-hardware data are collected by the polling thread */
821 if (rp->flag & RAPL_PRIMITIVE_DERIVED) {
822 *data = rd->rdd.primitives[prim];
823 return 0;
824 }
825
826 if (rdmsrl_safe_on_cpu(cpu, msr, &value)) {
827 pr_debug("failed to read msr 0x%x on cpu %d\n", msr, cpu);
828 return -EIO;
829 }
830
831 final = value & rp->mask;
832 final = final >> rp->shift;
833 if (xlate)
Jacob Pan309557f2016-02-24 13:31:37 -0800834 *data = rapl_unit_xlate(rd, rp->unit, final, 0);
Jacob Pan2d281d82013-10-17 10:28:35 -0700835 else
836 *data = final;
837
838 return 0;
839}
840
Jacob Panf14a1392016-02-24 13:31:36 -0800841
842static int msrl_update_safe(u32 msr_no, u64 clear_mask, u64 set_mask)
843{
844 int err;
845 u64 val;
846
847 err = rdmsrl_safe(msr_no, &val);
848 if (err)
849 goto out;
850
851 val &= ~clear_mask;
852 val |= set_mask;
853
854 err = wrmsrl_safe(msr_no, val);
855
856out:
857 return err;
858}
859
860static void msrl_update_func(void *info)
861{
862 struct msrl_action *ma = info;
863
864 ma->err = msrl_update_safe(ma->msr_no, ma->clear_mask, ma->set_mask);
865}
866
Jacob Pan2d281d82013-10-17 10:28:35 -0700867/* Similar use of primitive info in the read counterpart */
868static int rapl_write_data_raw(struct rapl_domain *rd,
869 enum rapl_primitives prim,
870 unsigned long long value)
871{
Jacob Pan2d281d82013-10-17 10:28:35 -0700872 struct rapl_primitive_info *rp = &rpi[prim];
873 int cpu;
Jacob Panf14a1392016-02-24 13:31:36 -0800874 u64 bits;
875 struct msrl_action ma;
876 int ret;
Jacob Pan2d281d82013-10-17 10:28:35 -0700877
Jacob Pan323ee642016-02-24 13:31:38 -0800878 cpu = rd->rp->lead_cpu;
Jacob Pan309557f2016-02-24 13:31:37 -0800879 bits = rapl_unit_xlate(rd, rp->unit, value, 1);
Adam Lessnauedbdabc2017-06-01 11:21:50 +0200880 bits <<= rp->shift;
881 bits &= rp->mask;
882
Jacob Panf14a1392016-02-24 13:31:36 -0800883 memset(&ma, 0, sizeof(ma));
884
885 ma.msr_no = rd->msrs[rp->id];
886 ma.clear_mask = rp->mask;
887 ma.set_mask = bits;
888
889 ret = smp_call_function_single(cpu, msrl_update_func, &ma, 1);
890 if (ret)
891 WARN_ON_ONCE(ret);
892 else
893 ret = ma.err;
894
895 return ret;
Jacob Pan2d281d82013-10-17 10:28:35 -0700896}
897
Jacob Pan3c2c0842014-11-07 09:29:26 -0800898/*
899 * Raw RAPL data stored in MSRs are in certain scales. We need to
900 * convert them into standard units based on the units reported in
901 * the RAPL unit MSRs. This is specific to CPUs as the method to
902 * calculate units differ on different CPUs.
903 * We convert the units to below format based on CPUs.
904 * i.e.
Jacob Pand474a4d2015-03-13 03:48:56 -0700905 * energy unit: picoJoules : Represented in picoJoules by default
Jacob Pan3c2c0842014-11-07 09:29:26 -0800906 * power unit : microWatts : Represented in milliWatts by default
907 * time unit : microseconds: Represented in seconds by default
908 */
909static int rapl_check_unit_core(struct rapl_package *rp, int cpu)
Jacob Pan2d281d82013-10-17 10:28:35 -0700910{
911 u64 msr_val;
912 u32 value;
913
914 if (rdmsrl_safe_on_cpu(cpu, MSR_RAPL_POWER_UNIT, &msr_val)) {
915 pr_err("Failed to read power unit MSR 0x%x on CPU %d, exit.\n",
916 MSR_RAPL_POWER_UNIT, cpu);
917 return -ENODEV;
918 }
919
Jacob Pan2d281d82013-10-17 10:28:35 -0700920 value = (msr_val & ENERGY_UNIT_MASK) >> ENERGY_UNIT_OFFSET;
Jacob Pand474a4d2015-03-13 03:48:56 -0700921 rp->energy_unit = ENERGY_UNIT_SCALE * 1000000 / (1 << value);
Jacob Pan2d281d82013-10-17 10:28:35 -0700922
923 value = (msr_val & POWER_UNIT_MASK) >> POWER_UNIT_OFFSET;
Jacob Pan3c2c0842014-11-07 09:29:26 -0800924 rp->power_unit = 1000000 / (1 << value);
Jacob Pan2d281d82013-10-17 10:28:35 -0700925
926 value = (msr_val & TIME_UNIT_MASK) >> TIME_UNIT_OFFSET;
Jacob Pan3c2c0842014-11-07 09:29:26 -0800927 rp->time_unit = 1000000 / (1 << value);
Jacob Pan2d281d82013-10-17 10:28:35 -0700928
Jacob Pand474a4d2015-03-13 03:48:56 -0700929 pr_debug("Core CPU package %d energy=%dpJ, time=%dus, power=%duW\n",
Jacob Pan3c2c0842014-11-07 09:29:26 -0800930 rp->id, rp->energy_unit, rp->time_unit, rp->power_unit);
Jacob Pan2d281d82013-10-17 10:28:35 -0700931
932 return 0;
933}
934
Jacob Pan3c2c0842014-11-07 09:29:26 -0800935static int rapl_check_unit_atom(struct rapl_package *rp, int cpu)
936{
937 u64 msr_val;
938 u32 value;
939
940 if (rdmsrl_safe_on_cpu(cpu, MSR_RAPL_POWER_UNIT, &msr_val)) {
941 pr_err("Failed to read power unit MSR 0x%x on CPU %d, exit.\n",
942 MSR_RAPL_POWER_UNIT, cpu);
943 return -ENODEV;
944 }
945 value = (msr_val & ENERGY_UNIT_MASK) >> ENERGY_UNIT_OFFSET;
Jacob Pand474a4d2015-03-13 03:48:56 -0700946 rp->energy_unit = ENERGY_UNIT_SCALE * 1 << value;
Jacob Pan3c2c0842014-11-07 09:29:26 -0800947
948 value = (msr_val & POWER_UNIT_MASK) >> POWER_UNIT_OFFSET;
949 rp->power_unit = (1 << value) * 1000;
950
951 value = (msr_val & TIME_UNIT_MASK) >> TIME_UNIT_OFFSET;
952 rp->time_unit = 1000000 / (1 << value);
953
Jacob Pand474a4d2015-03-13 03:48:56 -0700954 pr_debug("Atom package %d energy=%dpJ, time=%dus, power=%duW\n",
Jacob Pan3c2c0842014-11-07 09:29:26 -0800955 rp->id, rp->energy_unit, rp->time_unit, rp->power_unit);
956
957 return 0;
958}
959
Jacob Panf14a1392016-02-24 13:31:36 -0800960static void power_limit_irq_save_cpu(void *info)
961{
962 u32 l, h = 0;
963 struct rapl_package *rp = (struct rapl_package *)info;
964
965 /* save the state of PLN irq mask bit before disabling it */
966 rdmsr_safe(MSR_IA32_PACKAGE_THERM_INTERRUPT, &l, &h);
967 if (!(rp->power_limit_irq & PACKAGE_PLN_INT_SAVED)) {
968 rp->power_limit_irq = l & PACKAGE_THERM_INT_PLN_ENABLE;
969 rp->power_limit_irq |= PACKAGE_PLN_INT_SAVED;
970 }
971 l &= ~PACKAGE_THERM_INT_PLN_ENABLE;
972 wrmsr_safe(MSR_IA32_PACKAGE_THERM_INTERRUPT, l, h);
973}
974
Jacob Pan3c2c0842014-11-07 09:29:26 -0800975
Jacob Pan2d281d82013-10-17 10:28:35 -0700976/* REVISIT:
977 * When package power limit is set artificially low by RAPL, LVT
978 * thermal interrupt for package power limit should be ignored
979 * since we are not really exceeding the real limit. The intention
980 * is to avoid excessive interrupts while we are trying to save power.
981 * A useful feature might be routing the package_power_limit interrupt
982 * to userspace via eventfd. once we have a usecase, this is simple
983 * to do by adding an atomic notifier.
984 */
985
Jacob Pan309557f2016-02-24 13:31:37 -0800986static void package_power_limit_irq_save(struct rapl_package *rp)
Jacob Pan2d281d82013-10-17 10:28:35 -0700987{
Jacob Pan2d281d82013-10-17 10:28:35 -0700988 if (!boot_cpu_has(X86_FEATURE_PTS) || !boot_cpu_has(X86_FEATURE_PLN))
989 return;
990
Jacob Pan323ee642016-02-24 13:31:38 -0800991 smp_call_function_single(rp->lead_cpu, power_limit_irq_save_cpu, rp, 1);
Jacob Panf14a1392016-02-24 13:31:36 -0800992}
993
Thomas Gleixner58705062016-11-22 21:16:02 +0000994/*
995 * Restore per package power limit interrupt enable state. Called from cpu
996 * hotplug code on package removal.
997 */
998static void package_power_limit_irq_restore(struct rapl_package *rp)
Jacob Panf14a1392016-02-24 13:31:36 -0800999{
Thomas Gleixner58705062016-11-22 21:16:02 +00001000 u32 l, h;
1001
1002 if (!boot_cpu_has(X86_FEATURE_PTS) || !boot_cpu_has(X86_FEATURE_PLN))
1003 return;
1004
1005 /* irq enable state not saved, nothing to restore */
1006 if (!(rp->power_limit_irq & PACKAGE_PLN_INT_SAVED))
1007 return;
Jacob Panf14a1392016-02-24 13:31:36 -08001008
1009 rdmsr_safe(MSR_IA32_PACKAGE_THERM_INTERRUPT, &l, &h);
1010
1011 if (rp->power_limit_irq & PACKAGE_THERM_INT_PLN_ENABLE)
1012 l |= PACKAGE_THERM_INT_PLN_ENABLE;
1013 else
1014 l &= ~PACKAGE_THERM_INT_PLN_ENABLE;
1015
1016 wrmsr_safe(MSR_IA32_PACKAGE_THERM_INTERRUPT, l, h);
Jacob Pan2d281d82013-10-17 10:28:35 -07001017}
1018
Jacob Pan3c2c0842014-11-07 09:29:26 -08001019static void set_floor_freq_default(struct rapl_domain *rd, bool mode)
1020{
1021 int nr_powerlimit = find_nr_power_limit(rd);
1022
1023 /* always enable clamp such that p-state can go below OS requested
1024 * range. power capping priority over guranteed frequency.
1025 */
1026 rapl_write_data_raw(rd, PL1_CLAMP, mode);
1027
1028 /* some domains have pl2 */
1029 if (nr_powerlimit > 1) {
1030 rapl_write_data_raw(rd, PL2_ENABLE, mode);
1031 rapl_write_data_raw(rd, PL2_CLAMP, mode);
1032 }
1033}
1034
1035static void set_floor_freq_atom(struct rapl_domain *rd, bool enable)
1036{
1037 static u32 power_ctrl_orig_val;
1038 u32 mdata;
1039
Ajay Thomas51b63402015-04-30 01:43:23 +05301040 if (!rapl_defaults->floor_freq_reg_addr) {
1041 pr_err("Invalid floor frequency config register\n");
1042 return;
1043 }
1044
Jacob Pan3c2c0842014-11-07 09:29:26 -08001045 if (!power_ctrl_orig_val)
Andy Shevchenko4077a382015-11-11 19:59:29 +02001046 iosf_mbi_read(BT_MBI_UNIT_PMC, MBI_CR_READ,
1047 rapl_defaults->floor_freq_reg_addr,
1048 &power_ctrl_orig_val);
Jacob Pan3c2c0842014-11-07 09:29:26 -08001049 mdata = power_ctrl_orig_val;
1050 if (enable) {
1051 mdata &= ~(0x7f << 8);
1052 mdata |= 1 << 8;
1053 }
Andy Shevchenko4077a382015-11-11 19:59:29 +02001054 iosf_mbi_write(BT_MBI_UNIT_PMC, MBI_CR_WRITE,
1055 rapl_defaults->floor_freq_reg_addr, mdata);
Jacob Pan3c2c0842014-11-07 09:29:26 -08001056}
1057
1058static u64 rapl_compute_time_window_core(struct rapl_package *rp, u64 value,
1059 bool to_raw)
1060{
1061 u64 f, y; /* fraction and exp. used for time unit */
1062
1063 /*
1064 * Special processing based on 2^Y*(1+F/4), refer
1065 * to Intel Software Developer's manual Vol.3B: CH 14.9.3.
1066 */
1067 if (!to_raw) {
1068 f = (value & 0x60) >> 5;
1069 y = value & 0x1f;
1070 value = (1 << y) * (4 + f) * rp->time_unit / 4;
1071 } else {
1072 do_div(value, rp->time_unit);
1073 y = ilog2(value);
1074 f = div64_u64(4 * (value - (1 << y)), 1 << y);
1075 value = (y & 0x1f) | ((f & 0x3) << 5);
1076 }
1077 return value;
1078}
1079
1080static u64 rapl_compute_time_window_atom(struct rapl_package *rp, u64 value,
1081 bool to_raw)
1082{
1083 /*
1084 * Atom time unit encoding is straight forward val * time_unit,
1085 * where time_unit is default to 1 sec. Never 0.
1086 */
1087 if (!to_raw)
1088 return (value) ? value *= rp->time_unit : rp->time_unit;
1089 else
1090 value = div64_u64(value, rp->time_unit);
1091
1092 return value;
1093}
1094
Jacob Pan087e9cb2014-11-07 09:29:25 -08001095static const struct rapl_defaults rapl_defaults_core = {
Ajay Thomas51b63402015-04-30 01:43:23 +05301096 .floor_freq_reg_addr = 0,
Jacob Pan3c2c0842014-11-07 09:29:26 -08001097 .check_unit = rapl_check_unit_core,
1098 .set_floor_freq = set_floor_freq_default,
1099 .compute_time_window = rapl_compute_time_window_core,
Jacob Pan087e9cb2014-11-07 09:29:25 -08001100};
1101
Jacob Pand474a4d2015-03-13 03:48:56 -07001102static const struct rapl_defaults rapl_defaults_hsw_server = {
1103 .check_unit = rapl_check_unit_core,
1104 .set_floor_freq = set_floor_freq_default,
1105 .compute_time_window = rapl_compute_time_window_core,
1106 .dram_domain_energy_unit = 15300,
1107};
1108
Ajay Thomas51b63402015-04-30 01:43:23 +05301109static const struct rapl_defaults rapl_defaults_byt = {
1110 .floor_freq_reg_addr = IOSF_CPU_POWER_BUDGET_CTL_BYT,
Jacob Pan3c2c0842014-11-07 09:29:26 -08001111 .check_unit = rapl_check_unit_atom,
1112 .set_floor_freq = set_floor_freq_atom,
1113 .compute_time_window = rapl_compute_time_window_atom,
Jacob Pan087e9cb2014-11-07 09:29:25 -08001114};
1115
Ajay Thomas51b63402015-04-30 01:43:23 +05301116static const struct rapl_defaults rapl_defaults_tng = {
1117 .floor_freq_reg_addr = IOSF_CPU_POWER_BUDGET_CTL_TNG,
1118 .check_unit = rapl_check_unit_atom,
1119 .set_floor_freq = set_floor_freq_atom,
1120 .compute_time_window = rapl_compute_time_window_atom,
1121};
1122
1123static const struct rapl_defaults rapl_defaults_ann = {
1124 .floor_freq_reg_addr = 0,
1125 .check_unit = rapl_check_unit_atom,
1126 .set_floor_freq = NULL,
1127 .compute_time_window = rapl_compute_time_window_atom,
1128};
1129
1130static const struct rapl_defaults rapl_defaults_cht = {
1131 .floor_freq_reg_addr = 0,
1132 .check_unit = rapl_check_unit_atom,
1133 .set_floor_freq = NULL,
1134 .compute_time_window = rapl_compute_time_window_atom,
1135};
1136
Mathias Krauseea85dbc2015-03-25 22:15:52 +01001137static const struct x86_cpu_id rapl_ids[] __initconst = {
Andy Shevchenko17ed1512018-08-31 11:25:13 +03001138 INTEL_CPU_FAM6(SANDYBRIDGE, rapl_defaults_core),
1139 INTEL_CPU_FAM6(SANDYBRIDGE_X, rapl_defaults_core),
Dave Hansen0bb04b52016-06-02 17:19:37 -07001140
Andy Shevchenko17ed1512018-08-31 11:25:13 +03001141 INTEL_CPU_FAM6(IVYBRIDGE, rapl_defaults_core),
1142 INTEL_CPU_FAM6(IVYBRIDGE_X, rapl_defaults_core),
Dave Hansen0bb04b52016-06-02 17:19:37 -07001143
Andy Shevchenko17ed1512018-08-31 11:25:13 +03001144 INTEL_CPU_FAM6(HASWELL_CORE, rapl_defaults_core),
1145 INTEL_CPU_FAM6(HASWELL_ULT, rapl_defaults_core),
1146 INTEL_CPU_FAM6(HASWELL_GT3E, rapl_defaults_core),
1147 INTEL_CPU_FAM6(HASWELL_X, rapl_defaults_hsw_server),
Dave Hansen0bb04b52016-06-02 17:19:37 -07001148
Andy Shevchenko17ed1512018-08-31 11:25:13 +03001149 INTEL_CPU_FAM6(BROADWELL_CORE, rapl_defaults_core),
1150 INTEL_CPU_FAM6(BROADWELL_GT3E, rapl_defaults_core),
1151 INTEL_CPU_FAM6(BROADWELL_XEON_D, rapl_defaults_core),
1152 INTEL_CPU_FAM6(BROADWELL_X, rapl_defaults_hsw_server),
Dave Hansen0bb04b52016-06-02 17:19:37 -07001153
Andy Shevchenko17ed1512018-08-31 11:25:13 +03001154 INTEL_CPU_FAM6(SKYLAKE_DESKTOP, rapl_defaults_core),
1155 INTEL_CPU_FAM6(SKYLAKE_MOBILE, rapl_defaults_core),
1156 INTEL_CPU_FAM6(SKYLAKE_X, rapl_defaults_hsw_server),
1157 INTEL_CPU_FAM6(KABYLAKE_MOBILE, rapl_defaults_core),
1158 INTEL_CPU_FAM6(KABYLAKE_DESKTOP, rapl_defaults_core),
1159 INTEL_CPU_FAM6(CANNONLAKE_MOBILE, rapl_defaults_core),
Gayatri Kammelaba6f3ec2019-02-18 15:01:02 +08001160 INTEL_CPU_FAM6(ICELAKE_MOBILE, rapl_defaults_core),
Dave Hansen0bb04b52016-06-02 17:19:37 -07001161
Linus Torvaldsc05f3642018-10-23 13:32:18 +01001162 INTEL_CPU_FAM6(ATOM_SILVERMONT, rapl_defaults_byt),
Andy Shevchenko17ed1512018-08-31 11:25:13 +03001163 INTEL_CPU_FAM6(ATOM_AIRMONT, rapl_defaults_cht),
Linus Torvaldsc05f3642018-10-23 13:32:18 +01001164 INTEL_CPU_FAM6(ATOM_SILVERMONT_MID, rapl_defaults_tng),
1165 INTEL_CPU_FAM6(ATOM_AIRMONT_MID, rapl_defaults_ann),
Andy Shevchenko17ed1512018-08-31 11:25:13 +03001166 INTEL_CPU_FAM6(ATOM_GOLDMONT, rapl_defaults_core),
Linus Torvaldsc05f3642018-10-23 13:32:18 +01001167 INTEL_CPU_FAM6(ATOM_GOLDMONT_PLUS, rapl_defaults_core),
1168 INTEL_CPU_FAM6(ATOM_GOLDMONT_X, rapl_defaults_core),
Zhang Ruidf7f8e02019-02-12 10:47:10 +08001169 INTEL_CPU_FAM6(ATOM_TREMONT_X, rapl_defaults_core),
Dave Hansen0bb04b52016-06-02 17:19:37 -07001170
Andy Shevchenko17ed1512018-08-31 11:25:13 +03001171 INTEL_CPU_FAM6(XEON_PHI_KNL, rapl_defaults_hsw_server),
1172 INTEL_CPU_FAM6(XEON_PHI_KNM, rapl_defaults_hsw_server),
Jacob Pan2d281d82013-10-17 10:28:35 -07001173 {}
1174};
1175MODULE_DEVICE_TABLE(x86cpu, rapl_ids);
1176
Thomas Gleixnerbed5ab62016-11-22 21:15:58 +00001177/* Read once for all raw primitive data for domains */
1178static void rapl_update_domain_data(struct rapl_package *rp)
Jacob Pan2d281d82013-10-17 10:28:35 -07001179{
1180 int dmn, prim;
1181 u64 val;
Jacob Pan2d281d82013-10-17 10:28:35 -07001182
Thomas Gleixnerbed5ab62016-11-22 21:15:58 +00001183 for (dmn = 0; dmn < rp->nr_domains; dmn++) {
1184 pr_debug("update package %d domain %s data\n", rp->id,
1185 rp->domains[dmn].name);
1186 /* exclude non-raw primitives */
1187 for (prim = 0; prim < NR_RAW_PRIMITIVES; prim++) {
1188 if (!rapl_read_data_raw(&rp->domains[dmn], prim,
1189 rpi[prim].unit, &val))
1190 rp->domains[dmn].rdd.primitives[prim] = val;
Jacob Pan2d281d82013-10-17 10:28:35 -07001191 }
1192 }
1193
1194}
1195
Thomas Gleixner58705062016-11-22 21:16:02 +00001196static void rapl_unregister_powercap(void)
Jacob Pan2d281d82013-10-17 10:28:35 -07001197{
Srinivas Pandruvada3521ba12016-04-17 15:03:01 -07001198 if (platform_rapl_domain) {
1199 powercap_unregister_zone(control_type,
1200 &platform_rapl_domain->power_zone);
1201 kfree(platform_rapl_domain);
1202 }
Jacob Pan2d281d82013-10-17 10:28:35 -07001203 powercap_unregister_control_type(control_type);
Jacob Pan2d281d82013-10-17 10:28:35 -07001204}
1205
1206static int rapl_package_register_powercap(struct rapl_package *rp)
1207{
1208 struct rapl_domain *rd;
Jacob Pan2d281d82013-10-17 10:28:35 -07001209 char dev_name[17]; /* max domain name = 7 + 1 + 8 for int + 1 for null*/
1210 struct powercap_zone *power_zone = NULL;
Luis de Bethencourt01857cf2018-01-17 10:30:34 +00001211 int nr_pl, ret;
Thomas Gleixnerbed5ab62016-11-22 21:15:58 +00001212
1213 /* Update the domain data of the new package */
1214 rapl_update_domain_data(rp);
Jacob Pan2d281d82013-10-17 10:28:35 -07001215
1216 /* first we register package domain as the parent zone*/
1217 for (rd = rp->domains; rd < rp->domains + rp->nr_domains; rd++) {
1218 if (rd->id == RAPL_DOMAIN_PACKAGE) {
1219 nr_pl = find_nr_power_limit(rd);
1220 pr_debug("register socket %d package domain %s\n",
1221 rp->id, rd->name);
1222 memset(dev_name, 0, sizeof(dev_name));
1223 snprintf(dev_name, sizeof(dev_name), "%s-%d",
1224 rd->name, rp->id);
1225 power_zone = powercap_register_zone(&rd->power_zone,
1226 control_type,
1227 dev_name, NULL,
1228 &zone_ops[rd->id],
1229 nr_pl,
1230 &constraint_ops);
1231 if (IS_ERR(power_zone)) {
1232 pr_debug("failed to register package, %d\n",
1233 rp->id);
Thomas Gleixnerbed5ab62016-11-22 21:15:58 +00001234 return PTR_ERR(power_zone);
Jacob Pan2d281d82013-10-17 10:28:35 -07001235 }
1236 /* track parent zone in per package/socket data */
1237 rp->power_zone = power_zone;
1238 /* done, only one package domain per socket */
1239 break;
1240 }
1241 }
1242 if (!power_zone) {
1243 pr_err("no package domain found, unknown topology!\n");
Thomas Gleixnerbed5ab62016-11-22 21:15:58 +00001244 return -ENODEV;
Jacob Pan2d281d82013-10-17 10:28:35 -07001245 }
1246 /* now register domains as children of the socket/package*/
1247 for (rd = rp->domains; rd < rp->domains + rp->nr_domains; rd++) {
1248 if (rd->id == RAPL_DOMAIN_PACKAGE)
1249 continue;
1250 /* number of power limits per domain varies */
1251 nr_pl = find_nr_power_limit(rd);
1252 power_zone = powercap_register_zone(&rd->power_zone,
1253 control_type, rd->name,
1254 rp->power_zone,
1255 &zone_ops[rd->id], nr_pl,
1256 &constraint_ops);
1257
1258 if (IS_ERR(power_zone)) {
1259 pr_debug("failed to register power_zone, %d:%s:%s\n",
1260 rp->id, rd->name, dev_name);
1261 ret = PTR_ERR(power_zone);
1262 goto err_cleanup;
1263 }
1264 }
Thomas Gleixnerbed5ab62016-11-22 21:15:58 +00001265 return 0;
Jacob Pan2d281d82013-10-17 10:28:35 -07001266
Jacob Pan2d281d82013-10-17 10:28:35 -07001267err_cleanup:
Thomas Gleixner58705062016-11-22 21:16:02 +00001268 /*
1269 * Clean up previously initialized domains within the package if we
Jacob Pan2d281d82013-10-17 10:28:35 -07001270 * failed after the first domain setup.
1271 */
1272 while (--rd >= rp->domains) {
1273 pr_debug("unregister package %d domain %s\n", rp->id, rd->name);
1274 powercap_unregister_zone(control_type, &rd->power_zone);
1275 }
1276
1277 return ret;
1278}
1279
Thomas Gleixner58705062016-11-22 21:16:02 +00001280static int __init rapl_register_psys(void)
Srinivas Pandruvada3521ba12016-04-17 15:03:01 -07001281{
1282 struct rapl_domain *rd;
1283 struct powercap_zone *power_zone;
1284 u64 val;
1285
1286 if (rdmsrl_safe_on_cpu(0, MSR_PLATFORM_ENERGY_STATUS, &val) || !val)
1287 return -ENODEV;
1288
1289 if (rdmsrl_safe_on_cpu(0, MSR_PLATFORM_POWER_LIMIT, &val) || !val)
1290 return -ENODEV;
1291
1292 rd = kzalloc(sizeof(*rd), GFP_KERNEL);
1293 if (!rd)
1294 return -ENOMEM;
1295
1296 rd->name = rapl_domain_names[RAPL_DOMAIN_PLATFORM];
1297 rd->id = RAPL_DOMAIN_PLATFORM;
1298 rd->msrs[0] = MSR_PLATFORM_POWER_LIMIT;
1299 rd->msrs[1] = MSR_PLATFORM_ENERGY_STATUS;
1300 rd->rpl[0].prim_id = PL1_ENABLE;
1301 rd->rpl[0].name = pl1_name;
1302 rd->rpl[1].prim_id = PL2_ENABLE;
1303 rd->rpl[1].name = pl2_name;
Zhang Ruiaadf7b32019-05-13 13:58:50 -04001304 rd->rp = rapl_find_package_domain(0);
Srinivas Pandruvada3521ba12016-04-17 15:03:01 -07001305
1306 power_zone = powercap_register_zone(&rd->power_zone, control_type,
1307 "psys", NULL,
1308 &zone_ops[RAPL_DOMAIN_PLATFORM],
1309 2, &constraint_ops);
1310
1311 if (IS_ERR(power_zone)) {
1312 kfree(rd);
1313 return PTR_ERR(power_zone);
1314 }
1315
1316 platform_rapl_domain = rd;
1317
1318 return 0;
1319}
1320
Thomas Gleixner58705062016-11-22 21:16:02 +00001321static int __init rapl_register_powercap(void)
Jacob Pan2d281d82013-10-17 10:28:35 -07001322{
Jacob Pan2d281d82013-10-17 10:28:35 -07001323 control_type = powercap_register_control_type(NULL, "intel-rapl", NULL);
1324 if (IS_ERR(control_type)) {
1325 pr_debug("failed to register powercap control_type.\n");
1326 return PTR_ERR(control_type);
1327 }
Thomas Gleixner58705062016-11-22 21:16:02 +00001328 return 0;
Jacob Pan2d281d82013-10-17 10:28:35 -07001329}
1330
1331static int rapl_check_domain(int cpu, int domain)
1332{
1333 unsigned msr;
Jacob Pan9d31c672014-04-29 15:33:06 -07001334 u64 val = 0;
Jacob Pan2d281d82013-10-17 10:28:35 -07001335
1336 switch (domain) {
1337 case RAPL_DOMAIN_PACKAGE:
1338 msr = MSR_PKG_ENERGY_STATUS;
1339 break;
1340 case RAPL_DOMAIN_PP0:
1341 msr = MSR_PP0_ENERGY_STATUS;
1342 break;
1343 case RAPL_DOMAIN_PP1:
1344 msr = MSR_PP1_ENERGY_STATUS;
1345 break;
1346 case RAPL_DOMAIN_DRAM:
1347 msr = MSR_DRAM_ENERGY_STATUS;
1348 break;
Srinivas Pandruvada3521ba12016-04-17 15:03:01 -07001349 case RAPL_DOMAIN_PLATFORM:
1350 /* PSYS(PLATFORM) is not a CPU domain, so avoid printng error */
1351 return -EINVAL;
Jacob Pan2d281d82013-10-17 10:28:35 -07001352 default:
1353 pr_err("invalid domain id %d\n", domain);
1354 return -EINVAL;
1355 }
Jacob Pan9d31c672014-04-29 15:33:06 -07001356 /* make sure domain counters are available and contains non-zero
1357 * values, otherwise skip it.
1358 */
1359 if (rdmsrl_safe_on_cpu(cpu, msr, &val) || !val)
Jacob Pan2d281d82013-10-17 10:28:35 -07001360 return -ENODEV;
1361
Jacob Pan9d31c672014-04-29 15:33:06 -07001362 return 0;
Jacob Pan2d281d82013-10-17 10:28:35 -07001363}
1364
Jacob Pane1399ba2016-05-31 13:41:29 -07001365
1366/*
1367 * Check if power limits are available. Two cases when they are not available:
1368 * 1. Locked by BIOS, in this case we still provide read-only access so that
1369 * users can see what limit is set by the BIOS.
1370 * 2. Some CPUs make some domains monitoring only which means PLx MSRs may not
1371 * exist at all. In this case, we do not show the contraints in powercap.
1372 *
1373 * Called after domains are detected and initialized.
1374 */
1375static void rapl_detect_powerlimit(struct rapl_domain *rd)
1376{
1377 u64 val64;
1378 int i;
1379
1380 /* check if the domain is locked by BIOS, ignore if MSR doesn't exist */
1381 if (!rapl_read_data_raw(rd, FW_LOCK, false, &val64)) {
1382 if (val64) {
1383 pr_info("RAPL package %d domain %s locked by BIOS\n",
1384 rd->rp->id, rd->name);
1385 rd->state |= DOMAIN_STATE_BIOS_LOCKED;
1386 }
1387 }
1388 /* check if power limit MSRs exists, otherwise domain is monitoring only */
1389 for (i = 0; i < NR_POWER_LIMITS; i++) {
1390 int prim = rd->rpl[i].prim_id;
1391 if (rapl_read_data_raw(rd, prim, false, &val64))
1392 rd->rpl[i].name = NULL;
1393 }
1394}
1395
Jacob Pan2d281d82013-10-17 10:28:35 -07001396/* Detect active and valid domains for the given CPU, caller must
1397 * ensure the CPU belongs to the targeted package and CPU hotlug is disabled.
1398 */
1399static int rapl_detect_domains(struct rapl_package *rp, int cpu)
1400{
Jacob Pan2d281d82013-10-17 10:28:35 -07001401 struct rapl_domain *rd;
Thomas Gleixner58705062016-11-22 21:16:02 +00001402 int i;
Jacob Pan2d281d82013-10-17 10:28:35 -07001403
1404 for (i = 0; i < RAPL_DOMAIN_MAX; i++) {
1405 /* use physical package id to read counters */
Jacob Panfcdf1792014-09-02 02:55:21 -07001406 if (!rapl_check_domain(cpu, i)) {
Jacob Pan2d281d82013-10-17 10:28:35 -07001407 rp->domain_map |= 1 << i;
Jacob Panfcdf1792014-09-02 02:55:21 -07001408 pr_info("Found RAPL domain %s\n", rapl_domain_names[i]);
1409 }
Jacob Pan2d281d82013-10-17 10:28:35 -07001410 }
1411 rp->nr_domains = bitmap_weight(&rp->domain_map, RAPL_DOMAIN_MAX);
1412 if (!rp->nr_domains) {
Jacob Pane1a27e82016-05-23 09:45:43 -07001413 pr_debug("no valid rapl domains found in package %d\n", rp->id);
Thomas Gleixner58705062016-11-22 21:16:02 +00001414 return -ENODEV;
Jacob Pan2d281d82013-10-17 10:28:35 -07001415 }
1416 pr_debug("found %d domains on package %d\n", rp->nr_domains, rp->id);
1417
1418 rp->domains = kcalloc(rp->nr_domains + 1, sizeof(struct rapl_domain),
1419 GFP_KERNEL);
Thomas Gleixner58705062016-11-22 21:16:02 +00001420 if (!rp->domains)
1421 return -ENOMEM;
1422
Jacob Pan2d281d82013-10-17 10:28:35 -07001423 rapl_init_domains(rp);
1424
Jacob Pane1399ba2016-05-31 13:41:29 -07001425 for (rd = rp->domains; rd < rp->domains + rp->nr_domains; rd++)
1426 rapl_detect_powerlimit(rd);
1427
Jacob Pan2d281d82013-10-17 10:28:35 -07001428 return 0;
1429}
1430
1431/* called from CPU hotplug notifier, hotplug lock held */
1432static void rapl_remove_package(struct rapl_package *rp)
1433{
1434 struct rapl_domain *rd, *rd_package = NULL;
1435
Thomas Gleixner58705062016-11-22 21:16:02 +00001436 package_power_limit_irq_restore(rp);
1437
Jacob Pan2d281d82013-10-17 10:28:35 -07001438 for (rd = rp->domains; rd < rp->domains + rp->nr_domains; rd++) {
Thomas Gleixner58705062016-11-22 21:16:02 +00001439 rapl_write_data_raw(rd, PL1_ENABLE, 0);
1440 rapl_write_data_raw(rd, PL1_CLAMP, 0);
1441 if (find_nr_power_limit(rd) > 1) {
1442 rapl_write_data_raw(rd, PL2_ENABLE, 0);
1443 rapl_write_data_raw(rd, PL2_CLAMP, 0);
1444 }
Jacob Pan2d281d82013-10-17 10:28:35 -07001445 if (rd->id == RAPL_DOMAIN_PACKAGE) {
1446 rd_package = rd;
1447 continue;
1448 }
Thomas Gleixner58705062016-11-22 21:16:02 +00001449 pr_debug("remove package, undo power limit on %d: %s\n",
1450 rp->id, rd->name);
Jacob Pan2d281d82013-10-17 10:28:35 -07001451 powercap_unregister_zone(control_type, &rd->power_zone);
1452 }
1453 /* do parent zone last */
1454 powercap_unregister_zone(control_type, &rd_package->power_zone);
1455 list_del(&rp->plist);
1456 kfree(rp);
1457}
1458
1459/* called from CPU hotplug notifier, hotplug lock held */
Zhang Ruiaadf7b32019-05-13 13:58:50 -04001460static struct rapl_package *rapl_add_package(int cpu)
Jacob Pan2d281d82013-10-17 10:28:35 -07001461{
Zhang Ruiaadf7b32019-05-13 13:58:50 -04001462 int id = topology_physical_package_id(cpu);
Jacob Pan2d281d82013-10-17 10:28:35 -07001463 struct rapl_package *rp;
Thomas Gleixnerb4005e92016-11-22 21:16:05 +00001464 int ret;
Jacob Pan2d281d82013-10-17 10:28:35 -07001465
Jacob Pan2d281d82013-10-17 10:28:35 -07001466 rp = kzalloc(sizeof(struct rapl_package), GFP_KERNEL);
1467 if (!rp)
Thomas Gleixnerb4005e92016-11-22 21:16:05 +00001468 return ERR_PTR(-ENOMEM);
Jacob Pan2d281d82013-10-17 10:28:35 -07001469
1470 /* add the new package to the list */
Zhang Ruiaadf7b32019-05-13 13:58:50 -04001471 rp->id = id;
Jacob Pan323ee642016-02-24 13:31:38 -08001472 rp->lead_cpu = cpu;
1473
Jacob Pan2d281d82013-10-17 10:28:35 -07001474 /* check if the package contains valid domains */
1475 if (rapl_detect_domains(rp, cpu) ||
Jacob Pan3c2c0842014-11-07 09:29:26 -08001476 rapl_defaults->check_unit(rp, cpu)) {
Jacob Pan2d281d82013-10-17 10:28:35 -07001477 ret = -ENODEV;
1478 goto err_free_package;
1479 }
Thomas Gleixnera74f4362016-11-22 21:15:59 +00001480 ret = rapl_package_register_powercap(rp);
1481 if (!ret) {
Jacob Pan2d281d82013-10-17 10:28:35 -07001482 INIT_LIST_HEAD(&rp->plist);
1483 list_add(&rp->plist, &rapl_packages);
Thomas Gleixnerb4005e92016-11-22 21:16:05 +00001484 return rp;
Jacob Pan2d281d82013-10-17 10:28:35 -07001485 }
1486
1487err_free_package:
1488 kfree(rp->domains);
1489 kfree(rp);
Thomas Gleixnerb4005e92016-11-22 21:16:05 +00001490 return ERR_PTR(ret);
Jacob Pan2d281d82013-10-17 10:28:35 -07001491}
1492
1493/* Handles CPU hotplug on multi-socket systems.
1494 * If a CPU goes online as the first CPU of the physical package
1495 * we add the RAPL package to the system. Similarly, when the last
1496 * CPU of the package is removed, we remove the RAPL package and its
1497 * associated domains. Cooling devices are handled accordingly at
1498 * per-domain level.
1499 */
Sebastian Andrzej Siewior5e4dc792016-11-22 21:16:00 +00001500static int rapl_cpu_online(unsigned int cpu)
Jacob Pan2d281d82013-10-17 10:28:35 -07001501{
Sebastian Andrzej Siewior5e4dc792016-11-22 21:16:00 +00001502 struct rapl_package *rp;
Sebastian Andrzej Siewior5e4dc792016-11-22 21:16:00 +00001503
Zhang Ruiaadf7b32019-05-13 13:58:50 -04001504 rp = rapl_find_package_domain(cpu);
Thomas Gleixnerb4005e92016-11-22 21:16:05 +00001505 if (!rp) {
Zhang Ruiaadf7b32019-05-13 13:58:50 -04001506 rp = rapl_add_package(cpu);
Thomas Gleixnerb4005e92016-11-22 21:16:05 +00001507 if (IS_ERR(rp))
1508 return PTR_ERR(rp);
Thomas Gleixner58705062016-11-22 21:16:02 +00001509 }
Thomas Gleixnerb4005e92016-11-22 21:16:05 +00001510 cpumask_set_cpu(cpu, &rp->cpumask);
1511 return 0;
Sebastian Andrzej Siewior5e4dc792016-11-22 21:16:00 +00001512}
1513
1514static int rapl_cpu_down_prep(unsigned int cpu)
1515{
Jacob Pan2d281d82013-10-17 10:28:35 -07001516 struct rapl_package *rp;
Jacob Pan323ee642016-02-24 13:31:38 -08001517 int lead_cpu;
Jacob Pan2d281d82013-10-17 10:28:35 -07001518
Zhang Ruiaadf7b32019-05-13 13:58:50 -04001519 rp = rapl_find_package_domain(cpu);
Sebastian Andrzej Siewior5e4dc792016-11-22 21:16:00 +00001520 if (!rp)
1521 return 0;
Thomas Gleixnerb4005e92016-11-22 21:16:05 +00001522
1523 cpumask_clear_cpu(cpu, &rp->cpumask);
1524 lead_cpu = cpumask_first(&rp->cpumask);
1525 if (lead_cpu >= nr_cpu_ids)
Sebastian Andrzej Siewior5e4dc792016-11-22 21:16:00 +00001526 rapl_remove_package(rp);
Thomas Gleixnerb4005e92016-11-22 21:16:05 +00001527 else if (rp->lead_cpu == cpu)
1528 rp->lead_cpu = lead_cpu;
Sebastian Andrzej Siewior5e4dc792016-11-22 21:16:00 +00001529 return 0;
Jacob Pan2d281d82013-10-17 10:28:35 -07001530}
1531
Sebastian Andrzej Siewior5e4dc792016-11-22 21:16:00 +00001532static enum cpuhp_state pcap_rapl_online;
Jacob Pan2d281d82013-10-17 10:28:35 -07001533
Zhen Han52b36722018-01-10 08:38:23 +08001534static void power_limit_state_save(void)
1535{
1536 struct rapl_package *rp;
1537 struct rapl_domain *rd;
1538 int nr_pl, ret, i;
1539
1540 get_online_cpus();
1541 list_for_each_entry(rp, &rapl_packages, plist) {
1542 if (!rp->power_zone)
1543 continue;
1544 rd = power_zone_to_rapl_domain(rp->power_zone);
1545 nr_pl = find_nr_power_limit(rd);
1546 for (i = 0; i < nr_pl; i++) {
1547 switch (rd->rpl[i].prim_id) {
1548 case PL1_ENABLE:
1549 ret = rapl_read_data_raw(rd,
1550 POWER_LIMIT1,
1551 true,
1552 &rd->rpl[i].last_power_limit);
1553 if (ret)
1554 rd->rpl[i].last_power_limit = 0;
1555 break;
1556 case PL2_ENABLE:
1557 ret = rapl_read_data_raw(rd,
1558 POWER_LIMIT2,
1559 true,
1560 &rd->rpl[i].last_power_limit);
1561 if (ret)
1562 rd->rpl[i].last_power_limit = 0;
1563 break;
1564 }
1565 }
1566 }
1567 put_online_cpus();
1568}
1569
1570static void power_limit_state_restore(void)
1571{
1572 struct rapl_package *rp;
1573 struct rapl_domain *rd;
1574 int nr_pl, i;
1575
1576 get_online_cpus();
1577 list_for_each_entry(rp, &rapl_packages, plist) {
1578 if (!rp->power_zone)
1579 continue;
1580 rd = power_zone_to_rapl_domain(rp->power_zone);
1581 nr_pl = find_nr_power_limit(rd);
1582 for (i = 0; i < nr_pl; i++) {
1583 switch (rd->rpl[i].prim_id) {
1584 case PL1_ENABLE:
1585 if (rd->rpl[i].last_power_limit)
1586 rapl_write_data_raw(rd,
1587 POWER_LIMIT1,
1588 rd->rpl[i].last_power_limit);
1589 break;
1590 case PL2_ENABLE:
1591 if (rd->rpl[i].last_power_limit)
1592 rapl_write_data_raw(rd,
1593 POWER_LIMIT2,
1594 rd->rpl[i].last_power_limit);
1595 break;
1596 }
1597 }
1598 }
1599 put_online_cpus();
1600}
1601
1602static int rapl_pm_callback(struct notifier_block *nb,
1603 unsigned long mode, void *_unused)
1604{
1605 switch (mode) {
1606 case PM_SUSPEND_PREPARE:
1607 power_limit_state_save();
1608 break;
1609 case PM_POST_SUSPEND:
1610 power_limit_state_restore();
1611 break;
1612 }
1613 return NOTIFY_OK;
1614}
1615
1616static struct notifier_block rapl_pm_notifier = {
1617 .notifier_call = rapl_pm_callback,
1618};
1619
Jacob Pan2d281d82013-10-17 10:28:35 -07001620static int __init rapl_init(void)
1621{
Jacob Pan087e9cb2014-11-07 09:29:25 -08001622 const struct x86_cpu_id *id;
Thomas Gleixner58705062016-11-22 21:16:02 +00001623 int ret;
Jacob Pan2d281d82013-10-17 10:28:35 -07001624
Jacob Pan087e9cb2014-11-07 09:29:25 -08001625 id = x86_match_cpu(rapl_ids);
1626 if (!id) {
Jacob Pan2d281d82013-10-17 10:28:35 -07001627 pr_err("driver does not support CPU family %d model %d\n",
1628 boot_cpu_data.x86, boot_cpu_data.x86_model);
1629
1630 return -ENODEV;
1631 }
Srivatsa S. Bhat009f2252014-03-11 02:09:26 +05301632
Jacob Pan087e9cb2014-11-07 09:29:25 -08001633 rapl_defaults = (struct rapl_defaults *)id->driver_data;
1634
Thomas Gleixner58705062016-11-22 21:16:02 +00001635 ret = rapl_register_powercap();
Jacob Pan2d281d82013-10-17 10:28:35 -07001636 if (ret)
Thomas Gleixner58705062016-11-22 21:16:02 +00001637 return ret;
Jacob Pan2d281d82013-10-17 10:28:35 -07001638
Thomas Gleixner58705062016-11-22 21:16:02 +00001639 ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "powercap/rapl:online",
1640 rapl_cpu_online, rapl_cpu_down_prep);
Sebastian Andrzej Siewior5e4dc792016-11-22 21:16:00 +00001641 if (ret < 0)
1642 goto err_unreg;
1643 pcap_rapl_online = ret;
Thomas Gleixner58705062016-11-22 21:16:02 +00001644
1645 /* Don't bail out if PSys is not supported */
1646 rapl_register_psys();
Zhen Han52b36722018-01-10 08:38:23 +08001647
1648 ret = register_pm_notifier(&rapl_pm_notifier);
1649 if (ret)
1650 goto err_unreg_all;
1651
Sebastian Andrzej Siewior5e4dc792016-11-22 21:16:00 +00001652 return 0;
Jacob Pan2d281d82013-10-17 10:28:35 -07001653
Zhen Han52b36722018-01-10 08:38:23 +08001654err_unreg_all:
1655 cpuhp_remove_state(pcap_rapl_online);
1656
Sebastian Andrzej Siewior5e4dc792016-11-22 21:16:00 +00001657err_unreg:
1658 rapl_unregister_powercap();
Jacob Pan2d281d82013-10-17 10:28:35 -07001659 return ret;
1660}
1661
1662static void __exit rapl_exit(void)
1663{
Zhen Han52b36722018-01-10 08:38:23 +08001664 unregister_pm_notifier(&rapl_pm_notifier);
Sebastian Andrzej Siewior5e4dc792016-11-22 21:16:00 +00001665 cpuhp_remove_state(pcap_rapl_online);
Jacob Pan2d281d82013-10-17 10:28:35 -07001666 rapl_unregister_powercap();
Jacob Pan2d281d82013-10-17 10:28:35 -07001667}
1668
1669module_init(rapl_init);
1670module_exit(rapl_exit);
1671
1672MODULE_DESCRIPTION("Driver for Intel RAPL (Running Average Power Limit)");
1673MODULE_AUTHOR("Jacob Pan <jacob.jun.pan@intel.com>");
1674MODULE_LICENSE("GPL v2");