blob: 28865938aadf267e42829c3393cc405b0bf0672e [file] [log] [blame]
Stephane Eranian4788e5b2013-11-12 17:58:50 +01001/*
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 *
Srinivas Pandruvadadcee75b2016-04-17 15:03:00 -070030 * gpu counter: consumption of the builtin-gpu domain (client only)
Stephane Eranianf228c5b2014-01-08 11:15:53 +010031 * event: rapl_energy_gpu
32 * perf code: 0x4
33 *
Srinivas Pandruvadadcee75b2016-04-17 15:03:00 -070034 * psys counter: consumption of the builtin-psys domain (client only)
35 * event: rapl_energy_psys
36 * perf code: 0x5
37 *
Stephane Eranian4788e5b2013-11-12 17:58:50 +010038 * We manage those counters as free running (read-only). They may be
39 * use simultaneously by other tools, such as turbostat.
40 *
41 * The events only support system-wide mode counting. There is no
42 * sampling support because it does not make sense and is not
43 * supported by the RAPL hardware.
44 *
45 * Because we want to avoid floating-point operations in the kernel,
46 * the events are all reported in fixed point arithmetic (32.32).
47 * Tools must adjust the counts to convert them to Watts using
48 * the duration of the measurement. Tools may use a function such as
49 * ldexp(raw_count, -32);
50 */
Thomas Gleixner512089d2016-02-22 22:19:23 +000051
52#define pr_fmt(fmt) "RAPL PMU: " fmt
53
Stephane Eranian4788e5b2013-11-12 17:58:50 +010054#include <linux/module.h>
55#include <linux/slab.h>
56#include <linux/perf_event.h>
57#include <asm/cpu_device_id.h>
Dave Hansen7f2236d2016-06-02 17:19:30 -070058#include <asm/intel-family.h>
Borislav Petkov27f6d222016-02-10 10:55:23 +010059#include "../perf_event.h"
Stephane Eranian4788e5b2013-11-12 17:58:50 +010060
Kan Liang4b6e2572016-03-19 00:20:50 -070061MODULE_LICENSE("GPL");
62
Stephane Eranian4788e5b2013-11-12 17:58:50 +010063/*
64 * RAPL energy status counters
65 */
66#define RAPL_IDX_PP0_NRG_STAT 0 /* all cores */
67#define INTEL_RAPL_PP0 0x1 /* pseudo-encoding */
68#define RAPL_IDX_PKG_NRG_STAT 1 /* entire package */
69#define INTEL_RAPL_PKG 0x2 /* pseudo-encoding */
70#define RAPL_IDX_RAM_NRG_STAT 2 /* DRAM */
71#define INTEL_RAPL_RAM 0x3 /* pseudo-encoding */
Vince Weavere69af462014-04-02 00:49:55 -040072#define RAPL_IDX_PP1_NRG_STAT 3 /* gpu */
Stephane Eranianf228c5b2014-01-08 11:15:53 +010073#define INTEL_RAPL_PP1 0x4 /* pseudo-encoding */
Srinivas Pandruvadadcee75b2016-04-17 15:03:00 -070074#define RAPL_IDX_PSYS_NRG_STAT 4 /* psys */
75#define INTEL_RAPL_PSYS 0x5 /* pseudo-encoding */
Stephane Eranian4788e5b2013-11-12 17:58:50 +010076
Srinivas Pandruvadadcee75b2016-04-17 15:03:00 -070077#define NR_RAPL_DOMAINS 0x5
Andi Kleenda008ee2015-11-30 09:48:42 -080078static const char *const rapl_domain_names[NR_RAPL_DOMAINS] __initconst = {
Jacob Pan64552392015-03-26 14:28:45 -070079 "pp0-core",
80 "package",
81 "dram",
82 "pp1-gpu",
Srinivas Pandruvadadcee75b2016-04-17 15:03:00 -070083 "psys",
Jacob Pan64552392015-03-26 14:28:45 -070084};
85
Stephane Eranian4788e5b2013-11-12 17:58:50 +010086/* Clients have PP0, PKG */
87#define RAPL_IDX_CLN (1<<RAPL_IDX_PP0_NRG_STAT|\
Stephane Eranianf228c5b2014-01-08 11:15:53 +010088 1<<RAPL_IDX_PKG_NRG_STAT|\
89 1<<RAPL_IDX_PP1_NRG_STAT)
Stephane Eranian4788e5b2013-11-12 17:58:50 +010090
91/* Servers have PP0, PKG, RAM */
92#define RAPL_IDX_SRV (1<<RAPL_IDX_PP0_NRG_STAT|\
93 1<<RAPL_IDX_PKG_NRG_STAT|\
94 1<<RAPL_IDX_RAM_NRG_STAT)
95
Vince Weavere69af462014-04-02 00:49:55 -040096/* Servers have PP0, PKG, RAM, PP1 */
97#define RAPL_IDX_HSW (1<<RAPL_IDX_PP0_NRG_STAT|\
98 1<<RAPL_IDX_PKG_NRG_STAT|\
99 1<<RAPL_IDX_RAM_NRG_STAT|\
100 1<<RAPL_IDX_PP1_NRG_STAT)
101
Srinivas Pandruvadadcee75b2016-04-17 15:03:00 -0700102/* SKL clients have PP0, PKG, RAM, PP1, PSYS */
103#define RAPL_IDX_SKL_CLN (1<<RAPL_IDX_PP0_NRG_STAT|\
104 1<<RAPL_IDX_PKG_NRG_STAT|\
105 1<<RAPL_IDX_RAM_NRG_STAT|\
106 1<<RAPL_IDX_PP1_NRG_STAT|\
107 1<<RAPL_IDX_PSYS_NRG_STAT)
108
Dasaratharaman Chandramouli3a2a7792015-05-26 11:47:39 -0700109/* Knights Landing has PKG, RAM */
110#define RAPL_IDX_KNL (1<<RAPL_IDX_PKG_NRG_STAT|\
111 1<<RAPL_IDX_RAM_NRG_STAT)
112
Stephane Eranian4788e5b2013-11-12 17:58:50 +0100113/*
114 * event code: LSB 8 bits, passed in attr->config
115 * any other bit is reserved
116 */
117#define RAPL_EVENT_MASK 0xFFULL
118
119#define DEFINE_RAPL_FORMAT_ATTR(_var, _name, _format) \
120static ssize_t __rapl_##_var##_show(struct kobject *kobj, \
121 struct kobj_attribute *attr, \
122 char *page) \
123{ \
124 BUILD_BUG_ON(sizeof(_format) >= PAGE_SIZE); \
125 return sprintf(page, _format "\n"); \
126} \
127static struct kobj_attribute format_attr_##_var = \
128 __ATTR(_name, 0444, __rapl_##_var##_show, NULL)
129
Thomas Gleixner7162b8f2016-02-22 22:19:24 +0000130#define RAPL_CNTR_WIDTH 32
Stephane Eranian4788e5b2013-11-12 17:58:50 +0100131
Huang Ruid3bcd642015-12-04 18:07:41 +0800132#define RAPL_EVENT_ATTR_STR(_name, v, str) \
133static struct perf_pmu_events_attr event_attr_##v = { \
134 .attr = __ATTR(_name, 0444, perf_event_sysfs_show, NULL), \
135 .id = 0, \
136 .event_str = str, \
Stephane Eranian433678b2015-01-13 23:59:53 +0100137};
138
Stephane Eranian4788e5b2013-11-12 17:58:50 +0100139struct rapl_pmu {
Thomas Gleixnera2087492016-02-22 22:19:25 +0000140 raw_spinlock_t lock;
Thomas Gleixner7162b8f2016-02-22 22:19:24 +0000141 int n_active;
Thomas Gleixner8a6d2f82016-02-22 22:19:25 +0000142 int cpu;
Thomas Gleixner7162b8f2016-02-22 22:19:24 +0000143 struct list_head active_list;
144 struct pmu *pmu;
145 ktime_t timer_interval;
146 struct hrtimer hrtimer;
Stephane Eranian4788e5b2013-11-12 17:58:50 +0100147};
148
Thomas Gleixner9de8d682016-02-22 22:19:26 +0000149struct rapl_pmus {
150 struct pmu pmu;
151 unsigned int maxpkg;
152 struct rapl_pmu *pmus[];
153};
154
Thomas Gleixner7162b8f2016-02-22 22:19:24 +0000155 /* 1/2^hw_unit Joule */
156static int rapl_hw_unit[NR_RAPL_DOMAINS] __read_mostly;
Thomas Gleixner9de8d682016-02-22 22:19:26 +0000157static struct rapl_pmus *rapl_pmus;
Stephane Eranian4788e5b2013-11-12 17:58:50 +0100158static cpumask_t rapl_cpu_mask;
Thomas Gleixner9de8d682016-02-22 22:19:26 +0000159static unsigned int rapl_cntr_mask;
Thomas Gleixner75c70032016-02-22 22:19:22 +0000160static u64 rapl_timer_ms;
Stephane Eranian4788e5b2013-11-12 17:58:50 +0100161
Thomas Gleixner9de8d682016-02-22 22:19:26 +0000162static inline struct rapl_pmu *cpu_to_rapl_pmu(unsigned int cpu)
163{
164 return rapl_pmus->pmus[topology_logical_package_id(cpu)];
165}
Stephane Eranian4788e5b2013-11-12 17:58:50 +0100166
167static inline u64 rapl_read_counter(struct perf_event *event)
168{
169 u64 raw;
170 rdmsrl(event->hw.event_base, raw);
171 return raw;
172}
173
Jacob Pan64552392015-03-26 14:28:45 -0700174static inline u64 rapl_scale(u64 v, int cfg)
Stephane Eranian4788e5b2013-11-12 17:58:50 +0100175{
Jacob Pan64552392015-03-26 14:28:45 -0700176 if (cfg > NR_RAPL_DOMAINS) {
Thomas Gleixner512089d2016-02-22 22:19:23 +0000177 pr_warn("Invalid domain %d, failed to scale data\n", cfg);
Jacob Pan64552392015-03-26 14:28:45 -0700178 return v;
179 }
Stephane Eranian4788e5b2013-11-12 17:58:50 +0100180 /*
181 * scale delta to smallest unit (1/2^32)
182 * users must then scale back: count * 1/(1e9*2^32) to get Joules
183 * or use ldexp(count, -32).
184 * Watts = Joules/Time delta
185 */
Jacob Pan64552392015-03-26 14:28:45 -0700186 return v << (32 - rapl_hw_unit[cfg - 1]);
Stephane Eranian4788e5b2013-11-12 17:58:50 +0100187}
188
189static u64 rapl_event_update(struct perf_event *event)
190{
191 struct hw_perf_event *hwc = &event->hw;
192 u64 prev_raw_count, new_raw_count;
193 s64 delta, sdelta;
194 int shift = RAPL_CNTR_WIDTH;
195
196again:
197 prev_raw_count = local64_read(&hwc->prev_count);
198 rdmsrl(event->hw.event_base, new_raw_count);
199
200 if (local64_cmpxchg(&hwc->prev_count, prev_raw_count,
201 new_raw_count) != prev_raw_count) {
202 cpu_relax();
203 goto again;
204 }
205
206 /*
207 * Now we have the new raw value and have updated the prev
208 * timestamp already. We can now calculate the elapsed delta
209 * (event-)time and add that to the generic event.
210 *
211 * Careful, not all hw sign-extends above the physical width
212 * of the count.
213 */
214 delta = (new_raw_count << shift) - (prev_raw_count << shift);
215 delta >>= shift;
216
Jacob Pan64552392015-03-26 14:28:45 -0700217 sdelta = rapl_scale(delta, event->hw.config);
Stephane Eranian4788e5b2013-11-12 17:58:50 +0100218
219 local64_add(sdelta, &event->count);
220
221 return new_raw_count;
222}
223
Stephane Eranian65661f92013-11-12 17:58:51 +0100224static void rapl_start_hrtimer(struct rapl_pmu *pmu)
225{
Thomas Gleixner514c2302015-04-14 21:09:00 +0000226 hrtimer_start(&pmu->hrtimer, pmu->timer_interval,
227 HRTIMER_MODE_REL_PINNED);
Stephane Eranian65661f92013-11-12 17:58:51 +0100228}
229
Stephane Eranian65661f92013-11-12 17:58:51 +0100230static enum hrtimer_restart rapl_hrtimer_handle(struct hrtimer *hrtimer)
231{
Thomas Gleixner8a6d2f82016-02-22 22:19:25 +0000232 struct rapl_pmu *pmu = container_of(hrtimer, struct rapl_pmu, hrtimer);
Stephane Eranian65661f92013-11-12 17:58:51 +0100233 struct perf_event *event;
234 unsigned long flags;
235
236 if (!pmu->n_active)
237 return HRTIMER_NORESTART;
238
Thomas Gleixnera2087492016-02-22 22:19:25 +0000239 raw_spin_lock_irqsave(&pmu->lock, flags);
Stephane Eranian65661f92013-11-12 17:58:51 +0100240
Thomas Gleixner7162b8f2016-02-22 22:19:24 +0000241 list_for_each_entry(event, &pmu->active_list, active_entry)
Stephane Eranian65661f92013-11-12 17:58:51 +0100242 rapl_event_update(event);
Stephane Eranian65661f92013-11-12 17:58:51 +0100243
Thomas Gleixnera2087492016-02-22 22:19:25 +0000244 raw_spin_unlock_irqrestore(&pmu->lock, flags);
Stephane Eranian65661f92013-11-12 17:58:51 +0100245
246 hrtimer_forward_now(hrtimer, pmu->timer_interval);
247
248 return HRTIMER_RESTART;
249}
250
251static void rapl_hrtimer_init(struct rapl_pmu *pmu)
252{
253 struct hrtimer *hr = &pmu->hrtimer;
254
255 hrtimer_init(hr, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
256 hr->function = rapl_hrtimer_handle;
257}
258
Stephane Eranian4788e5b2013-11-12 17:58:50 +0100259static void __rapl_pmu_event_start(struct rapl_pmu *pmu,
260 struct perf_event *event)
261{
262 if (WARN_ON_ONCE(!(event->hw.state & PERF_HES_STOPPED)))
263 return;
264
265 event->hw.state = 0;
266
267 list_add_tail(&event->active_entry, &pmu->active_list);
268
269 local64_set(&event->hw.prev_count, rapl_read_counter(event));
270
271 pmu->n_active++;
Stephane Eranian65661f92013-11-12 17:58:51 +0100272 if (pmu->n_active == 1)
273 rapl_start_hrtimer(pmu);
Stephane Eranian4788e5b2013-11-12 17:58:50 +0100274}
275
276static void rapl_pmu_event_start(struct perf_event *event, int mode)
277{
Thomas Gleixner8a6d2f82016-02-22 22:19:25 +0000278 struct rapl_pmu *pmu = event->pmu_private;
Stephane Eranian4788e5b2013-11-12 17:58:50 +0100279 unsigned long flags;
280
Thomas Gleixnera2087492016-02-22 22:19:25 +0000281 raw_spin_lock_irqsave(&pmu->lock, flags);
Stephane Eranian4788e5b2013-11-12 17:58:50 +0100282 __rapl_pmu_event_start(pmu, event);
Thomas Gleixnera2087492016-02-22 22:19:25 +0000283 raw_spin_unlock_irqrestore(&pmu->lock, flags);
Stephane Eranian4788e5b2013-11-12 17:58:50 +0100284}
285
286static void rapl_pmu_event_stop(struct perf_event *event, int mode)
287{
Thomas Gleixner8a6d2f82016-02-22 22:19:25 +0000288 struct rapl_pmu *pmu = event->pmu_private;
Stephane Eranian4788e5b2013-11-12 17:58:50 +0100289 struct hw_perf_event *hwc = &event->hw;
290 unsigned long flags;
291
Thomas Gleixnera2087492016-02-22 22:19:25 +0000292 raw_spin_lock_irqsave(&pmu->lock, flags);
Stephane Eranian4788e5b2013-11-12 17:58:50 +0100293
294 /* mark event as deactivated and stopped */
295 if (!(hwc->state & PERF_HES_STOPPED)) {
296 WARN_ON_ONCE(pmu->n_active <= 0);
297 pmu->n_active--;
Stephane Eranian65661f92013-11-12 17:58:51 +0100298 if (pmu->n_active == 0)
Thomas Gleixner7162b8f2016-02-22 22:19:24 +0000299 hrtimer_cancel(&pmu->hrtimer);
Stephane Eranian4788e5b2013-11-12 17:58:50 +0100300
301 list_del(&event->active_entry);
302
303 WARN_ON_ONCE(hwc->state & PERF_HES_STOPPED);
304 hwc->state |= PERF_HES_STOPPED;
305 }
306
307 /* check if update of sw counter is necessary */
308 if ((mode & PERF_EF_UPDATE) && !(hwc->state & PERF_HES_UPTODATE)) {
309 /*
310 * Drain the remaining delta count out of a event
311 * that we are disabling:
312 */
313 rapl_event_update(event);
314 hwc->state |= PERF_HES_UPTODATE;
315 }
316
Thomas Gleixnera2087492016-02-22 22:19:25 +0000317 raw_spin_unlock_irqrestore(&pmu->lock, flags);
Stephane Eranian4788e5b2013-11-12 17:58:50 +0100318}
319
320static int rapl_pmu_event_add(struct perf_event *event, int mode)
321{
Thomas Gleixner8a6d2f82016-02-22 22:19:25 +0000322 struct rapl_pmu *pmu = event->pmu_private;
Stephane Eranian4788e5b2013-11-12 17:58:50 +0100323 struct hw_perf_event *hwc = &event->hw;
324 unsigned long flags;
325
Thomas Gleixnera2087492016-02-22 22:19:25 +0000326 raw_spin_lock_irqsave(&pmu->lock, flags);
Stephane Eranian4788e5b2013-11-12 17:58:50 +0100327
328 hwc->state = PERF_HES_UPTODATE | PERF_HES_STOPPED;
329
330 if (mode & PERF_EF_START)
331 __rapl_pmu_event_start(pmu, event);
332
Thomas Gleixnera2087492016-02-22 22:19:25 +0000333 raw_spin_unlock_irqrestore(&pmu->lock, flags);
Stephane Eranian4788e5b2013-11-12 17:58:50 +0100334
335 return 0;
336}
337
338static void rapl_pmu_event_del(struct perf_event *event, int flags)
339{
340 rapl_pmu_event_stop(event, PERF_EF_UPDATE);
341}
342
343static int rapl_pmu_event_init(struct perf_event *event)
344{
345 u64 cfg = event->attr.config & RAPL_EVENT_MASK;
346 int bit, msr, ret = 0;
Thomas Gleixner9de8d682016-02-22 22:19:26 +0000347 struct rapl_pmu *pmu;
Stephane Eranian4788e5b2013-11-12 17:58:50 +0100348
349 /* only look at RAPL events */
Thomas Gleixner9de8d682016-02-22 22:19:26 +0000350 if (event->attr.type != rapl_pmus->pmu.type)
Stephane Eranian4788e5b2013-11-12 17:58:50 +0100351 return -ENOENT;
352
353 /* check only supported bits are set */
354 if (event->attr.config & ~RAPL_EVENT_MASK)
355 return -EINVAL;
356
Thomas Gleixner8a6d2f82016-02-22 22:19:25 +0000357 if (event->cpu < 0)
358 return -EINVAL;
359
Stephane Eranian4788e5b2013-11-12 17:58:50 +0100360 /*
361 * check event is known (determines counter)
362 */
363 switch (cfg) {
364 case INTEL_RAPL_PP0:
365 bit = RAPL_IDX_PP0_NRG_STAT;
366 msr = MSR_PP0_ENERGY_STATUS;
367 break;
368 case INTEL_RAPL_PKG:
369 bit = RAPL_IDX_PKG_NRG_STAT;
370 msr = MSR_PKG_ENERGY_STATUS;
371 break;
372 case INTEL_RAPL_RAM:
373 bit = RAPL_IDX_RAM_NRG_STAT;
374 msr = MSR_DRAM_ENERGY_STATUS;
375 break;
Stephane Eranianf228c5b2014-01-08 11:15:53 +0100376 case INTEL_RAPL_PP1:
377 bit = RAPL_IDX_PP1_NRG_STAT;
378 msr = MSR_PP1_ENERGY_STATUS;
379 break;
Srinivas Pandruvadadcee75b2016-04-17 15:03:00 -0700380 case INTEL_RAPL_PSYS:
381 bit = RAPL_IDX_PSYS_NRG_STAT;
382 msr = MSR_PLATFORM_ENERGY_STATUS;
383 break;
Stephane Eranian4788e5b2013-11-12 17:58:50 +0100384 default:
385 return -EINVAL;
386 }
387 /* check event supported */
388 if (!(rapl_cntr_mask & (1 << bit)))
389 return -EINVAL;
390
391 /* unsupported modes and filters */
392 if (event->attr.exclude_user ||
393 event->attr.exclude_kernel ||
394 event->attr.exclude_hv ||
395 event->attr.exclude_idle ||
396 event->attr.exclude_host ||
397 event->attr.exclude_guest ||
398 event->attr.sample_period) /* no sampling */
399 return -EINVAL;
400
401 /* must be done before validate_group */
Thomas Gleixner9de8d682016-02-22 22:19:26 +0000402 pmu = cpu_to_rapl_pmu(event->cpu);
Thomas Gleixner8a6d2f82016-02-22 22:19:25 +0000403 event->cpu = pmu->cpu;
404 event->pmu_private = pmu;
Stephane Eranian4788e5b2013-11-12 17:58:50 +0100405 event->hw.event_base = msr;
406 event->hw.config = cfg;
407 event->hw.idx = bit;
408
409 return ret;
410}
411
412static void rapl_pmu_event_read(struct perf_event *event)
413{
414 rapl_event_update(event);
415}
416
417static ssize_t rapl_get_attr_cpumask(struct device *dev,
418 struct device_attribute *attr, char *buf)
419{
Sudeep Holla5aaba362014-09-30 14:48:22 +0100420 return cpumap_print_to_pagebuf(true, buf, &rapl_cpu_mask);
Stephane Eranian4788e5b2013-11-12 17:58:50 +0100421}
422
423static DEVICE_ATTR(cpumask, S_IRUGO, rapl_get_attr_cpumask, NULL);
424
425static struct attribute *rapl_pmu_attrs[] = {
426 &dev_attr_cpumask.attr,
427 NULL,
428};
429
430static struct attribute_group rapl_pmu_attr_group = {
431 .attrs = rapl_pmu_attrs,
432};
433
Stephane Eranian433678b2015-01-13 23:59:53 +0100434RAPL_EVENT_ATTR_STR(energy-cores, rapl_cores, "event=0x01");
435RAPL_EVENT_ATTR_STR(energy-pkg , rapl_pkg, "event=0x02");
436RAPL_EVENT_ATTR_STR(energy-ram , rapl_ram, "event=0x03");
437RAPL_EVENT_ATTR_STR(energy-gpu , rapl_gpu, "event=0x04");
Srinivas Pandruvadadcee75b2016-04-17 15:03:00 -0700438RAPL_EVENT_ATTR_STR(energy-psys, rapl_psys, "event=0x05");
Stephane Eranian433678b2015-01-13 23:59:53 +0100439
440RAPL_EVENT_ATTR_STR(energy-cores.unit, rapl_cores_unit, "Joules");
441RAPL_EVENT_ATTR_STR(energy-pkg.unit , rapl_pkg_unit, "Joules");
442RAPL_EVENT_ATTR_STR(energy-ram.unit , rapl_ram_unit, "Joules");
443RAPL_EVENT_ATTR_STR(energy-gpu.unit , rapl_gpu_unit, "Joules");
Srinivas Pandruvadadcee75b2016-04-17 15:03:00 -0700444RAPL_EVENT_ATTR_STR(energy-psys.unit, rapl_psys_unit, "Joules");
Stephane Eranian4788e5b2013-11-12 17:58:50 +0100445
446/*
447 * we compute in 0.23 nJ increments regardless of MSR
448 */
Stephane Eranian433678b2015-01-13 23:59:53 +0100449RAPL_EVENT_ATTR_STR(energy-cores.scale, rapl_cores_scale, "2.3283064365386962890625e-10");
450RAPL_EVENT_ATTR_STR(energy-pkg.scale, rapl_pkg_scale, "2.3283064365386962890625e-10");
451RAPL_EVENT_ATTR_STR(energy-ram.scale, rapl_ram_scale, "2.3283064365386962890625e-10");
452RAPL_EVENT_ATTR_STR(energy-gpu.scale, rapl_gpu_scale, "2.3283064365386962890625e-10");
Srinivas Pandruvadadcee75b2016-04-17 15:03:00 -0700453RAPL_EVENT_ATTR_STR(energy-psys.scale, rapl_psys_scale, "2.3283064365386962890625e-10");
Stephane Eranian4788e5b2013-11-12 17:58:50 +0100454
455static struct attribute *rapl_events_srv_attr[] = {
456 EVENT_PTR(rapl_cores),
457 EVENT_PTR(rapl_pkg),
458 EVENT_PTR(rapl_ram),
459
460 EVENT_PTR(rapl_cores_unit),
461 EVENT_PTR(rapl_pkg_unit),
462 EVENT_PTR(rapl_ram_unit),
463
464 EVENT_PTR(rapl_cores_scale),
465 EVENT_PTR(rapl_pkg_scale),
466 EVENT_PTR(rapl_ram_scale),
467 NULL,
468};
469
470static struct attribute *rapl_events_cln_attr[] = {
471 EVENT_PTR(rapl_cores),
472 EVENT_PTR(rapl_pkg),
Stephane Eranianf228c5b2014-01-08 11:15:53 +0100473 EVENT_PTR(rapl_gpu),
Stephane Eranian4788e5b2013-11-12 17:58:50 +0100474
475 EVENT_PTR(rapl_cores_unit),
476 EVENT_PTR(rapl_pkg_unit),
Stephane Eranianf228c5b2014-01-08 11:15:53 +0100477 EVENT_PTR(rapl_gpu_unit),
Stephane Eranian4788e5b2013-11-12 17:58:50 +0100478
479 EVENT_PTR(rapl_cores_scale),
480 EVENT_PTR(rapl_pkg_scale),
Stephane Eranianf228c5b2014-01-08 11:15:53 +0100481 EVENT_PTR(rapl_gpu_scale),
Stephane Eranian4788e5b2013-11-12 17:58:50 +0100482 NULL,
483};
484
Vince Weavere69af462014-04-02 00:49:55 -0400485static struct attribute *rapl_events_hsw_attr[] = {
486 EVENT_PTR(rapl_cores),
487 EVENT_PTR(rapl_pkg),
488 EVENT_PTR(rapl_gpu),
489 EVENT_PTR(rapl_ram),
490
491 EVENT_PTR(rapl_cores_unit),
492 EVENT_PTR(rapl_pkg_unit),
493 EVENT_PTR(rapl_gpu_unit),
494 EVENT_PTR(rapl_ram_unit),
495
496 EVENT_PTR(rapl_cores_scale),
497 EVENT_PTR(rapl_pkg_scale),
498 EVENT_PTR(rapl_gpu_scale),
499 EVENT_PTR(rapl_ram_scale),
500 NULL,
501};
502
Srinivas Pandruvadadcee75b2016-04-17 15:03:00 -0700503static struct attribute *rapl_events_skl_attr[] = {
504 EVENT_PTR(rapl_cores),
505 EVENT_PTR(rapl_pkg),
506 EVENT_PTR(rapl_gpu),
507 EVENT_PTR(rapl_ram),
508 EVENT_PTR(rapl_psys),
509
510 EVENT_PTR(rapl_cores_unit),
511 EVENT_PTR(rapl_pkg_unit),
512 EVENT_PTR(rapl_gpu_unit),
513 EVENT_PTR(rapl_ram_unit),
514 EVENT_PTR(rapl_psys_unit),
515
516 EVENT_PTR(rapl_cores_scale),
517 EVENT_PTR(rapl_pkg_scale),
518 EVENT_PTR(rapl_gpu_scale),
519 EVENT_PTR(rapl_ram_scale),
520 EVENT_PTR(rapl_psys_scale),
521 NULL,
522};
523
Dasaratharaman Chandramouli3a2a7792015-05-26 11:47:39 -0700524static struct attribute *rapl_events_knl_attr[] = {
525 EVENT_PTR(rapl_pkg),
526 EVENT_PTR(rapl_ram),
527
528 EVENT_PTR(rapl_pkg_unit),
529 EVENT_PTR(rapl_ram_unit),
530
531 EVENT_PTR(rapl_pkg_scale),
532 EVENT_PTR(rapl_ram_scale),
533 NULL,
534};
535
Stephane Eranian4788e5b2013-11-12 17:58:50 +0100536static struct attribute_group rapl_pmu_events_group = {
537 .name = "events",
538 .attrs = NULL, /* patched at runtime */
539};
540
541DEFINE_RAPL_FORMAT_ATTR(event, event, "config:0-7");
542static struct attribute *rapl_formats_attr[] = {
543 &format_attr_event.attr,
544 NULL,
545};
546
547static struct attribute_group rapl_pmu_format_group = {
548 .name = "format",
549 .attrs = rapl_formats_attr,
550};
551
552const struct attribute_group *rapl_attr_groups[] = {
553 &rapl_pmu_attr_group,
554 &rapl_pmu_format_group,
555 &rapl_pmu_events_group,
556 NULL,
557};
558
Richard Cochran8b5b7732016-07-13 17:16:15 +0000559static int rapl_cpu_offline(unsigned int cpu)
Stephane Eranian4788e5b2013-11-12 17:58:50 +0100560{
Thomas Gleixner9de8d682016-02-22 22:19:26 +0000561 struct rapl_pmu *pmu = cpu_to_rapl_pmu(cpu);
562 int target;
Stephane Eranian4788e5b2013-11-12 17:58:50 +0100563
Thomas Gleixner9de8d682016-02-22 22:19:26 +0000564 /* Check if exiting cpu is used for collecting rapl events */
565 if (!cpumask_test_and_clear_cpu(cpu, &rapl_cpu_mask))
Richard Cochran8b5b7732016-07-13 17:16:15 +0000566 return 0;
Thomas Gleixner9de8d682016-02-22 22:19:26 +0000567
568 pmu->cpu = -1;
569 /* Find a new cpu to collect rapl events */
570 target = cpumask_any_but(topology_core_cpumask(cpu), cpu);
571
572 /* Migrate rapl events to the new target */
573 if (target < nr_cpu_ids) {
Stephane Eranian4788e5b2013-11-12 17:58:50 +0100574 cpumask_set_cpu(target, &rapl_cpu_mask);
Thomas Gleixner9de8d682016-02-22 22:19:26 +0000575 pmu->cpu = target;
Stephane Eranian4788e5b2013-11-12 17:58:50 +0100576 perf_pmu_migrate_context(pmu->pmu, cpu, target);
Thomas Gleixner9de8d682016-02-22 22:19:26 +0000577 }
Richard Cochran8b5b7732016-07-13 17:16:15 +0000578 return 0;
Stephane Eranian4788e5b2013-11-12 17:58:50 +0100579}
580
Richard Cochran8b5b7732016-07-13 17:16:15 +0000581static int rapl_cpu_online(unsigned int cpu)
Stephane Eranian4788e5b2013-11-12 17:58:50 +0100582{
Thomas Gleixner9de8d682016-02-22 22:19:26 +0000583 struct rapl_pmu *pmu = cpu_to_rapl_pmu(cpu);
584 int target;
Stephane Eranian4788e5b2013-11-12 17:58:50 +0100585
Thomas Gleixner9de8d682016-02-22 22:19:26 +0000586 /*
587 * Check if there is an online cpu in the package which collects rapl
588 * events already.
589 */
590 target = cpumask_any_and(&rapl_cpu_mask, topology_core_cpumask(cpu));
591 if (target < nr_cpu_ids)
Richard Cochran8b5b7732016-07-13 17:16:15 +0000592 return 0;
Thomas Gleixner9de8d682016-02-22 22:19:26 +0000593
Stephane Eranian4788e5b2013-11-12 17:58:50 +0100594 cpumask_set_cpu(cpu, &rapl_cpu_mask);
Thomas Gleixner9de8d682016-02-22 22:19:26 +0000595 pmu->cpu = cpu;
Richard Cochran8b5b7732016-07-13 17:16:15 +0000596 return 0;
Stephane Eranian4788e5b2013-11-12 17:58:50 +0100597}
598
Richard Cochran8b5b7732016-07-13 17:16:15 +0000599static int rapl_cpu_prepare(unsigned int cpu)
Stephane Eranian4788e5b2013-11-12 17:58:50 +0100600{
Thomas Gleixner9de8d682016-02-22 22:19:26 +0000601 struct rapl_pmu *pmu = cpu_to_rapl_pmu(cpu);
Stephane Eranian4788e5b2013-11-12 17:58:50 +0100602
603 if (pmu)
604 return 0;
605
Stephane Eranian4788e5b2013-11-12 17:58:50 +0100606 pmu = kzalloc_node(sizeof(*pmu), GFP_KERNEL, cpu_to_node(cpu));
607 if (!pmu)
Thomas Gleixner9de8d682016-02-22 22:19:26 +0000608 return -ENOMEM;
609
Thomas Gleixnera2087492016-02-22 22:19:25 +0000610 raw_spin_lock_init(&pmu->lock);
Stephane Eranian4788e5b2013-11-12 17:58:50 +0100611 INIT_LIST_HEAD(&pmu->active_list);
Thomas Gleixner9de8d682016-02-22 22:19:26 +0000612 pmu->pmu = &rapl_pmus->pmu;
Thomas Gleixner75c70032016-02-22 22:19:22 +0000613 pmu->timer_interval = ms_to_ktime(rapl_timer_ms);
Thomas Gleixner9de8d682016-02-22 22:19:26 +0000614 pmu->cpu = -1;
Stephane Eranian65661f92013-11-12 17:58:51 +0100615 rapl_hrtimer_init(pmu);
Thomas Gleixner9de8d682016-02-22 22:19:26 +0000616 rapl_pmus->pmus[topology_logical_package_id(cpu)] = pmu;
Stephane Eranian4788e5b2013-11-12 17:58:50 +0100617 return 0;
618}
619
Borislav Petkov7a869802016-03-08 17:40:41 +0100620static int rapl_check_hw_unit(bool apply_quirk)
Jacob Pan64552392015-03-26 14:28:45 -0700621{
622 u64 msr_rapl_power_unit_bits;
623 int i;
624
625 /* protect rdmsrl() to handle virtualization */
626 if (rdmsrl_safe(MSR_RAPL_POWER_UNIT, &msr_rapl_power_unit_bits))
627 return -1;
628 for (i = 0; i < NR_RAPL_DOMAINS; i++)
629 rapl_hw_unit[i] = (msr_rapl_power_unit_bits >> 8) & 0x1FULL;
630
Borislav Petkov7a869802016-03-08 17:40:41 +0100631 /*
632 * DRAM domain on HSW server and KNL has fixed energy unit which can be
633 * different than the unit from power unit MSR. See
634 * "Intel Xeon Processor E5-1600 and E5-2600 v3 Product Families, V2
635 * of 2. Datasheet, September 2014, Reference Number: 330784-001 "
636 */
637 if (apply_quirk)
638 rapl_hw_unit[RAPL_IDX_RAM_NRG_STAT] = 16;
Thomas Gleixner75c70032016-02-22 22:19:22 +0000639
640 /*
641 * Calculate the timer rate:
642 * Use reference of 200W for scaling the timeout to avoid counter
643 * overflows. 200W = 200 Joules/sec
644 * Divide interval by 2 to avoid lockstep (2 * 100)
645 * if hw unit is 32, then we use 2 ms 1/200/2
646 */
647 rapl_timer_ms = 2;
648 if (rapl_hw_unit[0] < 32) {
649 rapl_timer_ms = (1000 / (2 * 100));
650 rapl_timer_ms *= (1ULL << (32 - rapl_hw_unit[0] - 1));
651 }
Jacob Pan64552392015-03-26 14:28:45 -0700652 return 0;
653}
654
Thomas Gleixner512089d2016-02-22 22:19:23 +0000655static void __init rapl_advertise(void)
656{
657 int i;
658
659 pr_info("API unit is 2^-32 Joules, %d fixed counters, %llu ms ovfl timer\n",
660 hweight32(rapl_cntr_mask), rapl_timer_ms);
661
662 for (i = 0; i < NR_RAPL_DOMAINS; i++) {
663 if (rapl_cntr_mask & (1 << i)) {
664 pr_info("hw unit of domain %s 2^-%d Joules\n",
665 rapl_domain_names[i], rapl_hw_unit[i]);
666 }
667 }
668}
669
Kan Liang4b6e2572016-03-19 00:20:50 -0700670static void cleanup_rapl_pmus(void)
Thomas Gleixner55f28902016-02-22 22:19:21 +0000671{
Thomas Gleixner9de8d682016-02-22 22:19:26 +0000672 int i;
Thomas Gleixner55f28902016-02-22 22:19:21 +0000673
Thomas Gleixner9de8d682016-02-22 22:19:26 +0000674 for (i = 0; i < rapl_pmus->maxpkg; i++)
Vincent Stehlé275ae412016-05-24 16:53:49 +0200675 kfree(rapl_pmus->pmus[i]);
Thomas Gleixner9de8d682016-02-22 22:19:26 +0000676 kfree(rapl_pmus);
677}
678
679static int __init init_rapl_pmus(void)
680{
681 int maxpkg = topology_max_packages();
682 size_t size;
683
684 size = sizeof(*rapl_pmus) + maxpkg * sizeof(struct rapl_pmu *);
685 rapl_pmus = kzalloc(size, GFP_KERNEL);
686 if (!rapl_pmus)
687 return -ENOMEM;
688
689 rapl_pmus->maxpkg = maxpkg;
690 rapl_pmus->pmu.attr_groups = rapl_attr_groups;
691 rapl_pmus->pmu.task_ctx_nr = perf_invalid_context;
692 rapl_pmus->pmu.event_init = rapl_pmu_event_init;
693 rapl_pmus->pmu.add = rapl_pmu_event_add;
694 rapl_pmus->pmu.del = rapl_pmu_event_del;
695 rapl_pmus->pmu.start = rapl_pmu_event_start;
696 rapl_pmus->pmu.stop = rapl_pmu_event_stop;
697 rapl_pmus->pmu.read = rapl_pmu_event_read;
698 return 0;
Thomas Gleixner55f28902016-02-22 22:19:21 +0000699}
700
Kan Liang4b6e2572016-03-19 00:20:50 -0700701#define X86_RAPL_MODEL_MATCH(model, init) \
702 { X86_VENDOR_INTEL, 6, model, X86_FEATURE_ANY, (unsigned long)&init }
703
704struct intel_rapl_init_fun {
705 bool apply_quirk;
706 int cntr_mask;
707 struct attribute **attrs;
Stephane Eranian4788e5b2013-11-12 17:58:50 +0100708};
709
Kan Liang4b6e2572016-03-19 00:20:50 -0700710static const struct intel_rapl_init_fun snb_rapl_init __initconst = {
711 .apply_quirk = false,
712 .cntr_mask = RAPL_IDX_CLN,
713 .attrs = rapl_events_cln_attr,
714};
715
716static const struct intel_rapl_init_fun hsx_rapl_init __initconst = {
717 .apply_quirk = true,
718 .cntr_mask = RAPL_IDX_SRV,
719 .attrs = rapl_events_srv_attr,
720};
721
722static const struct intel_rapl_init_fun hsw_rapl_init __initconst = {
723 .apply_quirk = false,
724 .cntr_mask = RAPL_IDX_HSW,
725 .attrs = rapl_events_hsw_attr,
726};
727
728static const struct intel_rapl_init_fun snbep_rapl_init __initconst = {
729 .apply_quirk = false,
730 .cntr_mask = RAPL_IDX_SRV,
731 .attrs = rapl_events_srv_attr,
732};
733
734static const struct intel_rapl_init_fun knl_rapl_init __initconst = {
735 .apply_quirk = true,
736 .cntr_mask = RAPL_IDX_KNL,
737 .attrs = rapl_events_knl_attr,
738};
739
Srinivas Pandruvadadcee75b2016-04-17 15:03:00 -0700740static const struct intel_rapl_init_fun skl_rapl_init __initconst = {
741 .apply_quirk = false,
742 .cntr_mask = RAPL_IDX_SKL_CLN,
743 .attrs = rapl_events_skl_attr,
744};
745
Kan Liang4b6e2572016-03-19 00:20:50 -0700746static const struct x86_cpu_id rapl_cpu_match[] __initconst = {
Dave Hansen7f2236d2016-06-02 17:19:30 -0700747 X86_RAPL_MODEL_MATCH(INTEL_FAM6_SANDYBRIDGE, snb_rapl_init),
748 X86_RAPL_MODEL_MATCH(INTEL_FAM6_SANDYBRIDGE_X, snbep_rapl_init),
Peter Zijlstrac416e5a2016-04-21 15:14:17 +0200749
Dave Hansen7f2236d2016-06-02 17:19:30 -0700750 X86_RAPL_MODEL_MATCH(INTEL_FAM6_IVYBRIDGE, snb_rapl_init),
751 X86_RAPL_MODEL_MATCH(INTEL_FAM6_IVYBRIDGE_X, snbep_rapl_init),
Peter Zijlstrac416e5a2016-04-21 15:14:17 +0200752
Dave Hansen7f2236d2016-06-02 17:19:30 -0700753 X86_RAPL_MODEL_MATCH(INTEL_FAM6_HASWELL_CORE, hsw_rapl_init),
754 X86_RAPL_MODEL_MATCH(INTEL_FAM6_HASWELL_X, hsw_rapl_init),
755 X86_RAPL_MODEL_MATCH(INTEL_FAM6_HASWELL_ULT, hsw_rapl_init),
756 X86_RAPL_MODEL_MATCH(INTEL_FAM6_HASWELL_GT3E, hsw_rapl_init),
Peter Zijlstrac416e5a2016-04-21 15:14:17 +0200757
Dave Hansen7f2236d2016-06-02 17:19:30 -0700758 X86_RAPL_MODEL_MATCH(INTEL_FAM6_BROADWELL_CORE, hsw_rapl_init),
759 X86_RAPL_MODEL_MATCH(INTEL_FAM6_BROADWELL_GT3E, hsw_rapl_init),
760 X86_RAPL_MODEL_MATCH(INTEL_FAM6_BROADWELL_X, hsw_rapl_init),
761 X86_RAPL_MODEL_MATCH(INTEL_FAM6_BROADWELL_XEON_D, hsw_rapl_init),
Peter Zijlstrac416e5a2016-04-21 15:14:17 +0200762
Dave Hansen7f2236d2016-06-02 17:19:30 -0700763 X86_RAPL_MODEL_MATCH(INTEL_FAM6_XEON_PHI_KNL, knl_rapl_init),
Peter Zijlstrac416e5a2016-04-21 15:14:17 +0200764
Dave Hansen7f2236d2016-06-02 17:19:30 -0700765 X86_RAPL_MODEL_MATCH(INTEL_FAM6_SKYLAKE_MOBILE, skl_rapl_init),
766 X86_RAPL_MODEL_MATCH(INTEL_FAM6_SKYLAKE_DESKTOP, skl_rapl_init),
Jacob Pan348c5ac2016-06-02 17:19:53 -0700767 X86_RAPL_MODEL_MATCH(INTEL_FAM6_SKYLAKE_X, hsx_rapl_init),
Kan Liang4b6e2572016-03-19 00:20:50 -0700768 {},
769};
770
771MODULE_DEVICE_TABLE(x86cpu, rapl_cpu_match);
772
Stephane Eranian4788e5b2013-11-12 17:58:50 +0100773static int __init rapl_pmu_init(void)
774{
Kan Liang4b6e2572016-03-19 00:20:50 -0700775 const struct x86_cpu_id *id;
776 struct intel_rapl_init_fun *rapl_init;
777 bool apply_quirk;
Thomas Gleixner7162b8f2016-02-22 22:19:24 +0000778 int ret;
Stephane Eranian4788e5b2013-11-12 17:58:50 +0100779
Kan Liang4b6e2572016-03-19 00:20:50 -0700780 id = x86_match_cpu(rapl_cpu_match);
781 if (!id)
Thomas Gleixner55f28902016-02-22 22:19:21 +0000782 return -ENODEV;
Stephane Eranian4788e5b2013-11-12 17:58:50 +0100783
Kan Liang4b6e2572016-03-19 00:20:50 -0700784 rapl_init = (struct intel_rapl_init_fun *)id->driver_data;
785 apply_quirk = rapl_init->apply_quirk;
786 rapl_cntr_mask = rapl_init->cntr_mask;
787 rapl_pmu_events_group.attrs = rapl_init->attrs;
Thomas Gleixner55f28902016-02-22 22:19:21 +0000788
Borislav Petkov7a869802016-03-08 17:40:41 +0100789 ret = rapl_check_hw_unit(apply_quirk);
Jacob Pan64552392015-03-26 14:28:45 -0700790 if (ret)
791 return ret;
Srivatsa S. Bhatfd537e52014-03-11 02:08:09 +0530792
Thomas Gleixner9de8d682016-02-22 22:19:26 +0000793 ret = init_rapl_pmus();
794 if (ret)
795 return ret;
796
Richard Cochran8b5b7732016-07-13 17:16:15 +0000797 /*
798 * Install callbacks. Core will call them for each online cpu.
799 */
Stephane Eranian4788e5b2013-11-12 17:58:50 +0100800
Richard Cochran8b5b7732016-07-13 17:16:15 +0000801 ret = cpuhp_setup_state(CPUHP_PERF_X86_RAPL_PREP, "PERF_X86_RAPL_PREP",
802 rapl_cpu_prepare, NULL);
Thomas Gleixner7162b8f2016-02-22 22:19:24 +0000803 if (ret)
804 goto out;
Stephane Eranian4788e5b2013-11-12 17:58:50 +0100805
Richard Cochran8b5b7732016-07-13 17:16:15 +0000806 ret = cpuhp_setup_state(CPUHP_AP_PERF_X86_RAPL_ONLINE,
807 "AP_PERF_X86_RAPL_ONLINE",
808 rapl_cpu_online, rapl_cpu_offline);
809 if (ret)
810 goto out1;
811
Thomas Gleixner9de8d682016-02-22 22:19:26 +0000812 ret = perf_pmu_register(&rapl_pmus->pmu, "power", -1);
Thomas Gleixner512089d2016-02-22 22:19:23 +0000813 if (ret)
Richard Cochran8b5b7732016-07-13 17:16:15 +0000814 goto out2;
Stephane Eranian4788e5b2013-11-12 17:58:50 +0100815
Thomas Gleixner512089d2016-02-22 22:19:23 +0000816 rapl_advertise();
Stephane Eranian4788e5b2013-11-12 17:58:50 +0100817 return 0;
Thomas Gleixner55f28902016-02-22 22:19:21 +0000818
Richard Cochran8b5b7732016-07-13 17:16:15 +0000819out2:
820 cpuhp_remove_state(CPUHP_AP_PERF_X86_RAPL_ONLINE);
821out1:
822 cpuhp_remove_state(CPUHP_PERF_X86_RAPL_PREP);
Thomas Gleixner55f28902016-02-22 22:19:21 +0000823out:
Thomas Gleixner512089d2016-02-22 22:19:23 +0000824 pr_warn("Initialization failed (%d), disabled\n", ret);
Thomas Gleixner55f28902016-02-22 22:19:21 +0000825 cleanup_rapl_pmus();
Thomas Gleixner55f28902016-02-22 22:19:21 +0000826 return ret;
Stephane Eranian4788e5b2013-11-12 17:58:50 +0100827}
Kan Liang4b6e2572016-03-19 00:20:50 -0700828module_init(rapl_pmu_init);
829
830static void __exit intel_rapl_exit(void)
831{
Richard Cochran8b5b7732016-07-13 17:16:15 +0000832 cpuhp_remove_state_nocalls(CPUHP_AP_PERF_X86_RAPL_ONLINE);
833 cpuhp_remove_state_nocalls(CPUHP_PERF_X86_RAPL_PREP);
Kan Liang4b6e2572016-03-19 00:20:50 -0700834 perf_pmu_unregister(&rapl_pmus->pmu);
835 cleanup_rapl_pmus();
Kan Liang4b6e2572016-03-19 00:20:50 -0700836}
837module_exit(intel_rapl_exit);