blob: 65ed88a28030266c05d21b572f7ad2a64081d289 [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 */
Jacob Pan2d281d82013-10-17 10:28:35 -0700192 unsigned long power_limit_irq; /* keep track of package power limit
193 * notify interrupt enable status.
194 */
195 struct list_head plist;
Jacob Pan323ee642016-02-24 13:31:38 -0800196 int lead_cpu; /* one active cpu per package for access */
Thomas Gleixnerb4005e92016-11-22 21:16:05 +0000197 /* Track active cpus */
198 struct cpumask cpumask;
Jacob Pan2d281d82013-10-17 10:28:35 -0700199};
Jacob Pan087e9cb2014-11-07 09:29:25 -0800200
201struct rapl_defaults {
Ajay Thomas51b63402015-04-30 01:43:23 +0530202 u8 floor_freq_reg_addr;
Jacob Pan087e9cb2014-11-07 09:29:25 -0800203 int (*check_unit)(struct rapl_package *rp, int cpu);
204 void (*set_floor_freq)(struct rapl_domain *rd, bool mode);
205 u64 (*compute_time_window)(struct rapl_package *rp, u64 val,
206 bool to_raw);
Jacob Pand474a4d2015-03-13 03:48:56 -0700207 unsigned int dram_domain_energy_unit;
Jacob Pan087e9cb2014-11-07 09:29:25 -0800208};
209static struct rapl_defaults *rapl_defaults;
210
Jacob Pan3c2c0842014-11-07 09:29:26 -0800211/* Sideband MBI registers */
Ajay Thomas51b63402015-04-30 01:43:23 +0530212#define IOSF_CPU_POWER_BUDGET_CTL_BYT (0x2)
213#define IOSF_CPU_POWER_BUDGET_CTL_TNG (0xdf)
Jacob Pan3c2c0842014-11-07 09:29:26 -0800214
Jacob Pan2d281d82013-10-17 10:28:35 -0700215#define PACKAGE_PLN_INT_SAVED BIT(0)
216#define MAX_PRIM_NAME (32)
217
218/* per domain data. used to describe individual knobs such that access function
219 * can be consolidated into one instead of many inline functions.
220 */
221struct rapl_primitive_info {
222 const char *name;
223 u64 mask;
224 int shift;
225 enum rapl_domain_msr_id id;
226 enum unit_type unit;
227 u32 flag;
228};
229
230#define PRIMITIVE_INFO_INIT(p, m, s, i, u, f) { \
231 .name = #p, \
232 .mask = m, \
233 .shift = s, \
234 .id = i, \
235 .unit = u, \
236 .flag = f \
237 }
238
239static void rapl_init_domains(struct rapl_package *rp);
240static int rapl_read_data_raw(struct rapl_domain *rd,
241 enum rapl_primitives prim,
242 bool xlate, u64 *data);
243static int rapl_write_data_raw(struct rapl_domain *rd,
244 enum rapl_primitives prim,
245 unsigned long long value);
Jacob Pan309557f2016-02-24 13:31:37 -0800246static u64 rapl_unit_xlate(struct rapl_domain *rd,
Jacob Pand474a4d2015-03-13 03:48:56 -0700247 enum unit_type type, u64 value,
Jacob Pan2d281d82013-10-17 10:28:35 -0700248 int to_raw);
Jacob Pan309557f2016-02-24 13:31:37 -0800249static void package_power_limit_irq_save(struct rapl_package *rp);
Jacob Pan2d281d82013-10-17 10:28:35 -0700250
251static LIST_HEAD(rapl_packages); /* guarded by CPU hotplug lock */
252
253static const char * const rapl_domain_names[] = {
254 "package",
255 "core",
256 "uncore",
257 "dram",
Srinivas Pandruvada3521ba12016-04-17 15:03:01 -0700258 "psys",
Jacob Pan2d281d82013-10-17 10:28:35 -0700259};
260
261static struct powercap_control_type *control_type; /* PowerCap Controller */
Srinivas Pandruvada3521ba12016-04-17 15:03:01 -0700262static struct rapl_domain *platform_rapl_domain; /* Platform (PSys) domain */
Jacob Pan2d281d82013-10-17 10:28:35 -0700263
264/* caller to ensure CPU hotplug lock is held */
265static struct rapl_package *find_package_by_id(int id)
266{
267 struct rapl_package *rp;
268
269 list_for_each_entry(rp, &rapl_packages, plist) {
270 if (rp->id == id)
271 return rp;
272 }
273
274 return NULL;
275}
276
Jacob Pan2d281d82013-10-17 10:28:35 -0700277static int get_energy_counter(struct powercap_zone *power_zone, u64 *energy_raw)
278{
279 struct rapl_domain *rd;
280 u64 energy_now;
281
282 /* prevent CPU hotplug, make sure the RAPL domain does not go
283 * away while reading the counter.
284 */
285 get_online_cpus();
286 rd = power_zone_to_rapl_domain(power_zone);
287
288 if (!rapl_read_data_raw(rd, ENERGY_COUNTER, true, &energy_now)) {
289 *energy_raw = energy_now;
290 put_online_cpus();
291
292 return 0;
293 }
294 put_online_cpus();
295
296 return -EIO;
297}
298
299static int get_max_energy_counter(struct powercap_zone *pcd_dev, u64 *energy)
300{
Jacob Pand474a4d2015-03-13 03:48:56 -0700301 struct rapl_domain *rd = power_zone_to_rapl_domain(pcd_dev);
302
Jacob Pan309557f2016-02-24 13:31:37 -0800303 *energy = rapl_unit_xlate(rd, ENERGY_UNIT, ENERGY_STATUS_MASK, 0);
Jacob Pan2d281d82013-10-17 10:28:35 -0700304 return 0;
305}
306
307static int release_zone(struct powercap_zone *power_zone)
308{
309 struct rapl_domain *rd = power_zone_to_rapl_domain(power_zone);
Jacob Pan309557f2016-02-24 13:31:37 -0800310 struct rapl_package *rp = rd->rp;
Jacob Pan2d281d82013-10-17 10:28:35 -0700311
312 /* package zone is the last zone of a package, we can free
313 * memory here since all children has been unregistered.
314 */
315 if (rd->id == RAPL_DOMAIN_PACKAGE) {
Jacob Pan2d281d82013-10-17 10:28:35 -0700316 kfree(rd);
317 rp->domains = NULL;
318 }
319
320 return 0;
321
322}
323
324static int find_nr_power_limit(struct rapl_domain *rd)
325{
Jacob Pane1399ba2016-05-31 13:41:29 -0700326 int i, nr_pl = 0;
Jacob Pan2d281d82013-10-17 10:28:35 -0700327
328 for (i = 0; i < NR_POWER_LIMITS; i++) {
Jacob Pane1399ba2016-05-31 13:41:29 -0700329 if (rd->rpl[i].name)
330 nr_pl++;
Jacob Pan2d281d82013-10-17 10:28:35 -0700331 }
332
Jacob Pane1399ba2016-05-31 13:41:29 -0700333 return nr_pl;
Jacob Pan2d281d82013-10-17 10:28:35 -0700334}
335
336static int set_domain_enable(struct powercap_zone *power_zone, bool mode)
337{
338 struct rapl_domain *rd = power_zone_to_rapl_domain(power_zone);
Jacob Pan2d281d82013-10-17 10:28:35 -0700339
340 if (rd->state & DOMAIN_STATE_BIOS_LOCKED)
341 return -EACCES;
Jacob Pan3c2c0842014-11-07 09:29:26 -0800342
Jacob Pan2d281d82013-10-17 10:28:35 -0700343 get_online_cpus();
Jacob Pan2d281d82013-10-17 10:28:35 -0700344 rapl_write_data_raw(rd, PL1_ENABLE, mode);
Ajay Thomas51b63402015-04-30 01:43:23 +0530345 if (rapl_defaults->set_floor_freq)
346 rapl_defaults->set_floor_freq(rd, mode);
Jacob Pan2d281d82013-10-17 10:28:35 -0700347 put_online_cpus();
348
349 return 0;
350}
351
352static int get_domain_enable(struct powercap_zone *power_zone, bool *mode)
353{
354 struct rapl_domain *rd = power_zone_to_rapl_domain(power_zone);
355 u64 val;
356
357 if (rd->state & DOMAIN_STATE_BIOS_LOCKED) {
358 *mode = false;
359 return 0;
360 }
361 get_online_cpus();
362 if (rapl_read_data_raw(rd, PL1_ENABLE, true, &val)) {
363 put_online_cpus();
364 return -EIO;
365 }
366 *mode = val;
367 put_online_cpus();
368
369 return 0;
370}
371
372/* per RAPL domain ops, in the order of rapl_domain_type */
Julia Lawall600c3952015-12-23 22:59:55 +0100373static const struct powercap_zone_ops zone_ops[] = {
Jacob Pan2d281d82013-10-17 10:28:35 -0700374 /* RAPL_DOMAIN_PACKAGE */
375 {
376 .get_energy_uj = get_energy_counter,
377 .get_max_energy_range_uj = get_max_energy_counter,
378 .release = release_zone,
379 .set_enable = set_domain_enable,
380 .get_enable = get_domain_enable,
381 },
382 /* RAPL_DOMAIN_PP0 */
383 {
384 .get_energy_uj = get_energy_counter,
385 .get_max_energy_range_uj = get_max_energy_counter,
386 .release = release_zone,
387 .set_enable = set_domain_enable,
388 .get_enable = get_domain_enable,
389 },
390 /* RAPL_DOMAIN_PP1 */
391 {
392 .get_energy_uj = get_energy_counter,
393 .get_max_energy_range_uj = get_max_energy_counter,
394 .release = release_zone,
395 .set_enable = set_domain_enable,
396 .get_enable = get_domain_enable,
397 },
398 /* RAPL_DOMAIN_DRAM */
399 {
400 .get_energy_uj = get_energy_counter,
401 .get_max_energy_range_uj = get_max_energy_counter,
402 .release = release_zone,
403 .set_enable = set_domain_enable,
404 .get_enable = get_domain_enable,
405 },
Srinivas Pandruvada3521ba12016-04-17 15:03:01 -0700406 /* RAPL_DOMAIN_PLATFORM */
407 {
408 .get_energy_uj = get_energy_counter,
409 .get_max_energy_range_uj = get_max_energy_counter,
410 .release = release_zone,
411 .set_enable = set_domain_enable,
412 .get_enable = get_domain_enable,
413 },
Jacob Pan2d281d82013-10-17 10:28:35 -0700414};
415
Jacob Pane1399ba2016-05-31 13:41:29 -0700416
417/*
418 * Constraint index used by powercap can be different than power limit (PL)
419 * index in that some PLs maybe missing due to non-existant MSRs. So we
420 * need to convert here by finding the valid PLs only (name populated).
421 */
422static int contraint_to_pl(struct rapl_domain *rd, int cid)
423{
424 int i, j;
425
426 for (i = 0, j = 0; i < NR_POWER_LIMITS; i++) {
427 if ((rd->rpl[i].name) && j++ == cid) {
428 pr_debug("%s: index %d\n", __func__, i);
429 return i;
430 }
431 }
432
433 return -EINVAL;
434}
435
436static int set_power_limit(struct powercap_zone *power_zone, int cid,
Jacob Pan2d281d82013-10-17 10:28:35 -0700437 u64 power_limit)
438{
439 struct rapl_domain *rd;
440 struct rapl_package *rp;
441 int ret = 0;
Jacob Pane1399ba2016-05-31 13:41:29 -0700442 int id;
Jacob Pan2d281d82013-10-17 10:28:35 -0700443
444 get_online_cpus();
445 rd = power_zone_to_rapl_domain(power_zone);
Jacob Pane1399ba2016-05-31 13:41:29 -0700446 id = contraint_to_pl(rd, cid);
447
Jacob Pan309557f2016-02-24 13:31:37 -0800448 rp = rd->rp;
Jacob Pan2d281d82013-10-17 10:28:35 -0700449
450 if (rd->state & DOMAIN_STATE_BIOS_LOCKED) {
451 dev_warn(&power_zone->dev, "%s locked by BIOS, monitoring only\n",
452 rd->name);
453 ret = -EACCES;
454 goto set_exit;
455 }
456
457 switch (rd->rpl[id].prim_id) {
458 case PL1_ENABLE:
459 rapl_write_data_raw(rd, POWER_LIMIT1, power_limit);
460 break;
461 case PL2_ENABLE:
462 rapl_write_data_raw(rd, POWER_LIMIT2, power_limit);
463 break;
464 default:
465 ret = -EINVAL;
466 }
467 if (!ret)
Jacob Pan309557f2016-02-24 13:31:37 -0800468 package_power_limit_irq_save(rp);
Jacob Pan2d281d82013-10-17 10:28:35 -0700469set_exit:
470 put_online_cpus();
471 return ret;
472}
473
Jacob Pane1399ba2016-05-31 13:41:29 -0700474static int get_current_power_limit(struct powercap_zone *power_zone, int cid,
Jacob Pan2d281d82013-10-17 10:28:35 -0700475 u64 *data)
476{
477 struct rapl_domain *rd;
478 u64 val;
479 int prim;
480 int ret = 0;
Jacob Pane1399ba2016-05-31 13:41:29 -0700481 int id;
Jacob Pan2d281d82013-10-17 10:28:35 -0700482
483 get_online_cpus();
484 rd = power_zone_to_rapl_domain(power_zone);
Jacob Pane1399ba2016-05-31 13:41:29 -0700485 id = contraint_to_pl(rd, cid);
Jacob Pan2d281d82013-10-17 10:28:35 -0700486 switch (rd->rpl[id].prim_id) {
487 case PL1_ENABLE:
488 prim = POWER_LIMIT1;
489 break;
490 case PL2_ENABLE:
491 prim = POWER_LIMIT2;
492 break;
493 default:
494 put_online_cpus();
495 return -EINVAL;
496 }
497 if (rapl_read_data_raw(rd, prim, true, &val))
498 ret = -EIO;
499 else
500 *data = val;
501
502 put_online_cpus();
503
504 return ret;
505}
506
Jacob Pane1399ba2016-05-31 13:41:29 -0700507static int set_time_window(struct powercap_zone *power_zone, int cid,
Jacob Pan2d281d82013-10-17 10:28:35 -0700508 u64 window)
509{
510 struct rapl_domain *rd;
511 int ret = 0;
Jacob Pane1399ba2016-05-31 13:41:29 -0700512 int id;
Jacob Pan2d281d82013-10-17 10:28:35 -0700513
514 get_online_cpus();
515 rd = power_zone_to_rapl_domain(power_zone);
Jacob Pane1399ba2016-05-31 13:41:29 -0700516 id = contraint_to_pl(rd, cid);
517
Jacob Pan2d281d82013-10-17 10:28:35 -0700518 switch (rd->rpl[id].prim_id) {
519 case PL1_ENABLE:
520 rapl_write_data_raw(rd, TIME_WINDOW1, window);
521 break;
522 case PL2_ENABLE:
523 rapl_write_data_raw(rd, TIME_WINDOW2, window);
524 break;
525 default:
526 ret = -EINVAL;
527 }
528 put_online_cpus();
529 return ret;
530}
531
Jacob Pane1399ba2016-05-31 13:41:29 -0700532static int get_time_window(struct powercap_zone *power_zone, int cid, u64 *data)
Jacob Pan2d281d82013-10-17 10:28:35 -0700533{
534 struct rapl_domain *rd;
535 u64 val;
536 int ret = 0;
Jacob Pane1399ba2016-05-31 13:41:29 -0700537 int id;
Jacob Pan2d281d82013-10-17 10:28:35 -0700538
539 get_online_cpus();
540 rd = power_zone_to_rapl_domain(power_zone);
Jacob Pane1399ba2016-05-31 13:41:29 -0700541 id = contraint_to_pl(rd, cid);
542
Jacob Pan2d281d82013-10-17 10:28:35 -0700543 switch (rd->rpl[id].prim_id) {
544 case PL1_ENABLE:
545 ret = rapl_read_data_raw(rd, TIME_WINDOW1, true, &val);
546 break;
547 case PL2_ENABLE:
548 ret = rapl_read_data_raw(rd, TIME_WINDOW2, true, &val);
549 break;
550 default:
551 put_online_cpus();
552 return -EINVAL;
553 }
554 if (!ret)
555 *data = val;
556 put_online_cpus();
557
558 return ret;
559}
560
Jacob Pane1399ba2016-05-31 13:41:29 -0700561static const char *get_constraint_name(struct powercap_zone *power_zone, int cid)
Jacob Pan2d281d82013-10-17 10:28:35 -0700562{
Jacob Pan2d281d82013-10-17 10:28:35 -0700563 struct rapl_domain *rd;
Jacob Pane1399ba2016-05-31 13:41:29 -0700564 int id;
Jacob Pan2d281d82013-10-17 10:28:35 -0700565
566 rd = power_zone_to_rapl_domain(power_zone);
Jacob Pane1399ba2016-05-31 13:41:29 -0700567 id = contraint_to_pl(rd, cid);
568 if (id >= 0)
569 return rd->rpl[id].name;
Jacob Pan2d281d82013-10-17 10:28:35 -0700570
Jacob Pane1399ba2016-05-31 13:41:29 -0700571 return NULL;
Jacob Pan2d281d82013-10-17 10:28:35 -0700572}
573
574
575static int get_max_power(struct powercap_zone *power_zone, int id,
576 u64 *data)
577{
578 struct rapl_domain *rd;
579 u64 val;
580 int prim;
581 int ret = 0;
582
583 get_online_cpus();
584 rd = power_zone_to_rapl_domain(power_zone);
585 switch (rd->rpl[id].prim_id) {
586 case PL1_ENABLE:
587 prim = THERMAL_SPEC_POWER;
588 break;
589 case PL2_ENABLE:
590 prim = MAX_POWER;
591 break;
592 default:
593 put_online_cpus();
594 return -EINVAL;
595 }
596 if (rapl_read_data_raw(rd, prim, true, &val))
597 ret = -EIO;
598 else
599 *data = val;
600
601 put_online_cpus();
602
603 return ret;
604}
605
Julia Lawall600c3952015-12-23 22:59:55 +0100606static const struct powercap_zone_constraint_ops constraint_ops = {
Jacob Pan2d281d82013-10-17 10:28:35 -0700607 .set_power_limit_uw = set_power_limit,
608 .get_power_limit_uw = get_current_power_limit,
609 .set_time_window_us = set_time_window,
610 .get_time_window_us = get_time_window,
611 .get_max_power_uw = get_max_power,
612 .get_name = get_constraint_name,
613};
614
615/* called after domain detection and package level data are set */
616static void rapl_init_domains(struct rapl_package *rp)
617{
618 int i;
619 struct rapl_domain *rd = rp->domains;
620
621 for (i = 0; i < RAPL_DOMAIN_MAX; i++) {
622 unsigned int mask = rp->domain_map & (1 << i);
623 switch (mask) {
624 case BIT(RAPL_DOMAIN_PACKAGE):
625 rd->name = rapl_domain_names[RAPL_DOMAIN_PACKAGE];
626 rd->id = RAPL_DOMAIN_PACKAGE;
627 rd->msrs[0] = MSR_PKG_POWER_LIMIT;
628 rd->msrs[1] = MSR_PKG_ENERGY_STATUS;
629 rd->msrs[2] = MSR_PKG_PERF_STATUS;
630 rd->msrs[3] = 0;
631 rd->msrs[4] = MSR_PKG_POWER_INFO;
632 rd->rpl[0].prim_id = PL1_ENABLE;
633 rd->rpl[0].name = pl1_name;
634 rd->rpl[1].prim_id = PL2_ENABLE;
635 rd->rpl[1].name = pl2_name;
636 break;
637 case BIT(RAPL_DOMAIN_PP0):
638 rd->name = rapl_domain_names[RAPL_DOMAIN_PP0];
639 rd->id = RAPL_DOMAIN_PP0;
640 rd->msrs[0] = MSR_PP0_POWER_LIMIT;
641 rd->msrs[1] = MSR_PP0_ENERGY_STATUS;
642 rd->msrs[2] = 0;
643 rd->msrs[3] = MSR_PP0_POLICY;
644 rd->msrs[4] = 0;
645 rd->rpl[0].prim_id = PL1_ENABLE;
646 rd->rpl[0].name = pl1_name;
647 break;
648 case BIT(RAPL_DOMAIN_PP1):
649 rd->name = rapl_domain_names[RAPL_DOMAIN_PP1];
650 rd->id = RAPL_DOMAIN_PP1;
651 rd->msrs[0] = MSR_PP1_POWER_LIMIT;
652 rd->msrs[1] = MSR_PP1_ENERGY_STATUS;
653 rd->msrs[2] = 0;
654 rd->msrs[3] = MSR_PP1_POLICY;
655 rd->msrs[4] = 0;
656 rd->rpl[0].prim_id = PL1_ENABLE;
657 rd->rpl[0].name = pl1_name;
658 break;
659 case BIT(RAPL_DOMAIN_DRAM):
660 rd->name = rapl_domain_names[RAPL_DOMAIN_DRAM];
661 rd->id = RAPL_DOMAIN_DRAM;
662 rd->msrs[0] = MSR_DRAM_POWER_LIMIT;
663 rd->msrs[1] = MSR_DRAM_ENERGY_STATUS;
664 rd->msrs[2] = MSR_DRAM_PERF_STATUS;
665 rd->msrs[3] = 0;
666 rd->msrs[4] = MSR_DRAM_POWER_INFO;
667 rd->rpl[0].prim_id = PL1_ENABLE;
668 rd->rpl[0].name = pl1_name;
Jacob Pand474a4d2015-03-13 03:48:56 -0700669 rd->domain_energy_unit =
670 rapl_defaults->dram_domain_energy_unit;
671 if (rd->domain_energy_unit)
672 pr_info("DRAM domain energy unit %dpj\n",
673 rd->domain_energy_unit);
Jacob Pan2d281d82013-10-17 10:28:35 -0700674 break;
675 }
676 if (mask) {
Jacob Pan309557f2016-02-24 13:31:37 -0800677 rd->rp = rp;
Jacob Pan2d281d82013-10-17 10:28:35 -0700678 rd++;
679 }
680 }
681}
682
Jacob Pan309557f2016-02-24 13:31:37 -0800683static u64 rapl_unit_xlate(struct rapl_domain *rd, enum unit_type type,
684 u64 value, int to_raw)
Jacob Pan2d281d82013-10-17 10:28:35 -0700685{
Jacob Pan3c2c0842014-11-07 09:29:26 -0800686 u64 units = 1;
Jacob Pan309557f2016-02-24 13:31:37 -0800687 struct rapl_package *rp = rd->rp;
Jacob Pand474a4d2015-03-13 03:48:56 -0700688 u64 scale = 1;
Jacob Pan2d281d82013-10-17 10:28:35 -0700689
Jacob Pan2d281d82013-10-17 10:28:35 -0700690 switch (type) {
691 case POWER_UNIT:
Jacob Pan3c2c0842014-11-07 09:29:26 -0800692 units = rp->power_unit;
Jacob Pan2d281d82013-10-17 10:28:35 -0700693 break;
694 case ENERGY_UNIT:
Jacob Pand474a4d2015-03-13 03:48:56 -0700695 scale = ENERGY_UNIT_SCALE;
696 /* per domain unit takes precedence */
697 if (rd && rd->domain_energy_unit)
698 units = rd->domain_energy_unit;
699 else
700 units = rp->energy_unit;
Jacob Pan2d281d82013-10-17 10:28:35 -0700701 break;
702 case TIME_UNIT:
Jacob Pan3c2c0842014-11-07 09:29:26 -0800703 return rapl_defaults->compute_time_window(rp, value, to_raw);
Jacob Pan2d281d82013-10-17 10:28:35 -0700704 case ARBITRARY_UNIT:
705 default:
706 return value;
707 };
708
709 if (to_raw)
Jacob Pand474a4d2015-03-13 03:48:56 -0700710 return div64_u64(value, units) * scale;
Jacob Pan3c2c0842014-11-07 09:29:26 -0800711
712 value *= units;
713
Jacob Pand474a4d2015-03-13 03:48:56 -0700714 return div64_u64(value, scale);
Jacob Pan2d281d82013-10-17 10:28:35 -0700715}
716
717/* in the order of enum rapl_primitives */
718static struct rapl_primitive_info rpi[] = {
719 /* name, mask, shift, msr index, unit divisor */
720 PRIMITIVE_INFO_INIT(ENERGY_COUNTER, ENERGY_STATUS_MASK, 0,
721 RAPL_DOMAIN_MSR_STATUS, ENERGY_UNIT, 0),
722 PRIMITIVE_INFO_INIT(POWER_LIMIT1, POWER_LIMIT1_MASK, 0,
723 RAPL_DOMAIN_MSR_LIMIT, POWER_UNIT, 0),
724 PRIMITIVE_INFO_INIT(POWER_LIMIT2, POWER_LIMIT2_MASK, 32,
725 RAPL_DOMAIN_MSR_LIMIT, POWER_UNIT, 0),
726 PRIMITIVE_INFO_INIT(FW_LOCK, POWER_PP_LOCK, 31,
727 RAPL_DOMAIN_MSR_LIMIT, ARBITRARY_UNIT, 0),
728 PRIMITIVE_INFO_INIT(PL1_ENABLE, POWER_LIMIT1_ENABLE, 15,
729 RAPL_DOMAIN_MSR_LIMIT, ARBITRARY_UNIT, 0),
730 PRIMITIVE_INFO_INIT(PL1_CLAMP, POWER_LIMIT1_CLAMP, 16,
731 RAPL_DOMAIN_MSR_LIMIT, ARBITRARY_UNIT, 0),
732 PRIMITIVE_INFO_INIT(PL2_ENABLE, POWER_LIMIT2_ENABLE, 47,
733 RAPL_DOMAIN_MSR_LIMIT, ARBITRARY_UNIT, 0),
734 PRIMITIVE_INFO_INIT(PL2_CLAMP, POWER_LIMIT2_CLAMP, 48,
735 RAPL_DOMAIN_MSR_LIMIT, ARBITRARY_UNIT, 0),
736 PRIMITIVE_INFO_INIT(TIME_WINDOW1, TIME_WINDOW1_MASK, 17,
737 RAPL_DOMAIN_MSR_LIMIT, TIME_UNIT, 0),
738 PRIMITIVE_INFO_INIT(TIME_WINDOW2, TIME_WINDOW2_MASK, 49,
739 RAPL_DOMAIN_MSR_LIMIT, TIME_UNIT, 0),
740 PRIMITIVE_INFO_INIT(THERMAL_SPEC_POWER, POWER_INFO_THERMAL_SPEC_MASK,
741 0, RAPL_DOMAIN_MSR_INFO, POWER_UNIT, 0),
742 PRIMITIVE_INFO_INIT(MAX_POWER, POWER_INFO_MAX_MASK, 32,
743 RAPL_DOMAIN_MSR_INFO, POWER_UNIT, 0),
744 PRIMITIVE_INFO_INIT(MIN_POWER, POWER_INFO_MIN_MASK, 16,
745 RAPL_DOMAIN_MSR_INFO, POWER_UNIT, 0),
746 PRIMITIVE_INFO_INIT(MAX_TIME_WINDOW, POWER_INFO_MAX_TIME_WIN_MASK, 48,
747 RAPL_DOMAIN_MSR_INFO, TIME_UNIT, 0),
748 PRIMITIVE_INFO_INIT(THROTTLED_TIME, PERF_STATUS_THROTTLE_TIME_MASK, 0,
749 RAPL_DOMAIN_MSR_PERF, TIME_UNIT, 0),
750 PRIMITIVE_INFO_INIT(PRIORITY_LEVEL, PP_POLICY_MASK, 0,
751 RAPL_DOMAIN_MSR_POLICY, ARBITRARY_UNIT, 0),
752 /* non-hardware */
753 PRIMITIVE_INFO_INIT(AVERAGE_POWER, 0, 0, 0, POWER_UNIT,
754 RAPL_PRIMITIVE_DERIVED),
755 {NULL, 0, 0, 0},
756};
757
758/* Read primitive data based on its related struct rapl_primitive_info.
759 * if xlate flag is set, return translated data based on data units, i.e.
760 * time, energy, and power.
761 * RAPL MSRs are non-architectual and are laid out not consistently across
762 * domains. Here we use primitive info to allow writing consolidated access
763 * functions.
764 * For a given primitive, it is processed by MSR mask and shift. Unit conversion
765 * is pre-assigned based on RAPL unit MSRs read at init time.
766 * 63-------------------------- 31--------------------------- 0
767 * | xxxxx (mask) |
768 * | |<- shift ----------------|
769 * 63-------------------------- 31--------------------------- 0
770 */
771static int rapl_read_data_raw(struct rapl_domain *rd,
772 enum rapl_primitives prim,
773 bool xlate, u64 *data)
774{
775 u64 value, final;
776 u32 msr;
777 struct rapl_primitive_info *rp = &rpi[prim];
778 int cpu;
779
780 if (!rp->name || rp->flag & RAPL_PRIMITIVE_DUMMY)
781 return -EINVAL;
782
783 msr = rd->msrs[rp->id];
784 if (!msr)
785 return -EINVAL;
Jacob Pan323ee642016-02-24 13:31:38 -0800786
787 cpu = rd->rp->lead_cpu;
Jacob Pan2d281d82013-10-17 10:28:35 -0700788
789 /* special-case package domain, which uses a different bit*/
790 if (prim == FW_LOCK && rd->id == RAPL_DOMAIN_PACKAGE) {
791 rp->mask = POWER_PACKAGE_LOCK;
792 rp->shift = 63;
793 }
794 /* non-hardware data are collected by the polling thread */
795 if (rp->flag & RAPL_PRIMITIVE_DERIVED) {
796 *data = rd->rdd.primitives[prim];
797 return 0;
798 }
799
800 if (rdmsrl_safe_on_cpu(cpu, msr, &value)) {
801 pr_debug("failed to read msr 0x%x on cpu %d\n", msr, cpu);
802 return -EIO;
803 }
804
805 final = value & rp->mask;
806 final = final >> rp->shift;
807 if (xlate)
Jacob Pan309557f2016-02-24 13:31:37 -0800808 *data = rapl_unit_xlate(rd, rp->unit, final, 0);
Jacob Pan2d281d82013-10-17 10:28:35 -0700809 else
810 *data = final;
811
812 return 0;
813}
814
Jacob Panf14a1392016-02-24 13:31:36 -0800815
816static int msrl_update_safe(u32 msr_no, u64 clear_mask, u64 set_mask)
817{
818 int err;
819 u64 val;
820
821 err = rdmsrl_safe(msr_no, &val);
822 if (err)
823 goto out;
824
825 val &= ~clear_mask;
826 val |= set_mask;
827
828 err = wrmsrl_safe(msr_no, val);
829
830out:
831 return err;
832}
833
834static void msrl_update_func(void *info)
835{
836 struct msrl_action *ma = info;
837
838 ma->err = msrl_update_safe(ma->msr_no, ma->clear_mask, ma->set_mask);
839}
840
Jacob Pan2d281d82013-10-17 10:28:35 -0700841/* Similar use of primitive info in the read counterpart */
842static int rapl_write_data_raw(struct rapl_domain *rd,
843 enum rapl_primitives prim,
844 unsigned long long value)
845{
Jacob Pan2d281d82013-10-17 10:28:35 -0700846 struct rapl_primitive_info *rp = &rpi[prim];
847 int cpu;
Jacob Panf14a1392016-02-24 13:31:36 -0800848 u64 bits;
849 struct msrl_action ma;
850 int ret;
Jacob Pan2d281d82013-10-17 10:28:35 -0700851
Jacob Pan323ee642016-02-24 13:31:38 -0800852 cpu = rd->rp->lead_cpu;
Jacob Pan309557f2016-02-24 13:31:37 -0800853 bits = rapl_unit_xlate(rd, rp->unit, value, 1);
Jacob Panf14a1392016-02-24 13:31:36 -0800854 bits |= bits << rp->shift;
855 memset(&ma, 0, sizeof(ma));
856
857 ma.msr_no = rd->msrs[rp->id];
858 ma.clear_mask = rp->mask;
859 ma.set_mask = bits;
860
861 ret = smp_call_function_single(cpu, msrl_update_func, &ma, 1);
862 if (ret)
863 WARN_ON_ONCE(ret);
864 else
865 ret = ma.err;
866
867 return ret;
Jacob Pan2d281d82013-10-17 10:28:35 -0700868}
869
Jacob Pan3c2c0842014-11-07 09:29:26 -0800870/*
871 * Raw RAPL data stored in MSRs are in certain scales. We need to
872 * convert them into standard units based on the units reported in
873 * the RAPL unit MSRs. This is specific to CPUs as the method to
874 * calculate units differ on different CPUs.
875 * We convert the units to below format based on CPUs.
876 * i.e.
Jacob Pand474a4d2015-03-13 03:48:56 -0700877 * energy unit: picoJoules : Represented in picoJoules by default
Jacob Pan3c2c0842014-11-07 09:29:26 -0800878 * power unit : microWatts : Represented in milliWatts by default
879 * time unit : microseconds: Represented in seconds by default
880 */
881static int rapl_check_unit_core(struct rapl_package *rp, int cpu)
Jacob Pan2d281d82013-10-17 10:28:35 -0700882{
883 u64 msr_val;
884 u32 value;
885
886 if (rdmsrl_safe_on_cpu(cpu, MSR_RAPL_POWER_UNIT, &msr_val)) {
887 pr_err("Failed to read power unit MSR 0x%x on CPU %d, exit.\n",
888 MSR_RAPL_POWER_UNIT, cpu);
889 return -ENODEV;
890 }
891
Jacob Pan2d281d82013-10-17 10:28:35 -0700892 value = (msr_val & ENERGY_UNIT_MASK) >> ENERGY_UNIT_OFFSET;
Jacob Pand474a4d2015-03-13 03:48:56 -0700893 rp->energy_unit = ENERGY_UNIT_SCALE * 1000000 / (1 << value);
Jacob Pan2d281d82013-10-17 10:28:35 -0700894
895 value = (msr_val & POWER_UNIT_MASK) >> POWER_UNIT_OFFSET;
Jacob Pan3c2c0842014-11-07 09:29:26 -0800896 rp->power_unit = 1000000 / (1 << value);
Jacob Pan2d281d82013-10-17 10:28:35 -0700897
898 value = (msr_val & TIME_UNIT_MASK) >> TIME_UNIT_OFFSET;
Jacob Pan3c2c0842014-11-07 09:29:26 -0800899 rp->time_unit = 1000000 / (1 << value);
Jacob Pan2d281d82013-10-17 10:28:35 -0700900
Jacob Pand474a4d2015-03-13 03:48:56 -0700901 pr_debug("Core CPU package %d energy=%dpJ, time=%dus, power=%duW\n",
Jacob Pan3c2c0842014-11-07 09:29:26 -0800902 rp->id, rp->energy_unit, rp->time_unit, rp->power_unit);
Jacob Pan2d281d82013-10-17 10:28:35 -0700903
904 return 0;
905}
906
Jacob Pan3c2c0842014-11-07 09:29:26 -0800907static int rapl_check_unit_atom(struct rapl_package *rp, int cpu)
908{
909 u64 msr_val;
910 u32 value;
911
912 if (rdmsrl_safe_on_cpu(cpu, MSR_RAPL_POWER_UNIT, &msr_val)) {
913 pr_err("Failed to read power unit MSR 0x%x on CPU %d, exit.\n",
914 MSR_RAPL_POWER_UNIT, cpu);
915 return -ENODEV;
916 }
917 value = (msr_val & ENERGY_UNIT_MASK) >> ENERGY_UNIT_OFFSET;
Jacob Pand474a4d2015-03-13 03:48:56 -0700918 rp->energy_unit = ENERGY_UNIT_SCALE * 1 << value;
Jacob Pan3c2c0842014-11-07 09:29:26 -0800919
920 value = (msr_val & POWER_UNIT_MASK) >> POWER_UNIT_OFFSET;
921 rp->power_unit = (1 << value) * 1000;
922
923 value = (msr_val & TIME_UNIT_MASK) >> TIME_UNIT_OFFSET;
924 rp->time_unit = 1000000 / (1 << value);
925
Jacob Pand474a4d2015-03-13 03:48:56 -0700926 pr_debug("Atom package %d energy=%dpJ, time=%dus, power=%duW\n",
Jacob Pan3c2c0842014-11-07 09:29:26 -0800927 rp->id, rp->energy_unit, rp->time_unit, rp->power_unit);
928
929 return 0;
930}
931
Jacob Panf14a1392016-02-24 13:31:36 -0800932static void power_limit_irq_save_cpu(void *info)
933{
934 u32 l, h = 0;
935 struct rapl_package *rp = (struct rapl_package *)info;
936
937 /* save the state of PLN irq mask bit before disabling it */
938 rdmsr_safe(MSR_IA32_PACKAGE_THERM_INTERRUPT, &l, &h);
939 if (!(rp->power_limit_irq & PACKAGE_PLN_INT_SAVED)) {
940 rp->power_limit_irq = l & PACKAGE_THERM_INT_PLN_ENABLE;
941 rp->power_limit_irq |= PACKAGE_PLN_INT_SAVED;
942 }
943 l &= ~PACKAGE_THERM_INT_PLN_ENABLE;
944 wrmsr_safe(MSR_IA32_PACKAGE_THERM_INTERRUPT, l, h);
945}
946
Jacob Pan3c2c0842014-11-07 09:29:26 -0800947
Jacob Pan2d281d82013-10-17 10:28:35 -0700948/* REVISIT:
949 * When package power limit is set artificially low by RAPL, LVT
950 * thermal interrupt for package power limit should be ignored
951 * since we are not really exceeding the real limit. The intention
952 * is to avoid excessive interrupts while we are trying to save power.
953 * A useful feature might be routing the package_power_limit interrupt
954 * to userspace via eventfd. once we have a usecase, this is simple
955 * to do by adding an atomic notifier.
956 */
957
Jacob Pan309557f2016-02-24 13:31:37 -0800958static void package_power_limit_irq_save(struct rapl_package *rp)
Jacob Pan2d281d82013-10-17 10:28:35 -0700959{
Jacob Pan2d281d82013-10-17 10:28:35 -0700960 if (!boot_cpu_has(X86_FEATURE_PTS) || !boot_cpu_has(X86_FEATURE_PLN))
961 return;
962
Jacob Pan323ee642016-02-24 13:31:38 -0800963 smp_call_function_single(rp->lead_cpu, power_limit_irq_save_cpu, rp, 1);
Jacob Panf14a1392016-02-24 13:31:36 -0800964}
965
Thomas Gleixner58705062016-11-22 21:16:02 +0000966/*
967 * Restore per package power limit interrupt enable state. Called from cpu
968 * hotplug code on package removal.
969 */
970static void package_power_limit_irq_restore(struct rapl_package *rp)
Jacob Panf14a1392016-02-24 13:31:36 -0800971{
Thomas Gleixner58705062016-11-22 21:16:02 +0000972 u32 l, h;
973
974 if (!boot_cpu_has(X86_FEATURE_PTS) || !boot_cpu_has(X86_FEATURE_PLN))
975 return;
976
977 /* irq enable state not saved, nothing to restore */
978 if (!(rp->power_limit_irq & PACKAGE_PLN_INT_SAVED))
979 return;
Jacob Panf14a1392016-02-24 13:31:36 -0800980
981 rdmsr_safe(MSR_IA32_PACKAGE_THERM_INTERRUPT, &l, &h);
982
983 if (rp->power_limit_irq & PACKAGE_THERM_INT_PLN_ENABLE)
984 l |= PACKAGE_THERM_INT_PLN_ENABLE;
985 else
986 l &= ~PACKAGE_THERM_INT_PLN_ENABLE;
987
988 wrmsr_safe(MSR_IA32_PACKAGE_THERM_INTERRUPT, l, h);
Jacob Pan2d281d82013-10-17 10:28:35 -0700989}
990
Jacob Pan3c2c0842014-11-07 09:29:26 -0800991static void set_floor_freq_default(struct rapl_domain *rd, bool mode)
992{
993 int nr_powerlimit = find_nr_power_limit(rd);
994
995 /* always enable clamp such that p-state can go below OS requested
996 * range. power capping priority over guranteed frequency.
997 */
998 rapl_write_data_raw(rd, PL1_CLAMP, mode);
999
1000 /* some domains have pl2 */
1001 if (nr_powerlimit > 1) {
1002 rapl_write_data_raw(rd, PL2_ENABLE, mode);
1003 rapl_write_data_raw(rd, PL2_CLAMP, mode);
1004 }
1005}
1006
1007static void set_floor_freq_atom(struct rapl_domain *rd, bool enable)
1008{
1009 static u32 power_ctrl_orig_val;
1010 u32 mdata;
1011
Ajay Thomas51b63402015-04-30 01:43:23 +05301012 if (!rapl_defaults->floor_freq_reg_addr) {
1013 pr_err("Invalid floor frequency config register\n");
1014 return;
1015 }
1016
Jacob Pan3c2c0842014-11-07 09:29:26 -08001017 if (!power_ctrl_orig_val)
Andy Shevchenko4077a382015-11-11 19:59:29 +02001018 iosf_mbi_read(BT_MBI_UNIT_PMC, MBI_CR_READ,
1019 rapl_defaults->floor_freq_reg_addr,
1020 &power_ctrl_orig_val);
Jacob Pan3c2c0842014-11-07 09:29:26 -08001021 mdata = power_ctrl_orig_val;
1022 if (enable) {
1023 mdata &= ~(0x7f << 8);
1024 mdata |= 1 << 8;
1025 }
Andy Shevchenko4077a382015-11-11 19:59:29 +02001026 iosf_mbi_write(BT_MBI_UNIT_PMC, MBI_CR_WRITE,
1027 rapl_defaults->floor_freq_reg_addr, mdata);
Jacob Pan3c2c0842014-11-07 09:29:26 -08001028}
1029
1030static u64 rapl_compute_time_window_core(struct rapl_package *rp, u64 value,
1031 bool to_raw)
1032{
1033 u64 f, y; /* fraction and exp. used for time unit */
1034
1035 /*
1036 * Special processing based on 2^Y*(1+F/4), refer
1037 * to Intel Software Developer's manual Vol.3B: CH 14.9.3.
1038 */
1039 if (!to_raw) {
1040 f = (value & 0x60) >> 5;
1041 y = value & 0x1f;
1042 value = (1 << y) * (4 + f) * rp->time_unit / 4;
1043 } else {
1044 do_div(value, rp->time_unit);
1045 y = ilog2(value);
1046 f = div64_u64(4 * (value - (1 << y)), 1 << y);
1047 value = (y & 0x1f) | ((f & 0x3) << 5);
1048 }
1049 return value;
1050}
1051
1052static u64 rapl_compute_time_window_atom(struct rapl_package *rp, u64 value,
1053 bool to_raw)
1054{
1055 /*
1056 * Atom time unit encoding is straight forward val * time_unit,
1057 * where time_unit is default to 1 sec. Never 0.
1058 */
1059 if (!to_raw)
1060 return (value) ? value *= rp->time_unit : rp->time_unit;
1061 else
1062 value = div64_u64(value, rp->time_unit);
1063
1064 return value;
1065}
1066
Jacob Pan087e9cb2014-11-07 09:29:25 -08001067static const struct rapl_defaults rapl_defaults_core = {
Ajay Thomas51b63402015-04-30 01:43:23 +05301068 .floor_freq_reg_addr = 0,
Jacob Pan3c2c0842014-11-07 09:29:26 -08001069 .check_unit = rapl_check_unit_core,
1070 .set_floor_freq = set_floor_freq_default,
1071 .compute_time_window = rapl_compute_time_window_core,
Jacob Pan087e9cb2014-11-07 09:29:25 -08001072};
1073
Jacob Pand474a4d2015-03-13 03:48:56 -07001074static const struct rapl_defaults rapl_defaults_hsw_server = {
1075 .check_unit = rapl_check_unit_core,
1076 .set_floor_freq = set_floor_freq_default,
1077 .compute_time_window = rapl_compute_time_window_core,
1078 .dram_domain_energy_unit = 15300,
1079};
1080
Ajay Thomas51b63402015-04-30 01:43:23 +05301081static const struct rapl_defaults rapl_defaults_byt = {
1082 .floor_freq_reg_addr = IOSF_CPU_POWER_BUDGET_CTL_BYT,
Jacob Pan3c2c0842014-11-07 09:29:26 -08001083 .check_unit = rapl_check_unit_atom,
1084 .set_floor_freq = set_floor_freq_atom,
1085 .compute_time_window = rapl_compute_time_window_atom,
Jacob Pan087e9cb2014-11-07 09:29:25 -08001086};
1087
Ajay Thomas51b63402015-04-30 01:43:23 +05301088static const struct rapl_defaults rapl_defaults_tng = {
1089 .floor_freq_reg_addr = IOSF_CPU_POWER_BUDGET_CTL_TNG,
1090 .check_unit = rapl_check_unit_atom,
1091 .set_floor_freq = set_floor_freq_atom,
1092 .compute_time_window = rapl_compute_time_window_atom,
1093};
1094
1095static const struct rapl_defaults rapl_defaults_ann = {
1096 .floor_freq_reg_addr = 0,
1097 .check_unit = rapl_check_unit_atom,
1098 .set_floor_freq = NULL,
1099 .compute_time_window = rapl_compute_time_window_atom,
1100};
1101
1102static const struct rapl_defaults rapl_defaults_cht = {
1103 .floor_freq_reg_addr = 0,
1104 .check_unit = rapl_check_unit_atom,
1105 .set_floor_freq = NULL,
1106 .compute_time_window = rapl_compute_time_window_atom,
1107};
1108
Jacob Pan087e9cb2014-11-07 09:29:25 -08001109#define RAPL_CPU(_model, _ops) { \
1110 .vendor = X86_VENDOR_INTEL, \
1111 .family = 6, \
1112 .model = _model, \
1113 .driver_data = (kernel_ulong_t)&_ops, \
1114 }
1115
Mathias Krauseea85dbc2015-03-25 22:15:52 +01001116static const struct x86_cpu_id rapl_ids[] __initconst = {
Dave Hansen62d16732016-06-02 17:19:36 -07001117 RAPL_CPU(INTEL_FAM6_SANDYBRIDGE, rapl_defaults_core),
1118 RAPL_CPU(INTEL_FAM6_SANDYBRIDGE_X, rapl_defaults_core),
Dave Hansen0bb04b52016-06-02 17:19:37 -07001119
Dave Hansen62d16732016-06-02 17:19:36 -07001120 RAPL_CPU(INTEL_FAM6_IVYBRIDGE, rapl_defaults_core),
Xiaolong Wang7d188472016-06-24 11:28:20 +08001121 RAPL_CPU(INTEL_FAM6_IVYBRIDGE_X, rapl_defaults_core),
Dave Hansen0bb04b52016-06-02 17:19:37 -07001122
Dave Hansen62d16732016-06-02 17:19:36 -07001123 RAPL_CPU(INTEL_FAM6_HASWELL_CORE, rapl_defaults_core),
Dave Hansen62d16732016-06-02 17:19:36 -07001124 RAPL_CPU(INTEL_FAM6_HASWELL_ULT, rapl_defaults_core),
1125 RAPL_CPU(INTEL_FAM6_HASWELL_GT3E, rapl_defaults_core),
Dave Hansen0bb04b52016-06-02 17:19:37 -07001126 RAPL_CPU(INTEL_FAM6_HASWELL_X, rapl_defaults_hsw_server),
1127
1128 RAPL_CPU(INTEL_FAM6_BROADWELL_CORE, rapl_defaults_core),
Dave Hansen62d16732016-06-02 17:19:36 -07001129 RAPL_CPU(INTEL_FAM6_BROADWELL_GT3E, rapl_defaults_core),
Dave Hansen62d16732016-06-02 17:19:36 -07001130 RAPL_CPU(INTEL_FAM6_BROADWELL_XEON_D, rapl_defaults_core),
Dave Hansen0bb04b52016-06-02 17:19:37 -07001131 RAPL_CPU(INTEL_FAM6_BROADWELL_X, rapl_defaults_hsw_server),
1132
Dave Hansen62d16732016-06-02 17:19:36 -07001133 RAPL_CPU(INTEL_FAM6_SKYLAKE_DESKTOP, rapl_defaults_core),
Dave Hansen0bb04b52016-06-02 17:19:37 -07001134 RAPL_CPU(INTEL_FAM6_SKYLAKE_MOBILE, rapl_defaults_core),
Dave Hansend40671e2016-06-02 17:19:55 -07001135 RAPL_CPU(INTEL_FAM6_SKYLAKE_X, rapl_defaults_hsw_server),
Dave Hansen62d16732016-06-02 17:19:36 -07001136 RAPL_CPU(INTEL_FAM6_KABYLAKE_MOBILE, rapl_defaults_core),
1137 RAPL_CPU(INTEL_FAM6_KABYLAKE_DESKTOP, rapl_defaults_core),
Dave Hansen0bb04b52016-06-02 17:19:37 -07001138
1139 RAPL_CPU(INTEL_FAM6_ATOM_SILVERMONT1, rapl_defaults_byt),
1140 RAPL_CPU(INTEL_FAM6_ATOM_AIRMONT, rapl_defaults_cht),
Andy Shevchenkof5fbf842016-09-06 21:42:54 +03001141 RAPL_CPU(INTEL_FAM6_ATOM_MERRIFIELD, rapl_defaults_tng),
1142 RAPL_CPU(INTEL_FAM6_ATOM_MOOREFIELD, rapl_defaults_ann),
Dave Hansen0bb04b52016-06-02 17:19:37 -07001143 RAPL_CPU(INTEL_FAM6_ATOM_GOLDMONT, rapl_defaults_core),
Jacob Panab0d15d2016-06-13 17:02:28 -07001144 RAPL_CPU(INTEL_FAM6_ATOM_DENVERTON, rapl_defaults_core),
Dave Hansen0bb04b52016-06-02 17:19:37 -07001145
1146 RAPL_CPU(INTEL_FAM6_XEON_PHI_KNL, rapl_defaults_hsw_server),
Jacob Pan2d281d82013-10-17 10:28:35 -07001147 {}
1148};
1149MODULE_DEVICE_TABLE(x86cpu, rapl_ids);
1150
Thomas Gleixnerbed5ab62016-11-22 21:15:58 +00001151/* Read once for all raw primitive data for domains */
1152static void rapl_update_domain_data(struct rapl_package *rp)
Jacob Pan2d281d82013-10-17 10:28:35 -07001153{
1154 int dmn, prim;
1155 u64 val;
Jacob Pan2d281d82013-10-17 10:28:35 -07001156
Thomas Gleixnerbed5ab62016-11-22 21:15:58 +00001157 for (dmn = 0; dmn < rp->nr_domains; dmn++) {
1158 pr_debug("update package %d domain %s data\n", rp->id,
1159 rp->domains[dmn].name);
1160 /* exclude non-raw primitives */
1161 for (prim = 0; prim < NR_RAW_PRIMITIVES; prim++) {
1162 if (!rapl_read_data_raw(&rp->domains[dmn], prim,
1163 rpi[prim].unit, &val))
1164 rp->domains[dmn].rdd.primitives[prim] = val;
Jacob Pan2d281d82013-10-17 10:28:35 -07001165 }
1166 }
1167
1168}
1169
Thomas Gleixner58705062016-11-22 21:16:02 +00001170static void rapl_unregister_powercap(void)
Jacob Pan2d281d82013-10-17 10:28:35 -07001171{
Srinivas Pandruvada3521ba12016-04-17 15:03:01 -07001172 if (platform_rapl_domain) {
1173 powercap_unregister_zone(control_type,
1174 &platform_rapl_domain->power_zone);
1175 kfree(platform_rapl_domain);
1176 }
Jacob Pan2d281d82013-10-17 10:28:35 -07001177 powercap_unregister_control_type(control_type);
Jacob Pan2d281d82013-10-17 10:28:35 -07001178}
1179
1180static int rapl_package_register_powercap(struct rapl_package *rp)
1181{
1182 struct rapl_domain *rd;
Jacob Pan2d281d82013-10-17 10:28:35 -07001183 char dev_name[17]; /* max domain name = 7 + 1 + 8 for int + 1 for null*/
1184 struct powercap_zone *power_zone = NULL;
Thomas Gleixnerbed5ab62016-11-22 21:15:58 +00001185 int nr_pl, ret;;
1186
1187 /* Update the domain data of the new package */
1188 rapl_update_domain_data(rp);
Jacob Pan2d281d82013-10-17 10:28:35 -07001189
1190 /* first we register package domain as the parent zone*/
1191 for (rd = rp->domains; rd < rp->domains + rp->nr_domains; rd++) {
1192 if (rd->id == RAPL_DOMAIN_PACKAGE) {
1193 nr_pl = find_nr_power_limit(rd);
1194 pr_debug("register socket %d package domain %s\n",
1195 rp->id, rd->name);
1196 memset(dev_name, 0, sizeof(dev_name));
1197 snprintf(dev_name, sizeof(dev_name), "%s-%d",
1198 rd->name, rp->id);
1199 power_zone = powercap_register_zone(&rd->power_zone,
1200 control_type,
1201 dev_name, NULL,
1202 &zone_ops[rd->id],
1203 nr_pl,
1204 &constraint_ops);
1205 if (IS_ERR(power_zone)) {
1206 pr_debug("failed to register package, %d\n",
1207 rp->id);
Thomas Gleixnerbed5ab62016-11-22 21:15:58 +00001208 return PTR_ERR(power_zone);
Jacob Pan2d281d82013-10-17 10:28:35 -07001209 }
1210 /* track parent zone in per package/socket data */
1211 rp->power_zone = power_zone;
1212 /* done, only one package domain per socket */
1213 break;
1214 }
1215 }
1216 if (!power_zone) {
1217 pr_err("no package domain found, unknown topology!\n");
Thomas Gleixnerbed5ab62016-11-22 21:15:58 +00001218 return -ENODEV;
Jacob Pan2d281d82013-10-17 10:28:35 -07001219 }
1220 /* now register domains as children of the socket/package*/
1221 for (rd = rp->domains; rd < rp->domains + rp->nr_domains; rd++) {
1222 if (rd->id == RAPL_DOMAIN_PACKAGE)
1223 continue;
1224 /* number of power limits per domain varies */
1225 nr_pl = find_nr_power_limit(rd);
1226 power_zone = powercap_register_zone(&rd->power_zone,
1227 control_type, rd->name,
1228 rp->power_zone,
1229 &zone_ops[rd->id], nr_pl,
1230 &constraint_ops);
1231
1232 if (IS_ERR(power_zone)) {
1233 pr_debug("failed to register power_zone, %d:%s:%s\n",
1234 rp->id, rd->name, dev_name);
1235 ret = PTR_ERR(power_zone);
1236 goto err_cleanup;
1237 }
1238 }
Thomas Gleixnerbed5ab62016-11-22 21:15:58 +00001239 return 0;
Jacob Pan2d281d82013-10-17 10:28:35 -07001240
Jacob Pan2d281d82013-10-17 10:28:35 -07001241err_cleanup:
Thomas Gleixner58705062016-11-22 21:16:02 +00001242 /*
1243 * Clean up previously initialized domains within the package if we
Jacob Pan2d281d82013-10-17 10:28:35 -07001244 * failed after the first domain setup.
1245 */
1246 while (--rd >= rp->domains) {
1247 pr_debug("unregister package %d domain %s\n", rp->id, rd->name);
1248 powercap_unregister_zone(control_type, &rd->power_zone);
1249 }
1250
1251 return ret;
1252}
1253
Thomas Gleixner58705062016-11-22 21:16:02 +00001254static int __init rapl_register_psys(void)
Srinivas Pandruvada3521ba12016-04-17 15:03:01 -07001255{
1256 struct rapl_domain *rd;
1257 struct powercap_zone *power_zone;
1258 u64 val;
1259
1260 if (rdmsrl_safe_on_cpu(0, MSR_PLATFORM_ENERGY_STATUS, &val) || !val)
1261 return -ENODEV;
1262
1263 if (rdmsrl_safe_on_cpu(0, MSR_PLATFORM_POWER_LIMIT, &val) || !val)
1264 return -ENODEV;
1265
1266 rd = kzalloc(sizeof(*rd), GFP_KERNEL);
1267 if (!rd)
1268 return -ENOMEM;
1269
1270 rd->name = rapl_domain_names[RAPL_DOMAIN_PLATFORM];
1271 rd->id = RAPL_DOMAIN_PLATFORM;
1272 rd->msrs[0] = MSR_PLATFORM_POWER_LIMIT;
1273 rd->msrs[1] = MSR_PLATFORM_ENERGY_STATUS;
1274 rd->rpl[0].prim_id = PL1_ENABLE;
1275 rd->rpl[0].name = pl1_name;
1276 rd->rpl[1].prim_id = PL2_ENABLE;
1277 rd->rpl[1].name = pl2_name;
1278 rd->rp = find_package_by_id(0);
1279
1280 power_zone = powercap_register_zone(&rd->power_zone, control_type,
1281 "psys", NULL,
1282 &zone_ops[RAPL_DOMAIN_PLATFORM],
1283 2, &constraint_ops);
1284
1285 if (IS_ERR(power_zone)) {
1286 kfree(rd);
1287 return PTR_ERR(power_zone);
1288 }
1289
1290 platform_rapl_domain = rd;
1291
1292 return 0;
1293}
1294
Thomas Gleixner58705062016-11-22 21:16:02 +00001295static int __init rapl_register_powercap(void)
Jacob Pan2d281d82013-10-17 10:28:35 -07001296{
Jacob Pan2d281d82013-10-17 10:28:35 -07001297 control_type = powercap_register_control_type(NULL, "intel-rapl", NULL);
1298 if (IS_ERR(control_type)) {
1299 pr_debug("failed to register powercap control_type.\n");
1300 return PTR_ERR(control_type);
1301 }
Thomas Gleixner58705062016-11-22 21:16:02 +00001302 return 0;
Jacob Pan2d281d82013-10-17 10:28:35 -07001303}
1304
1305static int rapl_check_domain(int cpu, int domain)
1306{
1307 unsigned msr;
Jacob Pan9d31c672014-04-29 15:33:06 -07001308 u64 val = 0;
Jacob Pan2d281d82013-10-17 10:28:35 -07001309
1310 switch (domain) {
1311 case RAPL_DOMAIN_PACKAGE:
1312 msr = MSR_PKG_ENERGY_STATUS;
1313 break;
1314 case RAPL_DOMAIN_PP0:
1315 msr = MSR_PP0_ENERGY_STATUS;
1316 break;
1317 case RAPL_DOMAIN_PP1:
1318 msr = MSR_PP1_ENERGY_STATUS;
1319 break;
1320 case RAPL_DOMAIN_DRAM:
1321 msr = MSR_DRAM_ENERGY_STATUS;
1322 break;
Srinivas Pandruvada3521ba12016-04-17 15:03:01 -07001323 case RAPL_DOMAIN_PLATFORM:
1324 /* PSYS(PLATFORM) is not a CPU domain, so avoid printng error */
1325 return -EINVAL;
Jacob Pan2d281d82013-10-17 10:28:35 -07001326 default:
1327 pr_err("invalid domain id %d\n", domain);
1328 return -EINVAL;
1329 }
Jacob Pan9d31c672014-04-29 15:33:06 -07001330 /* make sure domain counters are available and contains non-zero
1331 * values, otherwise skip it.
1332 */
1333 if (rdmsrl_safe_on_cpu(cpu, msr, &val) || !val)
Jacob Pan2d281d82013-10-17 10:28:35 -07001334 return -ENODEV;
1335
Jacob Pan9d31c672014-04-29 15:33:06 -07001336 return 0;
Jacob Pan2d281d82013-10-17 10:28:35 -07001337}
1338
Jacob Pane1399ba2016-05-31 13:41:29 -07001339
1340/*
1341 * Check if power limits are available. Two cases when they are not available:
1342 * 1. Locked by BIOS, in this case we still provide read-only access so that
1343 * users can see what limit is set by the BIOS.
1344 * 2. Some CPUs make some domains monitoring only which means PLx MSRs may not
1345 * exist at all. In this case, we do not show the contraints in powercap.
1346 *
1347 * Called after domains are detected and initialized.
1348 */
1349static void rapl_detect_powerlimit(struct rapl_domain *rd)
1350{
1351 u64 val64;
1352 int i;
1353
1354 /* check if the domain is locked by BIOS, ignore if MSR doesn't exist */
1355 if (!rapl_read_data_raw(rd, FW_LOCK, false, &val64)) {
1356 if (val64) {
1357 pr_info("RAPL package %d domain %s locked by BIOS\n",
1358 rd->rp->id, rd->name);
1359 rd->state |= DOMAIN_STATE_BIOS_LOCKED;
1360 }
1361 }
1362 /* check if power limit MSRs exists, otherwise domain is monitoring only */
1363 for (i = 0; i < NR_POWER_LIMITS; i++) {
1364 int prim = rd->rpl[i].prim_id;
1365 if (rapl_read_data_raw(rd, prim, false, &val64))
1366 rd->rpl[i].name = NULL;
1367 }
1368}
1369
Jacob Pan2d281d82013-10-17 10:28:35 -07001370/* Detect active and valid domains for the given CPU, caller must
1371 * ensure the CPU belongs to the targeted package and CPU hotlug is disabled.
1372 */
1373static int rapl_detect_domains(struct rapl_package *rp, int cpu)
1374{
Jacob Pan2d281d82013-10-17 10:28:35 -07001375 struct rapl_domain *rd;
Thomas Gleixner58705062016-11-22 21:16:02 +00001376 int i;
Jacob Pan2d281d82013-10-17 10:28:35 -07001377
1378 for (i = 0; i < RAPL_DOMAIN_MAX; i++) {
1379 /* use physical package id to read counters */
Jacob Panfcdf1792014-09-02 02:55:21 -07001380 if (!rapl_check_domain(cpu, i)) {
Jacob Pan2d281d82013-10-17 10:28:35 -07001381 rp->domain_map |= 1 << i;
Jacob Panfcdf1792014-09-02 02:55:21 -07001382 pr_info("Found RAPL domain %s\n", rapl_domain_names[i]);
1383 }
Jacob Pan2d281d82013-10-17 10:28:35 -07001384 }
1385 rp->nr_domains = bitmap_weight(&rp->domain_map, RAPL_DOMAIN_MAX);
1386 if (!rp->nr_domains) {
Jacob Pane1a27e82016-05-23 09:45:43 -07001387 pr_debug("no valid rapl domains found in package %d\n", rp->id);
Thomas Gleixner58705062016-11-22 21:16:02 +00001388 return -ENODEV;
Jacob Pan2d281d82013-10-17 10:28:35 -07001389 }
1390 pr_debug("found %d domains on package %d\n", rp->nr_domains, rp->id);
1391
1392 rp->domains = kcalloc(rp->nr_domains + 1, sizeof(struct rapl_domain),
1393 GFP_KERNEL);
Thomas Gleixner58705062016-11-22 21:16:02 +00001394 if (!rp->domains)
1395 return -ENOMEM;
1396
Jacob Pan2d281d82013-10-17 10:28:35 -07001397 rapl_init_domains(rp);
1398
Jacob Pane1399ba2016-05-31 13:41:29 -07001399 for (rd = rp->domains; rd < rp->domains + rp->nr_domains; rd++)
1400 rapl_detect_powerlimit(rd);
1401
Jacob Pan2d281d82013-10-17 10:28:35 -07001402 return 0;
1403}
1404
1405/* called from CPU hotplug notifier, hotplug lock held */
1406static void rapl_remove_package(struct rapl_package *rp)
1407{
1408 struct rapl_domain *rd, *rd_package = NULL;
1409
Thomas Gleixner58705062016-11-22 21:16:02 +00001410 package_power_limit_irq_restore(rp);
1411
Jacob Pan2d281d82013-10-17 10:28:35 -07001412 for (rd = rp->domains; rd < rp->domains + rp->nr_domains; rd++) {
Thomas Gleixner58705062016-11-22 21:16:02 +00001413 rapl_write_data_raw(rd, PL1_ENABLE, 0);
1414 rapl_write_data_raw(rd, PL1_CLAMP, 0);
1415 if (find_nr_power_limit(rd) > 1) {
1416 rapl_write_data_raw(rd, PL2_ENABLE, 0);
1417 rapl_write_data_raw(rd, PL2_CLAMP, 0);
1418 }
Jacob Pan2d281d82013-10-17 10:28:35 -07001419 if (rd->id == RAPL_DOMAIN_PACKAGE) {
1420 rd_package = rd;
1421 continue;
1422 }
Thomas Gleixner58705062016-11-22 21:16:02 +00001423 pr_debug("remove package, undo power limit on %d: %s\n",
1424 rp->id, rd->name);
Jacob Pan2d281d82013-10-17 10:28:35 -07001425 powercap_unregister_zone(control_type, &rd->power_zone);
1426 }
1427 /* do parent zone last */
1428 powercap_unregister_zone(control_type, &rd_package->power_zone);
1429 list_del(&rp->plist);
1430 kfree(rp);
1431}
1432
1433/* called from CPU hotplug notifier, hotplug lock held */
Thomas Gleixnerb4005e92016-11-22 21:16:05 +00001434static struct rapl_package *rapl_add_package(int cpu, int pkgid)
Jacob Pan2d281d82013-10-17 10:28:35 -07001435{
Jacob Pan2d281d82013-10-17 10:28:35 -07001436 struct rapl_package *rp;
Thomas Gleixnerb4005e92016-11-22 21:16:05 +00001437 int ret;
Jacob Pan2d281d82013-10-17 10:28:35 -07001438
Jacob Pan2d281d82013-10-17 10:28:35 -07001439 rp = kzalloc(sizeof(struct rapl_package), GFP_KERNEL);
1440 if (!rp)
Thomas Gleixnerb4005e92016-11-22 21:16:05 +00001441 return ERR_PTR(-ENOMEM);
Jacob Pan2d281d82013-10-17 10:28:35 -07001442
1443 /* add the new package to the list */
Thomas Gleixnerb4005e92016-11-22 21:16:05 +00001444 rp->id = pkgid;
Jacob Pan323ee642016-02-24 13:31:38 -08001445 rp->lead_cpu = cpu;
1446
Jacob Pan2d281d82013-10-17 10:28:35 -07001447 /* check if the package contains valid domains */
1448 if (rapl_detect_domains(rp, cpu) ||
Jacob Pan3c2c0842014-11-07 09:29:26 -08001449 rapl_defaults->check_unit(rp, cpu)) {
Jacob Pan2d281d82013-10-17 10:28:35 -07001450 ret = -ENODEV;
1451 goto err_free_package;
1452 }
Thomas Gleixnera74f4362016-11-22 21:15:59 +00001453 ret = rapl_package_register_powercap(rp);
1454 if (!ret) {
Jacob Pan2d281d82013-10-17 10:28:35 -07001455 INIT_LIST_HEAD(&rp->plist);
1456 list_add(&rp->plist, &rapl_packages);
Thomas Gleixnerb4005e92016-11-22 21:16:05 +00001457 return rp;
Jacob Pan2d281d82013-10-17 10:28:35 -07001458 }
1459
1460err_free_package:
1461 kfree(rp->domains);
1462 kfree(rp);
Thomas Gleixnerb4005e92016-11-22 21:16:05 +00001463 return ERR_PTR(ret);
Jacob Pan2d281d82013-10-17 10:28:35 -07001464}
1465
1466/* Handles CPU hotplug on multi-socket systems.
1467 * If a CPU goes online as the first CPU of the physical package
1468 * we add the RAPL package to the system. Similarly, when the last
1469 * CPU of the package is removed, we remove the RAPL package and its
1470 * associated domains. Cooling devices are handled accordingly at
1471 * per-domain level.
1472 */
Sebastian Andrzej Siewior5e4dc792016-11-22 21:16:00 +00001473static int rapl_cpu_online(unsigned int cpu)
Jacob Pan2d281d82013-10-17 10:28:35 -07001474{
Thomas Gleixnerb4005e92016-11-22 21:16:05 +00001475 int pkgid = topology_physical_package_id(cpu);
Sebastian Andrzej Siewior5e4dc792016-11-22 21:16:00 +00001476 struct rapl_package *rp;
Sebastian Andrzej Siewior5e4dc792016-11-22 21:16:00 +00001477
Thomas Gleixnerb4005e92016-11-22 21:16:05 +00001478 rp = find_package_by_id(pkgid);
1479 if (!rp) {
1480 rp = rapl_add_package(cpu, pkgid);
1481 if (IS_ERR(rp))
1482 return PTR_ERR(rp);
Thomas Gleixner58705062016-11-22 21:16:02 +00001483 }
Thomas Gleixnerb4005e92016-11-22 21:16:05 +00001484 cpumask_set_cpu(cpu, &rp->cpumask);
1485 return 0;
Sebastian Andrzej Siewior5e4dc792016-11-22 21:16:00 +00001486}
1487
1488static int rapl_cpu_down_prep(unsigned int cpu)
1489{
Thomas Gleixnerb4005e92016-11-22 21:16:05 +00001490 int pkgid = topology_physical_package_id(cpu);
Jacob Pan2d281d82013-10-17 10:28:35 -07001491 struct rapl_package *rp;
Jacob Pan323ee642016-02-24 13:31:38 -08001492 int lead_cpu;
Jacob Pan2d281d82013-10-17 10:28:35 -07001493
Thomas Gleixnerb4005e92016-11-22 21:16:05 +00001494 rp = find_package_by_id(pkgid);
Sebastian Andrzej Siewior5e4dc792016-11-22 21:16:00 +00001495 if (!rp)
1496 return 0;
Thomas Gleixnerb4005e92016-11-22 21:16:05 +00001497
1498 cpumask_clear_cpu(cpu, &rp->cpumask);
1499 lead_cpu = cpumask_first(&rp->cpumask);
1500 if (lead_cpu >= nr_cpu_ids)
Sebastian Andrzej Siewior5e4dc792016-11-22 21:16:00 +00001501 rapl_remove_package(rp);
Thomas Gleixnerb4005e92016-11-22 21:16:05 +00001502 else if (rp->lead_cpu == cpu)
1503 rp->lead_cpu = lead_cpu;
Sebastian Andrzej Siewior5e4dc792016-11-22 21:16:00 +00001504 return 0;
Jacob Pan2d281d82013-10-17 10:28:35 -07001505}
1506
Sebastian Andrzej Siewior5e4dc792016-11-22 21:16:00 +00001507static enum cpuhp_state pcap_rapl_online;
Jacob Pan2d281d82013-10-17 10:28:35 -07001508
1509static int __init rapl_init(void)
1510{
Jacob Pan087e9cb2014-11-07 09:29:25 -08001511 const struct x86_cpu_id *id;
Thomas Gleixner58705062016-11-22 21:16:02 +00001512 int ret;
Jacob Pan2d281d82013-10-17 10:28:35 -07001513
Jacob Pan087e9cb2014-11-07 09:29:25 -08001514 id = x86_match_cpu(rapl_ids);
1515 if (!id) {
Jacob Pan2d281d82013-10-17 10:28:35 -07001516 pr_err("driver does not support CPU family %d model %d\n",
1517 boot_cpu_data.x86, boot_cpu_data.x86_model);
1518
1519 return -ENODEV;
1520 }
Srivatsa S. Bhat009f2252014-03-11 02:09:26 +05301521
Jacob Pan087e9cb2014-11-07 09:29:25 -08001522 rapl_defaults = (struct rapl_defaults *)id->driver_data;
1523
Thomas Gleixner58705062016-11-22 21:16:02 +00001524 ret = rapl_register_powercap();
Jacob Pan2d281d82013-10-17 10:28:35 -07001525 if (ret)
Thomas Gleixner58705062016-11-22 21:16:02 +00001526 return ret;
Jacob Pan2d281d82013-10-17 10:28:35 -07001527
Thomas Gleixner58705062016-11-22 21:16:02 +00001528 ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "powercap/rapl:online",
1529 rapl_cpu_online, rapl_cpu_down_prep);
Sebastian Andrzej Siewior5e4dc792016-11-22 21:16:00 +00001530 if (ret < 0)
1531 goto err_unreg;
1532 pcap_rapl_online = ret;
Thomas Gleixner58705062016-11-22 21:16:02 +00001533
1534 /* Don't bail out if PSys is not supported */
1535 rapl_register_psys();
Sebastian Andrzej Siewior5e4dc792016-11-22 21:16:00 +00001536 return 0;
Jacob Pan2d281d82013-10-17 10:28:35 -07001537
Sebastian Andrzej Siewior5e4dc792016-11-22 21:16:00 +00001538err_unreg:
1539 rapl_unregister_powercap();
Jacob Pan2d281d82013-10-17 10:28:35 -07001540 return ret;
1541}
1542
1543static void __exit rapl_exit(void)
1544{
Sebastian Andrzej Siewior5e4dc792016-11-22 21:16:00 +00001545 cpuhp_remove_state(pcap_rapl_online);
Jacob Pan2d281d82013-10-17 10:28:35 -07001546 rapl_unregister_powercap();
Jacob Pan2d281d82013-10-17 10:28:35 -07001547}
1548
1549module_init(rapl_init);
1550module_exit(rapl_exit);
1551
1552MODULE_DESCRIPTION("Driver for Intel RAPL (Running Average Power Limit)");
1553MODULE_AUTHOR("Jacob Pan <jacob.jun.pan@intel.com>");
1554MODULE_LICENSE("GPL v2");