blob: 0188cff98cdd160b2cd46aec33c4986841cc3fd6 [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 */
267static struct rapl_package *find_package_by_id(int id)
268{
269 struct rapl_package *rp;
270
271 list_for_each_entry(rp, &rapl_packages, plist) {
272 if (rp->id == id)
273 return rp;
274 }
275
276 return NULL;
277}
278
Jacob Pan2d281d82013-10-17 10:28:35 -0700279static int get_energy_counter(struct powercap_zone *power_zone, u64 *energy_raw)
280{
281 struct rapl_domain *rd;
282 u64 energy_now;
283
284 /* prevent CPU hotplug, make sure the RAPL domain does not go
285 * away while reading the counter.
286 */
287 get_online_cpus();
288 rd = power_zone_to_rapl_domain(power_zone);
289
290 if (!rapl_read_data_raw(rd, ENERGY_COUNTER, true, &energy_now)) {
291 *energy_raw = energy_now;
292 put_online_cpus();
293
294 return 0;
295 }
296 put_online_cpus();
297
298 return -EIO;
299}
300
301static int get_max_energy_counter(struct powercap_zone *pcd_dev, u64 *energy)
302{
Jacob Pand474a4d2015-03-13 03:48:56 -0700303 struct rapl_domain *rd = power_zone_to_rapl_domain(pcd_dev);
304
Jacob Pan309557f2016-02-24 13:31:37 -0800305 *energy = rapl_unit_xlate(rd, ENERGY_UNIT, ENERGY_STATUS_MASK, 0);
Jacob Pan2d281d82013-10-17 10:28:35 -0700306 return 0;
307}
308
309static int release_zone(struct powercap_zone *power_zone)
310{
311 struct rapl_domain *rd = power_zone_to_rapl_domain(power_zone);
Jacob Pan309557f2016-02-24 13:31:37 -0800312 struct rapl_package *rp = rd->rp;
Jacob Pan2d281d82013-10-17 10:28:35 -0700313
314 /* package zone is the last zone of a package, we can free
315 * memory here since all children has been unregistered.
316 */
317 if (rd->id == RAPL_DOMAIN_PACKAGE) {
Jacob Pan2d281d82013-10-17 10:28:35 -0700318 kfree(rd);
319 rp->domains = NULL;
320 }
321
322 return 0;
323
324}
325
326static int find_nr_power_limit(struct rapl_domain *rd)
327{
Jacob Pane1399ba2016-05-31 13:41:29 -0700328 int i, nr_pl = 0;
Jacob Pan2d281d82013-10-17 10:28:35 -0700329
330 for (i = 0; i < NR_POWER_LIMITS; i++) {
Jacob Pane1399ba2016-05-31 13:41:29 -0700331 if (rd->rpl[i].name)
332 nr_pl++;
Jacob Pan2d281d82013-10-17 10:28:35 -0700333 }
334
Jacob Pane1399ba2016-05-31 13:41:29 -0700335 return nr_pl;
Jacob Pan2d281d82013-10-17 10:28:35 -0700336}
337
338static int set_domain_enable(struct powercap_zone *power_zone, bool mode)
339{
340 struct rapl_domain *rd = power_zone_to_rapl_domain(power_zone);
Jacob Pan2d281d82013-10-17 10:28:35 -0700341
342 if (rd->state & DOMAIN_STATE_BIOS_LOCKED)
343 return -EACCES;
Jacob Pan3c2c0842014-11-07 09:29:26 -0800344
Jacob Pan2d281d82013-10-17 10:28:35 -0700345 get_online_cpus();
Jacob Pan2d281d82013-10-17 10:28:35 -0700346 rapl_write_data_raw(rd, PL1_ENABLE, mode);
Ajay Thomas51b63402015-04-30 01:43:23 +0530347 if (rapl_defaults->set_floor_freq)
348 rapl_defaults->set_floor_freq(rd, mode);
Jacob Pan2d281d82013-10-17 10:28:35 -0700349 put_online_cpus();
350
351 return 0;
352}
353
354static int get_domain_enable(struct powercap_zone *power_zone, bool *mode)
355{
356 struct rapl_domain *rd = power_zone_to_rapl_domain(power_zone);
357 u64 val;
358
359 if (rd->state & DOMAIN_STATE_BIOS_LOCKED) {
360 *mode = false;
361 return 0;
362 }
363 get_online_cpus();
364 if (rapl_read_data_raw(rd, PL1_ENABLE, true, &val)) {
365 put_online_cpus();
366 return -EIO;
367 }
368 *mode = val;
369 put_online_cpus();
370
371 return 0;
372}
373
374/* per RAPL domain ops, in the order of rapl_domain_type */
Julia Lawall600c3952015-12-23 22:59:55 +0100375static const struct powercap_zone_ops zone_ops[] = {
Jacob Pan2d281d82013-10-17 10:28:35 -0700376 /* RAPL_DOMAIN_PACKAGE */
377 {
378 .get_energy_uj = get_energy_counter,
379 .get_max_energy_range_uj = get_max_energy_counter,
380 .release = release_zone,
381 .set_enable = set_domain_enable,
382 .get_enable = get_domain_enable,
383 },
384 /* RAPL_DOMAIN_PP0 */
385 {
386 .get_energy_uj = get_energy_counter,
387 .get_max_energy_range_uj = get_max_energy_counter,
388 .release = release_zone,
389 .set_enable = set_domain_enable,
390 .get_enable = get_domain_enable,
391 },
392 /* RAPL_DOMAIN_PP1 */
393 {
394 .get_energy_uj = get_energy_counter,
395 .get_max_energy_range_uj = get_max_energy_counter,
396 .release = release_zone,
397 .set_enable = set_domain_enable,
398 .get_enable = get_domain_enable,
399 },
400 /* RAPL_DOMAIN_DRAM */
401 {
402 .get_energy_uj = get_energy_counter,
403 .get_max_energy_range_uj = get_max_energy_counter,
404 .release = release_zone,
405 .set_enable = set_domain_enable,
406 .get_enable = get_domain_enable,
407 },
Srinivas Pandruvada3521ba12016-04-17 15:03:01 -0700408 /* RAPL_DOMAIN_PLATFORM */
409 {
410 .get_energy_uj = get_energy_counter,
411 .get_max_energy_range_uj = get_max_energy_counter,
412 .release = release_zone,
413 .set_enable = set_domain_enable,
414 .get_enable = get_domain_enable,
415 },
Jacob Pan2d281d82013-10-17 10:28:35 -0700416};
417
Jacob Pane1399ba2016-05-31 13:41:29 -0700418
419/*
420 * Constraint index used by powercap can be different than power limit (PL)
421 * index in that some PLs maybe missing due to non-existant MSRs. So we
422 * need to convert here by finding the valid PLs only (name populated).
423 */
424static int contraint_to_pl(struct rapl_domain *rd, int cid)
425{
426 int i, j;
427
428 for (i = 0, j = 0; i < NR_POWER_LIMITS; i++) {
429 if ((rd->rpl[i].name) && j++ == cid) {
430 pr_debug("%s: index %d\n", __func__, i);
431 return i;
432 }
433 }
Jacob Pancb43f812016-11-28 13:53:11 -0800434 pr_err("Cannot find matching power limit for constraint %d\n", cid);
Jacob Pane1399ba2016-05-31 13:41:29 -0700435
436 return -EINVAL;
437}
438
439static int set_power_limit(struct powercap_zone *power_zone, int cid,
Jacob Pan2d281d82013-10-17 10:28:35 -0700440 u64 power_limit)
441{
442 struct rapl_domain *rd;
443 struct rapl_package *rp;
444 int ret = 0;
Jacob Pane1399ba2016-05-31 13:41:29 -0700445 int id;
Jacob Pan2d281d82013-10-17 10:28:35 -0700446
447 get_online_cpus();
448 rd = power_zone_to_rapl_domain(power_zone);
Jacob Pane1399ba2016-05-31 13:41:29 -0700449 id = contraint_to_pl(rd, cid);
Jacob Pancb43f812016-11-28 13:53:11 -0800450 if (id < 0) {
451 ret = id;
452 goto set_exit;
453 }
Jacob Pane1399ba2016-05-31 13:41:29 -0700454
Jacob Pan309557f2016-02-24 13:31:37 -0800455 rp = rd->rp;
Jacob Pan2d281d82013-10-17 10:28:35 -0700456
457 if (rd->state & DOMAIN_STATE_BIOS_LOCKED) {
458 dev_warn(&power_zone->dev, "%s locked by BIOS, monitoring only\n",
459 rd->name);
460 ret = -EACCES;
461 goto set_exit;
462 }
463
464 switch (rd->rpl[id].prim_id) {
465 case PL1_ENABLE:
466 rapl_write_data_raw(rd, POWER_LIMIT1, power_limit);
467 break;
468 case PL2_ENABLE:
469 rapl_write_data_raw(rd, POWER_LIMIT2, power_limit);
470 break;
471 default:
472 ret = -EINVAL;
473 }
474 if (!ret)
Jacob Pan309557f2016-02-24 13:31:37 -0800475 package_power_limit_irq_save(rp);
Jacob Pan2d281d82013-10-17 10:28:35 -0700476set_exit:
477 put_online_cpus();
478 return ret;
479}
480
Jacob Pane1399ba2016-05-31 13:41:29 -0700481static int get_current_power_limit(struct powercap_zone *power_zone, int cid,
Jacob Pan2d281d82013-10-17 10:28:35 -0700482 u64 *data)
483{
484 struct rapl_domain *rd;
485 u64 val;
486 int prim;
487 int ret = 0;
Jacob Pane1399ba2016-05-31 13:41:29 -0700488 int id;
Jacob Pan2d281d82013-10-17 10:28:35 -0700489
490 get_online_cpus();
491 rd = power_zone_to_rapl_domain(power_zone);
Jacob Pane1399ba2016-05-31 13:41:29 -0700492 id = contraint_to_pl(rd, cid);
Jacob Pancb43f812016-11-28 13:53:11 -0800493 if (id < 0) {
494 ret = id;
495 goto get_exit;
496 }
497
Jacob Pan2d281d82013-10-17 10:28:35 -0700498 switch (rd->rpl[id].prim_id) {
499 case PL1_ENABLE:
500 prim = POWER_LIMIT1;
501 break;
502 case PL2_ENABLE:
503 prim = POWER_LIMIT2;
504 break;
505 default:
506 put_online_cpus();
507 return -EINVAL;
508 }
509 if (rapl_read_data_raw(rd, prim, true, &val))
510 ret = -EIO;
511 else
512 *data = val;
513
Jacob Pancb43f812016-11-28 13:53:11 -0800514get_exit:
Jacob Pan2d281d82013-10-17 10:28:35 -0700515 put_online_cpus();
516
517 return ret;
518}
519
Jacob Pane1399ba2016-05-31 13:41:29 -0700520static int set_time_window(struct powercap_zone *power_zone, int cid,
Jacob Pan2d281d82013-10-17 10:28:35 -0700521 u64 window)
522{
523 struct rapl_domain *rd;
524 int ret = 0;
Jacob Pane1399ba2016-05-31 13:41:29 -0700525 int id;
Jacob Pan2d281d82013-10-17 10:28:35 -0700526
527 get_online_cpus();
528 rd = power_zone_to_rapl_domain(power_zone);
Jacob Pane1399ba2016-05-31 13:41:29 -0700529 id = contraint_to_pl(rd, cid);
Jacob Pancb43f812016-11-28 13:53:11 -0800530 if (id < 0) {
531 ret = id;
532 goto set_time_exit;
533 }
Jacob Pane1399ba2016-05-31 13:41:29 -0700534
Jacob Pan2d281d82013-10-17 10:28:35 -0700535 switch (rd->rpl[id].prim_id) {
536 case PL1_ENABLE:
537 rapl_write_data_raw(rd, TIME_WINDOW1, window);
538 break;
539 case PL2_ENABLE:
540 rapl_write_data_raw(rd, TIME_WINDOW2, window);
541 break;
542 default:
543 ret = -EINVAL;
544 }
Jacob Pancb43f812016-11-28 13:53:11 -0800545
546set_time_exit:
Jacob Pan2d281d82013-10-17 10:28:35 -0700547 put_online_cpus();
548 return ret;
549}
550
Jacob Pane1399ba2016-05-31 13:41:29 -0700551static int get_time_window(struct powercap_zone *power_zone, int cid, u64 *data)
Jacob Pan2d281d82013-10-17 10:28:35 -0700552{
553 struct rapl_domain *rd;
554 u64 val;
555 int ret = 0;
Jacob Pane1399ba2016-05-31 13:41:29 -0700556 int id;
Jacob Pan2d281d82013-10-17 10:28:35 -0700557
558 get_online_cpus();
559 rd = power_zone_to_rapl_domain(power_zone);
Jacob Pane1399ba2016-05-31 13:41:29 -0700560 id = contraint_to_pl(rd, cid);
Jacob Pancb43f812016-11-28 13:53:11 -0800561 if (id < 0) {
562 ret = id;
563 goto get_time_exit;
564 }
Jacob Pane1399ba2016-05-31 13:41:29 -0700565
Jacob Pan2d281d82013-10-17 10:28:35 -0700566 switch (rd->rpl[id].prim_id) {
567 case PL1_ENABLE:
568 ret = rapl_read_data_raw(rd, TIME_WINDOW1, true, &val);
569 break;
570 case PL2_ENABLE:
571 ret = rapl_read_data_raw(rd, TIME_WINDOW2, true, &val);
572 break;
573 default:
574 put_online_cpus();
575 return -EINVAL;
576 }
577 if (!ret)
578 *data = val;
Jacob Pancb43f812016-11-28 13:53:11 -0800579
580get_time_exit:
Jacob Pan2d281d82013-10-17 10:28:35 -0700581 put_online_cpus();
582
583 return ret;
584}
585
Jacob Pane1399ba2016-05-31 13:41:29 -0700586static const char *get_constraint_name(struct powercap_zone *power_zone, int cid)
Jacob Pan2d281d82013-10-17 10:28:35 -0700587{
Jacob Pan2d281d82013-10-17 10:28:35 -0700588 struct rapl_domain *rd;
Jacob Pane1399ba2016-05-31 13:41:29 -0700589 int id;
Jacob Pan2d281d82013-10-17 10:28:35 -0700590
591 rd = power_zone_to_rapl_domain(power_zone);
Jacob Pane1399ba2016-05-31 13:41:29 -0700592 id = contraint_to_pl(rd, cid);
593 if (id >= 0)
594 return rd->rpl[id].name;
Jacob Pan2d281d82013-10-17 10:28:35 -0700595
Jacob Pane1399ba2016-05-31 13:41:29 -0700596 return NULL;
Jacob Pan2d281d82013-10-17 10:28:35 -0700597}
598
599
600static int get_max_power(struct powercap_zone *power_zone, int id,
601 u64 *data)
602{
603 struct rapl_domain *rd;
604 u64 val;
605 int prim;
606 int ret = 0;
607
608 get_online_cpus();
609 rd = power_zone_to_rapl_domain(power_zone);
610 switch (rd->rpl[id].prim_id) {
611 case PL1_ENABLE:
612 prim = THERMAL_SPEC_POWER;
613 break;
614 case PL2_ENABLE:
615 prim = MAX_POWER;
616 break;
617 default:
618 put_online_cpus();
619 return -EINVAL;
620 }
621 if (rapl_read_data_raw(rd, prim, true, &val))
622 ret = -EIO;
623 else
624 *data = val;
625
626 put_online_cpus();
627
628 return ret;
629}
630
Julia Lawall600c3952015-12-23 22:59:55 +0100631static const struct powercap_zone_constraint_ops constraint_ops = {
Jacob Pan2d281d82013-10-17 10:28:35 -0700632 .set_power_limit_uw = set_power_limit,
633 .get_power_limit_uw = get_current_power_limit,
634 .set_time_window_us = set_time_window,
635 .get_time_window_us = get_time_window,
636 .get_max_power_uw = get_max_power,
637 .get_name = get_constraint_name,
638};
639
640/* called after domain detection and package level data are set */
641static void rapl_init_domains(struct rapl_package *rp)
642{
643 int i;
644 struct rapl_domain *rd = rp->domains;
645
646 for (i = 0; i < RAPL_DOMAIN_MAX; i++) {
647 unsigned int mask = rp->domain_map & (1 << i);
648 switch (mask) {
649 case BIT(RAPL_DOMAIN_PACKAGE):
650 rd->name = rapl_domain_names[RAPL_DOMAIN_PACKAGE];
651 rd->id = RAPL_DOMAIN_PACKAGE;
652 rd->msrs[0] = MSR_PKG_POWER_LIMIT;
653 rd->msrs[1] = MSR_PKG_ENERGY_STATUS;
654 rd->msrs[2] = MSR_PKG_PERF_STATUS;
655 rd->msrs[3] = 0;
656 rd->msrs[4] = MSR_PKG_POWER_INFO;
657 rd->rpl[0].prim_id = PL1_ENABLE;
658 rd->rpl[0].name = pl1_name;
659 rd->rpl[1].prim_id = PL2_ENABLE;
660 rd->rpl[1].name = pl2_name;
661 break;
662 case BIT(RAPL_DOMAIN_PP0):
663 rd->name = rapl_domain_names[RAPL_DOMAIN_PP0];
664 rd->id = RAPL_DOMAIN_PP0;
665 rd->msrs[0] = MSR_PP0_POWER_LIMIT;
666 rd->msrs[1] = MSR_PP0_ENERGY_STATUS;
667 rd->msrs[2] = 0;
668 rd->msrs[3] = MSR_PP0_POLICY;
669 rd->msrs[4] = 0;
670 rd->rpl[0].prim_id = PL1_ENABLE;
671 rd->rpl[0].name = pl1_name;
672 break;
673 case BIT(RAPL_DOMAIN_PP1):
674 rd->name = rapl_domain_names[RAPL_DOMAIN_PP1];
675 rd->id = RAPL_DOMAIN_PP1;
676 rd->msrs[0] = MSR_PP1_POWER_LIMIT;
677 rd->msrs[1] = MSR_PP1_ENERGY_STATUS;
678 rd->msrs[2] = 0;
679 rd->msrs[3] = MSR_PP1_POLICY;
680 rd->msrs[4] = 0;
681 rd->rpl[0].prim_id = PL1_ENABLE;
682 rd->rpl[0].name = pl1_name;
683 break;
684 case BIT(RAPL_DOMAIN_DRAM):
685 rd->name = rapl_domain_names[RAPL_DOMAIN_DRAM];
686 rd->id = RAPL_DOMAIN_DRAM;
687 rd->msrs[0] = MSR_DRAM_POWER_LIMIT;
688 rd->msrs[1] = MSR_DRAM_ENERGY_STATUS;
689 rd->msrs[2] = MSR_DRAM_PERF_STATUS;
690 rd->msrs[3] = 0;
691 rd->msrs[4] = MSR_DRAM_POWER_INFO;
692 rd->rpl[0].prim_id = PL1_ENABLE;
693 rd->rpl[0].name = pl1_name;
Jacob Pand474a4d2015-03-13 03:48:56 -0700694 rd->domain_energy_unit =
695 rapl_defaults->dram_domain_energy_unit;
696 if (rd->domain_energy_unit)
697 pr_info("DRAM domain energy unit %dpj\n",
698 rd->domain_energy_unit);
Jacob Pan2d281d82013-10-17 10:28:35 -0700699 break;
700 }
701 if (mask) {
Jacob Pan309557f2016-02-24 13:31:37 -0800702 rd->rp = rp;
Jacob Pan2d281d82013-10-17 10:28:35 -0700703 rd++;
704 }
705 }
706}
707
Jacob Pan309557f2016-02-24 13:31:37 -0800708static u64 rapl_unit_xlate(struct rapl_domain *rd, enum unit_type type,
709 u64 value, int to_raw)
Jacob Pan2d281d82013-10-17 10:28:35 -0700710{
Jacob Pan3c2c0842014-11-07 09:29:26 -0800711 u64 units = 1;
Jacob Pan309557f2016-02-24 13:31:37 -0800712 struct rapl_package *rp = rd->rp;
Jacob Pand474a4d2015-03-13 03:48:56 -0700713 u64 scale = 1;
Jacob Pan2d281d82013-10-17 10:28:35 -0700714
Jacob Pan2d281d82013-10-17 10:28:35 -0700715 switch (type) {
716 case POWER_UNIT:
Jacob Pan3c2c0842014-11-07 09:29:26 -0800717 units = rp->power_unit;
Jacob Pan2d281d82013-10-17 10:28:35 -0700718 break;
719 case ENERGY_UNIT:
Jacob Pand474a4d2015-03-13 03:48:56 -0700720 scale = ENERGY_UNIT_SCALE;
721 /* per domain unit takes precedence */
Jacob Pancb43f812016-11-28 13:53:11 -0800722 if (rd->domain_energy_unit)
Jacob Pand474a4d2015-03-13 03:48:56 -0700723 units = rd->domain_energy_unit;
724 else
725 units = rp->energy_unit;
Jacob Pan2d281d82013-10-17 10:28:35 -0700726 break;
727 case TIME_UNIT:
Jacob Pan3c2c0842014-11-07 09:29:26 -0800728 return rapl_defaults->compute_time_window(rp, value, to_raw);
Jacob Pan2d281d82013-10-17 10:28:35 -0700729 case ARBITRARY_UNIT:
730 default:
731 return value;
732 };
733
734 if (to_raw)
Jacob Pand474a4d2015-03-13 03:48:56 -0700735 return div64_u64(value, units) * scale;
Jacob Pan3c2c0842014-11-07 09:29:26 -0800736
737 value *= units;
738
Jacob Pand474a4d2015-03-13 03:48:56 -0700739 return div64_u64(value, scale);
Jacob Pan2d281d82013-10-17 10:28:35 -0700740}
741
742/* in the order of enum rapl_primitives */
743static struct rapl_primitive_info rpi[] = {
744 /* name, mask, shift, msr index, unit divisor */
745 PRIMITIVE_INFO_INIT(ENERGY_COUNTER, ENERGY_STATUS_MASK, 0,
746 RAPL_DOMAIN_MSR_STATUS, ENERGY_UNIT, 0),
747 PRIMITIVE_INFO_INIT(POWER_LIMIT1, POWER_LIMIT1_MASK, 0,
748 RAPL_DOMAIN_MSR_LIMIT, POWER_UNIT, 0),
749 PRIMITIVE_INFO_INIT(POWER_LIMIT2, POWER_LIMIT2_MASK, 32,
750 RAPL_DOMAIN_MSR_LIMIT, POWER_UNIT, 0),
751 PRIMITIVE_INFO_INIT(FW_LOCK, POWER_PP_LOCK, 31,
752 RAPL_DOMAIN_MSR_LIMIT, ARBITRARY_UNIT, 0),
753 PRIMITIVE_INFO_INIT(PL1_ENABLE, POWER_LIMIT1_ENABLE, 15,
754 RAPL_DOMAIN_MSR_LIMIT, ARBITRARY_UNIT, 0),
755 PRIMITIVE_INFO_INIT(PL1_CLAMP, POWER_LIMIT1_CLAMP, 16,
756 RAPL_DOMAIN_MSR_LIMIT, ARBITRARY_UNIT, 0),
757 PRIMITIVE_INFO_INIT(PL2_ENABLE, POWER_LIMIT2_ENABLE, 47,
758 RAPL_DOMAIN_MSR_LIMIT, ARBITRARY_UNIT, 0),
759 PRIMITIVE_INFO_INIT(PL2_CLAMP, POWER_LIMIT2_CLAMP, 48,
760 RAPL_DOMAIN_MSR_LIMIT, ARBITRARY_UNIT, 0),
761 PRIMITIVE_INFO_INIT(TIME_WINDOW1, TIME_WINDOW1_MASK, 17,
762 RAPL_DOMAIN_MSR_LIMIT, TIME_UNIT, 0),
763 PRIMITIVE_INFO_INIT(TIME_WINDOW2, TIME_WINDOW2_MASK, 49,
764 RAPL_DOMAIN_MSR_LIMIT, TIME_UNIT, 0),
765 PRIMITIVE_INFO_INIT(THERMAL_SPEC_POWER, POWER_INFO_THERMAL_SPEC_MASK,
766 0, RAPL_DOMAIN_MSR_INFO, POWER_UNIT, 0),
767 PRIMITIVE_INFO_INIT(MAX_POWER, POWER_INFO_MAX_MASK, 32,
768 RAPL_DOMAIN_MSR_INFO, POWER_UNIT, 0),
769 PRIMITIVE_INFO_INIT(MIN_POWER, POWER_INFO_MIN_MASK, 16,
770 RAPL_DOMAIN_MSR_INFO, POWER_UNIT, 0),
771 PRIMITIVE_INFO_INIT(MAX_TIME_WINDOW, POWER_INFO_MAX_TIME_WIN_MASK, 48,
772 RAPL_DOMAIN_MSR_INFO, TIME_UNIT, 0),
773 PRIMITIVE_INFO_INIT(THROTTLED_TIME, PERF_STATUS_THROTTLE_TIME_MASK, 0,
774 RAPL_DOMAIN_MSR_PERF, TIME_UNIT, 0),
775 PRIMITIVE_INFO_INIT(PRIORITY_LEVEL, PP_POLICY_MASK, 0,
776 RAPL_DOMAIN_MSR_POLICY, ARBITRARY_UNIT, 0),
777 /* non-hardware */
778 PRIMITIVE_INFO_INIT(AVERAGE_POWER, 0, 0, 0, POWER_UNIT,
779 RAPL_PRIMITIVE_DERIVED),
780 {NULL, 0, 0, 0},
781};
782
783/* Read primitive data based on its related struct rapl_primitive_info.
784 * if xlate flag is set, return translated data based on data units, i.e.
785 * time, energy, and power.
786 * RAPL MSRs are non-architectual and are laid out not consistently across
787 * domains. Here we use primitive info to allow writing consolidated access
788 * functions.
789 * For a given primitive, it is processed by MSR mask and shift. Unit conversion
790 * is pre-assigned based on RAPL unit MSRs read at init time.
791 * 63-------------------------- 31--------------------------- 0
792 * | xxxxx (mask) |
793 * | |<- shift ----------------|
794 * 63-------------------------- 31--------------------------- 0
795 */
796static int rapl_read_data_raw(struct rapl_domain *rd,
797 enum rapl_primitives prim,
798 bool xlate, u64 *data)
799{
800 u64 value, final;
801 u32 msr;
802 struct rapl_primitive_info *rp = &rpi[prim];
803 int cpu;
804
805 if (!rp->name || rp->flag & RAPL_PRIMITIVE_DUMMY)
806 return -EINVAL;
807
808 msr = rd->msrs[rp->id];
809 if (!msr)
810 return -EINVAL;
Jacob Pan323ee642016-02-24 13:31:38 -0800811
812 cpu = rd->rp->lead_cpu;
Jacob Pan2d281d82013-10-17 10:28:35 -0700813
814 /* special-case package domain, which uses a different bit*/
815 if (prim == FW_LOCK && rd->id == RAPL_DOMAIN_PACKAGE) {
816 rp->mask = POWER_PACKAGE_LOCK;
817 rp->shift = 63;
818 }
819 /* non-hardware data are collected by the polling thread */
820 if (rp->flag & RAPL_PRIMITIVE_DERIVED) {
821 *data = rd->rdd.primitives[prim];
822 return 0;
823 }
824
825 if (rdmsrl_safe_on_cpu(cpu, msr, &value)) {
826 pr_debug("failed to read msr 0x%x on cpu %d\n", msr, cpu);
827 return -EIO;
828 }
829
830 final = value & rp->mask;
831 final = final >> rp->shift;
832 if (xlate)
Jacob Pan309557f2016-02-24 13:31:37 -0800833 *data = rapl_unit_xlate(rd, rp->unit, final, 0);
Jacob Pan2d281d82013-10-17 10:28:35 -0700834 else
835 *data = final;
836
837 return 0;
838}
839
Jacob Panf14a1392016-02-24 13:31:36 -0800840
841static int msrl_update_safe(u32 msr_no, u64 clear_mask, u64 set_mask)
842{
843 int err;
844 u64 val;
845
846 err = rdmsrl_safe(msr_no, &val);
847 if (err)
848 goto out;
849
850 val &= ~clear_mask;
851 val |= set_mask;
852
853 err = wrmsrl_safe(msr_no, val);
854
855out:
856 return err;
857}
858
859static void msrl_update_func(void *info)
860{
861 struct msrl_action *ma = info;
862
863 ma->err = msrl_update_safe(ma->msr_no, ma->clear_mask, ma->set_mask);
864}
865
Jacob Pan2d281d82013-10-17 10:28:35 -0700866/* Similar use of primitive info in the read counterpart */
867static int rapl_write_data_raw(struct rapl_domain *rd,
868 enum rapl_primitives prim,
869 unsigned long long value)
870{
Jacob Pan2d281d82013-10-17 10:28:35 -0700871 struct rapl_primitive_info *rp = &rpi[prim];
872 int cpu;
Jacob Panf14a1392016-02-24 13:31:36 -0800873 u64 bits;
874 struct msrl_action ma;
875 int ret;
Jacob Pan2d281d82013-10-17 10:28:35 -0700876
Jacob Pan323ee642016-02-24 13:31:38 -0800877 cpu = rd->rp->lead_cpu;
Jacob Pan309557f2016-02-24 13:31:37 -0800878 bits = rapl_unit_xlate(rd, rp->unit, value, 1);
Adam Lessnauedbdabc2017-06-01 11:21:50 +0200879 bits <<= rp->shift;
880 bits &= rp->mask;
881
Jacob Panf14a1392016-02-24 13:31:36 -0800882 memset(&ma, 0, sizeof(ma));
883
884 ma.msr_no = rd->msrs[rp->id];
885 ma.clear_mask = rp->mask;
886 ma.set_mask = bits;
887
888 ret = smp_call_function_single(cpu, msrl_update_func, &ma, 1);
889 if (ret)
890 WARN_ON_ONCE(ret);
891 else
892 ret = ma.err;
893
894 return ret;
Jacob Pan2d281d82013-10-17 10:28:35 -0700895}
896
Jacob Pan3c2c0842014-11-07 09:29:26 -0800897/*
898 * Raw RAPL data stored in MSRs are in certain scales. We need to
899 * convert them into standard units based on the units reported in
900 * the RAPL unit MSRs. This is specific to CPUs as the method to
901 * calculate units differ on different CPUs.
902 * We convert the units to below format based on CPUs.
903 * i.e.
Jacob Pand474a4d2015-03-13 03:48:56 -0700904 * energy unit: picoJoules : Represented in picoJoules by default
Jacob Pan3c2c0842014-11-07 09:29:26 -0800905 * power unit : microWatts : Represented in milliWatts by default
906 * time unit : microseconds: Represented in seconds by default
907 */
908static int rapl_check_unit_core(struct rapl_package *rp, int cpu)
Jacob Pan2d281d82013-10-17 10:28:35 -0700909{
910 u64 msr_val;
911 u32 value;
912
913 if (rdmsrl_safe_on_cpu(cpu, MSR_RAPL_POWER_UNIT, &msr_val)) {
914 pr_err("Failed to read power unit MSR 0x%x on CPU %d, exit.\n",
915 MSR_RAPL_POWER_UNIT, cpu);
916 return -ENODEV;
917 }
918
Jacob Pan2d281d82013-10-17 10:28:35 -0700919 value = (msr_val & ENERGY_UNIT_MASK) >> ENERGY_UNIT_OFFSET;
Jacob Pand474a4d2015-03-13 03:48:56 -0700920 rp->energy_unit = ENERGY_UNIT_SCALE * 1000000 / (1 << value);
Jacob Pan2d281d82013-10-17 10:28:35 -0700921
922 value = (msr_val & POWER_UNIT_MASK) >> POWER_UNIT_OFFSET;
Jacob Pan3c2c0842014-11-07 09:29:26 -0800923 rp->power_unit = 1000000 / (1 << value);
Jacob Pan2d281d82013-10-17 10:28:35 -0700924
925 value = (msr_val & TIME_UNIT_MASK) >> TIME_UNIT_OFFSET;
Jacob Pan3c2c0842014-11-07 09:29:26 -0800926 rp->time_unit = 1000000 / (1 << value);
Jacob Pan2d281d82013-10-17 10:28:35 -0700927
Jacob Pand474a4d2015-03-13 03:48:56 -0700928 pr_debug("Core CPU package %d energy=%dpJ, time=%dus, power=%duW\n",
Jacob Pan3c2c0842014-11-07 09:29:26 -0800929 rp->id, rp->energy_unit, rp->time_unit, rp->power_unit);
Jacob Pan2d281d82013-10-17 10:28:35 -0700930
931 return 0;
932}
933
Jacob Pan3c2c0842014-11-07 09:29:26 -0800934static int rapl_check_unit_atom(struct rapl_package *rp, int cpu)
935{
936 u64 msr_val;
937 u32 value;
938
939 if (rdmsrl_safe_on_cpu(cpu, MSR_RAPL_POWER_UNIT, &msr_val)) {
940 pr_err("Failed to read power unit MSR 0x%x on CPU %d, exit.\n",
941 MSR_RAPL_POWER_UNIT, cpu);
942 return -ENODEV;
943 }
944 value = (msr_val & ENERGY_UNIT_MASK) >> ENERGY_UNIT_OFFSET;
Jacob Pand474a4d2015-03-13 03:48:56 -0700945 rp->energy_unit = ENERGY_UNIT_SCALE * 1 << value;
Jacob Pan3c2c0842014-11-07 09:29:26 -0800946
947 value = (msr_val & POWER_UNIT_MASK) >> POWER_UNIT_OFFSET;
948 rp->power_unit = (1 << value) * 1000;
949
950 value = (msr_val & TIME_UNIT_MASK) >> TIME_UNIT_OFFSET;
951 rp->time_unit = 1000000 / (1 << value);
952
Jacob Pand474a4d2015-03-13 03:48:56 -0700953 pr_debug("Atom package %d energy=%dpJ, time=%dus, power=%duW\n",
Jacob Pan3c2c0842014-11-07 09:29:26 -0800954 rp->id, rp->energy_unit, rp->time_unit, rp->power_unit);
955
956 return 0;
957}
958
Jacob Panf14a1392016-02-24 13:31:36 -0800959static void power_limit_irq_save_cpu(void *info)
960{
961 u32 l, h = 0;
962 struct rapl_package *rp = (struct rapl_package *)info;
963
964 /* save the state of PLN irq mask bit before disabling it */
965 rdmsr_safe(MSR_IA32_PACKAGE_THERM_INTERRUPT, &l, &h);
966 if (!(rp->power_limit_irq & PACKAGE_PLN_INT_SAVED)) {
967 rp->power_limit_irq = l & PACKAGE_THERM_INT_PLN_ENABLE;
968 rp->power_limit_irq |= PACKAGE_PLN_INT_SAVED;
969 }
970 l &= ~PACKAGE_THERM_INT_PLN_ENABLE;
971 wrmsr_safe(MSR_IA32_PACKAGE_THERM_INTERRUPT, l, h);
972}
973
Jacob Pan3c2c0842014-11-07 09:29:26 -0800974
Jacob Pan2d281d82013-10-17 10:28:35 -0700975/* REVISIT:
976 * When package power limit is set artificially low by RAPL, LVT
977 * thermal interrupt for package power limit should be ignored
978 * since we are not really exceeding the real limit. The intention
979 * is to avoid excessive interrupts while we are trying to save power.
980 * A useful feature might be routing the package_power_limit interrupt
981 * to userspace via eventfd. once we have a usecase, this is simple
982 * to do by adding an atomic notifier.
983 */
984
Jacob Pan309557f2016-02-24 13:31:37 -0800985static void package_power_limit_irq_save(struct rapl_package *rp)
Jacob Pan2d281d82013-10-17 10:28:35 -0700986{
Jacob Pan2d281d82013-10-17 10:28:35 -0700987 if (!boot_cpu_has(X86_FEATURE_PTS) || !boot_cpu_has(X86_FEATURE_PLN))
988 return;
989
Jacob Pan323ee642016-02-24 13:31:38 -0800990 smp_call_function_single(rp->lead_cpu, power_limit_irq_save_cpu, rp, 1);
Jacob Panf14a1392016-02-24 13:31:36 -0800991}
992
Thomas Gleixner58705062016-11-22 21:16:02 +0000993/*
994 * Restore per package power limit interrupt enable state. Called from cpu
995 * hotplug code on package removal.
996 */
997static void package_power_limit_irq_restore(struct rapl_package *rp)
Jacob Panf14a1392016-02-24 13:31:36 -0800998{
Thomas Gleixner58705062016-11-22 21:16:02 +0000999 u32 l, h;
1000
1001 if (!boot_cpu_has(X86_FEATURE_PTS) || !boot_cpu_has(X86_FEATURE_PLN))
1002 return;
1003
1004 /* irq enable state not saved, nothing to restore */
1005 if (!(rp->power_limit_irq & PACKAGE_PLN_INT_SAVED))
1006 return;
Jacob Panf14a1392016-02-24 13:31:36 -08001007
1008 rdmsr_safe(MSR_IA32_PACKAGE_THERM_INTERRUPT, &l, &h);
1009
1010 if (rp->power_limit_irq & PACKAGE_THERM_INT_PLN_ENABLE)
1011 l |= PACKAGE_THERM_INT_PLN_ENABLE;
1012 else
1013 l &= ~PACKAGE_THERM_INT_PLN_ENABLE;
1014
1015 wrmsr_safe(MSR_IA32_PACKAGE_THERM_INTERRUPT, l, h);
Jacob Pan2d281d82013-10-17 10:28:35 -07001016}
1017
Jacob Pan3c2c0842014-11-07 09:29:26 -08001018static void set_floor_freq_default(struct rapl_domain *rd, bool mode)
1019{
1020 int nr_powerlimit = find_nr_power_limit(rd);
1021
1022 /* always enable clamp such that p-state can go below OS requested
1023 * range. power capping priority over guranteed frequency.
1024 */
1025 rapl_write_data_raw(rd, PL1_CLAMP, mode);
1026
1027 /* some domains have pl2 */
1028 if (nr_powerlimit > 1) {
1029 rapl_write_data_raw(rd, PL2_ENABLE, mode);
1030 rapl_write_data_raw(rd, PL2_CLAMP, mode);
1031 }
1032}
1033
1034static void set_floor_freq_atom(struct rapl_domain *rd, bool enable)
1035{
1036 static u32 power_ctrl_orig_val;
1037 u32 mdata;
1038
Ajay Thomas51b63402015-04-30 01:43:23 +05301039 if (!rapl_defaults->floor_freq_reg_addr) {
1040 pr_err("Invalid floor frequency config register\n");
1041 return;
1042 }
1043
Jacob Pan3c2c0842014-11-07 09:29:26 -08001044 if (!power_ctrl_orig_val)
Andy Shevchenko4077a382015-11-11 19:59:29 +02001045 iosf_mbi_read(BT_MBI_UNIT_PMC, MBI_CR_READ,
1046 rapl_defaults->floor_freq_reg_addr,
1047 &power_ctrl_orig_val);
Jacob Pan3c2c0842014-11-07 09:29:26 -08001048 mdata = power_ctrl_orig_val;
1049 if (enable) {
1050 mdata &= ~(0x7f << 8);
1051 mdata |= 1 << 8;
1052 }
Andy Shevchenko4077a382015-11-11 19:59:29 +02001053 iosf_mbi_write(BT_MBI_UNIT_PMC, MBI_CR_WRITE,
1054 rapl_defaults->floor_freq_reg_addr, mdata);
Jacob Pan3c2c0842014-11-07 09:29:26 -08001055}
1056
1057static u64 rapl_compute_time_window_core(struct rapl_package *rp, u64 value,
1058 bool to_raw)
1059{
1060 u64 f, y; /* fraction and exp. used for time unit */
1061
1062 /*
1063 * Special processing based on 2^Y*(1+F/4), refer
1064 * to Intel Software Developer's manual Vol.3B: CH 14.9.3.
1065 */
1066 if (!to_raw) {
1067 f = (value & 0x60) >> 5;
1068 y = value & 0x1f;
1069 value = (1 << y) * (4 + f) * rp->time_unit / 4;
1070 } else {
1071 do_div(value, rp->time_unit);
1072 y = ilog2(value);
1073 f = div64_u64(4 * (value - (1 << y)), 1 << y);
1074 value = (y & 0x1f) | ((f & 0x3) << 5);
1075 }
1076 return value;
1077}
1078
1079static u64 rapl_compute_time_window_atom(struct rapl_package *rp, u64 value,
1080 bool to_raw)
1081{
1082 /*
1083 * Atom time unit encoding is straight forward val * time_unit,
1084 * where time_unit is default to 1 sec. Never 0.
1085 */
1086 if (!to_raw)
1087 return (value) ? value *= rp->time_unit : rp->time_unit;
1088 else
1089 value = div64_u64(value, rp->time_unit);
1090
1091 return value;
1092}
1093
Jacob Pan087e9cb2014-11-07 09:29:25 -08001094static const struct rapl_defaults rapl_defaults_core = {
Ajay Thomas51b63402015-04-30 01:43:23 +05301095 .floor_freq_reg_addr = 0,
Jacob Pan3c2c0842014-11-07 09:29:26 -08001096 .check_unit = rapl_check_unit_core,
1097 .set_floor_freq = set_floor_freq_default,
1098 .compute_time_window = rapl_compute_time_window_core,
Jacob Pan087e9cb2014-11-07 09:29:25 -08001099};
1100
Jacob Pand474a4d2015-03-13 03:48:56 -07001101static const struct rapl_defaults rapl_defaults_hsw_server = {
1102 .check_unit = rapl_check_unit_core,
1103 .set_floor_freq = set_floor_freq_default,
1104 .compute_time_window = rapl_compute_time_window_core,
1105 .dram_domain_energy_unit = 15300,
1106};
1107
Ajay Thomas51b63402015-04-30 01:43:23 +05301108static const struct rapl_defaults rapl_defaults_byt = {
1109 .floor_freq_reg_addr = IOSF_CPU_POWER_BUDGET_CTL_BYT,
Jacob Pan3c2c0842014-11-07 09:29:26 -08001110 .check_unit = rapl_check_unit_atom,
1111 .set_floor_freq = set_floor_freq_atom,
1112 .compute_time_window = rapl_compute_time_window_atom,
Jacob Pan087e9cb2014-11-07 09:29:25 -08001113};
1114
Ajay Thomas51b63402015-04-30 01:43:23 +05301115static const struct rapl_defaults rapl_defaults_tng = {
1116 .floor_freq_reg_addr = IOSF_CPU_POWER_BUDGET_CTL_TNG,
1117 .check_unit = rapl_check_unit_atom,
1118 .set_floor_freq = set_floor_freq_atom,
1119 .compute_time_window = rapl_compute_time_window_atom,
1120};
1121
1122static const struct rapl_defaults rapl_defaults_ann = {
1123 .floor_freq_reg_addr = 0,
1124 .check_unit = rapl_check_unit_atom,
1125 .set_floor_freq = NULL,
1126 .compute_time_window = rapl_compute_time_window_atom,
1127};
1128
1129static const struct rapl_defaults rapl_defaults_cht = {
1130 .floor_freq_reg_addr = 0,
1131 .check_unit = rapl_check_unit_atom,
1132 .set_floor_freq = NULL,
1133 .compute_time_window = rapl_compute_time_window_atom,
1134};
1135
Jacob Pan087e9cb2014-11-07 09:29:25 -08001136#define RAPL_CPU(_model, _ops) { \
1137 .vendor = X86_VENDOR_INTEL, \
1138 .family = 6, \
1139 .model = _model, \
1140 .driver_data = (kernel_ulong_t)&_ops, \
1141 }
1142
Mathias Krauseea85dbc2015-03-25 22:15:52 +01001143static const struct x86_cpu_id rapl_ids[] __initconst = {
Dave Hansen62d16732016-06-02 17:19:36 -07001144 RAPL_CPU(INTEL_FAM6_SANDYBRIDGE, rapl_defaults_core),
1145 RAPL_CPU(INTEL_FAM6_SANDYBRIDGE_X, rapl_defaults_core),
Dave Hansen0bb04b52016-06-02 17:19:37 -07001146
Dave Hansen62d16732016-06-02 17:19:36 -07001147 RAPL_CPU(INTEL_FAM6_IVYBRIDGE, rapl_defaults_core),
Xiaolong Wang7d188472016-06-24 11:28:20 +08001148 RAPL_CPU(INTEL_FAM6_IVYBRIDGE_X, rapl_defaults_core),
Dave Hansen0bb04b52016-06-02 17:19:37 -07001149
Dave Hansen62d16732016-06-02 17:19:36 -07001150 RAPL_CPU(INTEL_FAM6_HASWELL_CORE, rapl_defaults_core),
Dave Hansen62d16732016-06-02 17:19:36 -07001151 RAPL_CPU(INTEL_FAM6_HASWELL_ULT, rapl_defaults_core),
1152 RAPL_CPU(INTEL_FAM6_HASWELL_GT3E, rapl_defaults_core),
Dave Hansen0bb04b52016-06-02 17:19:37 -07001153 RAPL_CPU(INTEL_FAM6_HASWELL_X, rapl_defaults_hsw_server),
1154
1155 RAPL_CPU(INTEL_FAM6_BROADWELL_CORE, rapl_defaults_core),
Dave Hansen62d16732016-06-02 17:19:36 -07001156 RAPL_CPU(INTEL_FAM6_BROADWELL_GT3E, rapl_defaults_core),
Dave Hansen62d16732016-06-02 17:19:36 -07001157 RAPL_CPU(INTEL_FAM6_BROADWELL_XEON_D, rapl_defaults_core),
Dave Hansen0bb04b52016-06-02 17:19:37 -07001158 RAPL_CPU(INTEL_FAM6_BROADWELL_X, rapl_defaults_hsw_server),
1159
Dave Hansen62d16732016-06-02 17:19:36 -07001160 RAPL_CPU(INTEL_FAM6_SKYLAKE_DESKTOP, rapl_defaults_core),
Dave Hansen0bb04b52016-06-02 17:19:37 -07001161 RAPL_CPU(INTEL_FAM6_SKYLAKE_MOBILE, rapl_defaults_core),
Dave Hansend40671e2016-06-02 17:19:55 -07001162 RAPL_CPU(INTEL_FAM6_SKYLAKE_X, rapl_defaults_hsw_server),
Dave Hansen62d16732016-06-02 17:19:36 -07001163 RAPL_CPU(INTEL_FAM6_KABYLAKE_MOBILE, rapl_defaults_core),
1164 RAPL_CPU(INTEL_FAM6_KABYLAKE_DESKTOP, rapl_defaults_core),
Dave Hansen0bb04b52016-06-02 17:19:37 -07001165
1166 RAPL_CPU(INTEL_FAM6_ATOM_SILVERMONT1, rapl_defaults_byt),
1167 RAPL_CPU(INTEL_FAM6_ATOM_AIRMONT, rapl_defaults_cht),
Andy Shevchenkof5fbf842016-09-06 21:42:54 +03001168 RAPL_CPU(INTEL_FAM6_ATOM_MERRIFIELD, rapl_defaults_tng),
1169 RAPL_CPU(INTEL_FAM6_ATOM_MOOREFIELD, rapl_defaults_ann),
Dave Hansen0bb04b52016-06-02 17:19:37 -07001170 RAPL_CPU(INTEL_FAM6_ATOM_GOLDMONT, rapl_defaults_core),
David E. Box44867402017-04-23 07:15:20 -07001171 RAPL_CPU(INTEL_FAM6_ATOM_GEMINI_LAKE, rapl_defaults_core),
Jacob Panab0d15d2016-06-13 17:02:28 -07001172 RAPL_CPU(INTEL_FAM6_ATOM_DENVERTON, rapl_defaults_core),
Dave Hansen0bb04b52016-06-02 17:19:37 -07001173
1174 RAPL_CPU(INTEL_FAM6_XEON_PHI_KNL, rapl_defaults_hsw_server),
Piotr Lucbd908912016-10-13 17:31:04 +02001175 RAPL_CPU(INTEL_FAM6_XEON_PHI_KNM, rapl_defaults_hsw_server),
Jacob Pan2d281d82013-10-17 10:28:35 -07001176 {}
1177};
1178MODULE_DEVICE_TABLE(x86cpu, rapl_ids);
1179
Thomas Gleixnerbed5ab62016-11-22 21:15:58 +00001180/* Read once for all raw primitive data for domains */
1181static void rapl_update_domain_data(struct rapl_package *rp)
Jacob Pan2d281d82013-10-17 10:28:35 -07001182{
1183 int dmn, prim;
1184 u64 val;
Jacob Pan2d281d82013-10-17 10:28:35 -07001185
Thomas Gleixnerbed5ab62016-11-22 21:15:58 +00001186 for (dmn = 0; dmn < rp->nr_domains; dmn++) {
1187 pr_debug("update package %d domain %s data\n", rp->id,
1188 rp->domains[dmn].name);
1189 /* exclude non-raw primitives */
1190 for (prim = 0; prim < NR_RAW_PRIMITIVES; prim++) {
1191 if (!rapl_read_data_raw(&rp->domains[dmn], prim,
1192 rpi[prim].unit, &val))
1193 rp->domains[dmn].rdd.primitives[prim] = val;
Jacob Pan2d281d82013-10-17 10:28:35 -07001194 }
1195 }
1196
1197}
1198
Thomas Gleixner58705062016-11-22 21:16:02 +00001199static void rapl_unregister_powercap(void)
Jacob Pan2d281d82013-10-17 10:28:35 -07001200{
Srinivas Pandruvada3521ba12016-04-17 15:03:01 -07001201 if (platform_rapl_domain) {
1202 powercap_unregister_zone(control_type,
1203 &platform_rapl_domain->power_zone);
1204 kfree(platform_rapl_domain);
1205 }
Jacob Pan2d281d82013-10-17 10:28:35 -07001206 powercap_unregister_control_type(control_type);
Jacob Pan2d281d82013-10-17 10:28:35 -07001207}
1208
1209static int rapl_package_register_powercap(struct rapl_package *rp)
1210{
1211 struct rapl_domain *rd;
Jacob Pan2d281d82013-10-17 10:28:35 -07001212 char dev_name[17]; /* max domain name = 7 + 1 + 8 for int + 1 for null*/
1213 struct powercap_zone *power_zone = NULL;
Thomas Gleixnerbed5ab62016-11-22 21:15:58 +00001214 int nr_pl, ret;;
1215
1216 /* Update the domain data of the new package */
1217 rapl_update_domain_data(rp);
Jacob Pan2d281d82013-10-17 10:28:35 -07001218
1219 /* first we register package domain as the parent zone*/
1220 for (rd = rp->domains; rd < rp->domains + rp->nr_domains; rd++) {
1221 if (rd->id == RAPL_DOMAIN_PACKAGE) {
1222 nr_pl = find_nr_power_limit(rd);
1223 pr_debug("register socket %d package domain %s\n",
1224 rp->id, rd->name);
1225 memset(dev_name, 0, sizeof(dev_name));
1226 snprintf(dev_name, sizeof(dev_name), "%s-%d",
1227 rd->name, rp->id);
1228 power_zone = powercap_register_zone(&rd->power_zone,
1229 control_type,
1230 dev_name, NULL,
1231 &zone_ops[rd->id],
1232 nr_pl,
1233 &constraint_ops);
1234 if (IS_ERR(power_zone)) {
1235 pr_debug("failed to register package, %d\n",
1236 rp->id);
Thomas Gleixnerbed5ab62016-11-22 21:15:58 +00001237 return PTR_ERR(power_zone);
Jacob Pan2d281d82013-10-17 10:28:35 -07001238 }
1239 /* track parent zone in per package/socket data */
1240 rp->power_zone = power_zone;
1241 /* done, only one package domain per socket */
1242 break;
1243 }
1244 }
1245 if (!power_zone) {
1246 pr_err("no package domain found, unknown topology!\n");
Thomas Gleixnerbed5ab62016-11-22 21:15:58 +00001247 return -ENODEV;
Jacob Pan2d281d82013-10-17 10:28:35 -07001248 }
1249 /* now register domains as children of the socket/package*/
1250 for (rd = rp->domains; rd < rp->domains + rp->nr_domains; rd++) {
1251 if (rd->id == RAPL_DOMAIN_PACKAGE)
1252 continue;
1253 /* number of power limits per domain varies */
1254 nr_pl = find_nr_power_limit(rd);
1255 power_zone = powercap_register_zone(&rd->power_zone,
1256 control_type, rd->name,
1257 rp->power_zone,
1258 &zone_ops[rd->id], nr_pl,
1259 &constraint_ops);
1260
1261 if (IS_ERR(power_zone)) {
1262 pr_debug("failed to register power_zone, %d:%s:%s\n",
1263 rp->id, rd->name, dev_name);
1264 ret = PTR_ERR(power_zone);
1265 goto err_cleanup;
1266 }
1267 }
Thomas Gleixnerbed5ab62016-11-22 21:15:58 +00001268 return 0;
Jacob Pan2d281d82013-10-17 10:28:35 -07001269
Jacob Pan2d281d82013-10-17 10:28:35 -07001270err_cleanup:
Thomas Gleixner58705062016-11-22 21:16:02 +00001271 /*
1272 * Clean up previously initialized domains within the package if we
Jacob Pan2d281d82013-10-17 10:28:35 -07001273 * failed after the first domain setup.
1274 */
1275 while (--rd >= rp->domains) {
1276 pr_debug("unregister package %d domain %s\n", rp->id, rd->name);
1277 powercap_unregister_zone(control_type, &rd->power_zone);
1278 }
1279
1280 return ret;
1281}
1282
Thomas Gleixner58705062016-11-22 21:16:02 +00001283static int __init rapl_register_psys(void)
Srinivas Pandruvada3521ba12016-04-17 15:03:01 -07001284{
1285 struct rapl_domain *rd;
1286 struct powercap_zone *power_zone;
1287 u64 val;
1288
1289 if (rdmsrl_safe_on_cpu(0, MSR_PLATFORM_ENERGY_STATUS, &val) || !val)
1290 return -ENODEV;
1291
1292 if (rdmsrl_safe_on_cpu(0, MSR_PLATFORM_POWER_LIMIT, &val) || !val)
1293 return -ENODEV;
1294
1295 rd = kzalloc(sizeof(*rd), GFP_KERNEL);
1296 if (!rd)
1297 return -ENOMEM;
1298
1299 rd->name = rapl_domain_names[RAPL_DOMAIN_PLATFORM];
1300 rd->id = RAPL_DOMAIN_PLATFORM;
1301 rd->msrs[0] = MSR_PLATFORM_POWER_LIMIT;
1302 rd->msrs[1] = MSR_PLATFORM_ENERGY_STATUS;
1303 rd->rpl[0].prim_id = PL1_ENABLE;
1304 rd->rpl[0].name = pl1_name;
1305 rd->rpl[1].prim_id = PL2_ENABLE;
1306 rd->rpl[1].name = pl2_name;
1307 rd->rp = find_package_by_id(0);
1308
1309 power_zone = powercap_register_zone(&rd->power_zone, control_type,
1310 "psys", NULL,
1311 &zone_ops[RAPL_DOMAIN_PLATFORM],
1312 2, &constraint_ops);
1313
1314 if (IS_ERR(power_zone)) {
1315 kfree(rd);
1316 return PTR_ERR(power_zone);
1317 }
1318
1319 platform_rapl_domain = rd;
1320
1321 return 0;
1322}
1323
Thomas Gleixner58705062016-11-22 21:16:02 +00001324static int __init rapl_register_powercap(void)
Jacob Pan2d281d82013-10-17 10:28:35 -07001325{
Jacob Pan2d281d82013-10-17 10:28:35 -07001326 control_type = powercap_register_control_type(NULL, "intel-rapl", NULL);
1327 if (IS_ERR(control_type)) {
1328 pr_debug("failed to register powercap control_type.\n");
1329 return PTR_ERR(control_type);
1330 }
Thomas Gleixner58705062016-11-22 21:16:02 +00001331 return 0;
Jacob Pan2d281d82013-10-17 10:28:35 -07001332}
1333
1334static int rapl_check_domain(int cpu, int domain)
1335{
1336 unsigned msr;
Jacob Pan9d31c672014-04-29 15:33:06 -07001337 u64 val = 0;
Jacob Pan2d281d82013-10-17 10:28:35 -07001338
1339 switch (domain) {
1340 case RAPL_DOMAIN_PACKAGE:
1341 msr = MSR_PKG_ENERGY_STATUS;
1342 break;
1343 case RAPL_DOMAIN_PP0:
1344 msr = MSR_PP0_ENERGY_STATUS;
1345 break;
1346 case RAPL_DOMAIN_PP1:
1347 msr = MSR_PP1_ENERGY_STATUS;
1348 break;
1349 case RAPL_DOMAIN_DRAM:
1350 msr = MSR_DRAM_ENERGY_STATUS;
1351 break;
Srinivas Pandruvada3521ba12016-04-17 15:03:01 -07001352 case RAPL_DOMAIN_PLATFORM:
1353 /* PSYS(PLATFORM) is not a CPU domain, so avoid printng error */
1354 return -EINVAL;
Jacob Pan2d281d82013-10-17 10:28:35 -07001355 default:
1356 pr_err("invalid domain id %d\n", domain);
1357 return -EINVAL;
1358 }
Jacob Pan9d31c672014-04-29 15:33:06 -07001359 /* make sure domain counters are available and contains non-zero
1360 * values, otherwise skip it.
1361 */
1362 if (rdmsrl_safe_on_cpu(cpu, msr, &val) || !val)
Jacob Pan2d281d82013-10-17 10:28:35 -07001363 return -ENODEV;
1364
Jacob Pan9d31c672014-04-29 15:33:06 -07001365 return 0;
Jacob Pan2d281d82013-10-17 10:28:35 -07001366}
1367
Jacob Pane1399ba2016-05-31 13:41:29 -07001368
1369/*
1370 * Check if power limits are available. Two cases when they are not available:
1371 * 1. Locked by BIOS, in this case we still provide read-only access so that
1372 * users can see what limit is set by the BIOS.
1373 * 2. Some CPUs make some domains monitoring only which means PLx MSRs may not
1374 * exist at all. In this case, we do not show the contraints in powercap.
1375 *
1376 * Called after domains are detected and initialized.
1377 */
1378static void rapl_detect_powerlimit(struct rapl_domain *rd)
1379{
1380 u64 val64;
1381 int i;
1382
1383 /* check if the domain is locked by BIOS, ignore if MSR doesn't exist */
1384 if (!rapl_read_data_raw(rd, FW_LOCK, false, &val64)) {
1385 if (val64) {
1386 pr_info("RAPL package %d domain %s locked by BIOS\n",
1387 rd->rp->id, rd->name);
1388 rd->state |= DOMAIN_STATE_BIOS_LOCKED;
1389 }
1390 }
1391 /* check if power limit MSRs exists, otherwise domain is monitoring only */
1392 for (i = 0; i < NR_POWER_LIMITS; i++) {
1393 int prim = rd->rpl[i].prim_id;
1394 if (rapl_read_data_raw(rd, prim, false, &val64))
1395 rd->rpl[i].name = NULL;
1396 }
1397}
1398
Jacob Pan2d281d82013-10-17 10:28:35 -07001399/* Detect active and valid domains for the given CPU, caller must
1400 * ensure the CPU belongs to the targeted package and CPU hotlug is disabled.
1401 */
1402static int rapl_detect_domains(struct rapl_package *rp, int cpu)
1403{
Jacob Pan2d281d82013-10-17 10:28:35 -07001404 struct rapl_domain *rd;
Thomas Gleixner58705062016-11-22 21:16:02 +00001405 int i;
Jacob Pan2d281d82013-10-17 10:28:35 -07001406
1407 for (i = 0; i < RAPL_DOMAIN_MAX; i++) {
1408 /* use physical package id to read counters */
Jacob Panfcdf1792014-09-02 02:55:21 -07001409 if (!rapl_check_domain(cpu, i)) {
Jacob Pan2d281d82013-10-17 10:28:35 -07001410 rp->domain_map |= 1 << i;
Jacob Panfcdf1792014-09-02 02:55:21 -07001411 pr_info("Found RAPL domain %s\n", rapl_domain_names[i]);
1412 }
Jacob Pan2d281d82013-10-17 10:28:35 -07001413 }
1414 rp->nr_domains = bitmap_weight(&rp->domain_map, RAPL_DOMAIN_MAX);
1415 if (!rp->nr_domains) {
Jacob Pane1a27e82016-05-23 09:45:43 -07001416 pr_debug("no valid rapl domains found in package %d\n", rp->id);
Thomas Gleixner58705062016-11-22 21:16:02 +00001417 return -ENODEV;
Jacob Pan2d281d82013-10-17 10:28:35 -07001418 }
1419 pr_debug("found %d domains on package %d\n", rp->nr_domains, rp->id);
1420
1421 rp->domains = kcalloc(rp->nr_domains + 1, sizeof(struct rapl_domain),
1422 GFP_KERNEL);
Thomas Gleixner58705062016-11-22 21:16:02 +00001423 if (!rp->domains)
1424 return -ENOMEM;
1425
Jacob Pan2d281d82013-10-17 10:28:35 -07001426 rapl_init_domains(rp);
1427
Jacob Pane1399ba2016-05-31 13:41:29 -07001428 for (rd = rp->domains; rd < rp->domains + rp->nr_domains; rd++)
1429 rapl_detect_powerlimit(rd);
1430
Jacob Pan2d281d82013-10-17 10:28:35 -07001431 return 0;
1432}
1433
1434/* called from CPU hotplug notifier, hotplug lock held */
1435static void rapl_remove_package(struct rapl_package *rp)
1436{
1437 struct rapl_domain *rd, *rd_package = NULL;
1438
Thomas Gleixner58705062016-11-22 21:16:02 +00001439 package_power_limit_irq_restore(rp);
1440
Jacob Pan2d281d82013-10-17 10:28:35 -07001441 for (rd = rp->domains; rd < rp->domains + rp->nr_domains; rd++) {
Thomas Gleixner58705062016-11-22 21:16:02 +00001442 rapl_write_data_raw(rd, PL1_ENABLE, 0);
1443 rapl_write_data_raw(rd, PL1_CLAMP, 0);
1444 if (find_nr_power_limit(rd) > 1) {
1445 rapl_write_data_raw(rd, PL2_ENABLE, 0);
1446 rapl_write_data_raw(rd, PL2_CLAMP, 0);
1447 }
Jacob Pan2d281d82013-10-17 10:28:35 -07001448 if (rd->id == RAPL_DOMAIN_PACKAGE) {
1449 rd_package = rd;
1450 continue;
1451 }
Thomas Gleixner58705062016-11-22 21:16:02 +00001452 pr_debug("remove package, undo power limit on %d: %s\n",
1453 rp->id, rd->name);
Jacob Pan2d281d82013-10-17 10:28:35 -07001454 powercap_unregister_zone(control_type, &rd->power_zone);
1455 }
1456 /* do parent zone last */
1457 powercap_unregister_zone(control_type, &rd_package->power_zone);
1458 list_del(&rp->plist);
1459 kfree(rp);
1460}
1461
1462/* called from CPU hotplug notifier, hotplug lock held */
Thomas Gleixnerb4005e92016-11-22 21:16:05 +00001463static struct rapl_package *rapl_add_package(int cpu, int pkgid)
Jacob Pan2d281d82013-10-17 10:28:35 -07001464{
Jacob Pan2d281d82013-10-17 10:28:35 -07001465 struct rapl_package *rp;
Thomas Gleixnerb4005e92016-11-22 21:16:05 +00001466 int ret;
Jacob Pan2d281d82013-10-17 10:28:35 -07001467
Jacob Pan2d281d82013-10-17 10:28:35 -07001468 rp = kzalloc(sizeof(struct rapl_package), GFP_KERNEL);
1469 if (!rp)
Thomas Gleixnerb4005e92016-11-22 21:16:05 +00001470 return ERR_PTR(-ENOMEM);
Jacob Pan2d281d82013-10-17 10:28:35 -07001471
1472 /* add the new package to the list */
Thomas Gleixnerb4005e92016-11-22 21:16:05 +00001473 rp->id = pkgid;
Jacob Pan323ee642016-02-24 13:31:38 -08001474 rp->lead_cpu = cpu;
1475
Jacob Pan2d281d82013-10-17 10:28:35 -07001476 /* check if the package contains valid domains */
1477 if (rapl_detect_domains(rp, cpu) ||
Jacob Pan3c2c0842014-11-07 09:29:26 -08001478 rapl_defaults->check_unit(rp, cpu)) {
Jacob Pan2d281d82013-10-17 10:28:35 -07001479 ret = -ENODEV;
1480 goto err_free_package;
1481 }
Thomas Gleixnera74f4362016-11-22 21:15:59 +00001482 ret = rapl_package_register_powercap(rp);
1483 if (!ret) {
Jacob Pan2d281d82013-10-17 10:28:35 -07001484 INIT_LIST_HEAD(&rp->plist);
1485 list_add(&rp->plist, &rapl_packages);
Thomas Gleixnerb4005e92016-11-22 21:16:05 +00001486 return rp;
Jacob Pan2d281d82013-10-17 10:28:35 -07001487 }
1488
1489err_free_package:
1490 kfree(rp->domains);
1491 kfree(rp);
Thomas Gleixnerb4005e92016-11-22 21:16:05 +00001492 return ERR_PTR(ret);
Jacob Pan2d281d82013-10-17 10:28:35 -07001493}
1494
1495/* Handles CPU hotplug on multi-socket systems.
1496 * If a CPU goes online as the first CPU of the physical package
1497 * we add the RAPL package to the system. Similarly, when the last
1498 * CPU of the package is removed, we remove the RAPL package and its
1499 * associated domains. Cooling devices are handled accordingly at
1500 * per-domain level.
1501 */
Sebastian Andrzej Siewior5e4dc792016-11-22 21:16:00 +00001502static int rapl_cpu_online(unsigned int cpu)
Jacob Pan2d281d82013-10-17 10:28:35 -07001503{
Thomas Gleixnerb4005e92016-11-22 21:16:05 +00001504 int pkgid = topology_physical_package_id(cpu);
Sebastian Andrzej Siewior5e4dc792016-11-22 21:16:00 +00001505 struct rapl_package *rp;
Sebastian Andrzej Siewior5e4dc792016-11-22 21:16:00 +00001506
Thomas Gleixnerb4005e92016-11-22 21:16:05 +00001507 rp = find_package_by_id(pkgid);
1508 if (!rp) {
1509 rp = rapl_add_package(cpu, pkgid);
1510 if (IS_ERR(rp))
1511 return PTR_ERR(rp);
Thomas Gleixner58705062016-11-22 21:16:02 +00001512 }
Thomas Gleixnerb4005e92016-11-22 21:16:05 +00001513 cpumask_set_cpu(cpu, &rp->cpumask);
1514 return 0;
Sebastian Andrzej Siewior5e4dc792016-11-22 21:16:00 +00001515}
1516
1517static int rapl_cpu_down_prep(unsigned int cpu)
1518{
Thomas Gleixnerb4005e92016-11-22 21:16:05 +00001519 int pkgid = topology_physical_package_id(cpu);
Jacob Pan2d281d82013-10-17 10:28:35 -07001520 struct rapl_package *rp;
Jacob Pan323ee642016-02-24 13:31:38 -08001521 int lead_cpu;
Jacob Pan2d281d82013-10-17 10:28:35 -07001522
Thomas Gleixnerb4005e92016-11-22 21:16:05 +00001523 rp = find_package_by_id(pkgid);
Sebastian Andrzej Siewior5e4dc792016-11-22 21:16:00 +00001524 if (!rp)
1525 return 0;
Thomas Gleixnerb4005e92016-11-22 21:16:05 +00001526
1527 cpumask_clear_cpu(cpu, &rp->cpumask);
1528 lead_cpu = cpumask_first(&rp->cpumask);
1529 if (lead_cpu >= nr_cpu_ids)
Sebastian Andrzej Siewior5e4dc792016-11-22 21:16:00 +00001530 rapl_remove_package(rp);
Thomas Gleixnerb4005e92016-11-22 21:16:05 +00001531 else if (rp->lead_cpu == cpu)
1532 rp->lead_cpu = lead_cpu;
Sebastian Andrzej Siewior5e4dc792016-11-22 21:16:00 +00001533 return 0;
Jacob Pan2d281d82013-10-17 10:28:35 -07001534}
1535
Sebastian Andrzej Siewior5e4dc792016-11-22 21:16:00 +00001536static enum cpuhp_state pcap_rapl_online;
Jacob Pan2d281d82013-10-17 10:28:35 -07001537
Zhen Han52b36722018-01-10 08:38:23 +08001538static void power_limit_state_save(void)
1539{
1540 struct rapl_package *rp;
1541 struct rapl_domain *rd;
1542 int nr_pl, ret, i;
1543
1544 get_online_cpus();
1545 list_for_each_entry(rp, &rapl_packages, plist) {
1546 if (!rp->power_zone)
1547 continue;
1548 rd = power_zone_to_rapl_domain(rp->power_zone);
1549 nr_pl = find_nr_power_limit(rd);
1550 for (i = 0; i < nr_pl; i++) {
1551 switch (rd->rpl[i].prim_id) {
1552 case PL1_ENABLE:
1553 ret = rapl_read_data_raw(rd,
1554 POWER_LIMIT1,
1555 true,
1556 &rd->rpl[i].last_power_limit);
1557 if (ret)
1558 rd->rpl[i].last_power_limit = 0;
1559 break;
1560 case PL2_ENABLE:
1561 ret = rapl_read_data_raw(rd,
1562 POWER_LIMIT2,
1563 true,
1564 &rd->rpl[i].last_power_limit);
1565 if (ret)
1566 rd->rpl[i].last_power_limit = 0;
1567 break;
1568 }
1569 }
1570 }
1571 put_online_cpus();
1572}
1573
1574static void power_limit_state_restore(void)
1575{
1576 struct rapl_package *rp;
1577 struct rapl_domain *rd;
1578 int nr_pl, i;
1579
1580 get_online_cpus();
1581 list_for_each_entry(rp, &rapl_packages, plist) {
1582 if (!rp->power_zone)
1583 continue;
1584 rd = power_zone_to_rapl_domain(rp->power_zone);
1585 nr_pl = find_nr_power_limit(rd);
1586 for (i = 0; i < nr_pl; i++) {
1587 switch (rd->rpl[i].prim_id) {
1588 case PL1_ENABLE:
1589 if (rd->rpl[i].last_power_limit)
1590 rapl_write_data_raw(rd,
1591 POWER_LIMIT1,
1592 rd->rpl[i].last_power_limit);
1593 break;
1594 case PL2_ENABLE:
1595 if (rd->rpl[i].last_power_limit)
1596 rapl_write_data_raw(rd,
1597 POWER_LIMIT2,
1598 rd->rpl[i].last_power_limit);
1599 break;
1600 }
1601 }
1602 }
1603 put_online_cpus();
1604}
1605
1606static int rapl_pm_callback(struct notifier_block *nb,
1607 unsigned long mode, void *_unused)
1608{
1609 switch (mode) {
1610 case PM_SUSPEND_PREPARE:
1611 power_limit_state_save();
1612 break;
1613 case PM_POST_SUSPEND:
1614 power_limit_state_restore();
1615 break;
1616 }
1617 return NOTIFY_OK;
1618}
1619
1620static struct notifier_block rapl_pm_notifier = {
1621 .notifier_call = rapl_pm_callback,
1622};
1623
Jacob Pan2d281d82013-10-17 10:28:35 -07001624static int __init rapl_init(void)
1625{
Jacob Pan087e9cb2014-11-07 09:29:25 -08001626 const struct x86_cpu_id *id;
Thomas Gleixner58705062016-11-22 21:16:02 +00001627 int ret;
Jacob Pan2d281d82013-10-17 10:28:35 -07001628
Jacob Pan087e9cb2014-11-07 09:29:25 -08001629 id = x86_match_cpu(rapl_ids);
1630 if (!id) {
Jacob Pan2d281d82013-10-17 10:28:35 -07001631 pr_err("driver does not support CPU family %d model %d\n",
1632 boot_cpu_data.x86, boot_cpu_data.x86_model);
1633
1634 return -ENODEV;
1635 }
Srivatsa S. Bhat009f2252014-03-11 02:09:26 +05301636
Jacob Pan087e9cb2014-11-07 09:29:25 -08001637 rapl_defaults = (struct rapl_defaults *)id->driver_data;
1638
Thomas Gleixner58705062016-11-22 21:16:02 +00001639 ret = rapl_register_powercap();
Jacob Pan2d281d82013-10-17 10:28:35 -07001640 if (ret)
Thomas Gleixner58705062016-11-22 21:16:02 +00001641 return ret;
Jacob Pan2d281d82013-10-17 10:28:35 -07001642
Thomas Gleixner58705062016-11-22 21:16:02 +00001643 ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "powercap/rapl:online",
1644 rapl_cpu_online, rapl_cpu_down_prep);
Sebastian Andrzej Siewior5e4dc792016-11-22 21:16:00 +00001645 if (ret < 0)
1646 goto err_unreg;
1647 pcap_rapl_online = ret;
Thomas Gleixner58705062016-11-22 21:16:02 +00001648
1649 /* Don't bail out if PSys is not supported */
1650 rapl_register_psys();
Zhen Han52b36722018-01-10 08:38:23 +08001651
1652 ret = register_pm_notifier(&rapl_pm_notifier);
1653 if (ret)
1654 goto err_unreg_all;
1655
Sebastian Andrzej Siewior5e4dc792016-11-22 21:16:00 +00001656 return 0;
Jacob Pan2d281d82013-10-17 10:28:35 -07001657
Zhen Han52b36722018-01-10 08:38:23 +08001658err_unreg_all:
1659 cpuhp_remove_state(pcap_rapl_online);
1660
Sebastian Andrzej Siewior5e4dc792016-11-22 21:16:00 +00001661err_unreg:
1662 rapl_unregister_powercap();
Jacob Pan2d281d82013-10-17 10:28:35 -07001663 return ret;
1664}
1665
1666static void __exit rapl_exit(void)
1667{
Zhen Han52b36722018-01-10 08:38:23 +08001668 unregister_pm_notifier(&rapl_pm_notifier);
Sebastian Andrzej Siewior5e4dc792016-11-22 21:16:00 +00001669 cpuhp_remove_state(pcap_rapl_online);
Jacob Pan2d281d82013-10-17 10:28:35 -07001670 rapl_unregister_powercap();
Jacob Pan2d281d82013-10-17 10:28:35 -07001671}
1672
1673module_init(rapl_init);
1674module_exit(rapl_exit);
1675
1676MODULE_DESCRIPTION("Driver for Intel RAPL (Running Average Power Limit)");
1677MODULE_AUTHOR("Jacob Pan <jacob.jun.pan@intel.com>");
1678MODULE_LICENSE("GPL v2");