intel_pstate: fix PCT_TO_HWP macro
[linux-2.6-block.git] / arch / x86 / kernel / cpu / perf_event_intel_rapl.c
1 /*
2  * perf_event_intel_rapl.c: support Intel RAPL energy consumption counters
3  * Copyright (C) 2013 Google, Inc., Stephane Eranian
4  *
5  * Intel RAPL interface is specified in the IA-32 Manual Vol3b
6  * section 14.7.1 (September 2013)
7  *
8  * RAPL provides more controls than just reporting energy consumption
9  * however here we only expose the 3 energy consumption free running
10  * counters (pp0, pkg, dram).
11  *
12  * Each of those counters increments in a power unit defined by the
13  * RAPL_POWER_UNIT MSR. On SandyBridge, this unit is 1/(2^16) Joules
14  * but it can vary.
15  *
16  * Counter to rapl events mappings:
17  *
18  *  pp0 counter: consumption of all physical cores (power plane 0)
19  *        event: rapl_energy_cores
20  *    perf code: 0x1
21  *
22  *  pkg counter: consumption of the whole processor package
23  *        event: rapl_energy_pkg
24  *    perf code: 0x2
25  *
26  * dram counter: consumption of the dram domain (servers only)
27  *        event: rapl_energy_dram
28  *    perf code: 0x3
29  *
30  * dram counter: consumption of the builtin-gpu domain (client only)
31  *        event: rapl_energy_gpu
32  *    perf code: 0x4
33  *
34  * We manage those counters as free running (read-only). They may be
35  * use simultaneously by other tools, such as turbostat.
36  *
37  * The events only support system-wide mode counting. There is no
38  * sampling support because it does not make sense and is not
39  * supported by the RAPL hardware.
40  *
41  * Because we want to avoid floating-point operations in the kernel,
42  * the events are all reported in fixed point arithmetic (32.32).
43  * Tools must adjust the counts to convert them to Watts using
44  * the duration of the measurement. Tools may use a function such as
45  * ldexp(raw_count, -32);
46  */
47 #include <linux/module.h>
48 #include <linux/slab.h>
49 #include <linux/perf_event.h>
50 #include <asm/cpu_device_id.h>
51 #include "perf_event.h"
52
53 /*
54  * RAPL energy status counters
55  */
56 #define RAPL_IDX_PP0_NRG_STAT   0       /* all cores */
57 #define INTEL_RAPL_PP0          0x1     /* pseudo-encoding */
58 #define RAPL_IDX_PKG_NRG_STAT   1       /* entire package */
59 #define INTEL_RAPL_PKG          0x2     /* pseudo-encoding */
60 #define RAPL_IDX_RAM_NRG_STAT   2       /* DRAM */
61 #define INTEL_RAPL_RAM          0x3     /* pseudo-encoding */
62 #define RAPL_IDX_PP1_NRG_STAT   3       /* gpu */
63 #define INTEL_RAPL_PP1          0x4     /* pseudo-encoding */
64
65 #define NR_RAPL_DOMAINS         0x4
66 static const char *rapl_domain_names[NR_RAPL_DOMAINS] __initconst = {
67         "pp0-core",
68         "package",
69         "dram",
70         "pp1-gpu",
71 };
72
73 /* Clients have PP0, PKG */
74 #define RAPL_IDX_CLN    (1<<RAPL_IDX_PP0_NRG_STAT|\
75                          1<<RAPL_IDX_PKG_NRG_STAT|\
76                          1<<RAPL_IDX_PP1_NRG_STAT)
77
78 /* Servers have PP0, PKG, RAM */
79 #define RAPL_IDX_SRV    (1<<RAPL_IDX_PP0_NRG_STAT|\
80                          1<<RAPL_IDX_PKG_NRG_STAT|\
81                          1<<RAPL_IDX_RAM_NRG_STAT)
82
83 /* Servers have PP0, PKG, RAM, PP1 */
84 #define RAPL_IDX_HSW    (1<<RAPL_IDX_PP0_NRG_STAT|\
85                          1<<RAPL_IDX_PKG_NRG_STAT|\
86                          1<<RAPL_IDX_RAM_NRG_STAT|\
87                          1<<RAPL_IDX_PP1_NRG_STAT)
88
89 /*
90  * event code: LSB 8 bits, passed in attr->config
91  * any other bit is reserved
92  */
93 #define RAPL_EVENT_MASK 0xFFULL
94
95 #define DEFINE_RAPL_FORMAT_ATTR(_var, _name, _format)           \
96 static ssize_t __rapl_##_var##_show(struct kobject *kobj,       \
97                                 struct kobj_attribute *attr,    \
98                                 char *page)                     \
99 {                                                               \
100         BUILD_BUG_ON(sizeof(_format) >= PAGE_SIZE);             \
101         return sprintf(page, _format "\n");                     \
102 }                                                               \
103 static struct kobj_attribute format_attr_##_var =               \
104         __ATTR(_name, 0444, __rapl_##_var##_show, NULL)
105
106 #define RAPL_EVENT_DESC(_name, _config)                         \
107 {                                                               \
108         .attr   = __ATTR(_name, 0444, rapl_event_show, NULL),   \
109         .config = _config,                                      \
110 }
111
112 #define RAPL_CNTR_WIDTH 32 /* 32-bit rapl counters */
113
114 #define RAPL_EVENT_ATTR_STR(_name, v, str)                              \
115 static struct perf_pmu_events_attr event_attr_##v = {                   \
116         .attr           = __ATTR(_name, 0444, rapl_sysfs_show, NULL),   \
117         .id             = 0,                                            \
118         .event_str      = str,                                          \
119 };
120
121 struct rapl_pmu {
122         spinlock_t       lock;
123         int              n_active; /* number of active events */
124         struct list_head active_list;
125         struct pmu       *pmu; /* pointer to rapl_pmu_class */
126         ktime_t          timer_interval; /* in ktime_t unit */
127         struct hrtimer   hrtimer;
128 };
129
130 static int rapl_hw_unit[NR_RAPL_DOMAINS] __read_mostly;  /* 1/2^hw_unit Joule */
131 static struct pmu rapl_pmu_class;
132 static cpumask_t rapl_cpu_mask;
133 static int rapl_cntr_mask;
134
135 static DEFINE_PER_CPU(struct rapl_pmu *, rapl_pmu);
136 static DEFINE_PER_CPU(struct rapl_pmu *, rapl_pmu_to_free);
137
138 static struct x86_pmu_quirk *rapl_quirks;
139 static inline u64 rapl_read_counter(struct perf_event *event)
140 {
141         u64 raw;
142         rdmsrl(event->hw.event_base, raw);
143         return raw;
144 }
145
146 #define rapl_add_quirk(func_)                                           \
147 do {                                                                    \
148         static struct x86_pmu_quirk __quirk __initdata = {              \
149                 .func = func_,                                          \
150         };                                                              \
151         __quirk.next = rapl_quirks;                                     \
152         rapl_quirks = &__quirk;                                         \
153 } while (0)
154
155 static inline u64 rapl_scale(u64 v, int cfg)
156 {
157         if (cfg > NR_RAPL_DOMAINS) {
158                 pr_warn("invalid domain %d, failed to scale data\n", cfg);
159                 return v;
160         }
161         /*
162          * scale delta to smallest unit (1/2^32)
163          * users must then scale back: count * 1/(1e9*2^32) to get Joules
164          * or use ldexp(count, -32).
165          * Watts = Joules/Time delta
166          */
167         return v << (32 - rapl_hw_unit[cfg - 1]);
168 }
169
170 static u64 rapl_event_update(struct perf_event *event)
171 {
172         struct hw_perf_event *hwc = &event->hw;
173         u64 prev_raw_count, new_raw_count;
174         s64 delta, sdelta;
175         int shift = RAPL_CNTR_WIDTH;
176
177 again:
178         prev_raw_count = local64_read(&hwc->prev_count);
179         rdmsrl(event->hw.event_base, new_raw_count);
180
181         if (local64_cmpxchg(&hwc->prev_count, prev_raw_count,
182                             new_raw_count) != prev_raw_count) {
183                 cpu_relax();
184                 goto again;
185         }
186
187         /*
188          * Now we have the new raw value and have updated the prev
189          * timestamp already. We can now calculate the elapsed delta
190          * (event-)time and add that to the generic event.
191          *
192          * Careful, not all hw sign-extends above the physical width
193          * of the count.
194          */
195         delta = (new_raw_count << shift) - (prev_raw_count << shift);
196         delta >>= shift;
197
198         sdelta = rapl_scale(delta, event->hw.config);
199
200         local64_add(sdelta, &event->count);
201
202         return new_raw_count;
203 }
204
205 static void rapl_start_hrtimer(struct rapl_pmu *pmu)
206 {
207        hrtimer_start(&pmu->hrtimer, pmu->timer_interval,
208                      HRTIMER_MODE_REL_PINNED);
209 }
210
211 static void rapl_stop_hrtimer(struct rapl_pmu *pmu)
212 {
213         hrtimer_cancel(&pmu->hrtimer);
214 }
215
216 static enum hrtimer_restart rapl_hrtimer_handle(struct hrtimer *hrtimer)
217 {
218         struct rapl_pmu *pmu = __this_cpu_read(rapl_pmu);
219         struct perf_event *event;
220         unsigned long flags;
221
222         if (!pmu->n_active)
223                 return HRTIMER_NORESTART;
224
225         spin_lock_irqsave(&pmu->lock, flags);
226
227         list_for_each_entry(event, &pmu->active_list, active_entry) {
228                 rapl_event_update(event);
229         }
230
231         spin_unlock_irqrestore(&pmu->lock, flags);
232
233         hrtimer_forward_now(hrtimer, pmu->timer_interval);
234
235         return HRTIMER_RESTART;
236 }
237
238 static void rapl_hrtimer_init(struct rapl_pmu *pmu)
239 {
240         struct hrtimer *hr = &pmu->hrtimer;
241
242         hrtimer_init(hr, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
243         hr->function = rapl_hrtimer_handle;
244 }
245
246 static void __rapl_pmu_event_start(struct rapl_pmu *pmu,
247                                    struct perf_event *event)
248 {
249         if (WARN_ON_ONCE(!(event->hw.state & PERF_HES_STOPPED)))
250                 return;
251
252         event->hw.state = 0;
253
254         list_add_tail(&event->active_entry, &pmu->active_list);
255
256         local64_set(&event->hw.prev_count, rapl_read_counter(event));
257
258         pmu->n_active++;
259         if (pmu->n_active == 1)
260                 rapl_start_hrtimer(pmu);
261 }
262
263 static void rapl_pmu_event_start(struct perf_event *event, int mode)
264 {
265         struct rapl_pmu *pmu = __this_cpu_read(rapl_pmu);
266         unsigned long flags;
267
268         spin_lock_irqsave(&pmu->lock, flags);
269         __rapl_pmu_event_start(pmu, event);
270         spin_unlock_irqrestore(&pmu->lock, flags);
271 }
272
273 static void rapl_pmu_event_stop(struct perf_event *event, int mode)
274 {
275         struct rapl_pmu *pmu = __this_cpu_read(rapl_pmu);
276         struct hw_perf_event *hwc = &event->hw;
277         unsigned long flags;
278
279         spin_lock_irqsave(&pmu->lock, flags);
280
281         /* mark event as deactivated and stopped */
282         if (!(hwc->state & PERF_HES_STOPPED)) {
283                 WARN_ON_ONCE(pmu->n_active <= 0);
284                 pmu->n_active--;
285                 if (pmu->n_active == 0)
286                         rapl_stop_hrtimer(pmu);
287
288                 list_del(&event->active_entry);
289
290                 WARN_ON_ONCE(hwc->state & PERF_HES_STOPPED);
291                 hwc->state |= PERF_HES_STOPPED;
292         }
293
294         /* check if update of sw counter is necessary */
295         if ((mode & PERF_EF_UPDATE) && !(hwc->state & PERF_HES_UPTODATE)) {
296                 /*
297                  * Drain the remaining delta count out of a event
298                  * that we are disabling:
299                  */
300                 rapl_event_update(event);
301                 hwc->state |= PERF_HES_UPTODATE;
302         }
303
304         spin_unlock_irqrestore(&pmu->lock, flags);
305 }
306
307 static int rapl_pmu_event_add(struct perf_event *event, int mode)
308 {
309         struct rapl_pmu *pmu = __this_cpu_read(rapl_pmu);
310         struct hw_perf_event *hwc = &event->hw;
311         unsigned long flags;
312
313         spin_lock_irqsave(&pmu->lock, flags);
314
315         hwc->state = PERF_HES_UPTODATE | PERF_HES_STOPPED;
316
317         if (mode & PERF_EF_START)
318                 __rapl_pmu_event_start(pmu, event);
319
320         spin_unlock_irqrestore(&pmu->lock, flags);
321
322         return 0;
323 }
324
325 static void rapl_pmu_event_del(struct perf_event *event, int flags)
326 {
327         rapl_pmu_event_stop(event, PERF_EF_UPDATE);
328 }
329
330 static int rapl_pmu_event_init(struct perf_event *event)
331 {
332         u64 cfg = event->attr.config & RAPL_EVENT_MASK;
333         int bit, msr, ret = 0;
334
335         /* only look at RAPL events */
336         if (event->attr.type != rapl_pmu_class.type)
337                 return -ENOENT;
338
339         /* check only supported bits are set */
340         if (event->attr.config & ~RAPL_EVENT_MASK)
341                 return -EINVAL;
342
343         /*
344          * check event is known (determines counter)
345          */
346         switch (cfg) {
347         case INTEL_RAPL_PP0:
348                 bit = RAPL_IDX_PP0_NRG_STAT;
349                 msr = MSR_PP0_ENERGY_STATUS;
350                 break;
351         case INTEL_RAPL_PKG:
352                 bit = RAPL_IDX_PKG_NRG_STAT;
353                 msr = MSR_PKG_ENERGY_STATUS;
354                 break;
355         case INTEL_RAPL_RAM:
356                 bit = RAPL_IDX_RAM_NRG_STAT;
357                 msr = MSR_DRAM_ENERGY_STATUS;
358                 break;
359         case INTEL_RAPL_PP1:
360                 bit = RAPL_IDX_PP1_NRG_STAT;
361                 msr = MSR_PP1_ENERGY_STATUS;
362                 break;
363         default:
364                 return -EINVAL;
365         }
366         /* check event supported */
367         if (!(rapl_cntr_mask & (1 << bit)))
368                 return -EINVAL;
369
370         /* unsupported modes and filters */
371         if (event->attr.exclude_user   ||
372             event->attr.exclude_kernel ||
373             event->attr.exclude_hv     ||
374             event->attr.exclude_idle   ||
375             event->attr.exclude_host   ||
376             event->attr.exclude_guest  ||
377             event->attr.sample_period) /* no sampling */
378                 return -EINVAL;
379
380         /* must be done before validate_group */
381         event->hw.event_base = msr;
382         event->hw.config = cfg;
383         event->hw.idx = bit;
384
385         return ret;
386 }
387
388 static void rapl_pmu_event_read(struct perf_event *event)
389 {
390         rapl_event_update(event);
391 }
392
393 static ssize_t rapl_get_attr_cpumask(struct device *dev,
394                                 struct device_attribute *attr, char *buf)
395 {
396         return cpumap_print_to_pagebuf(true, buf, &rapl_cpu_mask);
397 }
398
399 static DEVICE_ATTR(cpumask, S_IRUGO, rapl_get_attr_cpumask, NULL);
400
401 static struct attribute *rapl_pmu_attrs[] = {
402         &dev_attr_cpumask.attr,
403         NULL,
404 };
405
406 static struct attribute_group rapl_pmu_attr_group = {
407         .attrs = rapl_pmu_attrs,
408 };
409
410 static ssize_t rapl_sysfs_show(struct device *dev,
411                                struct device_attribute *attr,
412                                char *page)
413 {
414         struct perf_pmu_events_attr *pmu_attr = \
415                 container_of(attr, struct perf_pmu_events_attr, attr);
416
417         if (pmu_attr->event_str)
418                 return sprintf(page, "%s", pmu_attr->event_str);
419
420         return 0;
421 }
422
423 RAPL_EVENT_ATTR_STR(energy-cores, rapl_cores, "event=0x01");
424 RAPL_EVENT_ATTR_STR(energy-pkg  ,   rapl_pkg, "event=0x02");
425 RAPL_EVENT_ATTR_STR(energy-ram  ,   rapl_ram, "event=0x03");
426 RAPL_EVENT_ATTR_STR(energy-gpu  ,   rapl_gpu, "event=0x04");
427
428 RAPL_EVENT_ATTR_STR(energy-cores.unit, rapl_cores_unit, "Joules");
429 RAPL_EVENT_ATTR_STR(energy-pkg.unit  ,   rapl_pkg_unit, "Joules");
430 RAPL_EVENT_ATTR_STR(energy-ram.unit  ,   rapl_ram_unit, "Joules");
431 RAPL_EVENT_ATTR_STR(energy-gpu.unit  ,   rapl_gpu_unit, "Joules");
432
433 /*
434  * we compute in 0.23 nJ increments regardless of MSR
435  */
436 RAPL_EVENT_ATTR_STR(energy-cores.scale, rapl_cores_scale, "2.3283064365386962890625e-10");
437 RAPL_EVENT_ATTR_STR(energy-pkg.scale,     rapl_pkg_scale, "2.3283064365386962890625e-10");
438 RAPL_EVENT_ATTR_STR(energy-ram.scale,     rapl_ram_scale, "2.3283064365386962890625e-10");
439 RAPL_EVENT_ATTR_STR(energy-gpu.scale,     rapl_gpu_scale, "2.3283064365386962890625e-10");
440
441 static struct attribute *rapl_events_srv_attr[] = {
442         EVENT_PTR(rapl_cores),
443         EVENT_PTR(rapl_pkg),
444         EVENT_PTR(rapl_ram),
445
446         EVENT_PTR(rapl_cores_unit),
447         EVENT_PTR(rapl_pkg_unit),
448         EVENT_PTR(rapl_ram_unit),
449
450         EVENT_PTR(rapl_cores_scale),
451         EVENT_PTR(rapl_pkg_scale),
452         EVENT_PTR(rapl_ram_scale),
453         NULL,
454 };
455
456 static struct attribute *rapl_events_cln_attr[] = {
457         EVENT_PTR(rapl_cores),
458         EVENT_PTR(rapl_pkg),
459         EVENT_PTR(rapl_gpu),
460
461         EVENT_PTR(rapl_cores_unit),
462         EVENT_PTR(rapl_pkg_unit),
463         EVENT_PTR(rapl_gpu_unit),
464
465         EVENT_PTR(rapl_cores_scale),
466         EVENT_PTR(rapl_pkg_scale),
467         EVENT_PTR(rapl_gpu_scale),
468         NULL,
469 };
470
471 static struct attribute *rapl_events_hsw_attr[] = {
472         EVENT_PTR(rapl_cores),
473         EVENT_PTR(rapl_pkg),
474         EVENT_PTR(rapl_gpu),
475         EVENT_PTR(rapl_ram),
476
477         EVENT_PTR(rapl_cores_unit),
478         EVENT_PTR(rapl_pkg_unit),
479         EVENT_PTR(rapl_gpu_unit),
480         EVENT_PTR(rapl_ram_unit),
481
482         EVENT_PTR(rapl_cores_scale),
483         EVENT_PTR(rapl_pkg_scale),
484         EVENT_PTR(rapl_gpu_scale),
485         EVENT_PTR(rapl_ram_scale),
486         NULL,
487 };
488
489 static struct attribute_group rapl_pmu_events_group = {
490         .name = "events",
491         .attrs = NULL, /* patched at runtime */
492 };
493
494 DEFINE_RAPL_FORMAT_ATTR(event, event, "config:0-7");
495 static struct attribute *rapl_formats_attr[] = {
496         &format_attr_event.attr,
497         NULL,
498 };
499
500 static struct attribute_group rapl_pmu_format_group = {
501         .name = "format",
502         .attrs = rapl_formats_attr,
503 };
504
505 const struct attribute_group *rapl_attr_groups[] = {
506         &rapl_pmu_attr_group,
507         &rapl_pmu_format_group,
508         &rapl_pmu_events_group,
509         NULL,
510 };
511
512 static struct pmu rapl_pmu_class = {
513         .attr_groups    = rapl_attr_groups,
514         .task_ctx_nr    = perf_invalid_context, /* system-wide only */
515         .event_init     = rapl_pmu_event_init,
516         .add            = rapl_pmu_event_add, /* must have */
517         .del            = rapl_pmu_event_del, /* must have */
518         .start          = rapl_pmu_event_start,
519         .stop           = rapl_pmu_event_stop,
520         .read           = rapl_pmu_event_read,
521 };
522
523 static void rapl_cpu_exit(int cpu)
524 {
525         struct rapl_pmu *pmu = per_cpu(rapl_pmu, cpu);
526         int i, phys_id = topology_physical_package_id(cpu);
527         int target = -1;
528
529         /* find a new cpu on same package */
530         for_each_online_cpu(i) {
531                 if (i == cpu)
532                         continue;
533                 if (phys_id == topology_physical_package_id(i)) {
534                         target = i;
535                         break;
536                 }
537         }
538         /*
539          * clear cpu from cpumask
540          * if was set in cpumask and still some cpu on package,
541          * then move to new cpu
542          */
543         if (cpumask_test_and_clear_cpu(cpu, &rapl_cpu_mask) && target >= 0)
544                 cpumask_set_cpu(target, &rapl_cpu_mask);
545
546         WARN_ON(cpumask_empty(&rapl_cpu_mask));
547         /*
548          * migrate events and context to new cpu
549          */
550         if (target >= 0)
551                 perf_pmu_migrate_context(pmu->pmu, cpu, target);
552
553         /* cancel overflow polling timer for CPU */
554         rapl_stop_hrtimer(pmu);
555 }
556
557 static void rapl_cpu_init(int cpu)
558 {
559         int i, phys_id = topology_physical_package_id(cpu);
560
561         /* check if phys_is is already covered */
562         for_each_cpu(i, &rapl_cpu_mask) {
563                 if (phys_id == topology_physical_package_id(i))
564                         return;
565         }
566         /* was not found, so add it */
567         cpumask_set_cpu(cpu, &rapl_cpu_mask);
568 }
569
570 static __init void rapl_hsw_server_quirk(void)
571 {
572         /*
573          * DRAM domain on HSW server has fixed energy unit which can be
574          * different than the unit from power unit MSR.
575          * "Intel Xeon Processor E5-1600 and E5-2600 v3 Product Families, V2
576          * of 2. Datasheet, September 2014, Reference Number: 330784-001 "
577          */
578         rapl_hw_unit[RAPL_IDX_RAM_NRG_STAT] = 16;
579 }
580
581 static int rapl_cpu_prepare(int cpu)
582 {
583         struct rapl_pmu *pmu = per_cpu(rapl_pmu, cpu);
584         int phys_id = topology_physical_package_id(cpu);
585         u64 ms;
586
587         if (pmu)
588                 return 0;
589
590         if (phys_id < 0)
591                 return -1;
592
593         pmu = kzalloc_node(sizeof(*pmu), GFP_KERNEL, cpu_to_node(cpu));
594         if (!pmu)
595                 return -1;
596         spin_lock_init(&pmu->lock);
597
598         INIT_LIST_HEAD(&pmu->active_list);
599
600         pmu->pmu = &rapl_pmu_class;
601
602         /*
603          * use reference of 200W for scaling the timeout
604          * to avoid missing counter overflows.
605          * 200W = 200 Joules/sec
606          * divide interval by 2 to avoid lockstep (2 * 100)
607          * if hw unit is 32, then we use 2 ms 1/200/2
608          */
609         if (rapl_hw_unit[0] < 32)
610                 ms = (1000 / (2 * 100)) * (1ULL << (32 - rapl_hw_unit[0] - 1));
611         else
612                 ms = 2;
613
614         pmu->timer_interval = ms_to_ktime(ms);
615
616         rapl_hrtimer_init(pmu);
617
618         /* set RAPL pmu for this cpu for now */
619         per_cpu(rapl_pmu, cpu) = pmu;
620         per_cpu(rapl_pmu_to_free, cpu) = NULL;
621
622         return 0;
623 }
624
625 static void rapl_cpu_kfree(int cpu)
626 {
627         struct rapl_pmu *pmu = per_cpu(rapl_pmu_to_free, cpu);
628
629         kfree(pmu);
630
631         per_cpu(rapl_pmu_to_free, cpu) = NULL;
632 }
633
634 static int rapl_cpu_dying(int cpu)
635 {
636         struct rapl_pmu *pmu = per_cpu(rapl_pmu, cpu);
637
638         if (!pmu)
639                 return 0;
640
641         per_cpu(rapl_pmu, cpu) = NULL;
642
643         per_cpu(rapl_pmu_to_free, cpu) = pmu;
644
645         return 0;
646 }
647
648 static int rapl_cpu_notifier(struct notifier_block *self,
649                              unsigned long action, void *hcpu)
650 {
651         unsigned int cpu = (long)hcpu;
652
653         switch (action & ~CPU_TASKS_FROZEN) {
654         case CPU_UP_PREPARE:
655                 rapl_cpu_prepare(cpu);
656                 break;
657         case CPU_STARTING:
658                 rapl_cpu_init(cpu);
659                 break;
660         case CPU_UP_CANCELED:
661         case CPU_DYING:
662                 rapl_cpu_dying(cpu);
663                 break;
664         case CPU_ONLINE:
665         case CPU_DEAD:
666                 rapl_cpu_kfree(cpu);
667                 break;
668         case CPU_DOWN_PREPARE:
669                 rapl_cpu_exit(cpu);
670                 break;
671         default:
672                 break;
673         }
674
675         return NOTIFY_OK;
676 }
677
678 static int rapl_check_hw_unit(void)
679 {
680         u64 msr_rapl_power_unit_bits;
681         int i;
682
683         /* protect rdmsrl() to handle virtualization */
684         if (rdmsrl_safe(MSR_RAPL_POWER_UNIT, &msr_rapl_power_unit_bits))
685                 return -1;
686         for (i = 0; i < NR_RAPL_DOMAINS; i++)
687                 rapl_hw_unit[i] = (msr_rapl_power_unit_bits >> 8) & 0x1FULL;
688
689         return 0;
690 }
691
692 static const struct x86_cpu_id rapl_cpu_match[] = {
693         [0] = { .vendor = X86_VENDOR_INTEL, .family = 6 },
694         [1] = {},
695 };
696
697 static int __init rapl_pmu_init(void)
698 {
699         struct rapl_pmu *pmu;
700         int cpu, ret;
701         struct x86_pmu_quirk *quirk;
702         int i;
703
704         /*
705          * check for Intel processor family 6
706          */
707         if (!x86_match_cpu(rapl_cpu_match))
708                 return 0;
709
710         /* check supported CPU */
711         switch (boot_cpu_data.x86_model) {
712         case 42: /* Sandy Bridge */
713         case 58: /* Ivy Bridge */
714                 rapl_cntr_mask = RAPL_IDX_CLN;
715                 rapl_pmu_events_group.attrs = rapl_events_cln_attr;
716                 break;
717         case 63: /* Haswell-Server */
718                 rapl_add_quirk(rapl_hsw_server_quirk);
719                 rapl_cntr_mask = RAPL_IDX_SRV;
720                 rapl_pmu_events_group.attrs = rapl_events_srv_attr;
721                 break;
722         case 60: /* Haswell */
723         case 69: /* Haswell-Celeron */
724         case 61: /* Broadwell */
725                 rapl_cntr_mask = RAPL_IDX_HSW;
726                 rapl_pmu_events_group.attrs = rapl_events_hsw_attr;
727                 break;
728         case 45: /* Sandy Bridge-EP */
729         case 62: /* IvyTown */
730                 rapl_cntr_mask = RAPL_IDX_SRV;
731                 rapl_pmu_events_group.attrs = rapl_events_srv_attr;
732                 break;
733
734         default:
735                 /* unsupported */
736                 return 0;
737         }
738         ret = rapl_check_hw_unit();
739         if (ret)
740                 return ret;
741
742         /* run cpu model quirks */
743         for (quirk = rapl_quirks; quirk; quirk = quirk->next)
744                 quirk->func();
745         cpu_notifier_register_begin();
746
747         for_each_online_cpu(cpu) {
748                 ret = rapl_cpu_prepare(cpu);
749                 if (ret)
750                         goto out;
751                 rapl_cpu_init(cpu);
752         }
753
754         __perf_cpu_notifier(rapl_cpu_notifier);
755
756         ret = perf_pmu_register(&rapl_pmu_class, "power", -1);
757         if (WARN_ON(ret)) {
758                 pr_info("RAPL PMU detected, registration failed (%d), RAPL PMU disabled\n", ret);
759                 cpu_notifier_register_done();
760                 return -1;
761         }
762
763         pmu = __this_cpu_read(rapl_pmu);
764
765         pr_info("RAPL PMU detected,"
766                 " API unit is 2^-32 Joules,"
767                 " %d fixed counters"
768                 " %llu ms ovfl timer\n",
769                 hweight32(rapl_cntr_mask),
770                 ktime_to_ms(pmu->timer_interval));
771         for (i = 0; i < NR_RAPL_DOMAINS; i++) {
772                 if (rapl_cntr_mask & (1 << i)) {
773                         pr_info("hw unit of domain %s 2^-%d Joules\n",
774                                 rapl_domain_names[i], rapl_hw_unit[i]);
775                 }
776         }
777 out:
778         cpu_notifier_register_done();
779
780         return 0;
781 }
782 device_initcall(rapl_pmu_init);