blob: 256efed5b88ffccdc53290e0527a2e79d2bb01f3 [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>
32
33#include <asm/processor.h>
34#include <asm/cpu_device_id.h>
35
36/* bitmasks for RAPL MSRs, used by primitive access functions */
37#define ENERGY_STATUS_MASK 0xffffffff
38
39#define POWER_LIMIT1_MASK 0x7FFF
40#define POWER_LIMIT1_ENABLE BIT(15)
41#define POWER_LIMIT1_CLAMP BIT(16)
42
43#define POWER_LIMIT2_MASK (0x7FFFULL<<32)
44#define POWER_LIMIT2_ENABLE BIT_ULL(47)
45#define POWER_LIMIT2_CLAMP BIT_ULL(48)
46#define POWER_PACKAGE_LOCK BIT_ULL(63)
47#define POWER_PP_LOCK BIT(31)
48
49#define TIME_WINDOW1_MASK (0x7FULL<<17)
50#define TIME_WINDOW2_MASK (0x7FULL<<49)
51
52#define POWER_UNIT_OFFSET 0
53#define POWER_UNIT_MASK 0x0F
54
55#define ENERGY_UNIT_OFFSET 0x08
56#define ENERGY_UNIT_MASK 0x1F00
57
58#define TIME_UNIT_OFFSET 0x10
59#define TIME_UNIT_MASK 0xF0000
60
61#define POWER_INFO_MAX_MASK (0x7fffULL<<32)
62#define POWER_INFO_MIN_MASK (0x7fffULL<<16)
63#define POWER_INFO_MAX_TIME_WIN_MASK (0x3fULL<<48)
64#define POWER_INFO_THERMAL_SPEC_MASK 0x7fff
65
66#define PERF_STATUS_THROTTLE_TIME_MASK 0xffffffff
67#define PP_POLICY_MASK 0x1F
68
69/* Non HW constants */
70#define RAPL_PRIMITIVE_DERIVED BIT(1) /* not from raw data */
71#define RAPL_PRIMITIVE_DUMMY BIT(2)
72
73/* scale RAPL units to avoid floating point math inside kernel */
74#define POWER_UNIT_SCALE (1000000)
75#define ENERGY_UNIT_SCALE (1000000)
76#define TIME_UNIT_SCALE (1000000)
77
78#define TIME_WINDOW_MAX_MSEC 40000
79#define TIME_WINDOW_MIN_MSEC 250
80
81enum 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 */
93 RAPL_DOMAIN_MAX,
94};
95
96enum rapl_domain_msr_id {
97 RAPL_DOMAIN_MSR_LIMIT,
98 RAPL_DOMAIN_MSR_STATUS,
99 RAPL_DOMAIN_MSR_PERF,
100 RAPL_DOMAIN_MSR_POLICY,
101 RAPL_DOMAIN_MSR_INFO,
102 RAPL_DOMAIN_MSR_MAX,
103};
104
105/* per domain data, some are optional */
106enum rapl_primitives {
107 ENERGY_COUNTER,
108 POWER_LIMIT1,
109 POWER_LIMIT2,
110 FW_LOCK,
111
112 PL1_ENABLE, /* power limit 1, aka long term */
113 PL1_CLAMP, /* allow frequency to go below OS request */
114 PL2_ENABLE, /* power limit 2, aka short term, instantaneous */
115 PL2_CLAMP,
116
117 TIME_WINDOW1, /* long term */
118 TIME_WINDOW2, /* short term */
119 THERMAL_SPEC_POWER,
120 MAX_POWER,
121
122 MIN_POWER,
123 MAX_TIME_WINDOW,
124 THROTTLED_TIME,
125 PRIORITY_LEVEL,
126
127 /* below are not raw primitive data */
128 AVERAGE_POWER,
129 NR_RAPL_PRIMITIVES,
130};
131
132#define NR_RAW_PRIMITIVES (NR_RAPL_PRIMITIVES - 2)
133
134/* Can be expanded to include events, etc.*/
135struct rapl_domain_data {
136 u64 primitives[NR_RAPL_PRIMITIVES];
137 unsigned long timestamp;
138};
139
140
141#define DOMAIN_STATE_INACTIVE BIT(0)
142#define DOMAIN_STATE_POWER_LIMIT_SET BIT(1)
143#define DOMAIN_STATE_BIOS_LOCKED BIT(2)
144
145#define NR_POWER_LIMITS (2)
146struct rapl_power_limit {
147 struct powercap_zone_constraint *constraint;
148 int prim_id; /* primitive ID used to enable */
149 struct rapl_domain *domain;
150 const char *name;
151};
152
153static const char pl1_name[] = "long_term";
154static const char pl2_name[] = "short_term";
155
156struct rapl_domain {
157 const char *name;
158 enum rapl_domain_type id;
159 int msrs[RAPL_DOMAIN_MSR_MAX];
160 struct powercap_zone power_zone;
161 struct rapl_domain_data rdd;
162 struct rapl_power_limit rpl[NR_POWER_LIMITS];
163 u64 attr_map; /* track capabilities */
164 unsigned int state;
165 int package_id;
166};
167#define power_zone_to_rapl_domain(_zone) \
168 container_of(_zone, struct rapl_domain, power_zone)
169
170
171/* Each physical package contains multiple domains, these are the common
172 * data across RAPL domains within a package.
173 */
174struct rapl_package {
175 unsigned int id; /* physical package/socket id */
176 unsigned int nr_domains;
177 unsigned long domain_map; /* bit map of active domains */
178 unsigned int power_unit_divisor;
179 unsigned int energy_unit_divisor;
180 unsigned int time_unit_divisor;
181 struct rapl_domain *domains; /* array of domains, sized at runtime */
182 struct powercap_zone *power_zone; /* keep track of parent zone */
183 int nr_cpus; /* active cpus on the package, topology info is lost during
184 * cpu hotplug. so we have to track ourselves.
185 */
186 unsigned long power_limit_irq; /* keep track of package power limit
187 * notify interrupt enable status.
188 */
189 struct list_head plist;
190};
Jacob Pan087e9cb2014-11-07 09:29:25 -0800191
192struct rapl_defaults {
193 int (*check_unit)(struct rapl_package *rp, int cpu);
194 void (*set_floor_freq)(struct rapl_domain *rd, bool mode);
195 u64 (*compute_time_window)(struct rapl_package *rp, u64 val,
196 bool to_raw);
197};
198static struct rapl_defaults *rapl_defaults;
199
Jacob Pan2d281d82013-10-17 10:28:35 -0700200#define PACKAGE_PLN_INT_SAVED BIT(0)
201#define MAX_PRIM_NAME (32)
202
203/* per domain data. used to describe individual knobs such that access function
204 * can be consolidated into one instead of many inline functions.
205 */
206struct rapl_primitive_info {
207 const char *name;
208 u64 mask;
209 int shift;
210 enum rapl_domain_msr_id id;
211 enum unit_type unit;
212 u32 flag;
213};
214
215#define PRIMITIVE_INFO_INIT(p, m, s, i, u, f) { \
216 .name = #p, \
217 .mask = m, \
218 .shift = s, \
219 .id = i, \
220 .unit = u, \
221 .flag = f \
222 }
223
224static void rapl_init_domains(struct rapl_package *rp);
225static int rapl_read_data_raw(struct rapl_domain *rd,
226 enum rapl_primitives prim,
227 bool xlate, u64 *data);
228static int rapl_write_data_raw(struct rapl_domain *rd,
229 enum rapl_primitives prim,
230 unsigned long long value);
231static u64 rapl_unit_xlate(int package, enum unit_type type, u64 value,
232 int to_raw);
233static void package_power_limit_irq_save(int package_id);
234
235static LIST_HEAD(rapl_packages); /* guarded by CPU hotplug lock */
236
237static const char * const rapl_domain_names[] = {
238 "package",
239 "core",
240 "uncore",
241 "dram",
242};
243
244static struct powercap_control_type *control_type; /* PowerCap Controller */
245
246/* caller to ensure CPU hotplug lock is held */
247static struct rapl_package *find_package_by_id(int id)
248{
249 struct rapl_package *rp;
250
251 list_for_each_entry(rp, &rapl_packages, plist) {
252 if (rp->id == id)
253 return rp;
254 }
255
256 return NULL;
257}
258
259/* caller to ensure CPU hotplug lock is held */
260static int find_active_cpu_on_package(int package_id)
261{
262 int i;
263
264 for_each_online_cpu(i) {
265 if (topology_physical_package_id(i) == package_id)
266 return i;
267 }
268 /* all CPUs on this package are offline */
269
270 return -ENODEV;
271}
272
273/* caller must hold cpu hotplug lock */
274static void rapl_cleanup_data(void)
275{
276 struct rapl_package *p, *tmp;
277
278 list_for_each_entry_safe(p, tmp, &rapl_packages, plist) {
279 kfree(p->domains);
280 list_del(&p->plist);
281 kfree(p);
282 }
283}
284
285static int get_energy_counter(struct powercap_zone *power_zone, u64 *energy_raw)
286{
287 struct rapl_domain *rd;
288 u64 energy_now;
289
290 /* prevent CPU hotplug, make sure the RAPL domain does not go
291 * away while reading the counter.
292 */
293 get_online_cpus();
294 rd = power_zone_to_rapl_domain(power_zone);
295
296 if (!rapl_read_data_raw(rd, ENERGY_COUNTER, true, &energy_now)) {
297 *energy_raw = energy_now;
298 put_online_cpus();
299
300 return 0;
301 }
302 put_online_cpus();
303
304 return -EIO;
305}
306
307static int get_max_energy_counter(struct powercap_zone *pcd_dev, u64 *energy)
308{
309 *energy = rapl_unit_xlate(0, ENERGY_UNIT, ENERGY_STATUS_MASK, 0);
310 return 0;
311}
312
313static int release_zone(struct powercap_zone *power_zone)
314{
315 struct rapl_domain *rd = power_zone_to_rapl_domain(power_zone);
316 struct rapl_package *rp;
317
318 /* package zone is the last zone of a package, we can free
319 * memory here since all children has been unregistered.
320 */
321 if (rd->id == RAPL_DOMAIN_PACKAGE) {
322 rp = find_package_by_id(rd->package_id);
323 if (!rp) {
324 dev_warn(&power_zone->dev, "no package id %s\n",
325 rd->name);
326 return -ENODEV;
327 }
328 kfree(rd);
329 rp->domains = NULL;
330 }
331
332 return 0;
333
334}
335
336static int find_nr_power_limit(struct rapl_domain *rd)
337{
338 int i;
339
340 for (i = 0; i < NR_POWER_LIMITS; i++) {
341 if (rd->rpl[i].name == NULL)
342 break;
343 }
344
345 return i;
346}
347
348static int set_domain_enable(struct powercap_zone *power_zone, bool mode)
349{
350 struct rapl_domain *rd = power_zone_to_rapl_domain(power_zone);
351 int nr_powerlimit;
352
353 if (rd->state & DOMAIN_STATE_BIOS_LOCKED)
354 return -EACCES;
355 get_online_cpus();
356 nr_powerlimit = find_nr_power_limit(rd);
357 /* here we activate/deactivate the hardware for power limiting */
358 rapl_write_data_raw(rd, PL1_ENABLE, mode);
359 /* always enable clamp such that p-state can go below OS requested
360 * range. power capping priority over guranteed frequency.
361 */
362 rapl_write_data_raw(rd, PL1_CLAMP, mode);
363 /* some domains have pl2 */
364 if (nr_powerlimit > 1) {
365 rapl_write_data_raw(rd, PL2_ENABLE, mode);
366 rapl_write_data_raw(rd, PL2_CLAMP, mode);
367 }
368 put_online_cpus();
369
370 return 0;
371}
372
373static int get_domain_enable(struct powercap_zone *power_zone, bool *mode)
374{
375 struct rapl_domain *rd = power_zone_to_rapl_domain(power_zone);
376 u64 val;
377
378 if (rd->state & DOMAIN_STATE_BIOS_LOCKED) {
379 *mode = false;
380 return 0;
381 }
382 get_online_cpus();
383 if (rapl_read_data_raw(rd, PL1_ENABLE, true, &val)) {
384 put_online_cpus();
385 return -EIO;
386 }
387 *mode = val;
388 put_online_cpus();
389
390 return 0;
391}
392
393/* per RAPL domain ops, in the order of rapl_domain_type */
394static struct powercap_zone_ops zone_ops[] = {
395 /* RAPL_DOMAIN_PACKAGE */
396 {
397 .get_energy_uj = get_energy_counter,
398 .get_max_energy_range_uj = get_max_energy_counter,
399 .release = release_zone,
400 .set_enable = set_domain_enable,
401 .get_enable = get_domain_enable,
402 },
403 /* RAPL_DOMAIN_PP0 */
404 {
405 .get_energy_uj = get_energy_counter,
406 .get_max_energy_range_uj = get_max_energy_counter,
407 .release = release_zone,
408 .set_enable = set_domain_enable,
409 .get_enable = get_domain_enable,
410 },
411 /* RAPL_DOMAIN_PP1 */
412 {
413 .get_energy_uj = get_energy_counter,
414 .get_max_energy_range_uj = get_max_energy_counter,
415 .release = release_zone,
416 .set_enable = set_domain_enable,
417 .get_enable = get_domain_enable,
418 },
419 /* RAPL_DOMAIN_DRAM */
420 {
421 .get_energy_uj = get_energy_counter,
422 .get_max_energy_range_uj = get_max_energy_counter,
423 .release = release_zone,
424 .set_enable = set_domain_enable,
425 .get_enable = get_domain_enable,
426 },
427};
428
429static int set_power_limit(struct powercap_zone *power_zone, int id,
430 u64 power_limit)
431{
432 struct rapl_domain *rd;
433 struct rapl_package *rp;
434 int ret = 0;
435
436 get_online_cpus();
437 rd = power_zone_to_rapl_domain(power_zone);
438 rp = find_package_by_id(rd->package_id);
439 if (!rp) {
440 ret = -ENODEV;
441 goto set_exit;
442 }
443
444 if (rd->state & DOMAIN_STATE_BIOS_LOCKED) {
445 dev_warn(&power_zone->dev, "%s locked by BIOS, monitoring only\n",
446 rd->name);
447 ret = -EACCES;
448 goto set_exit;
449 }
450
451 switch (rd->rpl[id].prim_id) {
452 case PL1_ENABLE:
453 rapl_write_data_raw(rd, POWER_LIMIT1, power_limit);
454 break;
455 case PL2_ENABLE:
456 rapl_write_data_raw(rd, POWER_LIMIT2, power_limit);
457 break;
458 default:
459 ret = -EINVAL;
460 }
461 if (!ret)
462 package_power_limit_irq_save(rd->package_id);
463set_exit:
464 put_online_cpus();
465 return ret;
466}
467
468static int get_current_power_limit(struct powercap_zone *power_zone, int id,
469 u64 *data)
470{
471 struct rapl_domain *rd;
472 u64 val;
473 int prim;
474 int ret = 0;
475
476 get_online_cpus();
477 rd = power_zone_to_rapl_domain(power_zone);
478 switch (rd->rpl[id].prim_id) {
479 case PL1_ENABLE:
480 prim = POWER_LIMIT1;
481 break;
482 case PL2_ENABLE:
483 prim = POWER_LIMIT2;
484 break;
485 default:
486 put_online_cpus();
487 return -EINVAL;
488 }
489 if (rapl_read_data_raw(rd, prim, true, &val))
490 ret = -EIO;
491 else
492 *data = val;
493
494 put_online_cpus();
495
496 return ret;
497}
498
499static int set_time_window(struct powercap_zone *power_zone, int id,
500 u64 window)
501{
502 struct rapl_domain *rd;
503 int ret = 0;
504
505 get_online_cpus();
506 rd = power_zone_to_rapl_domain(power_zone);
507 switch (rd->rpl[id].prim_id) {
508 case PL1_ENABLE:
509 rapl_write_data_raw(rd, TIME_WINDOW1, window);
510 break;
511 case PL2_ENABLE:
512 rapl_write_data_raw(rd, TIME_WINDOW2, window);
513 break;
514 default:
515 ret = -EINVAL;
516 }
517 put_online_cpus();
518 return ret;
519}
520
521static int get_time_window(struct powercap_zone *power_zone, int id, u64 *data)
522{
523 struct rapl_domain *rd;
524 u64 val;
525 int ret = 0;
526
527 get_online_cpus();
528 rd = power_zone_to_rapl_domain(power_zone);
529 switch (rd->rpl[id].prim_id) {
530 case PL1_ENABLE:
531 ret = rapl_read_data_raw(rd, TIME_WINDOW1, true, &val);
532 break;
533 case PL2_ENABLE:
534 ret = rapl_read_data_raw(rd, TIME_WINDOW2, true, &val);
535 break;
536 default:
537 put_online_cpus();
538 return -EINVAL;
539 }
540 if (!ret)
541 *data = val;
542 put_online_cpus();
543
544 return ret;
545}
546
547static const char *get_constraint_name(struct powercap_zone *power_zone, int id)
548{
549 struct rapl_power_limit *rpl;
550 struct rapl_domain *rd;
551
552 rd = power_zone_to_rapl_domain(power_zone);
553 rpl = (struct rapl_power_limit *) &rd->rpl[id];
554
555 return rpl->name;
556}
557
558
559static int get_max_power(struct powercap_zone *power_zone, int id,
560 u64 *data)
561{
562 struct rapl_domain *rd;
563 u64 val;
564 int prim;
565 int ret = 0;
566
567 get_online_cpus();
568 rd = power_zone_to_rapl_domain(power_zone);
569 switch (rd->rpl[id].prim_id) {
570 case PL1_ENABLE:
571 prim = THERMAL_SPEC_POWER;
572 break;
573 case PL2_ENABLE:
574 prim = MAX_POWER;
575 break;
576 default:
577 put_online_cpus();
578 return -EINVAL;
579 }
580 if (rapl_read_data_raw(rd, prim, true, &val))
581 ret = -EIO;
582 else
583 *data = val;
584
585 put_online_cpus();
586
587 return ret;
588}
589
590static struct powercap_zone_constraint_ops constraint_ops = {
591 .set_power_limit_uw = set_power_limit,
592 .get_power_limit_uw = get_current_power_limit,
593 .set_time_window_us = set_time_window,
594 .get_time_window_us = get_time_window,
595 .get_max_power_uw = get_max_power,
596 .get_name = get_constraint_name,
597};
598
599/* called after domain detection and package level data are set */
600static void rapl_init_domains(struct rapl_package *rp)
601{
602 int i;
603 struct rapl_domain *rd = rp->domains;
604
605 for (i = 0; i < RAPL_DOMAIN_MAX; i++) {
606 unsigned int mask = rp->domain_map & (1 << i);
607 switch (mask) {
608 case BIT(RAPL_DOMAIN_PACKAGE):
609 rd->name = rapl_domain_names[RAPL_DOMAIN_PACKAGE];
610 rd->id = RAPL_DOMAIN_PACKAGE;
611 rd->msrs[0] = MSR_PKG_POWER_LIMIT;
612 rd->msrs[1] = MSR_PKG_ENERGY_STATUS;
613 rd->msrs[2] = MSR_PKG_PERF_STATUS;
614 rd->msrs[3] = 0;
615 rd->msrs[4] = MSR_PKG_POWER_INFO;
616 rd->rpl[0].prim_id = PL1_ENABLE;
617 rd->rpl[0].name = pl1_name;
618 rd->rpl[1].prim_id = PL2_ENABLE;
619 rd->rpl[1].name = pl2_name;
620 break;
621 case BIT(RAPL_DOMAIN_PP0):
622 rd->name = rapl_domain_names[RAPL_DOMAIN_PP0];
623 rd->id = RAPL_DOMAIN_PP0;
624 rd->msrs[0] = MSR_PP0_POWER_LIMIT;
625 rd->msrs[1] = MSR_PP0_ENERGY_STATUS;
626 rd->msrs[2] = 0;
627 rd->msrs[3] = MSR_PP0_POLICY;
628 rd->msrs[4] = 0;
629 rd->rpl[0].prim_id = PL1_ENABLE;
630 rd->rpl[0].name = pl1_name;
631 break;
632 case BIT(RAPL_DOMAIN_PP1):
633 rd->name = rapl_domain_names[RAPL_DOMAIN_PP1];
634 rd->id = RAPL_DOMAIN_PP1;
635 rd->msrs[0] = MSR_PP1_POWER_LIMIT;
636 rd->msrs[1] = MSR_PP1_ENERGY_STATUS;
637 rd->msrs[2] = 0;
638 rd->msrs[3] = MSR_PP1_POLICY;
639 rd->msrs[4] = 0;
640 rd->rpl[0].prim_id = PL1_ENABLE;
641 rd->rpl[0].name = pl1_name;
642 break;
643 case BIT(RAPL_DOMAIN_DRAM):
644 rd->name = rapl_domain_names[RAPL_DOMAIN_DRAM];
645 rd->id = RAPL_DOMAIN_DRAM;
646 rd->msrs[0] = MSR_DRAM_POWER_LIMIT;
647 rd->msrs[1] = MSR_DRAM_ENERGY_STATUS;
648 rd->msrs[2] = MSR_DRAM_PERF_STATUS;
649 rd->msrs[3] = 0;
650 rd->msrs[4] = MSR_DRAM_POWER_INFO;
651 rd->rpl[0].prim_id = PL1_ENABLE;
652 rd->rpl[0].name = pl1_name;
653 break;
654 }
655 if (mask) {
656 rd->package_id = rp->id;
657 rd++;
658 }
659 }
660}
661
662static u64 rapl_unit_xlate(int package, enum unit_type type, u64 value,
663 int to_raw)
664{
665 u64 divisor = 1;
666 int scale = 1; /* scale to user friendly data without floating point */
667 u64 f, y; /* fraction and exp. used for time unit */
668 struct rapl_package *rp;
669
670 rp = find_package_by_id(package);
671 if (!rp)
672 return value;
673
674 switch (type) {
675 case POWER_UNIT:
676 divisor = rp->power_unit_divisor;
677 scale = POWER_UNIT_SCALE;
678 break;
679 case ENERGY_UNIT:
680 scale = ENERGY_UNIT_SCALE;
681 divisor = rp->energy_unit_divisor;
682 break;
683 case TIME_UNIT:
684 divisor = rp->time_unit_divisor;
685 scale = TIME_UNIT_SCALE;
686 /* special processing based on 2^Y*(1+F)/4 = val/divisor, refer
687 * to Intel Software Developer's manual Vol. 3a, CH 14.7.4.
688 */
689 if (!to_raw) {
690 f = (value & 0x60) >> 5;
691 y = value & 0x1f;
692 value = (1 << y) * (4 + f) * scale / 4;
693 return div64_u64(value, divisor);
694 } else {
695 do_div(value, scale);
696 value *= divisor;
697 y = ilog2(value);
698 f = div64_u64(4 * (value - (1 << y)), 1 << y);
699 value = (y & 0x1f) | ((f & 0x3) << 5);
700 return value;
701 }
702 break;
703 case ARBITRARY_UNIT:
704 default:
705 return value;
706 };
707
708 if (to_raw)
709 return div64_u64(value * divisor, scale);
710 else
711 return div64_u64(value * scale, divisor);
712}
713
714/* in the order of enum rapl_primitives */
715static struct rapl_primitive_info rpi[] = {
716 /* name, mask, shift, msr index, unit divisor */
717 PRIMITIVE_INFO_INIT(ENERGY_COUNTER, ENERGY_STATUS_MASK, 0,
718 RAPL_DOMAIN_MSR_STATUS, ENERGY_UNIT, 0),
719 PRIMITIVE_INFO_INIT(POWER_LIMIT1, POWER_LIMIT1_MASK, 0,
720 RAPL_DOMAIN_MSR_LIMIT, POWER_UNIT, 0),
721 PRIMITIVE_INFO_INIT(POWER_LIMIT2, POWER_LIMIT2_MASK, 32,
722 RAPL_DOMAIN_MSR_LIMIT, POWER_UNIT, 0),
723 PRIMITIVE_INFO_INIT(FW_LOCK, POWER_PP_LOCK, 31,
724 RAPL_DOMAIN_MSR_LIMIT, ARBITRARY_UNIT, 0),
725 PRIMITIVE_INFO_INIT(PL1_ENABLE, POWER_LIMIT1_ENABLE, 15,
726 RAPL_DOMAIN_MSR_LIMIT, ARBITRARY_UNIT, 0),
727 PRIMITIVE_INFO_INIT(PL1_CLAMP, POWER_LIMIT1_CLAMP, 16,
728 RAPL_DOMAIN_MSR_LIMIT, ARBITRARY_UNIT, 0),
729 PRIMITIVE_INFO_INIT(PL2_ENABLE, POWER_LIMIT2_ENABLE, 47,
730 RAPL_DOMAIN_MSR_LIMIT, ARBITRARY_UNIT, 0),
731 PRIMITIVE_INFO_INIT(PL2_CLAMP, POWER_LIMIT2_CLAMP, 48,
732 RAPL_DOMAIN_MSR_LIMIT, ARBITRARY_UNIT, 0),
733 PRIMITIVE_INFO_INIT(TIME_WINDOW1, TIME_WINDOW1_MASK, 17,
734 RAPL_DOMAIN_MSR_LIMIT, TIME_UNIT, 0),
735 PRIMITIVE_INFO_INIT(TIME_WINDOW2, TIME_WINDOW2_MASK, 49,
736 RAPL_DOMAIN_MSR_LIMIT, TIME_UNIT, 0),
737 PRIMITIVE_INFO_INIT(THERMAL_SPEC_POWER, POWER_INFO_THERMAL_SPEC_MASK,
738 0, RAPL_DOMAIN_MSR_INFO, POWER_UNIT, 0),
739 PRIMITIVE_INFO_INIT(MAX_POWER, POWER_INFO_MAX_MASK, 32,
740 RAPL_DOMAIN_MSR_INFO, POWER_UNIT, 0),
741 PRIMITIVE_INFO_INIT(MIN_POWER, POWER_INFO_MIN_MASK, 16,
742 RAPL_DOMAIN_MSR_INFO, POWER_UNIT, 0),
743 PRIMITIVE_INFO_INIT(MAX_TIME_WINDOW, POWER_INFO_MAX_TIME_WIN_MASK, 48,
744 RAPL_DOMAIN_MSR_INFO, TIME_UNIT, 0),
745 PRIMITIVE_INFO_INIT(THROTTLED_TIME, PERF_STATUS_THROTTLE_TIME_MASK, 0,
746 RAPL_DOMAIN_MSR_PERF, TIME_UNIT, 0),
747 PRIMITIVE_INFO_INIT(PRIORITY_LEVEL, PP_POLICY_MASK, 0,
748 RAPL_DOMAIN_MSR_POLICY, ARBITRARY_UNIT, 0),
749 /* non-hardware */
750 PRIMITIVE_INFO_INIT(AVERAGE_POWER, 0, 0, 0, POWER_UNIT,
751 RAPL_PRIMITIVE_DERIVED),
752 {NULL, 0, 0, 0},
753};
754
755/* Read primitive data based on its related struct rapl_primitive_info.
756 * if xlate flag is set, return translated data based on data units, i.e.
757 * time, energy, and power.
758 * RAPL MSRs are non-architectual and are laid out not consistently across
759 * domains. Here we use primitive info to allow writing consolidated access
760 * functions.
761 * For a given primitive, it is processed by MSR mask and shift. Unit conversion
762 * is pre-assigned based on RAPL unit MSRs read at init time.
763 * 63-------------------------- 31--------------------------- 0
764 * | xxxxx (mask) |
765 * | |<- shift ----------------|
766 * 63-------------------------- 31--------------------------- 0
767 */
768static int rapl_read_data_raw(struct rapl_domain *rd,
769 enum rapl_primitives prim,
770 bool xlate, u64 *data)
771{
772 u64 value, final;
773 u32 msr;
774 struct rapl_primitive_info *rp = &rpi[prim];
775 int cpu;
776
777 if (!rp->name || rp->flag & RAPL_PRIMITIVE_DUMMY)
778 return -EINVAL;
779
780 msr = rd->msrs[rp->id];
781 if (!msr)
782 return -EINVAL;
783 /* use physical package id to look up active cpus */
784 cpu = find_active_cpu_on_package(rd->package_id);
785 if (cpu < 0)
786 return cpu;
787
788 /* special-case package domain, which uses a different bit*/
789 if (prim == FW_LOCK && rd->id == RAPL_DOMAIN_PACKAGE) {
790 rp->mask = POWER_PACKAGE_LOCK;
791 rp->shift = 63;
792 }
793 /* non-hardware data are collected by the polling thread */
794 if (rp->flag & RAPL_PRIMITIVE_DERIVED) {
795 *data = rd->rdd.primitives[prim];
796 return 0;
797 }
798
799 if (rdmsrl_safe_on_cpu(cpu, msr, &value)) {
800 pr_debug("failed to read msr 0x%x on cpu %d\n", msr, cpu);
801 return -EIO;
802 }
803
804 final = value & rp->mask;
805 final = final >> rp->shift;
806 if (xlate)
807 *data = rapl_unit_xlate(rd->package_id, rp->unit, final, 0);
808 else
809 *data = final;
810
811 return 0;
812}
813
814/* Similar use of primitive info in the read counterpart */
815static int rapl_write_data_raw(struct rapl_domain *rd,
816 enum rapl_primitives prim,
817 unsigned long long value)
818{
819 u64 msr_val;
820 u32 msr;
821 struct rapl_primitive_info *rp = &rpi[prim];
822 int cpu;
823
824 cpu = find_active_cpu_on_package(rd->package_id);
825 if (cpu < 0)
826 return cpu;
827 msr = rd->msrs[rp->id];
828 if (rdmsrl_safe_on_cpu(cpu, msr, &msr_val)) {
829 dev_dbg(&rd->power_zone.dev,
830 "failed to read msr 0x%x on cpu %d\n", msr, cpu);
831 return -EIO;
832 }
833 value = rapl_unit_xlate(rd->package_id, rp->unit, value, 1);
834 msr_val &= ~rp->mask;
835 msr_val |= value << rp->shift;
836 if (wrmsrl_safe_on_cpu(cpu, msr, msr_val)) {
837 dev_dbg(&rd->power_zone.dev,
838 "failed to write msr 0x%x on cpu %d\n", msr, cpu);
839 return -EIO;
840 }
841
842 return 0;
843}
844
Jacob Paned93b712013-12-11 14:39:27 -0800845static const struct x86_cpu_id energy_unit_quirk_ids[] = {
Jacob Pand6b2e6d2014-02-10 06:11:52 -0800846 { X86_VENDOR_INTEL, 6, 0x37},/* Valleyview */
Jacob Paned93b712013-12-11 14:39:27 -0800847 {}
848};
849
Jacob Pan2d281d82013-10-17 10:28:35 -0700850static int rapl_check_unit(struct rapl_package *rp, int cpu)
851{
852 u64 msr_val;
853 u32 value;
854
855 if (rdmsrl_safe_on_cpu(cpu, MSR_RAPL_POWER_UNIT, &msr_val)) {
856 pr_err("Failed to read power unit MSR 0x%x on CPU %d, exit.\n",
857 MSR_RAPL_POWER_UNIT, cpu);
858 return -ENODEV;
859 }
860
861 /* Raw RAPL data stored in MSRs are in certain scales. We need to
862 * convert them into standard units based on the divisors reported in
863 * the RAPL unit MSRs.
864 * i.e.
865 * energy unit: 1/enery_unit_divisor Joules
866 * power unit: 1/power_unit_divisor Watts
867 * time unit: 1/time_unit_divisor Seconds
868 */
869 value = (msr_val & ENERGY_UNIT_MASK) >> ENERGY_UNIT_OFFSET;
Jacob Paned93b712013-12-11 14:39:27 -0800870 /* some CPUs have different way to calculate energy unit */
871 if (x86_match_cpu(energy_unit_quirk_ids))
872 rp->energy_unit_divisor = 1000000 / (1 << value);
873 else
874 rp->energy_unit_divisor = 1 << value;
Jacob Pan2d281d82013-10-17 10:28:35 -0700875
876 value = (msr_val & POWER_UNIT_MASK) >> POWER_UNIT_OFFSET;
877 rp->power_unit_divisor = 1 << value;
878
879 value = (msr_val & TIME_UNIT_MASK) >> TIME_UNIT_OFFSET;
880 rp->time_unit_divisor = 1 << value;
881
882 pr_debug("Physical package %d units: energy=%d, time=%d, power=%d\n",
883 rp->id,
884 rp->energy_unit_divisor,
885 rp->time_unit_divisor,
886 rp->power_unit_divisor);
887
888 return 0;
889}
890
891/* REVISIT:
892 * When package power limit is set artificially low by RAPL, LVT
893 * thermal interrupt for package power limit should be ignored
894 * since we are not really exceeding the real limit. The intention
895 * is to avoid excessive interrupts while we are trying to save power.
896 * A useful feature might be routing the package_power_limit interrupt
897 * to userspace via eventfd. once we have a usecase, this is simple
898 * to do by adding an atomic notifier.
899 */
900
901static void package_power_limit_irq_save(int package_id)
902{
903 u32 l, h = 0;
904 int cpu;
905 struct rapl_package *rp;
906
907 rp = find_package_by_id(package_id);
908 if (!rp)
909 return;
910
911 if (!boot_cpu_has(X86_FEATURE_PTS) || !boot_cpu_has(X86_FEATURE_PLN))
912 return;
913
914 cpu = find_active_cpu_on_package(package_id);
915 if (cpu < 0)
916 return;
917 /* save the state of PLN irq mask bit before disabling it */
918 rdmsr_safe_on_cpu(cpu, MSR_IA32_PACKAGE_THERM_INTERRUPT, &l, &h);
919 if (!(rp->power_limit_irq & PACKAGE_PLN_INT_SAVED)) {
920 rp->power_limit_irq = l & PACKAGE_THERM_INT_PLN_ENABLE;
921 rp->power_limit_irq |= PACKAGE_PLN_INT_SAVED;
922 }
923 l &= ~PACKAGE_THERM_INT_PLN_ENABLE;
924 wrmsr_on_cpu(cpu, MSR_IA32_PACKAGE_THERM_INTERRUPT, l, h);
925}
926
927/* restore per package power limit interrupt enable state */
928static void package_power_limit_irq_restore(int package_id)
929{
930 u32 l, h;
931 int cpu;
932 struct rapl_package *rp;
933
934 rp = find_package_by_id(package_id);
935 if (!rp)
936 return;
937
938 if (!boot_cpu_has(X86_FEATURE_PTS) || !boot_cpu_has(X86_FEATURE_PLN))
939 return;
940
941 cpu = find_active_cpu_on_package(package_id);
942 if (cpu < 0)
943 return;
944
945 /* irq enable state not saved, nothing to restore */
946 if (!(rp->power_limit_irq & PACKAGE_PLN_INT_SAVED))
947 return;
948 rdmsr_safe_on_cpu(cpu, MSR_IA32_PACKAGE_THERM_INTERRUPT, &l, &h);
949
950 if (rp->power_limit_irq & PACKAGE_THERM_INT_PLN_ENABLE)
951 l |= PACKAGE_THERM_INT_PLN_ENABLE;
952 else
953 l &= ~PACKAGE_THERM_INT_PLN_ENABLE;
954
955 wrmsr_on_cpu(cpu, MSR_IA32_PACKAGE_THERM_INTERRUPT, l, h);
956}
957
Jacob Pan087e9cb2014-11-07 09:29:25 -0800958static const struct rapl_defaults rapl_defaults_core = {
959};
960
961static const struct rapl_defaults rapl_defaults_atom = {
962};
963
964#define RAPL_CPU(_model, _ops) { \
965 .vendor = X86_VENDOR_INTEL, \
966 .family = 6, \
967 .model = _model, \
968 .driver_data = (kernel_ulong_t)&_ops, \
969 }
970
Jacob Pan2d281d82013-10-17 10:28:35 -0700971static const struct x86_cpu_id rapl_ids[] = {
Jacob Pan087e9cb2014-11-07 09:29:25 -0800972 RAPL_CPU(0x2a, rapl_defaults_core),/* Sandy Bridge */
973 RAPL_CPU(0x2d, rapl_defaults_core),/* Sandy Bridge EP */
974 RAPL_CPU(0x37, rapl_defaults_atom),/* Valleyview */
975 RAPL_CPU(0x3a, rapl_defaults_core),/* Ivy Bridge */
976 RAPL_CPU(0x3c, rapl_defaults_core),/* Haswell */
977 RAPL_CPU(0x3d, rapl_defaults_core),/* Broadwell */
978 RAPL_CPU(0x3f, rapl_defaults_core),/* Haswell */
979 RAPL_CPU(0x45, rapl_defaults_core),/* Haswell ULT */
Jacob Pan2d281d82013-10-17 10:28:35 -0700980 {}
981};
982MODULE_DEVICE_TABLE(x86cpu, rapl_ids);
983
984/* read once for all raw primitive data for all packages, domains */
985static void rapl_update_domain_data(void)
986{
987 int dmn, prim;
988 u64 val;
989 struct rapl_package *rp;
990
991 list_for_each_entry(rp, &rapl_packages, plist) {
992 for (dmn = 0; dmn < rp->nr_domains; dmn++) {
993 pr_debug("update package %d domain %s data\n", rp->id,
994 rp->domains[dmn].name);
995 /* exclude non-raw primitives */
996 for (prim = 0; prim < NR_RAW_PRIMITIVES; prim++)
997 if (!rapl_read_data_raw(&rp->domains[dmn], prim,
998 rpi[prim].unit,
999 &val))
1000 rp->domains[dmn].rdd.primitives[prim] =
1001 val;
1002 }
1003 }
1004
1005}
1006
1007static int rapl_unregister_powercap(void)
1008{
1009 struct rapl_package *rp;
1010 struct rapl_domain *rd, *rd_package = NULL;
1011
1012 /* unregister all active rapl packages from the powercap layer,
1013 * hotplug lock held
1014 */
1015 list_for_each_entry(rp, &rapl_packages, plist) {
1016 package_power_limit_irq_restore(rp->id);
1017
1018 for (rd = rp->domains; rd < rp->domains + rp->nr_domains;
1019 rd++) {
1020 pr_debug("remove package, undo power limit on %d: %s\n",
1021 rp->id, rd->name);
1022 rapl_write_data_raw(rd, PL1_ENABLE, 0);
1023 rapl_write_data_raw(rd, PL2_ENABLE, 0);
1024 rapl_write_data_raw(rd, PL1_CLAMP, 0);
1025 rapl_write_data_raw(rd, PL2_CLAMP, 0);
1026 if (rd->id == RAPL_DOMAIN_PACKAGE) {
1027 rd_package = rd;
1028 continue;
1029 }
1030 powercap_unregister_zone(control_type, &rd->power_zone);
1031 }
1032 /* do the package zone last */
1033 if (rd_package)
1034 powercap_unregister_zone(control_type,
1035 &rd_package->power_zone);
1036 }
1037 powercap_unregister_control_type(control_type);
1038
1039 return 0;
1040}
1041
1042static int rapl_package_register_powercap(struct rapl_package *rp)
1043{
1044 struct rapl_domain *rd;
1045 int ret = 0;
1046 char dev_name[17]; /* max domain name = 7 + 1 + 8 for int + 1 for null*/
1047 struct powercap_zone *power_zone = NULL;
1048 int nr_pl;
1049
1050 /* first we register package domain as the parent zone*/
1051 for (rd = rp->domains; rd < rp->domains + rp->nr_domains; rd++) {
1052 if (rd->id == RAPL_DOMAIN_PACKAGE) {
1053 nr_pl = find_nr_power_limit(rd);
1054 pr_debug("register socket %d package domain %s\n",
1055 rp->id, rd->name);
1056 memset(dev_name, 0, sizeof(dev_name));
1057 snprintf(dev_name, sizeof(dev_name), "%s-%d",
1058 rd->name, rp->id);
1059 power_zone = powercap_register_zone(&rd->power_zone,
1060 control_type,
1061 dev_name, NULL,
1062 &zone_ops[rd->id],
1063 nr_pl,
1064 &constraint_ops);
1065 if (IS_ERR(power_zone)) {
1066 pr_debug("failed to register package, %d\n",
1067 rp->id);
1068 ret = PTR_ERR(power_zone);
1069 goto exit_package;
1070 }
1071 /* track parent zone in per package/socket data */
1072 rp->power_zone = power_zone;
1073 /* done, only one package domain per socket */
1074 break;
1075 }
1076 }
1077 if (!power_zone) {
1078 pr_err("no package domain found, unknown topology!\n");
1079 ret = -ENODEV;
1080 goto exit_package;
1081 }
1082 /* now register domains as children of the socket/package*/
1083 for (rd = rp->domains; rd < rp->domains + rp->nr_domains; rd++) {
1084 if (rd->id == RAPL_DOMAIN_PACKAGE)
1085 continue;
1086 /* number of power limits per domain varies */
1087 nr_pl = find_nr_power_limit(rd);
1088 power_zone = powercap_register_zone(&rd->power_zone,
1089 control_type, rd->name,
1090 rp->power_zone,
1091 &zone_ops[rd->id], nr_pl,
1092 &constraint_ops);
1093
1094 if (IS_ERR(power_zone)) {
1095 pr_debug("failed to register power_zone, %d:%s:%s\n",
1096 rp->id, rd->name, dev_name);
1097 ret = PTR_ERR(power_zone);
1098 goto err_cleanup;
1099 }
1100 }
1101
1102exit_package:
1103 return ret;
1104err_cleanup:
1105 /* clean up previously initialized domains within the package if we
1106 * failed after the first domain setup.
1107 */
1108 while (--rd >= rp->domains) {
1109 pr_debug("unregister package %d domain %s\n", rp->id, rd->name);
1110 powercap_unregister_zone(control_type, &rd->power_zone);
1111 }
1112
1113 return ret;
1114}
1115
1116static int rapl_register_powercap(void)
1117{
1118 struct rapl_domain *rd;
1119 struct rapl_package *rp;
1120 int ret = 0;
1121
1122 control_type = powercap_register_control_type(NULL, "intel-rapl", NULL);
1123 if (IS_ERR(control_type)) {
1124 pr_debug("failed to register powercap control_type.\n");
1125 return PTR_ERR(control_type);
1126 }
1127 /* read the initial data */
1128 rapl_update_domain_data();
1129 list_for_each_entry(rp, &rapl_packages, plist)
1130 if (rapl_package_register_powercap(rp))
1131 goto err_cleanup_package;
1132 return ret;
1133
1134err_cleanup_package:
1135 /* clean up previously initialized packages */
1136 list_for_each_entry_continue_reverse(rp, &rapl_packages, plist) {
1137 for (rd = rp->domains; rd < rp->domains + rp->nr_domains;
1138 rd++) {
1139 pr_debug("unregister zone/package %d, %s domain\n",
1140 rp->id, rd->name);
1141 powercap_unregister_zone(control_type, &rd->power_zone);
1142 }
1143 }
1144
1145 return ret;
1146}
1147
1148static int rapl_check_domain(int cpu, int domain)
1149{
1150 unsigned msr;
Jacob Pan9d31c672014-04-29 15:33:06 -07001151 u64 val = 0;
Jacob Pan2d281d82013-10-17 10:28:35 -07001152
1153 switch (domain) {
1154 case RAPL_DOMAIN_PACKAGE:
1155 msr = MSR_PKG_ENERGY_STATUS;
1156 break;
1157 case RAPL_DOMAIN_PP0:
1158 msr = MSR_PP0_ENERGY_STATUS;
1159 break;
1160 case RAPL_DOMAIN_PP1:
1161 msr = MSR_PP1_ENERGY_STATUS;
1162 break;
1163 case RAPL_DOMAIN_DRAM:
1164 msr = MSR_DRAM_ENERGY_STATUS;
1165 break;
1166 default:
1167 pr_err("invalid domain id %d\n", domain);
1168 return -EINVAL;
1169 }
Jacob Pan9d31c672014-04-29 15:33:06 -07001170 /* make sure domain counters are available and contains non-zero
1171 * values, otherwise skip it.
1172 */
1173 if (rdmsrl_safe_on_cpu(cpu, msr, &val) || !val)
Jacob Pan2d281d82013-10-17 10:28:35 -07001174 return -ENODEV;
1175
Jacob Pan9d31c672014-04-29 15:33:06 -07001176 return 0;
Jacob Pan2d281d82013-10-17 10:28:35 -07001177}
1178
1179/* Detect active and valid domains for the given CPU, caller must
1180 * ensure the CPU belongs to the targeted package and CPU hotlug is disabled.
1181 */
1182static int rapl_detect_domains(struct rapl_package *rp, int cpu)
1183{
1184 int i;
1185 int ret = 0;
1186 struct rapl_domain *rd;
1187 u64 locked;
1188
1189 for (i = 0; i < RAPL_DOMAIN_MAX; i++) {
1190 /* use physical package id to read counters */
Jacob Panfcdf1792014-09-02 02:55:21 -07001191 if (!rapl_check_domain(cpu, i)) {
Jacob Pan2d281d82013-10-17 10:28:35 -07001192 rp->domain_map |= 1 << i;
Jacob Panfcdf1792014-09-02 02:55:21 -07001193 pr_info("Found RAPL domain %s\n", rapl_domain_names[i]);
1194 }
Jacob Pan2d281d82013-10-17 10:28:35 -07001195 }
1196 rp->nr_domains = bitmap_weight(&rp->domain_map, RAPL_DOMAIN_MAX);
1197 if (!rp->nr_domains) {
1198 pr_err("no valid rapl domains found in package %d\n", rp->id);
1199 ret = -ENODEV;
1200 goto done;
1201 }
1202 pr_debug("found %d domains on package %d\n", rp->nr_domains, rp->id);
1203
1204 rp->domains = kcalloc(rp->nr_domains + 1, sizeof(struct rapl_domain),
1205 GFP_KERNEL);
1206 if (!rp->domains) {
1207 ret = -ENOMEM;
1208 goto done;
1209 }
1210 rapl_init_domains(rp);
1211
1212 for (rd = rp->domains; rd < rp->domains + rp->nr_domains; rd++) {
1213 /* check if the domain is locked by BIOS */
1214 if (rapl_read_data_raw(rd, FW_LOCK, false, &locked)) {
1215 pr_info("RAPL package %d domain %s locked by BIOS\n",
1216 rp->id, rd->name);
1217 rd->state |= DOMAIN_STATE_BIOS_LOCKED;
1218 }
1219 }
1220
1221
1222done:
1223 return ret;
1224}
1225
1226static bool is_package_new(int package)
1227{
1228 struct rapl_package *rp;
1229
1230 /* caller prevents cpu hotplug, there will be no new packages added
1231 * or deleted while traversing the package list, no need for locking.
1232 */
1233 list_for_each_entry(rp, &rapl_packages, plist)
1234 if (package == rp->id)
1235 return false;
1236
1237 return true;
1238}
1239
1240/* RAPL interface can be made of a two-level hierarchy: package level and domain
1241 * level. We first detect the number of packages then domains of each package.
1242 * We have to consider the possiblity of CPU online/offline due to hotplug and
1243 * other scenarios.
1244 */
1245static int rapl_detect_topology(void)
1246{
1247 int i;
1248 int phy_package_id;
1249 struct rapl_package *new_package, *rp;
1250
1251 for_each_online_cpu(i) {
1252 phy_package_id = topology_physical_package_id(i);
1253 if (is_package_new(phy_package_id)) {
1254 new_package = kzalloc(sizeof(*rp), GFP_KERNEL);
1255 if (!new_package) {
1256 rapl_cleanup_data();
1257 return -ENOMEM;
1258 }
1259 /* add the new package to the list */
1260 new_package->id = phy_package_id;
1261 new_package->nr_cpus = 1;
1262
1263 /* check if the package contains valid domains */
1264 if (rapl_detect_domains(new_package, i) ||
1265 rapl_check_unit(new_package, i)) {
1266 kfree(new_package->domains);
1267 kfree(new_package);
1268 /* free up the packages already initialized */
1269 rapl_cleanup_data();
1270 return -ENODEV;
1271 }
1272 INIT_LIST_HEAD(&new_package->plist);
1273 list_add(&new_package->plist, &rapl_packages);
1274 } else {
1275 rp = find_package_by_id(phy_package_id);
1276 if (rp)
1277 ++rp->nr_cpus;
1278 }
1279 }
1280
1281 return 0;
1282}
1283
1284/* called from CPU hotplug notifier, hotplug lock held */
1285static void rapl_remove_package(struct rapl_package *rp)
1286{
1287 struct rapl_domain *rd, *rd_package = NULL;
1288
1289 for (rd = rp->domains; rd < rp->domains + rp->nr_domains; rd++) {
1290 if (rd->id == RAPL_DOMAIN_PACKAGE) {
1291 rd_package = rd;
1292 continue;
1293 }
1294 pr_debug("remove package %d, %s domain\n", rp->id, rd->name);
1295 powercap_unregister_zone(control_type, &rd->power_zone);
1296 }
1297 /* do parent zone last */
1298 powercap_unregister_zone(control_type, &rd_package->power_zone);
1299 list_del(&rp->plist);
1300 kfree(rp);
1301}
1302
1303/* called from CPU hotplug notifier, hotplug lock held */
1304static int rapl_add_package(int cpu)
1305{
1306 int ret = 0;
1307 int phy_package_id;
1308 struct rapl_package *rp;
1309
1310 phy_package_id = topology_physical_package_id(cpu);
1311 rp = kzalloc(sizeof(struct rapl_package), GFP_KERNEL);
1312 if (!rp)
1313 return -ENOMEM;
1314
1315 /* add the new package to the list */
1316 rp->id = phy_package_id;
1317 rp->nr_cpus = 1;
1318 /* check if the package contains valid domains */
1319 if (rapl_detect_domains(rp, cpu) ||
1320 rapl_check_unit(rp, cpu)) {
1321 ret = -ENODEV;
1322 goto err_free_package;
1323 }
1324 if (!rapl_package_register_powercap(rp)) {
1325 INIT_LIST_HEAD(&rp->plist);
1326 list_add(&rp->plist, &rapl_packages);
1327 return ret;
1328 }
1329
1330err_free_package:
1331 kfree(rp->domains);
1332 kfree(rp);
1333
1334 return ret;
1335}
1336
1337/* Handles CPU hotplug on multi-socket systems.
1338 * If a CPU goes online as the first CPU of the physical package
1339 * we add the RAPL package to the system. Similarly, when the last
1340 * CPU of the package is removed, we remove the RAPL package and its
1341 * associated domains. Cooling devices are handled accordingly at
1342 * per-domain level.
1343 */
1344static int rapl_cpu_callback(struct notifier_block *nfb,
1345 unsigned long action, void *hcpu)
1346{
1347 unsigned long cpu = (unsigned long)hcpu;
1348 int phy_package_id;
1349 struct rapl_package *rp;
1350
1351 phy_package_id = topology_physical_package_id(cpu);
1352 switch (action) {
1353 case CPU_ONLINE:
1354 case CPU_ONLINE_FROZEN:
1355 case CPU_DOWN_FAILED:
1356 case CPU_DOWN_FAILED_FROZEN:
1357 rp = find_package_by_id(phy_package_id);
1358 if (rp)
1359 ++rp->nr_cpus;
1360 else
1361 rapl_add_package(cpu);
1362 break;
1363 case CPU_DOWN_PREPARE:
1364 case CPU_DOWN_PREPARE_FROZEN:
1365 rp = find_package_by_id(phy_package_id);
1366 if (!rp)
1367 break;
1368 if (--rp->nr_cpus == 0)
1369 rapl_remove_package(rp);
1370 }
1371
1372 return NOTIFY_OK;
1373}
1374
1375static struct notifier_block rapl_cpu_notifier = {
1376 .notifier_call = rapl_cpu_callback,
1377};
1378
1379static int __init rapl_init(void)
1380{
1381 int ret = 0;
Jacob Pan087e9cb2014-11-07 09:29:25 -08001382 const struct x86_cpu_id *id;
Jacob Pan2d281d82013-10-17 10:28:35 -07001383
Jacob Pan087e9cb2014-11-07 09:29:25 -08001384 id = x86_match_cpu(rapl_ids);
1385 if (!id) {
Jacob Pan2d281d82013-10-17 10:28:35 -07001386 pr_err("driver does not support CPU family %d model %d\n",
1387 boot_cpu_data.x86, boot_cpu_data.x86_model);
1388
1389 return -ENODEV;
1390 }
Srivatsa S. Bhat009f2252014-03-11 02:09:26 +05301391
Jacob Pan087e9cb2014-11-07 09:29:25 -08001392 rapl_defaults = (struct rapl_defaults *)id->driver_data;
1393
Srivatsa S. Bhat009f2252014-03-11 02:09:26 +05301394 cpu_notifier_register_begin();
1395
Jacob Pan2d281d82013-10-17 10:28:35 -07001396 /* prevent CPU hotplug during detection */
1397 get_online_cpus();
1398 ret = rapl_detect_topology();
1399 if (ret)
1400 goto done;
1401
1402 if (rapl_register_powercap()) {
1403 rapl_cleanup_data();
1404 ret = -ENODEV;
1405 goto done;
1406 }
Srivatsa S. Bhat009f2252014-03-11 02:09:26 +05301407 __register_hotcpu_notifier(&rapl_cpu_notifier);
Jacob Pan2d281d82013-10-17 10:28:35 -07001408done:
1409 put_online_cpus();
Srivatsa S. Bhat009f2252014-03-11 02:09:26 +05301410 cpu_notifier_register_done();
Jacob Pan2d281d82013-10-17 10:28:35 -07001411
1412 return ret;
1413}
1414
1415static void __exit rapl_exit(void)
1416{
Srivatsa S. Bhat009f2252014-03-11 02:09:26 +05301417 cpu_notifier_register_begin();
Jacob Pan2d281d82013-10-17 10:28:35 -07001418 get_online_cpus();
Srivatsa S. Bhat009f2252014-03-11 02:09:26 +05301419 __unregister_hotcpu_notifier(&rapl_cpu_notifier);
Jacob Pan2d281d82013-10-17 10:28:35 -07001420 rapl_unregister_powercap();
1421 rapl_cleanup_data();
1422 put_online_cpus();
Srivatsa S. Bhat009f2252014-03-11 02:09:26 +05301423 cpu_notifier_register_done();
Jacob Pan2d281d82013-10-17 10:28:35 -07001424}
1425
1426module_init(rapl_init);
1427module_exit(rapl_exit);
1428
1429MODULE_DESCRIPTION("Driver for Intel RAPL (Running Average Power Limit)");
1430MODULE_AUTHOR("Jacob Pan <jacob.jun.pan@intel.com>");
1431MODULE_LICENSE("GPL v2");