blob: 76a00696a5ceb0f80a2cd4cd21a9e12b474cd15a [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>
Jacob Pan3c2c0842014-11-07 09:29:26 -080032#include <asm/iosf_mbi.h>
Jacob Pan2d281d82013-10-17 10:28:35 -070033
34#include <asm/processor.h>
35#include <asm/cpu_device_id.h>
Dave Hansen62d16732016-06-02 17:19:36 -070036#include <asm/intel-family.h>
Jacob Pan2d281d82013-10-17 10:28:35 -070037
Srinivas Pandruvada3521ba12016-04-17 15:03:01 -070038/* Local defines */
39#define MSR_PLATFORM_POWER_LIMIT 0x0000065C
40
Jacob Pan2d281d82013-10-17 10:28:35 -070041/* bitmasks for RAPL MSRs, used by primitive access functions */
42#define ENERGY_STATUS_MASK 0xffffffff
43
44#define POWER_LIMIT1_MASK 0x7FFF
45#define POWER_LIMIT1_ENABLE BIT(15)
46#define POWER_LIMIT1_CLAMP BIT(16)
47
48#define POWER_LIMIT2_MASK (0x7FFFULL<<32)
49#define POWER_LIMIT2_ENABLE BIT_ULL(47)
50#define POWER_LIMIT2_CLAMP BIT_ULL(48)
51#define POWER_PACKAGE_LOCK BIT_ULL(63)
52#define POWER_PP_LOCK BIT(31)
53
54#define TIME_WINDOW1_MASK (0x7FULL<<17)
55#define TIME_WINDOW2_MASK (0x7FULL<<49)
56
57#define POWER_UNIT_OFFSET 0
58#define POWER_UNIT_MASK 0x0F
59
60#define ENERGY_UNIT_OFFSET 0x08
61#define ENERGY_UNIT_MASK 0x1F00
62
63#define TIME_UNIT_OFFSET 0x10
64#define TIME_UNIT_MASK 0xF0000
65
66#define POWER_INFO_MAX_MASK (0x7fffULL<<32)
67#define POWER_INFO_MIN_MASK (0x7fffULL<<16)
68#define POWER_INFO_MAX_TIME_WIN_MASK (0x3fULL<<48)
69#define POWER_INFO_THERMAL_SPEC_MASK 0x7fff
70
71#define PERF_STATUS_THROTTLE_TIME_MASK 0xffffffff
72#define PP_POLICY_MASK 0x1F
73
74/* Non HW constants */
75#define RAPL_PRIMITIVE_DERIVED BIT(1) /* not from raw data */
76#define RAPL_PRIMITIVE_DUMMY BIT(2)
77
Jacob Pan2d281d82013-10-17 10:28:35 -070078#define TIME_WINDOW_MAX_MSEC 40000
79#define TIME_WINDOW_MIN_MSEC 250
Jacob Pand474a4d2015-03-13 03:48:56 -070080#define ENERGY_UNIT_SCALE 1000 /* scale from driver unit to powercap unit */
Jacob Pan2d281d82013-10-17 10:28:35 -070081enum unit_type {
82 ARBITRARY_UNIT, /* no translation */
83 POWER_UNIT,
84 ENERGY_UNIT,
85 TIME_UNIT,
86};
87
88enum rapl_domain_type {
89 RAPL_DOMAIN_PACKAGE, /* entire package/socket */
90 RAPL_DOMAIN_PP0, /* core power plane */
91 RAPL_DOMAIN_PP1, /* graphics uncore */
92 RAPL_DOMAIN_DRAM,/* DRAM control_type */
Srinivas Pandruvada3521ba12016-04-17 15:03:01 -070093 RAPL_DOMAIN_PLATFORM, /* PSys control_type */
Jacob Pan2d281d82013-10-17 10:28:35 -070094 RAPL_DOMAIN_MAX,
95};
96
97enum rapl_domain_msr_id {
98 RAPL_DOMAIN_MSR_LIMIT,
99 RAPL_DOMAIN_MSR_STATUS,
100 RAPL_DOMAIN_MSR_PERF,
101 RAPL_DOMAIN_MSR_POLICY,
102 RAPL_DOMAIN_MSR_INFO,
103 RAPL_DOMAIN_MSR_MAX,
104};
105
106/* per domain data, some are optional */
107enum rapl_primitives {
108 ENERGY_COUNTER,
109 POWER_LIMIT1,
110 POWER_LIMIT2,
111 FW_LOCK,
112
113 PL1_ENABLE, /* power limit 1, aka long term */
114 PL1_CLAMP, /* allow frequency to go below OS request */
115 PL2_ENABLE, /* power limit 2, aka short term, instantaneous */
116 PL2_CLAMP,
117
118 TIME_WINDOW1, /* long term */
119 TIME_WINDOW2, /* short term */
120 THERMAL_SPEC_POWER,
121 MAX_POWER,
122
123 MIN_POWER,
124 MAX_TIME_WINDOW,
125 THROTTLED_TIME,
126 PRIORITY_LEVEL,
127
128 /* below are not raw primitive data */
129 AVERAGE_POWER,
130 NR_RAPL_PRIMITIVES,
131};
132
133#define NR_RAW_PRIMITIVES (NR_RAPL_PRIMITIVES - 2)
134
135/* Can be expanded to include events, etc.*/
136struct rapl_domain_data {
137 u64 primitives[NR_RAPL_PRIMITIVES];
138 unsigned long timestamp;
139};
140
Jacob Panf14a1392016-02-24 13:31:36 -0800141struct msrl_action {
142 u32 msr_no;
143 u64 clear_mask;
144 u64 set_mask;
145 int err;
146};
Jacob Pan2d281d82013-10-17 10:28:35 -0700147
148#define DOMAIN_STATE_INACTIVE BIT(0)
149#define DOMAIN_STATE_POWER_LIMIT_SET BIT(1)
150#define DOMAIN_STATE_BIOS_LOCKED BIT(2)
151
152#define NR_POWER_LIMITS (2)
153struct rapl_power_limit {
154 struct powercap_zone_constraint *constraint;
155 int prim_id; /* primitive ID used to enable */
156 struct rapl_domain *domain;
157 const char *name;
158};
159
160static const char pl1_name[] = "long_term";
161static const char pl2_name[] = "short_term";
162
Jacob Pan309557f2016-02-24 13:31:37 -0800163struct rapl_package;
Jacob Pan2d281d82013-10-17 10:28:35 -0700164struct rapl_domain {
165 const char *name;
166 enum rapl_domain_type id;
167 int msrs[RAPL_DOMAIN_MSR_MAX];
168 struct powercap_zone power_zone;
169 struct rapl_domain_data rdd;
170 struct rapl_power_limit rpl[NR_POWER_LIMITS];
171 u64 attr_map; /* track capabilities */
172 unsigned int state;
Jacob Pand474a4d2015-03-13 03:48:56 -0700173 unsigned int domain_energy_unit;
Jacob Pan309557f2016-02-24 13:31:37 -0800174 struct rapl_package *rp;
Jacob Pan2d281d82013-10-17 10:28:35 -0700175};
176#define power_zone_to_rapl_domain(_zone) \
177 container_of(_zone, struct rapl_domain, power_zone)
178
179
180/* Each physical package contains multiple domains, these are the common
181 * data across RAPL domains within a package.
182 */
183struct rapl_package {
184 unsigned int id; /* physical package/socket id */
185 unsigned int nr_domains;
186 unsigned long domain_map; /* bit map of active domains */
Jacob Pan3c2c0842014-11-07 09:29:26 -0800187 unsigned int power_unit;
188 unsigned int energy_unit;
189 unsigned int time_unit;
Jacob Pan2d281d82013-10-17 10:28:35 -0700190 struct rapl_domain *domains; /* array of domains, sized at runtime */
191 struct powercap_zone *power_zone; /* keep track of parent zone */
192 int nr_cpus; /* active cpus on the package, topology info is lost during
193 * cpu hotplug. so we have to track ourselves.
194 */
195 unsigned long power_limit_irq; /* keep track of package power limit
196 * notify interrupt enable status.
197 */
198 struct list_head plist;
Jacob Pan323ee642016-02-24 13:31:38 -0800199 int lead_cpu; /* one active cpu per package for access */
Jacob Pan2d281d82013-10-17 10:28:35 -0700200};
Jacob Pan087e9cb2014-11-07 09:29:25 -0800201
202struct rapl_defaults {
Ajay Thomas51b63402015-04-30 01:43:23 +0530203 u8 floor_freq_reg_addr;
Jacob Pan087e9cb2014-11-07 09:29:25 -0800204 int (*check_unit)(struct rapl_package *rp, int cpu);
205 void (*set_floor_freq)(struct rapl_domain *rd, bool mode);
206 u64 (*compute_time_window)(struct rapl_package *rp, u64 val,
207 bool to_raw);
Jacob Pand474a4d2015-03-13 03:48:56 -0700208 unsigned int dram_domain_energy_unit;
Jacob Pan087e9cb2014-11-07 09:29:25 -0800209};
210static struct rapl_defaults *rapl_defaults;
211
Jacob Pan3c2c0842014-11-07 09:29:26 -0800212/* Sideband MBI registers */
Ajay Thomas51b63402015-04-30 01:43:23 +0530213#define IOSF_CPU_POWER_BUDGET_CTL_BYT (0x2)
214#define IOSF_CPU_POWER_BUDGET_CTL_TNG (0xdf)
Jacob Pan3c2c0842014-11-07 09:29:26 -0800215
Jacob Pan2d281d82013-10-17 10:28:35 -0700216#define PACKAGE_PLN_INT_SAVED BIT(0)
217#define MAX_PRIM_NAME (32)
218
219/* per domain data. used to describe individual knobs such that access function
220 * can be consolidated into one instead of many inline functions.
221 */
222struct rapl_primitive_info {
223 const char *name;
224 u64 mask;
225 int shift;
226 enum rapl_domain_msr_id id;
227 enum unit_type unit;
228 u32 flag;
229};
230
231#define PRIMITIVE_INFO_INIT(p, m, s, i, u, f) { \
232 .name = #p, \
233 .mask = m, \
234 .shift = s, \
235 .id = i, \
236 .unit = u, \
237 .flag = f \
238 }
239
240static void rapl_init_domains(struct rapl_package *rp);
241static int rapl_read_data_raw(struct rapl_domain *rd,
242 enum rapl_primitives prim,
243 bool xlate, u64 *data);
244static int rapl_write_data_raw(struct rapl_domain *rd,
245 enum rapl_primitives prim,
246 unsigned long long value);
Jacob Pan309557f2016-02-24 13:31:37 -0800247static u64 rapl_unit_xlate(struct rapl_domain *rd,
Jacob Pand474a4d2015-03-13 03:48:56 -0700248 enum unit_type type, u64 value,
Jacob Pan2d281d82013-10-17 10:28:35 -0700249 int to_raw);
Jacob Pan309557f2016-02-24 13:31:37 -0800250static void package_power_limit_irq_save(struct rapl_package *rp);
Jacob Pan2d281d82013-10-17 10:28:35 -0700251
252static LIST_HEAD(rapl_packages); /* guarded by CPU hotplug lock */
253
254static const char * const rapl_domain_names[] = {
255 "package",
256 "core",
257 "uncore",
258 "dram",
Srinivas Pandruvada3521ba12016-04-17 15:03:01 -0700259 "psys",
Jacob Pan2d281d82013-10-17 10:28:35 -0700260};
261
262static struct powercap_control_type *control_type; /* PowerCap Controller */
Srinivas Pandruvada3521ba12016-04-17 15:03:01 -0700263static struct rapl_domain *platform_rapl_domain; /* Platform (PSys) domain */
Jacob Pan2d281d82013-10-17 10:28:35 -0700264
265/* caller to ensure CPU hotplug lock is held */
266static struct rapl_package *find_package_by_id(int id)
267{
268 struct rapl_package *rp;
269
270 list_for_each_entry(rp, &rapl_packages, plist) {
271 if (rp->id == id)
272 return rp;
273 }
274
275 return NULL;
276}
277
Jacob Pan2d281d82013-10-17 10:28:35 -0700278static int get_energy_counter(struct powercap_zone *power_zone, u64 *energy_raw)
279{
280 struct rapl_domain *rd;
281 u64 energy_now;
282
283 /* prevent CPU hotplug, make sure the RAPL domain does not go
284 * away while reading the counter.
285 */
286 get_online_cpus();
287 rd = power_zone_to_rapl_domain(power_zone);
288
289 if (!rapl_read_data_raw(rd, ENERGY_COUNTER, true, &energy_now)) {
290 *energy_raw = energy_now;
291 put_online_cpus();
292
293 return 0;
294 }
295 put_online_cpus();
296
297 return -EIO;
298}
299
300static int get_max_energy_counter(struct powercap_zone *pcd_dev, u64 *energy)
301{
Jacob Pand474a4d2015-03-13 03:48:56 -0700302 struct rapl_domain *rd = power_zone_to_rapl_domain(pcd_dev);
303
Jacob Pan309557f2016-02-24 13:31:37 -0800304 *energy = rapl_unit_xlate(rd, ENERGY_UNIT, ENERGY_STATUS_MASK, 0);
Jacob Pan2d281d82013-10-17 10:28:35 -0700305 return 0;
306}
307
308static int release_zone(struct powercap_zone *power_zone)
309{
310 struct rapl_domain *rd = power_zone_to_rapl_domain(power_zone);
Jacob Pan309557f2016-02-24 13:31:37 -0800311 struct rapl_package *rp = rd->rp;
Jacob Pan2d281d82013-10-17 10:28:35 -0700312
313 /* package zone is the last zone of a package, we can free
314 * memory here since all children has been unregistered.
315 */
316 if (rd->id == RAPL_DOMAIN_PACKAGE) {
Jacob Pan2d281d82013-10-17 10:28:35 -0700317 kfree(rd);
318 rp->domains = NULL;
319 }
320
321 return 0;
322
323}
324
325static int find_nr_power_limit(struct rapl_domain *rd)
326{
Jacob Pane1399ba2016-05-31 13:41:29 -0700327 int i, nr_pl = 0;
Jacob Pan2d281d82013-10-17 10:28:35 -0700328
329 for (i = 0; i < NR_POWER_LIMITS; i++) {
Jacob Pane1399ba2016-05-31 13:41:29 -0700330 if (rd->rpl[i].name)
331 nr_pl++;
Jacob Pan2d281d82013-10-17 10:28:35 -0700332 }
333
Jacob Pane1399ba2016-05-31 13:41:29 -0700334 return nr_pl;
Jacob Pan2d281d82013-10-17 10:28:35 -0700335}
336
337static int set_domain_enable(struct powercap_zone *power_zone, bool mode)
338{
339 struct rapl_domain *rd = power_zone_to_rapl_domain(power_zone);
Jacob Pan2d281d82013-10-17 10:28:35 -0700340
341 if (rd->state & DOMAIN_STATE_BIOS_LOCKED)
342 return -EACCES;
Jacob Pan3c2c0842014-11-07 09:29:26 -0800343
Jacob Pan2d281d82013-10-17 10:28:35 -0700344 get_online_cpus();
Jacob Pan2d281d82013-10-17 10:28:35 -0700345 rapl_write_data_raw(rd, PL1_ENABLE, mode);
Ajay Thomas51b63402015-04-30 01:43:23 +0530346 if (rapl_defaults->set_floor_freq)
347 rapl_defaults->set_floor_freq(rd, mode);
Jacob Pan2d281d82013-10-17 10:28:35 -0700348 put_online_cpus();
349
350 return 0;
351}
352
353static int get_domain_enable(struct powercap_zone *power_zone, bool *mode)
354{
355 struct rapl_domain *rd = power_zone_to_rapl_domain(power_zone);
356 u64 val;
357
358 if (rd->state & DOMAIN_STATE_BIOS_LOCKED) {
359 *mode = false;
360 return 0;
361 }
362 get_online_cpus();
363 if (rapl_read_data_raw(rd, PL1_ENABLE, true, &val)) {
364 put_online_cpus();
365 return -EIO;
366 }
367 *mode = val;
368 put_online_cpus();
369
370 return 0;
371}
372
373/* per RAPL domain ops, in the order of rapl_domain_type */
Julia Lawall600c3952015-12-23 22:59:55 +0100374static const struct powercap_zone_ops zone_ops[] = {
Jacob Pan2d281d82013-10-17 10:28:35 -0700375 /* RAPL_DOMAIN_PACKAGE */
376 {
377 .get_energy_uj = get_energy_counter,
378 .get_max_energy_range_uj = get_max_energy_counter,
379 .release = release_zone,
380 .set_enable = set_domain_enable,
381 .get_enable = get_domain_enable,
382 },
383 /* RAPL_DOMAIN_PP0 */
384 {
385 .get_energy_uj = get_energy_counter,
386 .get_max_energy_range_uj = get_max_energy_counter,
387 .release = release_zone,
388 .set_enable = set_domain_enable,
389 .get_enable = get_domain_enable,
390 },
391 /* RAPL_DOMAIN_PP1 */
392 {
393 .get_energy_uj = get_energy_counter,
394 .get_max_energy_range_uj = get_max_energy_counter,
395 .release = release_zone,
396 .set_enable = set_domain_enable,
397 .get_enable = get_domain_enable,
398 },
399 /* RAPL_DOMAIN_DRAM */
400 {
401 .get_energy_uj = get_energy_counter,
402 .get_max_energy_range_uj = get_max_energy_counter,
403 .release = release_zone,
404 .set_enable = set_domain_enable,
405 .get_enable = get_domain_enable,
406 },
Srinivas Pandruvada3521ba12016-04-17 15:03:01 -0700407 /* RAPL_DOMAIN_PLATFORM */
408 {
409 .get_energy_uj = get_energy_counter,
410 .get_max_energy_range_uj = get_max_energy_counter,
411 .release = release_zone,
412 .set_enable = set_domain_enable,
413 .get_enable = get_domain_enable,
414 },
Jacob Pan2d281d82013-10-17 10:28:35 -0700415};
416
Jacob Pane1399ba2016-05-31 13:41:29 -0700417
418/*
419 * Constraint index used by powercap can be different than power limit (PL)
420 * index in that some PLs maybe missing due to non-existant MSRs. So we
421 * need to convert here by finding the valid PLs only (name populated).
422 */
423static int contraint_to_pl(struct rapl_domain *rd, int cid)
424{
425 int i, j;
426
427 for (i = 0, j = 0; i < NR_POWER_LIMITS; i++) {
428 if ((rd->rpl[i].name) && j++ == cid) {
429 pr_debug("%s: index %d\n", __func__, i);
430 return i;
431 }
432 }
433
434 return -EINVAL;
435}
436
437static int set_power_limit(struct powercap_zone *power_zone, int cid,
Jacob Pan2d281d82013-10-17 10:28:35 -0700438 u64 power_limit)
439{
440 struct rapl_domain *rd;
441 struct rapl_package *rp;
442 int ret = 0;
Jacob Pane1399ba2016-05-31 13:41:29 -0700443 int id;
Jacob Pan2d281d82013-10-17 10:28:35 -0700444
445 get_online_cpus();
446 rd = power_zone_to_rapl_domain(power_zone);
Jacob Pane1399ba2016-05-31 13:41:29 -0700447 id = contraint_to_pl(rd, cid);
448
Jacob Pan309557f2016-02-24 13:31:37 -0800449 rp = rd->rp;
Jacob Pan2d281d82013-10-17 10:28:35 -0700450
451 if (rd->state & DOMAIN_STATE_BIOS_LOCKED) {
452 dev_warn(&power_zone->dev, "%s locked by BIOS, monitoring only\n",
453 rd->name);
454 ret = -EACCES;
455 goto set_exit;
456 }
457
458 switch (rd->rpl[id].prim_id) {
459 case PL1_ENABLE:
460 rapl_write_data_raw(rd, POWER_LIMIT1, power_limit);
461 break;
462 case PL2_ENABLE:
463 rapl_write_data_raw(rd, POWER_LIMIT2, power_limit);
464 break;
465 default:
466 ret = -EINVAL;
467 }
468 if (!ret)
Jacob Pan309557f2016-02-24 13:31:37 -0800469 package_power_limit_irq_save(rp);
Jacob Pan2d281d82013-10-17 10:28:35 -0700470set_exit:
471 put_online_cpus();
472 return ret;
473}
474
Jacob Pane1399ba2016-05-31 13:41:29 -0700475static int get_current_power_limit(struct powercap_zone *power_zone, int cid,
Jacob Pan2d281d82013-10-17 10:28:35 -0700476 u64 *data)
477{
478 struct rapl_domain *rd;
479 u64 val;
480 int prim;
481 int ret = 0;
Jacob Pane1399ba2016-05-31 13:41:29 -0700482 int id;
Jacob Pan2d281d82013-10-17 10:28:35 -0700483
484 get_online_cpus();
485 rd = power_zone_to_rapl_domain(power_zone);
Jacob Pane1399ba2016-05-31 13:41:29 -0700486 id = contraint_to_pl(rd, cid);
Jacob Pan2d281d82013-10-17 10:28:35 -0700487 switch (rd->rpl[id].prim_id) {
488 case PL1_ENABLE:
489 prim = POWER_LIMIT1;
490 break;
491 case PL2_ENABLE:
492 prim = POWER_LIMIT2;
493 break;
494 default:
495 put_online_cpus();
496 return -EINVAL;
497 }
498 if (rapl_read_data_raw(rd, prim, true, &val))
499 ret = -EIO;
500 else
501 *data = val;
502
503 put_online_cpus();
504
505 return ret;
506}
507
Jacob Pane1399ba2016-05-31 13:41:29 -0700508static int set_time_window(struct powercap_zone *power_zone, int cid,
Jacob Pan2d281d82013-10-17 10:28:35 -0700509 u64 window)
510{
511 struct rapl_domain *rd;
512 int ret = 0;
Jacob Pane1399ba2016-05-31 13:41:29 -0700513 int id;
Jacob Pan2d281d82013-10-17 10:28:35 -0700514
515 get_online_cpus();
516 rd = power_zone_to_rapl_domain(power_zone);
Jacob Pane1399ba2016-05-31 13:41:29 -0700517 id = contraint_to_pl(rd, cid);
518
Jacob Pan2d281d82013-10-17 10:28:35 -0700519 switch (rd->rpl[id].prim_id) {
520 case PL1_ENABLE:
521 rapl_write_data_raw(rd, TIME_WINDOW1, window);
522 break;
523 case PL2_ENABLE:
524 rapl_write_data_raw(rd, TIME_WINDOW2, window);
525 break;
526 default:
527 ret = -EINVAL;
528 }
529 put_online_cpus();
530 return ret;
531}
532
Jacob Pane1399ba2016-05-31 13:41:29 -0700533static int get_time_window(struct powercap_zone *power_zone, int cid, u64 *data)
Jacob Pan2d281d82013-10-17 10:28:35 -0700534{
535 struct rapl_domain *rd;
536 u64 val;
537 int ret = 0;
Jacob Pane1399ba2016-05-31 13:41:29 -0700538 int id;
Jacob Pan2d281d82013-10-17 10:28:35 -0700539
540 get_online_cpus();
541 rd = power_zone_to_rapl_domain(power_zone);
Jacob Pane1399ba2016-05-31 13:41:29 -0700542 id = contraint_to_pl(rd, cid);
543
Jacob Pan2d281d82013-10-17 10:28:35 -0700544 switch (rd->rpl[id].prim_id) {
545 case PL1_ENABLE:
546 ret = rapl_read_data_raw(rd, TIME_WINDOW1, true, &val);
547 break;
548 case PL2_ENABLE:
549 ret = rapl_read_data_raw(rd, TIME_WINDOW2, true, &val);
550 break;
551 default:
552 put_online_cpus();
553 return -EINVAL;
554 }
555 if (!ret)
556 *data = val;
557 put_online_cpus();
558
559 return ret;
560}
561
Jacob Pane1399ba2016-05-31 13:41:29 -0700562static const char *get_constraint_name(struct powercap_zone *power_zone, int cid)
Jacob Pan2d281d82013-10-17 10:28:35 -0700563{
Jacob Pan2d281d82013-10-17 10:28:35 -0700564 struct rapl_domain *rd;
Jacob Pane1399ba2016-05-31 13:41:29 -0700565 int id;
Jacob Pan2d281d82013-10-17 10:28:35 -0700566
567 rd = power_zone_to_rapl_domain(power_zone);
Jacob Pane1399ba2016-05-31 13:41:29 -0700568 id = contraint_to_pl(rd, cid);
569 if (id >= 0)
570 return rd->rpl[id].name;
Jacob Pan2d281d82013-10-17 10:28:35 -0700571
Jacob Pane1399ba2016-05-31 13:41:29 -0700572 return NULL;
Jacob Pan2d281d82013-10-17 10:28:35 -0700573}
574
575
576static int get_max_power(struct powercap_zone *power_zone, int id,
577 u64 *data)
578{
579 struct rapl_domain *rd;
580 u64 val;
581 int prim;
582 int ret = 0;
583
584 get_online_cpus();
585 rd = power_zone_to_rapl_domain(power_zone);
586 switch (rd->rpl[id].prim_id) {
587 case PL1_ENABLE:
588 prim = THERMAL_SPEC_POWER;
589 break;
590 case PL2_ENABLE:
591 prim = MAX_POWER;
592 break;
593 default:
594 put_online_cpus();
595 return -EINVAL;
596 }
597 if (rapl_read_data_raw(rd, prim, true, &val))
598 ret = -EIO;
599 else
600 *data = val;
601
602 put_online_cpus();
603
604 return ret;
605}
606
Julia Lawall600c3952015-12-23 22:59:55 +0100607static const struct powercap_zone_constraint_ops constraint_ops = {
Jacob Pan2d281d82013-10-17 10:28:35 -0700608 .set_power_limit_uw = set_power_limit,
609 .get_power_limit_uw = get_current_power_limit,
610 .set_time_window_us = set_time_window,
611 .get_time_window_us = get_time_window,
612 .get_max_power_uw = get_max_power,
613 .get_name = get_constraint_name,
614};
615
616/* called after domain detection and package level data are set */
617static void rapl_init_domains(struct rapl_package *rp)
618{
619 int i;
620 struct rapl_domain *rd = rp->domains;
621
622 for (i = 0; i < RAPL_DOMAIN_MAX; i++) {
623 unsigned int mask = rp->domain_map & (1 << i);
624 switch (mask) {
625 case BIT(RAPL_DOMAIN_PACKAGE):
626 rd->name = rapl_domain_names[RAPL_DOMAIN_PACKAGE];
627 rd->id = RAPL_DOMAIN_PACKAGE;
628 rd->msrs[0] = MSR_PKG_POWER_LIMIT;
629 rd->msrs[1] = MSR_PKG_ENERGY_STATUS;
630 rd->msrs[2] = MSR_PKG_PERF_STATUS;
631 rd->msrs[3] = 0;
632 rd->msrs[4] = MSR_PKG_POWER_INFO;
633 rd->rpl[0].prim_id = PL1_ENABLE;
634 rd->rpl[0].name = pl1_name;
635 rd->rpl[1].prim_id = PL2_ENABLE;
636 rd->rpl[1].name = pl2_name;
637 break;
638 case BIT(RAPL_DOMAIN_PP0):
639 rd->name = rapl_domain_names[RAPL_DOMAIN_PP0];
640 rd->id = RAPL_DOMAIN_PP0;
641 rd->msrs[0] = MSR_PP0_POWER_LIMIT;
642 rd->msrs[1] = MSR_PP0_ENERGY_STATUS;
643 rd->msrs[2] = 0;
644 rd->msrs[3] = MSR_PP0_POLICY;
645 rd->msrs[4] = 0;
646 rd->rpl[0].prim_id = PL1_ENABLE;
647 rd->rpl[0].name = pl1_name;
648 break;
649 case BIT(RAPL_DOMAIN_PP1):
650 rd->name = rapl_domain_names[RAPL_DOMAIN_PP1];
651 rd->id = RAPL_DOMAIN_PP1;
652 rd->msrs[0] = MSR_PP1_POWER_LIMIT;
653 rd->msrs[1] = MSR_PP1_ENERGY_STATUS;
654 rd->msrs[2] = 0;
655 rd->msrs[3] = MSR_PP1_POLICY;
656 rd->msrs[4] = 0;
657 rd->rpl[0].prim_id = PL1_ENABLE;
658 rd->rpl[0].name = pl1_name;
659 break;
660 case BIT(RAPL_DOMAIN_DRAM):
661 rd->name = rapl_domain_names[RAPL_DOMAIN_DRAM];
662 rd->id = RAPL_DOMAIN_DRAM;
663 rd->msrs[0] = MSR_DRAM_POWER_LIMIT;
664 rd->msrs[1] = MSR_DRAM_ENERGY_STATUS;
665 rd->msrs[2] = MSR_DRAM_PERF_STATUS;
666 rd->msrs[3] = 0;
667 rd->msrs[4] = MSR_DRAM_POWER_INFO;
668 rd->rpl[0].prim_id = PL1_ENABLE;
669 rd->rpl[0].name = pl1_name;
Jacob Pand474a4d2015-03-13 03:48:56 -0700670 rd->domain_energy_unit =
671 rapl_defaults->dram_domain_energy_unit;
672 if (rd->domain_energy_unit)
673 pr_info("DRAM domain energy unit %dpj\n",
674 rd->domain_energy_unit);
Jacob Pan2d281d82013-10-17 10:28:35 -0700675 break;
676 }
677 if (mask) {
Jacob Pan309557f2016-02-24 13:31:37 -0800678 rd->rp = rp;
Jacob Pan2d281d82013-10-17 10:28:35 -0700679 rd++;
680 }
681 }
682}
683
Jacob Pan309557f2016-02-24 13:31:37 -0800684static u64 rapl_unit_xlate(struct rapl_domain *rd, enum unit_type type,
685 u64 value, int to_raw)
Jacob Pan2d281d82013-10-17 10:28:35 -0700686{
Jacob Pan3c2c0842014-11-07 09:29:26 -0800687 u64 units = 1;
Jacob Pan309557f2016-02-24 13:31:37 -0800688 struct rapl_package *rp = rd->rp;
Jacob Pand474a4d2015-03-13 03:48:56 -0700689 u64 scale = 1;
Jacob Pan2d281d82013-10-17 10:28:35 -0700690
Jacob Pan2d281d82013-10-17 10:28:35 -0700691 switch (type) {
692 case POWER_UNIT:
Jacob Pan3c2c0842014-11-07 09:29:26 -0800693 units = rp->power_unit;
Jacob Pan2d281d82013-10-17 10:28:35 -0700694 break;
695 case ENERGY_UNIT:
Jacob Pand474a4d2015-03-13 03:48:56 -0700696 scale = ENERGY_UNIT_SCALE;
697 /* per domain unit takes precedence */
698 if (rd && rd->domain_energy_unit)
699 units = rd->domain_energy_unit;
700 else
701 units = rp->energy_unit;
Jacob Pan2d281d82013-10-17 10:28:35 -0700702 break;
703 case TIME_UNIT:
Jacob Pan3c2c0842014-11-07 09:29:26 -0800704 return rapl_defaults->compute_time_window(rp, value, to_raw);
Jacob Pan2d281d82013-10-17 10:28:35 -0700705 case ARBITRARY_UNIT:
706 default:
707 return value;
708 };
709
710 if (to_raw)
Jacob Pand474a4d2015-03-13 03:48:56 -0700711 return div64_u64(value, units) * scale;
Jacob Pan3c2c0842014-11-07 09:29:26 -0800712
713 value *= units;
714
Jacob Pand474a4d2015-03-13 03:48:56 -0700715 return div64_u64(value, scale);
Jacob Pan2d281d82013-10-17 10:28:35 -0700716}
717
718/* in the order of enum rapl_primitives */
719static struct rapl_primitive_info rpi[] = {
720 /* name, mask, shift, msr index, unit divisor */
721 PRIMITIVE_INFO_INIT(ENERGY_COUNTER, ENERGY_STATUS_MASK, 0,
722 RAPL_DOMAIN_MSR_STATUS, ENERGY_UNIT, 0),
723 PRIMITIVE_INFO_INIT(POWER_LIMIT1, POWER_LIMIT1_MASK, 0,
724 RAPL_DOMAIN_MSR_LIMIT, POWER_UNIT, 0),
725 PRIMITIVE_INFO_INIT(POWER_LIMIT2, POWER_LIMIT2_MASK, 32,
726 RAPL_DOMAIN_MSR_LIMIT, POWER_UNIT, 0),
727 PRIMITIVE_INFO_INIT(FW_LOCK, POWER_PP_LOCK, 31,
728 RAPL_DOMAIN_MSR_LIMIT, ARBITRARY_UNIT, 0),
729 PRIMITIVE_INFO_INIT(PL1_ENABLE, POWER_LIMIT1_ENABLE, 15,
730 RAPL_DOMAIN_MSR_LIMIT, ARBITRARY_UNIT, 0),
731 PRIMITIVE_INFO_INIT(PL1_CLAMP, POWER_LIMIT1_CLAMP, 16,
732 RAPL_DOMAIN_MSR_LIMIT, ARBITRARY_UNIT, 0),
733 PRIMITIVE_INFO_INIT(PL2_ENABLE, POWER_LIMIT2_ENABLE, 47,
734 RAPL_DOMAIN_MSR_LIMIT, ARBITRARY_UNIT, 0),
735 PRIMITIVE_INFO_INIT(PL2_CLAMP, POWER_LIMIT2_CLAMP, 48,
736 RAPL_DOMAIN_MSR_LIMIT, ARBITRARY_UNIT, 0),
737 PRIMITIVE_INFO_INIT(TIME_WINDOW1, TIME_WINDOW1_MASK, 17,
738 RAPL_DOMAIN_MSR_LIMIT, TIME_UNIT, 0),
739 PRIMITIVE_INFO_INIT(TIME_WINDOW2, TIME_WINDOW2_MASK, 49,
740 RAPL_DOMAIN_MSR_LIMIT, TIME_UNIT, 0),
741 PRIMITIVE_INFO_INIT(THERMAL_SPEC_POWER, POWER_INFO_THERMAL_SPEC_MASK,
742 0, RAPL_DOMAIN_MSR_INFO, POWER_UNIT, 0),
743 PRIMITIVE_INFO_INIT(MAX_POWER, POWER_INFO_MAX_MASK, 32,
744 RAPL_DOMAIN_MSR_INFO, POWER_UNIT, 0),
745 PRIMITIVE_INFO_INIT(MIN_POWER, POWER_INFO_MIN_MASK, 16,
746 RAPL_DOMAIN_MSR_INFO, POWER_UNIT, 0),
747 PRIMITIVE_INFO_INIT(MAX_TIME_WINDOW, POWER_INFO_MAX_TIME_WIN_MASK, 48,
748 RAPL_DOMAIN_MSR_INFO, TIME_UNIT, 0),
749 PRIMITIVE_INFO_INIT(THROTTLED_TIME, PERF_STATUS_THROTTLE_TIME_MASK, 0,
750 RAPL_DOMAIN_MSR_PERF, TIME_UNIT, 0),
751 PRIMITIVE_INFO_INIT(PRIORITY_LEVEL, PP_POLICY_MASK, 0,
752 RAPL_DOMAIN_MSR_POLICY, ARBITRARY_UNIT, 0),
753 /* non-hardware */
754 PRIMITIVE_INFO_INIT(AVERAGE_POWER, 0, 0, 0, POWER_UNIT,
755 RAPL_PRIMITIVE_DERIVED),
756 {NULL, 0, 0, 0},
757};
758
759/* Read primitive data based on its related struct rapl_primitive_info.
760 * if xlate flag is set, return translated data based on data units, i.e.
761 * time, energy, and power.
762 * RAPL MSRs are non-architectual and are laid out not consistently across
763 * domains. Here we use primitive info to allow writing consolidated access
764 * functions.
765 * For a given primitive, it is processed by MSR mask and shift. Unit conversion
766 * is pre-assigned based on RAPL unit MSRs read at init time.
767 * 63-------------------------- 31--------------------------- 0
768 * | xxxxx (mask) |
769 * | |<- shift ----------------|
770 * 63-------------------------- 31--------------------------- 0
771 */
772static int rapl_read_data_raw(struct rapl_domain *rd,
773 enum rapl_primitives prim,
774 bool xlate, u64 *data)
775{
776 u64 value, final;
777 u32 msr;
778 struct rapl_primitive_info *rp = &rpi[prim];
779 int cpu;
780
781 if (!rp->name || rp->flag & RAPL_PRIMITIVE_DUMMY)
782 return -EINVAL;
783
784 msr = rd->msrs[rp->id];
785 if (!msr)
786 return -EINVAL;
Jacob Pan323ee642016-02-24 13:31:38 -0800787
788 cpu = rd->rp->lead_cpu;
Jacob Pan2d281d82013-10-17 10:28:35 -0700789
790 /* special-case package domain, which uses a different bit*/
791 if (prim == FW_LOCK && rd->id == RAPL_DOMAIN_PACKAGE) {
792 rp->mask = POWER_PACKAGE_LOCK;
793 rp->shift = 63;
794 }
795 /* non-hardware data are collected by the polling thread */
796 if (rp->flag & RAPL_PRIMITIVE_DERIVED) {
797 *data = rd->rdd.primitives[prim];
798 return 0;
799 }
800
801 if (rdmsrl_safe_on_cpu(cpu, msr, &value)) {
802 pr_debug("failed to read msr 0x%x on cpu %d\n", msr, cpu);
803 return -EIO;
804 }
805
806 final = value & rp->mask;
807 final = final >> rp->shift;
808 if (xlate)
Jacob Pan309557f2016-02-24 13:31:37 -0800809 *data = rapl_unit_xlate(rd, rp->unit, final, 0);
Jacob Pan2d281d82013-10-17 10:28:35 -0700810 else
811 *data = final;
812
813 return 0;
814}
815
Jacob Panf14a1392016-02-24 13:31:36 -0800816
817static int msrl_update_safe(u32 msr_no, u64 clear_mask, u64 set_mask)
818{
819 int err;
820 u64 val;
821
822 err = rdmsrl_safe(msr_no, &val);
823 if (err)
824 goto out;
825
826 val &= ~clear_mask;
827 val |= set_mask;
828
829 err = wrmsrl_safe(msr_no, val);
830
831out:
832 return err;
833}
834
835static void msrl_update_func(void *info)
836{
837 struct msrl_action *ma = info;
838
839 ma->err = msrl_update_safe(ma->msr_no, ma->clear_mask, ma->set_mask);
840}
841
Jacob Pan2d281d82013-10-17 10:28:35 -0700842/* Similar use of primitive info in the read counterpart */
843static int rapl_write_data_raw(struct rapl_domain *rd,
844 enum rapl_primitives prim,
845 unsigned long long value)
846{
Jacob Pan2d281d82013-10-17 10:28:35 -0700847 struct rapl_primitive_info *rp = &rpi[prim];
848 int cpu;
Jacob Panf14a1392016-02-24 13:31:36 -0800849 u64 bits;
850 struct msrl_action ma;
851 int ret;
Jacob Pan2d281d82013-10-17 10:28:35 -0700852
Jacob Pan323ee642016-02-24 13:31:38 -0800853 cpu = rd->rp->lead_cpu;
Jacob Pan309557f2016-02-24 13:31:37 -0800854 bits = rapl_unit_xlate(rd, rp->unit, value, 1);
Jacob Panf14a1392016-02-24 13:31:36 -0800855 bits |= bits << rp->shift;
856 memset(&ma, 0, sizeof(ma));
857
858 ma.msr_no = rd->msrs[rp->id];
859 ma.clear_mask = rp->mask;
860 ma.set_mask = bits;
861
862 ret = smp_call_function_single(cpu, msrl_update_func, &ma, 1);
863 if (ret)
864 WARN_ON_ONCE(ret);
865 else
866 ret = ma.err;
867
868 return ret;
Jacob Pan2d281d82013-10-17 10:28:35 -0700869}
870
Jacob Pan3c2c0842014-11-07 09:29:26 -0800871/*
872 * Raw RAPL data stored in MSRs are in certain scales. We need to
873 * convert them into standard units based on the units reported in
874 * the RAPL unit MSRs. This is specific to CPUs as the method to
875 * calculate units differ on different CPUs.
876 * We convert the units to below format based on CPUs.
877 * i.e.
Jacob Pand474a4d2015-03-13 03:48:56 -0700878 * energy unit: picoJoules : Represented in picoJoules by default
Jacob Pan3c2c0842014-11-07 09:29:26 -0800879 * power unit : microWatts : Represented in milliWatts by default
880 * time unit : microseconds: Represented in seconds by default
881 */
882static int rapl_check_unit_core(struct rapl_package *rp, int cpu)
Jacob Pan2d281d82013-10-17 10:28:35 -0700883{
884 u64 msr_val;
885 u32 value;
886
887 if (rdmsrl_safe_on_cpu(cpu, MSR_RAPL_POWER_UNIT, &msr_val)) {
888 pr_err("Failed to read power unit MSR 0x%x on CPU %d, exit.\n",
889 MSR_RAPL_POWER_UNIT, cpu);
890 return -ENODEV;
891 }
892
Jacob Pan2d281d82013-10-17 10:28:35 -0700893 value = (msr_val & ENERGY_UNIT_MASK) >> ENERGY_UNIT_OFFSET;
Jacob Pand474a4d2015-03-13 03:48:56 -0700894 rp->energy_unit = ENERGY_UNIT_SCALE * 1000000 / (1 << value);
Jacob Pan2d281d82013-10-17 10:28:35 -0700895
896 value = (msr_val & POWER_UNIT_MASK) >> POWER_UNIT_OFFSET;
Jacob Pan3c2c0842014-11-07 09:29:26 -0800897 rp->power_unit = 1000000 / (1 << value);
Jacob Pan2d281d82013-10-17 10:28:35 -0700898
899 value = (msr_val & TIME_UNIT_MASK) >> TIME_UNIT_OFFSET;
Jacob Pan3c2c0842014-11-07 09:29:26 -0800900 rp->time_unit = 1000000 / (1 << value);
Jacob Pan2d281d82013-10-17 10:28:35 -0700901
Jacob Pand474a4d2015-03-13 03:48:56 -0700902 pr_debug("Core CPU package %d energy=%dpJ, time=%dus, power=%duW\n",
Jacob Pan3c2c0842014-11-07 09:29:26 -0800903 rp->id, rp->energy_unit, rp->time_unit, rp->power_unit);
Jacob Pan2d281d82013-10-17 10:28:35 -0700904
905 return 0;
906}
907
Jacob Pan3c2c0842014-11-07 09:29:26 -0800908static int rapl_check_unit_atom(struct rapl_package *rp, int cpu)
909{
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 value = (msr_val & ENERGY_UNIT_MASK) >> ENERGY_UNIT_OFFSET;
Jacob Pand474a4d2015-03-13 03:48:56 -0700919 rp->energy_unit = ENERGY_UNIT_SCALE * 1 << value;
Jacob Pan3c2c0842014-11-07 09:29:26 -0800920
921 value = (msr_val & POWER_UNIT_MASK) >> POWER_UNIT_OFFSET;
922 rp->power_unit = (1 << value) * 1000;
923
924 value = (msr_val & TIME_UNIT_MASK) >> TIME_UNIT_OFFSET;
925 rp->time_unit = 1000000 / (1 << value);
926
Jacob Pand474a4d2015-03-13 03:48:56 -0700927 pr_debug("Atom package %d energy=%dpJ, time=%dus, power=%duW\n",
Jacob Pan3c2c0842014-11-07 09:29:26 -0800928 rp->id, rp->energy_unit, rp->time_unit, rp->power_unit);
929
930 return 0;
931}
932
Jacob Panf14a1392016-02-24 13:31:36 -0800933static void power_limit_irq_save_cpu(void *info)
934{
935 u32 l, h = 0;
936 struct rapl_package *rp = (struct rapl_package *)info;
937
938 /* save the state of PLN irq mask bit before disabling it */
939 rdmsr_safe(MSR_IA32_PACKAGE_THERM_INTERRUPT, &l, &h);
940 if (!(rp->power_limit_irq & PACKAGE_PLN_INT_SAVED)) {
941 rp->power_limit_irq = l & PACKAGE_THERM_INT_PLN_ENABLE;
942 rp->power_limit_irq |= PACKAGE_PLN_INT_SAVED;
943 }
944 l &= ~PACKAGE_THERM_INT_PLN_ENABLE;
945 wrmsr_safe(MSR_IA32_PACKAGE_THERM_INTERRUPT, l, h);
946}
947
Jacob Pan3c2c0842014-11-07 09:29:26 -0800948
Jacob Pan2d281d82013-10-17 10:28:35 -0700949/* REVISIT:
950 * When package power limit is set artificially low by RAPL, LVT
951 * thermal interrupt for package power limit should be ignored
952 * since we are not really exceeding the real limit. The intention
953 * is to avoid excessive interrupts while we are trying to save power.
954 * A useful feature might be routing the package_power_limit interrupt
955 * to userspace via eventfd. once we have a usecase, this is simple
956 * to do by adding an atomic notifier.
957 */
958
Jacob Pan309557f2016-02-24 13:31:37 -0800959static void package_power_limit_irq_save(struct rapl_package *rp)
Jacob Pan2d281d82013-10-17 10:28:35 -0700960{
Jacob Pan2d281d82013-10-17 10:28:35 -0700961 if (!boot_cpu_has(X86_FEATURE_PTS) || !boot_cpu_has(X86_FEATURE_PLN))
962 return;
963
Jacob Pan323ee642016-02-24 13:31:38 -0800964 smp_call_function_single(rp->lead_cpu, power_limit_irq_save_cpu, rp, 1);
Jacob Panf14a1392016-02-24 13:31:36 -0800965}
966
Thomas Gleixner58705062016-11-22 21:16:02 +0000967/*
968 * Restore per package power limit interrupt enable state. Called from cpu
969 * hotplug code on package removal.
970 */
971static void package_power_limit_irq_restore(struct rapl_package *rp)
Jacob Panf14a1392016-02-24 13:31:36 -0800972{
Thomas Gleixner58705062016-11-22 21:16:02 +0000973 u32 l, h;
974
975 if (!boot_cpu_has(X86_FEATURE_PTS) || !boot_cpu_has(X86_FEATURE_PLN))
976 return;
977
978 /* irq enable state not saved, nothing to restore */
979 if (!(rp->power_limit_irq & PACKAGE_PLN_INT_SAVED))
980 return;
Jacob Panf14a1392016-02-24 13:31:36 -0800981
982 rdmsr_safe(MSR_IA32_PACKAGE_THERM_INTERRUPT, &l, &h);
983
984 if (rp->power_limit_irq & PACKAGE_THERM_INT_PLN_ENABLE)
985 l |= PACKAGE_THERM_INT_PLN_ENABLE;
986 else
987 l &= ~PACKAGE_THERM_INT_PLN_ENABLE;
988
989 wrmsr_safe(MSR_IA32_PACKAGE_THERM_INTERRUPT, l, h);
Jacob Pan2d281d82013-10-17 10:28:35 -0700990}
991
Jacob Pan3c2c0842014-11-07 09:29:26 -0800992static void set_floor_freq_default(struct rapl_domain *rd, bool mode)
993{
994 int nr_powerlimit = find_nr_power_limit(rd);
995
996 /* always enable clamp such that p-state can go below OS requested
997 * range. power capping priority over guranteed frequency.
998 */
999 rapl_write_data_raw(rd, PL1_CLAMP, mode);
1000
1001 /* some domains have pl2 */
1002 if (nr_powerlimit > 1) {
1003 rapl_write_data_raw(rd, PL2_ENABLE, mode);
1004 rapl_write_data_raw(rd, PL2_CLAMP, mode);
1005 }
1006}
1007
1008static void set_floor_freq_atom(struct rapl_domain *rd, bool enable)
1009{
1010 static u32 power_ctrl_orig_val;
1011 u32 mdata;
1012
Ajay Thomas51b63402015-04-30 01:43:23 +05301013 if (!rapl_defaults->floor_freq_reg_addr) {
1014 pr_err("Invalid floor frequency config register\n");
1015 return;
1016 }
1017
Jacob Pan3c2c0842014-11-07 09:29:26 -08001018 if (!power_ctrl_orig_val)
Andy Shevchenko4077a382015-11-11 19:59:29 +02001019 iosf_mbi_read(BT_MBI_UNIT_PMC, MBI_CR_READ,
1020 rapl_defaults->floor_freq_reg_addr,
1021 &power_ctrl_orig_val);
Jacob Pan3c2c0842014-11-07 09:29:26 -08001022 mdata = power_ctrl_orig_val;
1023 if (enable) {
1024 mdata &= ~(0x7f << 8);
1025 mdata |= 1 << 8;
1026 }
Andy Shevchenko4077a382015-11-11 19:59:29 +02001027 iosf_mbi_write(BT_MBI_UNIT_PMC, MBI_CR_WRITE,
1028 rapl_defaults->floor_freq_reg_addr, mdata);
Jacob Pan3c2c0842014-11-07 09:29:26 -08001029}
1030
1031static u64 rapl_compute_time_window_core(struct rapl_package *rp, u64 value,
1032 bool to_raw)
1033{
1034 u64 f, y; /* fraction and exp. used for time unit */
1035
1036 /*
1037 * Special processing based on 2^Y*(1+F/4), refer
1038 * to Intel Software Developer's manual Vol.3B: CH 14.9.3.
1039 */
1040 if (!to_raw) {
1041 f = (value & 0x60) >> 5;
1042 y = value & 0x1f;
1043 value = (1 << y) * (4 + f) * rp->time_unit / 4;
1044 } else {
1045 do_div(value, rp->time_unit);
1046 y = ilog2(value);
1047 f = div64_u64(4 * (value - (1 << y)), 1 << y);
1048 value = (y & 0x1f) | ((f & 0x3) << 5);
1049 }
1050 return value;
1051}
1052
1053static u64 rapl_compute_time_window_atom(struct rapl_package *rp, u64 value,
1054 bool to_raw)
1055{
1056 /*
1057 * Atom time unit encoding is straight forward val * time_unit,
1058 * where time_unit is default to 1 sec. Never 0.
1059 */
1060 if (!to_raw)
1061 return (value) ? value *= rp->time_unit : rp->time_unit;
1062 else
1063 value = div64_u64(value, rp->time_unit);
1064
1065 return value;
1066}
1067
Jacob Pan087e9cb2014-11-07 09:29:25 -08001068static const struct rapl_defaults rapl_defaults_core = {
Ajay Thomas51b63402015-04-30 01:43:23 +05301069 .floor_freq_reg_addr = 0,
Jacob Pan3c2c0842014-11-07 09:29:26 -08001070 .check_unit = rapl_check_unit_core,
1071 .set_floor_freq = set_floor_freq_default,
1072 .compute_time_window = rapl_compute_time_window_core,
Jacob Pan087e9cb2014-11-07 09:29:25 -08001073};
1074
Jacob Pand474a4d2015-03-13 03:48:56 -07001075static const struct rapl_defaults rapl_defaults_hsw_server = {
1076 .check_unit = rapl_check_unit_core,
1077 .set_floor_freq = set_floor_freq_default,
1078 .compute_time_window = rapl_compute_time_window_core,
1079 .dram_domain_energy_unit = 15300,
1080};
1081
Ajay Thomas51b63402015-04-30 01:43:23 +05301082static const struct rapl_defaults rapl_defaults_byt = {
1083 .floor_freq_reg_addr = IOSF_CPU_POWER_BUDGET_CTL_BYT,
Jacob Pan3c2c0842014-11-07 09:29:26 -08001084 .check_unit = rapl_check_unit_atom,
1085 .set_floor_freq = set_floor_freq_atom,
1086 .compute_time_window = rapl_compute_time_window_atom,
Jacob Pan087e9cb2014-11-07 09:29:25 -08001087};
1088
Ajay Thomas51b63402015-04-30 01:43:23 +05301089static const struct rapl_defaults rapl_defaults_tng = {
1090 .floor_freq_reg_addr = IOSF_CPU_POWER_BUDGET_CTL_TNG,
1091 .check_unit = rapl_check_unit_atom,
1092 .set_floor_freq = set_floor_freq_atom,
1093 .compute_time_window = rapl_compute_time_window_atom,
1094};
1095
1096static const struct rapl_defaults rapl_defaults_ann = {
1097 .floor_freq_reg_addr = 0,
1098 .check_unit = rapl_check_unit_atom,
1099 .set_floor_freq = NULL,
1100 .compute_time_window = rapl_compute_time_window_atom,
1101};
1102
1103static const struct rapl_defaults rapl_defaults_cht = {
1104 .floor_freq_reg_addr = 0,
1105 .check_unit = rapl_check_unit_atom,
1106 .set_floor_freq = NULL,
1107 .compute_time_window = rapl_compute_time_window_atom,
1108};
1109
Jacob Pan087e9cb2014-11-07 09:29:25 -08001110#define RAPL_CPU(_model, _ops) { \
1111 .vendor = X86_VENDOR_INTEL, \
1112 .family = 6, \
1113 .model = _model, \
1114 .driver_data = (kernel_ulong_t)&_ops, \
1115 }
1116
Mathias Krauseea85dbc2015-03-25 22:15:52 +01001117static const struct x86_cpu_id rapl_ids[] __initconst = {
Dave Hansen62d16732016-06-02 17:19:36 -07001118 RAPL_CPU(INTEL_FAM6_SANDYBRIDGE, rapl_defaults_core),
1119 RAPL_CPU(INTEL_FAM6_SANDYBRIDGE_X, rapl_defaults_core),
Dave Hansen0bb04b52016-06-02 17:19:37 -07001120
Dave Hansen62d16732016-06-02 17:19:36 -07001121 RAPL_CPU(INTEL_FAM6_IVYBRIDGE, rapl_defaults_core),
Xiaolong Wang7d188472016-06-24 11:28:20 +08001122 RAPL_CPU(INTEL_FAM6_IVYBRIDGE_X, rapl_defaults_core),
Dave Hansen0bb04b52016-06-02 17:19:37 -07001123
Dave Hansen62d16732016-06-02 17:19:36 -07001124 RAPL_CPU(INTEL_FAM6_HASWELL_CORE, rapl_defaults_core),
Dave Hansen62d16732016-06-02 17:19:36 -07001125 RAPL_CPU(INTEL_FAM6_HASWELL_ULT, rapl_defaults_core),
1126 RAPL_CPU(INTEL_FAM6_HASWELL_GT3E, rapl_defaults_core),
Dave Hansen0bb04b52016-06-02 17:19:37 -07001127 RAPL_CPU(INTEL_FAM6_HASWELL_X, rapl_defaults_hsw_server),
1128
1129 RAPL_CPU(INTEL_FAM6_BROADWELL_CORE, rapl_defaults_core),
Dave Hansen62d16732016-06-02 17:19:36 -07001130 RAPL_CPU(INTEL_FAM6_BROADWELL_GT3E, rapl_defaults_core),
Dave Hansen62d16732016-06-02 17:19:36 -07001131 RAPL_CPU(INTEL_FAM6_BROADWELL_XEON_D, rapl_defaults_core),
Dave Hansen0bb04b52016-06-02 17:19:37 -07001132 RAPL_CPU(INTEL_FAM6_BROADWELL_X, rapl_defaults_hsw_server),
1133
Dave Hansen62d16732016-06-02 17:19:36 -07001134 RAPL_CPU(INTEL_FAM6_SKYLAKE_DESKTOP, rapl_defaults_core),
Dave Hansen0bb04b52016-06-02 17:19:37 -07001135 RAPL_CPU(INTEL_FAM6_SKYLAKE_MOBILE, rapl_defaults_core),
Dave Hansend40671e2016-06-02 17:19:55 -07001136 RAPL_CPU(INTEL_FAM6_SKYLAKE_X, rapl_defaults_hsw_server),
Dave Hansen62d16732016-06-02 17:19:36 -07001137 RAPL_CPU(INTEL_FAM6_KABYLAKE_MOBILE, rapl_defaults_core),
1138 RAPL_CPU(INTEL_FAM6_KABYLAKE_DESKTOP, rapl_defaults_core),
Dave Hansen0bb04b52016-06-02 17:19:37 -07001139
1140 RAPL_CPU(INTEL_FAM6_ATOM_SILVERMONT1, rapl_defaults_byt),
1141 RAPL_CPU(INTEL_FAM6_ATOM_AIRMONT, rapl_defaults_cht),
Andy Shevchenkof5fbf842016-09-06 21:42:54 +03001142 RAPL_CPU(INTEL_FAM6_ATOM_MERRIFIELD, rapl_defaults_tng),
1143 RAPL_CPU(INTEL_FAM6_ATOM_MOOREFIELD, rapl_defaults_ann),
Dave Hansen0bb04b52016-06-02 17:19:37 -07001144 RAPL_CPU(INTEL_FAM6_ATOM_GOLDMONT, rapl_defaults_core),
Jacob Panab0d15d2016-06-13 17:02:28 -07001145 RAPL_CPU(INTEL_FAM6_ATOM_DENVERTON, rapl_defaults_core),
Dave Hansen0bb04b52016-06-02 17:19:37 -07001146
1147 RAPL_CPU(INTEL_FAM6_XEON_PHI_KNL, rapl_defaults_hsw_server),
Jacob Pan2d281d82013-10-17 10:28:35 -07001148 {}
1149};
1150MODULE_DEVICE_TABLE(x86cpu, rapl_ids);
1151
Thomas Gleixnerbed5ab62016-11-22 21:15:58 +00001152/* Read once for all raw primitive data for domains */
1153static void rapl_update_domain_data(struct rapl_package *rp)
Jacob Pan2d281d82013-10-17 10:28:35 -07001154{
1155 int dmn, prim;
1156 u64 val;
Jacob Pan2d281d82013-10-17 10:28:35 -07001157
Thomas Gleixnerbed5ab62016-11-22 21:15:58 +00001158 for (dmn = 0; dmn < rp->nr_domains; dmn++) {
1159 pr_debug("update package %d domain %s data\n", rp->id,
1160 rp->domains[dmn].name);
1161 /* exclude non-raw primitives */
1162 for (prim = 0; prim < NR_RAW_PRIMITIVES; prim++) {
1163 if (!rapl_read_data_raw(&rp->domains[dmn], prim,
1164 rpi[prim].unit, &val))
1165 rp->domains[dmn].rdd.primitives[prim] = val;
Jacob Pan2d281d82013-10-17 10:28:35 -07001166 }
1167 }
1168
1169}
1170
Thomas Gleixner58705062016-11-22 21:16:02 +00001171static void rapl_unregister_powercap(void)
Jacob Pan2d281d82013-10-17 10:28:35 -07001172{
Srinivas Pandruvada3521ba12016-04-17 15:03:01 -07001173 if (platform_rapl_domain) {
1174 powercap_unregister_zone(control_type,
1175 &platform_rapl_domain->power_zone);
1176 kfree(platform_rapl_domain);
1177 }
Jacob Pan2d281d82013-10-17 10:28:35 -07001178 powercap_unregister_control_type(control_type);
Jacob Pan2d281d82013-10-17 10:28:35 -07001179}
1180
1181static int rapl_package_register_powercap(struct rapl_package *rp)
1182{
1183 struct rapl_domain *rd;
Jacob Pan2d281d82013-10-17 10:28:35 -07001184 char dev_name[17]; /* max domain name = 7 + 1 + 8 for int + 1 for null*/
1185 struct powercap_zone *power_zone = NULL;
Thomas Gleixnerbed5ab62016-11-22 21:15:58 +00001186 int nr_pl, ret;;
1187
1188 /* Update the domain data of the new package */
1189 rapl_update_domain_data(rp);
Jacob Pan2d281d82013-10-17 10:28:35 -07001190
1191 /* first we register package domain as the parent zone*/
1192 for (rd = rp->domains; rd < rp->domains + rp->nr_domains; rd++) {
1193 if (rd->id == RAPL_DOMAIN_PACKAGE) {
1194 nr_pl = find_nr_power_limit(rd);
1195 pr_debug("register socket %d package domain %s\n",
1196 rp->id, rd->name);
1197 memset(dev_name, 0, sizeof(dev_name));
1198 snprintf(dev_name, sizeof(dev_name), "%s-%d",
1199 rd->name, rp->id);
1200 power_zone = powercap_register_zone(&rd->power_zone,
1201 control_type,
1202 dev_name, NULL,
1203 &zone_ops[rd->id],
1204 nr_pl,
1205 &constraint_ops);
1206 if (IS_ERR(power_zone)) {
1207 pr_debug("failed to register package, %d\n",
1208 rp->id);
Thomas Gleixnerbed5ab62016-11-22 21:15:58 +00001209 return PTR_ERR(power_zone);
Jacob Pan2d281d82013-10-17 10:28:35 -07001210 }
1211 /* track parent zone in per package/socket data */
1212 rp->power_zone = power_zone;
1213 /* done, only one package domain per socket */
1214 break;
1215 }
1216 }
1217 if (!power_zone) {
1218 pr_err("no package domain found, unknown topology!\n");
Thomas Gleixnerbed5ab62016-11-22 21:15:58 +00001219 return -ENODEV;
Jacob Pan2d281d82013-10-17 10:28:35 -07001220 }
1221 /* now register domains as children of the socket/package*/
1222 for (rd = rp->domains; rd < rp->domains + rp->nr_domains; rd++) {
1223 if (rd->id == RAPL_DOMAIN_PACKAGE)
1224 continue;
1225 /* number of power limits per domain varies */
1226 nr_pl = find_nr_power_limit(rd);
1227 power_zone = powercap_register_zone(&rd->power_zone,
1228 control_type, rd->name,
1229 rp->power_zone,
1230 &zone_ops[rd->id], nr_pl,
1231 &constraint_ops);
1232
1233 if (IS_ERR(power_zone)) {
1234 pr_debug("failed to register power_zone, %d:%s:%s\n",
1235 rp->id, rd->name, dev_name);
1236 ret = PTR_ERR(power_zone);
1237 goto err_cleanup;
1238 }
1239 }
Thomas Gleixnerbed5ab62016-11-22 21:15:58 +00001240 return 0;
Jacob Pan2d281d82013-10-17 10:28:35 -07001241
Jacob Pan2d281d82013-10-17 10:28:35 -07001242err_cleanup:
Thomas Gleixner58705062016-11-22 21:16:02 +00001243 /*
1244 * Clean up previously initialized domains within the package if we
Jacob Pan2d281d82013-10-17 10:28:35 -07001245 * failed after the first domain setup.
1246 */
1247 while (--rd >= rp->domains) {
1248 pr_debug("unregister package %d domain %s\n", rp->id, rd->name);
1249 powercap_unregister_zone(control_type, &rd->power_zone);
1250 }
1251
1252 return ret;
1253}
1254
Thomas Gleixner58705062016-11-22 21:16:02 +00001255static int __init rapl_register_psys(void)
Srinivas Pandruvada3521ba12016-04-17 15:03:01 -07001256{
1257 struct rapl_domain *rd;
1258 struct powercap_zone *power_zone;
1259 u64 val;
1260
1261 if (rdmsrl_safe_on_cpu(0, MSR_PLATFORM_ENERGY_STATUS, &val) || !val)
1262 return -ENODEV;
1263
1264 if (rdmsrl_safe_on_cpu(0, MSR_PLATFORM_POWER_LIMIT, &val) || !val)
1265 return -ENODEV;
1266
1267 rd = kzalloc(sizeof(*rd), GFP_KERNEL);
1268 if (!rd)
1269 return -ENOMEM;
1270
1271 rd->name = rapl_domain_names[RAPL_DOMAIN_PLATFORM];
1272 rd->id = RAPL_DOMAIN_PLATFORM;
1273 rd->msrs[0] = MSR_PLATFORM_POWER_LIMIT;
1274 rd->msrs[1] = MSR_PLATFORM_ENERGY_STATUS;
1275 rd->rpl[0].prim_id = PL1_ENABLE;
1276 rd->rpl[0].name = pl1_name;
1277 rd->rpl[1].prim_id = PL2_ENABLE;
1278 rd->rpl[1].name = pl2_name;
1279 rd->rp = find_package_by_id(0);
1280
1281 power_zone = powercap_register_zone(&rd->power_zone, control_type,
1282 "psys", NULL,
1283 &zone_ops[RAPL_DOMAIN_PLATFORM],
1284 2, &constraint_ops);
1285
1286 if (IS_ERR(power_zone)) {
1287 kfree(rd);
1288 return PTR_ERR(power_zone);
1289 }
1290
1291 platform_rapl_domain = rd;
1292
1293 return 0;
1294}
1295
Thomas Gleixner58705062016-11-22 21:16:02 +00001296static int __init rapl_register_powercap(void)
Jacob Pan2d281d82013-10-17 10:28:35 -07001297{
Jacob Pan2d281d82013-10-17 10:28:35 -07001298 control_type = powercap_register_control_type(NULL, "intel-rapl", NULL);
1299 if (IS_ERR(control_type)) {
1300 pr_debug("failed to register powercap control_type.\n");
1301 return PTR_ERR(control_type);
1302 }
Thomas Gleixner58705062016-11-22 21:16:02 +00001303 return 0;
Jacob Pan2d281d82013-10-17 10:28:35 -07001304}
1305
1306static int rapl_check_domain(int cpu, int domain)
1307{
1308 unsigned msr;
Jacob Pan9d31c672014-04-29 15:33:06 -07001309 u64 val = 0;
Jacob Pan2d281d82013-10-17 10:28:35 -07001310
1311 switch (domain) {
1312 case RAPL_DOMAIN_PACKAGE:
1313 msr = MSR_PKG_ENERGY_STATUS;
1314 break;
1315 case RAPL_DOMAIN_PP0:
1316 msr = MSR_PP0_ENERGY_STATUS;
1317 break;
1318 case RAPL_DOMAIN_PP1:
1319 msr = MSR_PP1_ENERGY_STATUS;
1320 break;
1321 case RAPL_DOMAIN_DRAM:
1322 msr = MSR_DRAM_ENERGY_STATUS;
1323 break;
Srinivas Pandruvada3521ba12016-04-17 15:03:01 -07001324 case RAPL_DOMAIN_PLATFORM:
1325 /* PSYS(PLATFORM) is not a CPU domain, so avoid printng error */
1326 return -EINVAL;
Jacob Pan2d281d82013-10-17 10:28:35 -07001327 default:
1328 pr_err("invalid domain id %d\n", domain);
1329 return -EINVAL;
1330 }
Jacob Pan9d31c672014-04-29 15:33:06 -07001331 /* make sure domain counters are available and contains non-zero
1332 * values, otherwise skip it.
1333 */
1334 if (rdmsrl_safe_on_cpu(cpu, msr, &val) || !val)
Jacob Pan2d281d82013-10-17 10:28:35 -07001335 return -ENODEV;
1336
Jacob Pan9d31c672014-04-29 15:33:06 -07001337 return 0;
Jacob Pan2d281d82013-10-17 10:28:35 -07001338}
1339
Jacob Pane1399ba2016-05-31 13:41:29 -07001340
1341/*
1342 * Check if power limits are available. Two cases when they are not available:
1343 * 1. Locked by BIOS, in this case we still provide read-only access so that
1344 * users can see what limit is set by the BIOS.
1345 * 2. Some CPUs make some domains monitoring only which means PLx MSRs may not
1346 * exist at all. In this case, we do not show the contraints in powercap.
1347 *
1348 * Called after domains are detected and initialized.
1349 */
1350static void rapl_detect_powerlimit(struct rapl_domain *rd)
1351{
1352 u64 val64;
1353 int i;
1354
1355 /* check if the domain is locked by BIOS, ignore if MSR doesn't exist */
1356 if (!rapl_read_data_raw(rd, FW_LOCK, false, &val64)) {
1357 if (val64) {
1358 pr_info("RAPL package %d domain %s locked by BIOS\n",
1359 rd->rp->id, rd->name);
1360 rd->state |= DOMAIN_STATE_BIOS_LOCKED;
1361 }
1362 }
1363 /* check if power limit MSRs exists, otherwise domain is monitoring only */
1364 for (i = 0; i < NR_POWER_LIMITS; i++) {
1365 int prim = rd->rpl[i].prim_id;
1366 if (rapl_read_data_raw(rd, prim, false, &val64))
1367 rd->rpl[i].name = NULL;
1368 }
1369}
1370
Jacob Pan2d281d82013-10-17 10:28:35 -07001371/* Detect active and valid domains for the given CPU, caller must
1372 * ensure the CPU belongs to the targeted package and CPU hotlug is disabled.
1373 */
1374static int rapl_detect_domains(struct rapl_package *rp, int cpu)
1375{
Jacob Pan2d281d82013-10-17 10:28:35 -07001376 struct rapl_domain *rd;
Thomas Gleixner58705062016-11-22 21:16:02 +00001377 int i;
Jacob Pan2d281d82013-10-17 10:28:35 -07001378
1379 for (i = 0; i < RAPL_DOMAIN_MAX; i++) {
1380 /* use physical package id to read counters */
Jacob Panfcdf1792014-09-02 02:55:21 -07001381 if (!rapl_check_domain(cpu, i)) {
Jacob Pan2d281d82013-10-17 10:28:35 -07001382 rp->domain_map |= 1 << i;
Jacob Panfcdf1792014-09-02 02:55:21 -07001383 pr_info("Found RAPL domain %s\n", rapl_domain_names[i]);
1384 }
Jacob Pan2d281d82013-10-17 10:28:35 -07001385 }
1386 rp->nr_domains = bitmap_weight(&rp->domain_map, RAPL_DOMAIN_MAX);
1387 if (!rp->nr_domains) {
Jacob Pane1a27e82016-05-23 09:45:43 -07001388 pr_debug("no valid rapl domains found in package %d\n", rp->id);
Thomas Gleixner58705062016-11-22 21:16:02 +00001389 return -ENODEV;
Jacob Pan2d281d82013-10-17 10:28:35 -07001390 }
1391 pr_debug("found %d domains on package %d\n", rp->nr_domains, rp->id);
1392
1393 rp->domains = kcalloc(rp->nr_domains + 1, sizeof(struct rapl_domain),
1394 GFP_KERNEL);
Thomas Gleixner58705062016-11-22 21:16:02 +00001395 if (!rp->domains)
1396 return -ENOMEM;
1397
Jacob Pan2d281d82013-10-17 10:28:35 -07001398 rapl_init_domains(rp);
1399
Jacob Pane1399ba2016-05-31 13:41:29 -07001400 for (rd = rp->domains; rd < rp->domains + rp->nr_domains; rd++)
1401 rapl_detect_powerlimit(rd);
1402
Jacob Pan2d281d82013-10-17 10:28:35 -07001403 return 0;
1404}
1405
1406/* called from CPU hotplug notifier, hotplug lock held */
1407static void rapl_remove_package(struct rapl_package *rp)
1408{
1409 struct rapl_domain *rd, *rd_package = NULL;
1410
Thomas Gleixner58705062016-11-22 21:16:02 +00001411 package_power_limit_irq_restore(rp);
1412
Jacob Pan2d281d82013-10-17 10:28:35 -07001413 for (rd = rp->domains; rd < rp->domains + rp->nr_domains; rd++) {
Thomas Gleixner58705062016-11-22 21:16:02 +00001414 rapl_write_data_raw(rd, PL1_ENABLE, 0);
1415 rapl_write_data_raw(rd, PL1_CLAMP, 0);
1416 if (find_nr_power_limit(rd) > 1) {
1417 rapl_write_data_raw(rd, PL2_ENABLE, 0);
1418 rapl_write_data_raw(rd, PL2_CLAMP, 0);
1419 }
Jacob Pan2d281d82013-10-17 10:28:35 -07001420 if (rd->id == RAPL_DOMAIN_PACKAGE) {
1421 rd_package = rd;
1422 continue;
1423 }
Thomas Gleixner58705062016-11-22 21:16:02 +00001424 pr_debug("remove package, undo power limit on %d: %s\n",
1425 rp->id, rd->name);
Jacob Pan2d281d82013-10-17 10:28:35 -07001426 powercap_unregister_zone(control_type, &rd->power_zone);
1427 }
1428 /* do parent zone last */
1429 powercap_unregister_zone(control_type, &rd_package->power_zone);
1430 list_del(&rp->plist);
1431 kfree(rp);
1432}
1433
1434/* called from CPU hotplug notifier, hotplug lock held */
1435static int rapl_add_package(int cpu)
1436{
Jacob Pan2d281d82013-10-17 10:28:35 -07001437 struct rapl_package *rp;
Thomas Gleixnera74f4362016-11-22 21:15:59 +00001438 int ret, phy_package_id;
Jacob Pan2d281d82013-10-17 10:28:35 -07001439
1440 phy_package_id = topology_physical_package_id(cpu);
1441 rp = kzalloc(sizeof(struct rapl_package), GFP_KERNEL);
1442 if (!rp)
1443 return -ENOMEM;
1444
1445 /* add the new package to the list */
1446 rp->id = phy_package_id;
1447 rp->nr_cpus = 1;
Jacob Pan323ee642016-02-24 13:31:38 -08001448 rp->lead_cpu = cpu;
1449
Jacob Pan2d281d82013-10-17 10:28:35 -07001450 /* check if the package contains valid domains */
1451 if (rapl_detect_domains(rp, cpu) ||
Jacob Pan3c2c0842014-11-07 09:29:26 -08001452 rapl_defaults->check_unit(rp, cpu)) {
Jacob Pan2d281d82013-10-17 10:28:35 -07001453 ret = -ENODEV;
1454 goto err_free_package;
1455 }
Thomas Gleixnera74f4362016-11-22 21:15:59 +00001456 ret = rapl_package_register_powercap(rp);
1457 if (!ret) {
Jacob Pan2d281d82013-10-17 10:28:35 -07001458 INIT_LIST_HEAD(&rp->plist);
1459 list_add(&rp->plist, &rapl_packages);
Thomas Gleixnera74f4362016-11-22 21:15:59 +00001460 return 0;
Jacob Pan2d281d82013-10-17 10:28:35 -07001461 }
1462
1463err_free_package:
1464 kfree(rp->domains);
1465 kfree(rp);
1466
1467 return ret;
1468}
1469
1470/* Handles CPU hotplug on multi-socket systems.
1471 * If a CPU goes online as the first CPU of the physical package
1472 * we add the RAPL package to the system. Similarly, when the last
1473 * CPU of the package is removed, we remove the RAPL package and its
1474 * associated domains. Cooling devices are handled accordingly at
1475 * per-domain level.
1476 */
Sebastian Andrzej Siewior5e4dc792016-11-22 21:16:00 +00001477static int rapl_cpu_online(unsigned int cpu)
Jacob Pan2d281d82013-10-17 10:28:35 -07001478{
Sebastian Andrzej Siewior5e4dc792016-11-22 21:16:00 +00001479 struct rapl_package *rp;
1480 int phy_package_id;
1481
1482 phy_package_id = topology_physical_package_id(cpu);
1483
1484 rp = find_package_by_id(phy_package_id);
Thomas Gleixner58705062016-11-22 21:16:02 +00001485 if (rp) {
Sebastian Andrzej Siewior5e4dc792016-11-22 21:16:00 +00001486 rp->nr_cpus++;
Thomas Gleixner58705062016-11-22 21:16:02 +00001487 return 0;
1488 }
1489 return rapl_add_package(cpu);
Sebastian Andrzej Siewior5e4dc792016-11-22 21:16:00 +00001490}
1491
1492static int rapl_cpu_down_prep(unsigned int cpu)
1493{
Jacob Pan2d281d82013-10-17 10:28:35 -07001494 int phy_package_id;
1495 struct rapl_package *rp;
Jacob Pan323ee642016-02-24 13:31:38 -08001496 int lead_cpu;
Jacob Pan2d281d82013-10-17 10:28:35 -07001497
1498 phy_package_id = topology_physical_package_id(cpu);
Sebastian Andrzej Siewior5e4dc792016-11-22 21:16:00 +00001499 rp = find_package_by_id(phy_package_id);
1500 if (!rp)
1501 return 0;
1502 if (--rp->nr_cpus == 0) {
1503 rapl_remove_package(rp);
1504 } else if (cpu == rp->lead_cpu) {
1505 /* choose another active cpu in the package */
1506 lead_cpu = cpumask_any_but(topology_core_cpumask(cpu), cpu);
1507 if (lead_cpu < nr_cpu_ids) {
1508 rp->lead_cpu = lead_cpu;
1509 } else {
1510 /* should never go here */
1511 pr_err("no active cpu available for package %d\n",
1512 phy_package_id);
Jacob Pan323ee642016-02-24 13:31:38 -08001513 }
Jacob Pan2d281d82013-10-17 10:28:35 -07001514 }
Sebastian Andrzej Siewior5e4dc792016-11-22 21:16:00 +00001515 return 0;
Jacob Pan2d281d82013-10-17 10:28:35 -07001516}
1517
Sebastian Andrzej Siewior5e4dc792016-11-22 21:16:00 +00001518static enum cpuhp_state pcap_rapl_online;
Jacob Pan2d281d82013-10-17 10:28:35 -07001519
1520static int __init rapl_init(void)
1521{
Jacob Pan087e9cb2014-11-07 09:29:25 -08001522 const struct x86_cpu_id *id;
Thomas Gleixner58705062016-11-22 21:16:02 +00001523 int ret;
Jacob Pan2d281d82013-10-17 10:28:35 -07001524
Jacob Pan087e9cb2014-11-07 09:29:25 -08001525 id = x86_match_cpu(rapl_ids);
1526 if (!id) {
Jacob Pan2d281d82013-10-17 10:28:35 -07001527 pr_err("driver does not support CPU family %d model %d\n",
1528 boot_cpu_data.x86, boot_cpu_data.x86_model);
1529
1530 return -ENODEV;
1531 }
Srivatsa S. Bhat009f2252014-03-11 02:09:26 +05301532
Jacob Pan087e9cb2014-11-07 09:29:25 -08001533 rapl_defaults = (struct rapl_defaults *)id->driver_data;
1534
Thomas Gleixner58705062016-11-22 21:16:02 +00001535 ret = rapl_register_powercap();
Jacob Pan2d281d82013-10-17 10:28:35 -07001536 if (ret)
Thomas Gleixner58705062016-11-22 21:16:02 +00001537 return ret;
Jacob Pan2d281d82013-10-17 10:28:35 -07001538
Thomas Gleixner58705062016-11-22 21:16:02 +00001539 ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "powercap/rapl:online",
1540 rapl_cpu_online, rapl_cpu_down_prep);
Sebastian Andrzej Siewior5e4dc792016-11-22 21:16:00 +00001541 if (ret < 0)
1542 goto err_unreg;
1543 pcap_rapl_online = ret;
Thomas Gleixner58705062016-11-22 21:16:02 +00001544
1545 /* Don't bail out if PSys is not supported */
1546 rapl_register_psys();
Sebastian Andrzej Siewior5e4dc792016-11-22 21:16:00 +00001547 return 0;
Jacob Pan2d281d82013-10-17 10:28:35 -07001548
Sebastian Andrzej Siewior5e4dc792016-11-22 21:16:00 +00001549err_unreg:
1550 rapl_unregister_powercap();
Jacob Pan2d281d82013-10-17 10:28:35 -07001551 return ret;
1552}
1553
1554static void __exit rapl_exit(void)
1555{
Sebastian Andrzej Siewior5e4dc792016-11-22 21:16:00 +00001556 cpuhp_remove_state(pcap_rapl_online);
Jacob Pan2d281d82013-10-17 10:28:35 -07001557 rapl_unregister_powercap();
Jacob Pan2d281d82013-10-17 10:28:35 -07001558}
1559
1560module_init(rapl_init);
1561module_exit(rapl_exit);
1562
1563MODULE_DESCRIPTION("Driver for Intel RAPL (Running Average Power Limit)");
1564MODULE_AUTHOR("Jacob Pan <jacob.jun.pan@intel.com>");
1565MODULE_LICENSE("GPL v2");