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