blob: 26c7d7d8a657edb813543cfbde2c71a570bb2b40 [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>
Borislav Petkov27f6d222016-02-10 10:55:23 +010058#include "../perf_event.h"
Stephane Eranian4788e5b2013-11-12 17:58:50 +010059
Kan Liang4b6e2572016-03-19 00:20:50 -070060MODULE_LICENSE("GPL");
61
Stephane Eranian4788e5b2013-11-12 17:58:50 +010062/*
63 * RAPL energy status counters
64 */
65#define RAPL_IDX_PP0_NRG_STAT 0 /* all cores */
66#define INTEL_RAPL_PP0 0x1 /* pseudo-encoding */
67#define RAPL_IDX_PKG_NRG_STAT 1 /* entire package */
68#define INTEL_RAPL_PKG 0x2 /* pseudo-encoding */
69#define RAPL_IDX_RAM_NRG_STAT 2 /* DRAM */
70#define INTEL_RAPL_RAM 0x3 /* pseudo-encoding */
Vince Weavere69af462014-04-02 00:49:55 -040071#define RAPL_IDX_PP1_NRG_STAT 3 /* gpu */
Stephane Eranianf228c5b2014-01-08 11:15:53 +010072#define INTEL_RAPL_PP1 0x4 /* pseudo-encoding */
Srinivas Pandruvadadcee75b2016-04-17 15:03:00 -070073#define RAPL_IDX_PSYS_NRG_STAT 4 /* psys */
74#define INTEL_RAPL_PSYS 0x5 /* pseudo-encoding */
Stephane Eranian4788e5b2013-11-12 17:58:50 +010075
Srinivas Pandruvadadcee75b2016-04-17 15:03:00 -070076#define NR_RAPL_DOMAINS 0x5
Andi Kleenda008ee2015-11-30 09:48:42 -080077static const char *const rapl_domain_names[NR_RAPL_DOMAINS] __initconst = {
Jacob Pan64552392015-03-26 14:28:45 -070078 "pp0-core",
79 "package",
80 "dram",
81 "pp1-gpu",
Srinivas Pandruvadadcee75b2016-04-17 15:03:00 -070082 "psys",
Jacob Pan64552392015-03-26 14:28:45 -070083};
84
Stephane Eranian4788e5b2013-11-12 17:58:50 +010085/* Clients have PP0, PKG */
86#define RAPL_IDX_CLN (1<<RAPL_IDX_PP0_NRG_STAT|\
Stephane Eranianf228c5b2014-01-08 11:15:53 +010087 1<<RAPL_IDX_PKG_NRG_STAT|\
88 1<<RAPL_IDX_PP1_NRG_STAT)
Stephane Eranian4788e5b2013-11-12 17:58:50 +010089
90/* Servers have PP0, PKG, RAM */
91#define RAPL_IDX_SRV (1<<RAPL_IDX_PP0_NRG_STAT|\
92 1<<RAPL_IDX_PKG_NRG_STAT|\
93 1<<RAPL_IDX_RAM_NRG_STAT)
94
Vince Weavere69af462014-04-02 00:49:55 -040095/* Servers have PP0, PKG, RAM, PP1 */
96#define RAPL_IDX_HSW (1<<RAPL_IDX_PP0_NRG_STAT|\
97 1<<RAPL_IDX_PKG_NRG_STAT|\
98 1<<RAPL_IDX_RAM_NRG_STAT|\
99 1<<RAPL_IDX_PP1_NRG_STAT)
100
Srinivas Pandruvadadcee75b2016-04-17 15:03:00 -0700101/* SKL clients have PP0, PKG, RAM, PP1, PSYS */
102#define RAPL_IDX_SKL_CLN (1<<RAPL_IDX_PP0_NRG_STAT|\
103 1<<RAPL_IDX_PKG_NRG_STAT|\
104 1<<RAPL_IDX_RAM_NRG_STAT|\
105 1<<RAPL_IDX_PP1_NRG_STAT|\
106 1<<RAPL_IDX_PSYS_NRG_STAT)
107
Dasaratharaman Chandramouli3a2a7792015-05-26 11:47:39 -0700108/* Knights Landing has PKG, RAM */
109#define RAPL_IDX_KNL (1<<RAPL_IDX_PKG_NRG_STAT|\
110 1<<RAPL_IDX_RAM_NRG_STAT)
111
Stephane Eranian4788e5b2013-11-12 17:58:50 +0100112/*
113 * event code: LSB 8 bits, passed in attr->config
114 * any other bit is reserved
115 */
116#define RAPL_EVENT_MASK 0xFFULL
117
118#define DEFINE_RAPL_FORMAT_ATTR(_var, _name, _format) \
119static ssize_t __rapl_##_var##_show(struct kobject *kobj, \
120 struct kobj_attribute *attr, \
121 char *page) \
122{ \
123 BUILD_BUG_ON(sizeof(_format) >= PAGE_SIZE); \
124 return sprintf(page, _format "\n"); \
125} \
126static struct kobj_attribute format_attr_##_var = \
127 __ATTR(_name, 0444, __rapl_##_var##_show, NULL)
128
Thomas Gleixner7162b8f2016-02-22 22:19:24 +0000129#define RAPL_CNTR_WIDTH 32
Stephane Eranian4788e5b2013-11-12 17:58:50 +0100130
Huang Ruid3bcd642015-12-04 18:07:41 +0800131#define RAPL_EVENT_ATTR_STR(_name, v, str) \
132static struct perf_pmu_events_attr event_attr_##v = { \
133 .attr = __ATTR(_name, 0444, perf_event_sysfs_show, NULL), \
134 .id = 0, \
135 .event_str = str, \
Stephane Eranian433678b2015-01-13 23:59:53 +0100136};
137
Stephane Eranian4788e5b2013-11-12 17:58:50 +0100138struct rapl_pmu {
Thomas Gleixnera2087492016-02-22 22:19:25 +0000139 raw_spinlock_t lock;
Thomas Gleixner7162b8f2016-02-22 22:19:24 +0000140 int n_active;
Thomas Gleixner8a6d2f82016-02-22 22:19:25 +0000141 int cpu;
Thomas Gleixner7162b8f2016-02-22 22:19:24 +0000142 struct list_head active_list;
143 struct pmu *pmu;
144 ktime_t timer_interval;
145 struct hrtimer hrtimer;
Stephane Eranian4788e5b2013-11-12 17:58:50 +0100146};
147
Thomas Gleixner9de8d682016-02-22 22:19:26 +0000148struct rapl_pmus {
149 struct pmu pmu;
150 unsigned int maxpkg;
151 struct rapl_pmu *pmus[];
152};
153
Thomas Gleixner7162b8f2016-02-22 22:19:24 +0000154 /* 1/2^hw_unit Joule */
155static int rapl_hw_unit[NR_RAPL_DOMAINS] __read_mostly;
Thomas Gleixner9de8d682016-02-22 22:19:26 +0000156static struct rapl_pmus *rapl_pmus;
Stephane Eranian4788e5b2013-11-12 17:58:50 +0100157static cpumask_t rapl_cpu_mask;
Thomas Gleixner9de8d682016-02-22 22:19:26 +0000158static unsigned int rapl_cntr_mask;
Thomas Gleixner75c70032016-02-22 22:19:22 +0000159static u64 rapl_timer_ms;
Stephane Eranian4788e5b2013-11-12 17:58:50 +0100160
Thomas Gleixner9de8d682016-02-22 22:19:26 +0000161static inline struct rapl_pmu *cpu_to_rapl_pmu(unsigned int cpu)
162{
163 return rapl_pmus->pmus[topology_logical_package_id(cpu)];
164}
Stephane Eranian4788e5b2013-11-12 17:58:50 +0100165
166static inline u64 rapl_read_counter(struct perf_event *event)
167{
168 u64 raw;
169 rdmsrl(event->hw.event_base, raw);
170 return raw;
171}
172
Jacob Pan64552392015-03-26 14:28:45 -0700173static inline u64 rapl_scale(u64 v, int cfg)
Stephane Eranian4788e5b2013-11-12 17:58:50 +0100174{
Jacob Pan64552392015-03-26 14:28:45 -0700175 if (cfg > NR_RAPL_DOMAINS) {
Thomas Gleixner512089d2016-02-22 22:19:23 +0000176 pr_warn("Invalid domain %d, failed to scale data\n", cfg);
Jacob Pan64552392015-03-26 14:28:45 -0700177 return v;
178 }
Stephane Eranian4788e5b2013-11-12 17:58:50 +0100179 /*
180 * scale delta to smallest unit (1/2^32)
181 * users must then scale back: count * 1/(1e9*2^32) to get Joules
182 * or use ldexp(count, -32).
183 * Watts = Joules/Time delta
184 */
Jacob Pan64552392015-03-26 14:28:45 -0700185 return v << (32 - rapl_hw_unit[cfg - 1]);
Stephane Eranian4788e5b2013-11-12 17:58:50 +0100186}
187
188static u64 rapl_event_update(struct perf_event *event)
189{
190 struct hw_perf_event *hwc = &event->hw;
191 u64 prev_raw_count, new_raw_count;
192 s64 delta, sdelta;
193 int shift = RAPL_CNTR_WIDTH;
194
195again:
196 prev_raw_count = local64_read(&hwc->prev_count);
197 rdmsrl(event->hw.event_base, new_raw_count);
198
199 if (local64_cmpxchg(&hwc->prev_count, prev_raw_count,
200 new_raw_count) != prev_raw_count) {
201 cpu_relax();
202 goto again;
203 }
204
205 /*
206 * Now we have the new raw value and have updated the prev
207 * timestamp already. We can now calculate the elapsed delta
208 * (event-)time and add that to the generic event.
209 *
210 * Careful, not all hw sign-extends above the physical width
211 * of the count.
212 */
213 delta = (new_raw_count << shift) - (prev_raw_count << shift);
214 delta >>= shift;
215
Jacob Pan64552392015-03-26 14:28:45 -0700216 sdelta = rapl_scale(delta, event->hw.config);
Stephane Eranian4788e5b2013-11-12 17:58:50 +0100217
218 local64_add(sdelta, &event->count);
219
220 return new_raw_count;
221}
222
Stephane Eranian65661f92013-11-12 17:58:51 +0100223static void rapl_start_hrtimer(struct rapl_pmu *pmu)
224{
Thomas Gleixner514c2302015-04-14 21:09:00 +0000225 hrtimer_start(&pmu->hrtimer, pmu->timer_interval,
226 HRTIMER_MODE_REL_PINNED);
Stephane Eranian65661f92013-11-12 17:58:51 +0100227}
228
Stephane Eranian65661f92013-11-12 17:58:51 +0100229static enum hrtimer_restart rapl_hrtimer_handle(struct hrtimer *hrtimer)
230{
Thomas Gleixner8a6d2f82016-02-22 22:19:25 +0000231 struct rapl_pmu *pmu = container_of(hrtimer, struct rapl_pmu, hrtimer);
Stephane Eranian65661f92013-11-12 17:58:51 +0100232 struct perf_event *event;
233 unsigned long flags;
234
235 if (!pmu->n_active)
236 return HRTIMER_NORESTART;
237
Thomas Gleixnera2087492016-02-22 22:19:25 +0000238 raw_spin_lock_irqsave(&pmu->lock, flags);
Stephane Eranian65661f92013-11-12 17:58:51 +0100239
Thomas Gleixner7162b8f2016-02-22 22:19:24 +0000240 list_for_each_entry(event, &pmu->active_list, active_entry)
Stephane Eranian65661f92013-11-12 17:58:51 +0100241 rapl_event_update(event);
Stephane Eranian65661f92013-11-12 17:58:51 +0100242
Thomas Gleixnera2087492016-02-22 22:19:25 +0000243 raw_spin_unlock_irqrestore(&pmu->lock, flags);
Stephane Eranian65661f92013-11-12 17:58:51 +0100244
245 hrtimer_forward_now(hrtimer, pmu->timer_interval);
246
247 return HRTIMER_RESTART;
248}
249
250static void rapl_hrtimer_init(struct rapl_pmu *pmu)
251{
252 struct hrtimer *hr = &pmu->hrtimer;
253
254 hrtimer_init(hr, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
255 hr->function = rapl_hrtimer_handle;
256}
257
Stephane Eranian4788e5b2013-11-12 17:58:50 +0100258static void __rapl_pmu_event_start(struct rapl_pmu *pmu,
259 struct perf_event *event)
260{
261 if (WARN_ON_ONCE(!(event->hw.state & PERF_HES_STOPPED)))
262 return;
263
264 event->hw.state = 0;
265
266 list_add_tail(&event->active_entry, &pmu->active_list);
267
268 local64_set(&event->hw.prev_count, rapl_read_counter(event));
269
270 pmu->n_active++;
Stephane Eranian65661f92013-11-12 17:58:51 +0100271 if (pmu->n_active == 1)
272 rapl_start_hrtimer(pmu);
Stephane Eranian4788e5b2013-11-12 17:58:50 +0100273}
274
275static void rapl_pmu_event_start(struct perf_event *event, int mode)
276{
Thomas Gleixner8a6d2f82016-02-22 22:19:25 +0000277 struct rapl_pmu *pmu = event->pmu_private;
Stephane Eranian4788e5b2013-11-12 17:58:50 +0100278 unsigned long flags;
279
Thomas Gleixnera2087492016-02-22 22:19:25 +0000280 raw_spin_lock_irqsave(&pmu->lock, flags);
Stephane Eranian4788e5b2013-11-12 17:58:50 +0100281 __rapl_pmu_event_start(pmu, event);
Thomas Gleixnera2087492016-02-22 22:19:25 +0000282 raw_spin_unlock_irqrestore(&pmu->lock, flags);
Stephane Eranian4788e5b2013-11-12 17:58:50 +0100283}
284
285static void rapl_pmu_event_stop(struct perf_event *event, int mode)
286{
Thomas Gleixner8a6d2f82016-02-22 22:19:25 +0000287 struct rapl_pmu *pmu = event->pmu_private;
Stephane Eranian4788e5b2013-11-12 17:58:50 +0100288 struct hw_perf_event *hwc = &event->hw;
289 unsigned long flags;
290
Thomas Gleixnera2087492016-02-22 22:19:25 +0000291 raw_spin_lock_irqsave(&pmu->lock, flags);
Stephane Eranian4788e5b2013-11-12 17:58:50 +0100292
293 /* mark event as deactivated and stopped */
294 if (!(hwc->state & PERF_HES_STOPPED)) {
295 WARN_ON_ONCE(pmu->n_active <= 0);
296 pmu->n_active--;
Stephane Eranian65661f92013-11-12 17:58:51 +0100297 if (pmu->n_active == 0)
Thomas Gleixner7162b8f2016-02-22 22:19:24 +0000298 hrtimer_cancel(&pmu->hrtimer);
Stephane Eranian4788e5b2013-11-12 17:58:50 +0100299
300 list_del(&event->active_entry);
301
302 WARN_ON_ONCE(hwc->state & PERF_HES_STOPPED);
303 hwc->state |= PERF_HES_STOPPED;
304 }
305
306 /* check if update of sw counter is necessary */
307 if ((mode & PERF_EF_UPDATE) && !(hwc->state & PERF_HES_UPTODATE)) {
308 /*
309 * Drain the remaining delta count out of a event
310 * that we are disabling:
311 */
312 rapl_event_update(event);
313 hwc->state |= PERF_HES_UPTODATE;
314 }
315
Thomas Gleixnera2087492016-02-22 22:19:25 +0000316 raw_spin_unlock_irqrestore(&pmu->lock, flags);
Stephane Eranian4788e5b2013-11-12 17:58:50 +0100317}
318
319static int rapl_pmu_event_add(struct perf_event *event, int mode)
320{
Thomas Gleixner8a6d2f82016-02-22 22:19:25 +0000321 struct rapl_pmu *pmu = event->pmu_private;
Stephane Eranian4788e5b2013-11-12 17:58:50 +0100322 struct hw_perf_event *hwc = &event->hw;
323 unsigned long flags;
324
Thomas Gleixnera2087492016-02-22 22:19:25 +0000325 raw_spin_lock_irqsave(&pmu->lock, flags);
Stephane Eranian4788e5b2013-11-12 17:58:50 +0100326
327 hwc->state = PERF_HES_UPTODATE | PERF_HES_STOPPED;
328
329 if (mode & PERF_EF_START)
330 __rapl_pmu_event_start(pmu, event);
331
Thomas Gleixnera2087492016-02-22 22:19:25 +0000332 raw_spin_unlock_irqrestore(&pmu->lock, flags);
Stephane Eranian4788e5b2013-11-12 17:58:50 +0100333
334 return 0;
335}
336
337static void rapl_pmu_event_del(struct perf_event *event, int flags)
338{
339 rapl_pmu_event_stop(event, PERF_EF_UPDATE);
340}
341
342static int rapl_pmu_event_init(struct perf_event *event)
343{
344 u64 cfg = event->attr.config & RAPL_EVENT_MASK;
345 int bit, msr, ret = 0;
Thomas Gleixner9de8d682016-02-22 22:19:26 +0000346 struct rapl_pmu *pmu;
Stephane Eranian4788e5b2013-11-12 17:58:50 +0100347
348 /* only look at RAPL events */
Thomas Gleixner9de8d682016-02-22 22:19:26 +0000349 if (event->attr.type != rapl_pmus->pmu.type)
Stephane Eranian4788e5b2013-11-12 17:58:50 +0100350 return -ENOENT;
351
352 /* check only supported bits are set */
353 if (event->attr.config & ~RAPL_EVENT_MASK)
354 return -EINVAL;
355
Thomas Gleixner8a6d2f82016-02-22 22:19:25 +0000356 if (event->cpu < 0)
357 return -EINVAL;
358
Stephane Eranian4788e5b2013-11-12 17:58:50 +0100359 /*
360 * check event is known (determines counter)
361 */
362 switch (cfg) {
363 case INTEL_RAPL_PP0:
364 bit = RAPL_IDX_PP0_NRG_STAT;
365 msr = MSR_PP0_ENERGY_STATUS;
366 break;
367 case INTEL_RAPL_PKG:
368 bit = RAPL_IDX_PKG_NRG_STAT;
369 msr = MSR_PKG_ENERGY_STATUS;
370 break;
371 case INTEL_RAPL_RAM:
372 bit = RAPL_IDX_RAM_NRG_STAT;
373 msr = MSR_DRAM_ENERGY_STATUS;
374 break;
Stephane Eranianf228c5b2014-01-08 11:15:53 +0100375 case INTEL_RAPL_PP1:
376 bit = RAPL_IDX_PP1_NRG_STAT;
377 msr = MSR_PP1_ENERGY_STATUS;
378 break;
Srinivas Pandruvadadcee75b2016-04-17 15:03:00 -0700379 case INTEL_RAPL_PSYS:
380 bit = RAPL_IDX_PSYS_NRG_STAT;
381 msr = MSR_PLATFORM_ENERGY_STATUS;
382 break;
Stephane Eranian4788e5b2013-11-12 17:58:50 +0100383 default:
384 return -EINVAL;
385 }
386 /* check event supported */
387 if (!(rapl_cntr_mask & (1 << bit)))
388 return -EINVAL;
389
390 /* unsupported modes and filters */
391 if (event->attr.exclude_user ||
392 event->attr.exclude_kernel ||
393 event->attr.exclude_hv ||
394 event->attr.exclude_idle ||
395 event->attr.exclude_host ||
396 event->attr.exclude_guest ||
397 event->attr.sample_period) /* no sampling */
398 return -EINVAL;
399
400 /* must be done before validate_group */
Thomas Gleixner9de8d682016-02-22 22:19:26 +0000401 pmu = cpu_to_rapl_pmu(event->cpu);
Thomas Gleixner8a6d2f82016-02-22 22:19:25 +0000402 event->cpu = pmu->cpu;
403 event->pmu_private = pmu;
Stephane Eranian4788e5b2013-11-12 17:58:50 +0100404 event->hw.event_base = msr;
405 event->hw.config = cfg;
406 event->hw.idx = bit;
407
408 return ret;
409}
410
411static void rapl_pmu_event_read(struct perf_event *event)
412{
413 rapl_event_update(event);
414}
415
416static ssize_t rapl_get_attr_cpumask(struct device *dev,
417 struct device_attribute *attr, char *buf)
418{
Sudeep Holla5aaba362014-09-30 14:48:22 +0100419 return cpumap_print_to_pagebuf(true, buf, &rapl_cpu_mask);
Stephane Eranian4788e5b2013-11-12 17:58:50 +0100420}
421
422static DEVICE_ATTR(cpumask, S_IRUGO, rapl_get_attr_cpumask, NULL);
423
424static struct attribute *rapl_pmu_attrs[] = {
425 &dev_attr_cpumask.attr,
426 NULL,
427};
428
429static struct attribute_group rapl_pmu_attr_group = {
430 .attrs = rapl_pmu_attrs,
431};
432
Stephane Eranian433678b2015-01-13 23:59:53 +0100433RAPL_EVENT_ATTR_STR(energy-cores, rapl_cores, "event=0x01");
434RAPL_EVENT_ATTR_STR(energy-pkg , rapl_pkg, "event=0x02");
435RAPL_EVENT_ATTR_STR(energy-ram , rapl_ram, "event=0x03");
436RAPL_EVENT_ATTR_STR(energy-gpu , rapl_gpu, "event=0x04");
Srinivas Pandruvadadcee75b2016-04-17 15:03:00 -0700437RAPL_EVENT_ATTR_STR(energy-psys, rapl_psys, "event=0x05");
Stephane Eranian433678b2015-01-13 23:59:53 +0100438
439RAPL_EVENT_ATTR_STR(energy-cores.unit, rapl_cores_unit, "Joules");
440RAPL_EVENT_ATTR_STR(energy-pkg.unit , rapl_pkg_unit, "Joules");
441RAPL_EVENT_ATTR_STR(energy-ram.unit , rapl_ram_unit, "Joules");
442RAPL_EVENT_ATTR_STR(energy-gpu.unit , rapl_gpu_unit, "Joules");
Srinivas Pandruvadadcee75b2016-04-17 15:03:00 -0700443RAPL_EVENT_ATTR_STR(energy-psys.unit, rapl_psys_unit, "Joules");
Stephane Eranian4788e5b2013-11-12 17:58:50 +0100444
445/*
446 * we compute in 0.23 nJ increments regardless of MSR
447 */
Stephane Eranian433678b2015-01-13 23:59:53 +0100448RAPL_EVENT_ATTR_STR(energy-cores.scale, rapl_cores_scale, "2.3283064365386962890625e-10");
449RAPL_EVENT_ATTR_STR(energy-pkg.scale, rapl_pkg_scale, "2.3283064365386962890625e-10");
450RAPL_EVENT_ATTR_STR(energy-ram.scale, rapl_ram_scale, "2.3283064365386962890625e-10");
451RAPL_EVENT_ATTR_STR(energy-gpu.scale, rapl_gpu_scale, "2.3283064365386962890625e-10");
Srinivas Pandruvadadcee75b2016-04-17 15:03:00 -0700452RAPL_EVENT_ATTR_STR(energy-psys.scale, rapl_psys_scale, "2.3283064365386962890625e-10");
Stephane Eranian4788e5b2013-11-12 17:58:50 +0100453
454static struct attribute *rapl_events_srv_attr[] = {
455 EVENT_PTR(rapl_cores),
456 EVENT_PTR(rapl_pkg),
457 EVENT_PTR(rapl_ram),
458
459 EVENT_PTR(rapl_cores_unit),
460 EVENT_PTR(rapl_pkg_unit),
461 EVENT_PTR(rapl_ram_unit),
462
463 EVENT_PTR(rapl_cores_scale),
464 EVENT_PTR(rapl_pkg_scale),
465 EVENT_PTR(rapl_ram_scale),
466 NULL,
467};
468
469static struct attribute *rapl_events_cln_attr[] = {
470 EVENT_PTR(rapl_cores),
471 EVENT_PTR(rapl_pkg),
Stephane Eranianf228c5b2014-01-08 11:15:53 +0100472 EVENT_PTR(rapl_gpu),
Stephane Eranian4788e5b2013-11-12 17:58:50 +0100473
474 EVENT_PTR(rapl_cores_unit),
475 EVENT_PTR(rapl_pkg_unit),
Stephane Eranianf228c5b2014-01-08 11:15:53 +0100476 EVENT_PTR(rapl_gpu_unit),
Stephane Eranian4788e5b2013-11-12 17:58:50 +0100477
478 EVENT_PTR(rapl_cores_scale),
479 EVENT_PTR(rapl_pkg_scale),
Stephane Eranianf228c5b2014-01-08 11:15:53 +0100480 EVENT_PTR(rapl_gpu_scale),
Stephane Eranian4788e5b2013-11-12 17:58:50 +0100481 NULL,
482};
483
Vince Weavere69af462014-04-02 00:49:55 -0400484static struct attribute *rapl_events_hsw_attr[] = {
485 EVENT_PTR(rapl_cores),
486 EVENT_PTR(rapl_pkg),
487 EVENT_PTR(rapl_gpu),
488 EVENT_PTR(rapl_ram),
489
490 EVENT_PTR(rapl_cores_unit),
491 EVENT_PTR(rapl_pkg_unit),
492 EVENT_PTR(rapl_gpu_unit),
493 EVENT_PTR(rapl_ram_unit),
494
495 EVENT_PTR(rapl_cores_scale),
496 EVENT_PTR(rapl_pkg_scale),
497 EVENT_PTR(rapl_gpu_scale),
498 EVENT_PTR(rapl_ram_scale),
499 NULL,
500};
501
Srinivas Pandruvadadcee75b2016-04-17 15:03:00 -0700502static struct attribute *rapl_events_skl_attr[] = {
503 EVENT_PTR(rapl_cores),
504 EVENT_PTR(rapl_pkg),
505 EVENT_PTR(rapl_gpu),
506 EVENT_PTR(rapl_ram),
507 EVENT_PTR(rapl_psys),
508
509 EVENT_PTR(rapl_cores_unit),
510 EVENT_PTR(rapl_pkg_unit),
511 EVENT_PTR(rapl_gpu_unit),
512 EVENT_PTR(rapl_ram_unit),
513 EVENT_PTR(rapl_psys_unit),
514
515 EVENT_PTR(rapl_cores_scale),
516 EVENT_PTR(rapl_pkg_scale),
517 EVENT_PTR(rapl_gpu_scale),
518 EVENT_PTR(rapl_ram_scale),
519 EVENT_PTR(rapl_psys_scale),
520 NULL,
521};
522
Dasaratharaman Chandramouli3a2a7792015-05-26 11:47:39 -0700523static struct attribute *rapl_events_knl_attr[] = {
524 EVENT_PTR(rapl_pkg),
525 EVENT_PTR(rapl_ram),
526
527 EVENT_PTR(rapl_pkg_unit),
528 EVENT_PTR(rapl_ram_unit),
529
530 EVENT_PTR(rapl_pkg_scale),
531 EVENT_PTR(rapl_ram_scale),
532 NULL,
533};
534
Stephane Eranian4788e5b2013-11-12 17:58:50 +0100535static struct attribute_group rapl_pmu_events_group = {
536 .name = "events",
537 .attrs = NULL, /* patched at runtime */
538};
539
540DEFINE_RAPL_FORMAT_ATTR(event, event, "config:0-7");
541static struct attribute *rapl_formats_attr[] = {
542 &format_attr_event.attr,
543 NULL,
544};
545
546static struct attribute_group rapl_pmu_format_group = {
547 .name = "format",
548 .attrs = rapl_formats_attr,
549};
550
551const struct attribute_group *rapl_attr_groups[] = {
552 &rapl_pmu_attr_group,
553 &rapl_pmu_format_group,
554 &rapl_pmu_events_group,
555 NULL,
556};
557
Stephane Eranian4788e5b2013-11-12 17:58:50 +0100558static void rapl_cpu_exit(int cpu)
559{
Thomas Gleixner9de8d682016-02-22 22:19:26 +0000560 struct rapl_pmu *pmu = cpu_to_rapl_pmu(cpu);
561 int target;
Stephane Eranian4788e5b2013-11-12 17:58:50 +0100562
Thomas Gleixner9de8d682016-02-22 22:19:26 +0000563 /* Check if exiting cpu is used for collecting rapl events */
564 if (!cpumask_test_and_clear_cpu(cpu, &rapl_cpu_mask))
565 return;
566
567 pmu->cpu = -1;
568 /* Find a new cpu to collect rapl events */
569 target = cpumask_any_but(topology_core_cpumask(cpu), cpu);
570
571 /* Migrate rapl events to the new target */
572 if (target < nr_cpu_ids) {
Stephane Eranian4788e5b2013-11-12 17:58:50 +0100573 cpumask_set_cpu(target, &rapl_cpu_mask);
Thomas Gleixner9de8d682016-02-22 22:19:26 +0000574 pmu->cpu = target;
Stephane Eranian4788e5b2013-11-12 17:58:50 +0100575 perf_pmu_migrate_context(pmu->pmu, cpu, target);
Thomas Gleixner9de8d682016-02-22 22:19:26 +0000576 }
Stephane Eranian4788e5b2013-11-12 17:58:50 +0100577}
578
579static void rapl_cpu_init(int cpu)
580{
Thomas Gleixner9de8d682016-02-22 22:19:26 +0000581 struct rapl_pmu *pmu = cpu_to_rapl_pmu(cpu);
582 int target;
Stephane Eranian4788e5b2013-11-12 17:58:50 +0100583
Thomas Gleixner9de8d682016-02-22 22:19:26 +0000584 /*
585 * Check if there is an online cpu in the package which collects rapl
586 * events already.
587 */
588 target = cpumask_any_and(&rapl_cpu_mask, topology_core_cpumask(cpu));
589 if (target < nr_cpu_ids)
590 return;
591
Stephane Eranian4788e5b2013-11-12 17:58:50 +0100592 cpumask_set_cpu(cpu, &rapl_cpu_mask);
Thomas Gleixner9de8d682016-02-22 22:19:26 +0000593 pmu->cpu = cpu;
Stephane Eranian4788e5b2013-11-12 17:58:50 +0100594}
595
596static int rapl_cpu_prepare(int cpu)
597{
Thomas Gleixner9de8d682016-02-22 22:19:26 +0000598 struct rapl_pmu *pmu = cpu_to_rapl_pmu(cpu);
Stephane Eranian4788e5b2013-11-12 17:58:50 +0100599
600 if (pmu)
601 return 0;
602
Stephane Eranian4788e5b2013-11-12 17:58:50 +0100603 pmu = kzalloc_node(sizeof(*pmu), GFP_KERNEL, cpu_to_node(cpu));
604 if (!pmu)
Thomas Gleixner9de8d682016-02-22 22:19:26 +0000605 return -ENOMEM;
606
Thomas Gleixnera2087492016-02-22 22:19:25 +0000607 raw_spin_lock_init(&pmu->lock);
Stephane Eranian4788e5b2013-11-12 17:58:50 +0100608 INIT_LIST_HEAD(&pmu->active_list);
Thomas Gleixner9de8d682016-02-22 22:19:26 +0000609 pmu->pmu = &rapl_pmus->pmu;
Thomas Gleixner75c70032016-02-22 22:19:22 +0000610 pmu->timer_interval = ms_to_ktime(rapl_timer_ms);
Thomas Gleixner9de8d682016-02-22 22:19:26 +0000611 pmu->cpu = -1;
Stephane Eranian65661f92013-11-12 17:58:51 +0100612 rapl_hrtimer_init(pmu);
Thomas Gleixner9de8d682016-02-22 22:19:26 +0000613 rapl_pmus->pmus[topology_logical_package_id(cpu)] = pmu;
Stephane Eranian4788e5b2013-11-12 17:58:50 +0100614 return 0;
615}
616
617static int rapl_cpu_notifier(struct notifier_block *self,
618 unsigned long action, void *hcpu)
619{
620 unsigned int cpu = (long)hcpu;
621
622 switch (action & ~CPU_TASKS_FROZEN) {
623 case CPU_UP_PREPARE:
624 rapl_cpu_prepare(cpu);
625 break;
Thomas Gleixner9de8d682016-02-22 22:19:26 +0000626
627 case CPU_DOWN_FAILED:
628 case CPU_ONLINE:
Stephane Eranian4788e5b2013-11-12 17:58:50 +0100629 rapl_cpu_init(cpu);
630 break;
Thomas Gleixner9de8d682016-02-22 22:19:26 +0000631
Stephane Eranian4788e5b2013-11-12 17:58:50 +0100632 case CPU_DOWN_PREPARE:
633 rapl_cpu_exit(cpu);
634 break;
Stephane Eranian4788e5b2013-11-12 17:58:50 +0100635 }
Stephane Eranian4788e5b2013-11-12 17:58:50 +0100636 return NOTIFY_OK;
637}
638
Kan Liang4b6e2572016-03-19 00:20:50 -0700639static struct notifier_block rapl_cpu_nb = {
640 .notifier_call = rapl_cpu_notifier,
641 .priority = CPU_PRI_PERF + 1,
642};
643
Borislav Petkov7a869802016-03-08 17:40:41 +0100644static int rapl_check_hw_unit(bool apply_quirk)
Jacob Pan64552392015-03-26 14:28:45 -0700645{
646 u64 msr_rapl_power_unit_bits;
647 int i;
648
649 /* protect rdmsrl() to handle virtualization */
650 if (rdmsrl_safe(MSR_RAPL_POWER_UNIT, &msr_rapl_power_unit_bits))
651 return -1;
652 for (i = 0; i < NR_RAPL_DOMAINS; i++)
653 rapl_hw_unit[i] = (msr_rapl_power_unit_bits >> 8) & 0x1FULL;
654
Borislav Petkov7a869802016-03-08 17:40:41 +0100655 /*
656 * DRAM domain on HSW server and KNL has fixed energy unit which can be
657 * different than the unit from power unit MSR. See
658 * "Intel Xeon Processor E5-1600 and E5-2600 v3 Product Families, V2
659 * of 2. Datasheet, September 2014, Reference Number: 330784-001 "
660 */
661 if (apply_quirk)
662 rapl_hw_unit[RAPL_IDX_RAM_NRG_STAT] = 16;
Thomas Gleixner75c70032016-02-22 22:19:22 +0000663
664 /*
665 * Calculate the timer rate:
666 * Use reference of 200W for scaling the timeout to avoid counter
667 * overflows. 200W = 200 Joules/sec
668 * Divide interval by 2 to avoid lockstep (2 * 100)
669 * if hw unit is 32, then we use 2 ms 1/200/2
670 */
671 rapl_timer_ms = 2;
672 if (rapl_hw_unit[0] < 32) {
673 rapl_timer_ms = (1000 / (2 * 100));
674 rapl_timer_ms *= (1ULL << (32 - rapl_hw_unit[0] - 1));
675 }
Jacob Pan64552392015-03-26 14:28:45 -0700676 return 0;
677}
678
Thomas Gleixner512089d2016-02-22 22:19:23 +0000679static void __init rapl_advertise(void)
680{
681 int i;
682
683 pr_info("API unit is 2^-32 Joules, %d fixed counters, %llu ms ovfl timer\n",
684 hweight32(rapl_cntr_mask), rapl_timer_ms);
685
686 for (i = 0; i < NR_RAPL_DOMAINS; i++) {
687 if (rapl_cntr_mask & (1 << i)) {
688 pr_info("hw unit of domain %s 2^-%d Joules\n",
689 rapl_domain_names[i], rapl_hw_unit[i]);
690 }
691 }
692}
693
Thomas Gleixner7162b8f2016-02-22 22:19:24 +0000694static int __init rapl_prepare_cpus(void)
695{
Thomas Gleixner9de8d682016-02-22 22:19:26 +0000696 unsigned int cpu, pkg;
Thomas Gleixner7162b8f2016-02-22 22:19:24 +0000697 int ret;
698
699 for_each_online_cpu(cpu) {
Thomas Gleixner9de8d682016-02-22 22:19:26 +0000700 pkg = topology_logical_package_id(cpu);
701 if (rapl_pmus->pmus[pkg])
702 continue;
703
Thomas Gleixner7162b8f2016-02-22 22:19:24 +0000704 ret = rapl_cpu_prepare(cpu);
705 if (ret)
706 return ret;
707 rapl_cpu_init(cpu);
708 }
709 return 0;
710}
711
Kan Liang4b6e2572016-03-19 00:20:50 -0700712static void cleanup_rapl_pmus(void)
Thomas Gleixner55f28902016-02-22 22:19:21 +0000713{
Thomas Gleixner9de8d682016-02-22 22:19:26 +0000714 int i;
Thomas Gleixner55f28902016-02-22 22:19:21 +0000715
Thomas Gleixner9de8d682016-02-22 22:19:26 +0000716 for (i = 0; i < rapl_pmus->maxpkg; i++)
717 kfree(rapl_pmus->pmus + i);
718 kfree(rapl_pmus);
719}
720
721static int __init init_rapl_pmus(void)
722{
723 int maxpkg = topology_max_packages();
724 size_t size;
725
726 size = sizeof(*rapl_pmus) + maxpkg * sizeof(struct rapl_pmu *);
727 rapl_pmus = kzalloc(size, GFP_KERNEL);
728 if (!rapl_pmus)
729 return -ENOMEM;
730
731 rapl_pmus->maxpkg = maxpkg;
732 rapl_pmus->pmu.attr_groups = rapl_attr_groups;
733 rapl_pmus->pmu.task_ctx_nr = perf_invalid_context;
734 rapl_pmus->pmu.event_init = rapl_pmu_event_init;
735 rapl_pmus->pmu.add = rapl_pmu_event_add;
736 rapl_pmus->pmu.del = rapl_pmu_event_del;
737 rapl_pmus->pmu.start = rapl_pmu_event_start;
738 rapl_pmus->pmu.stop = rapl_pmu_event_stop;
739 rapl_pmus->pmu.read = rapl_pmu_event_read;
740 return 0;
Thomas Gleixner55f28902016-02-22 22:19:21 +0000741}
742
Kan Liang4b6e2572016-03-19 00:20:50 -0700743#define X86_RAPL_MODEL_MATCH(model, init) \
744 { X86_VENDOR_INTEL, 6, model, X86_FEATURE_ANY, (unsigned long)&init }
745
746struct intel_rapl_init_fun {
747 bool apply_quirk;
748 int cntr_mask;
749 struct attribute **attrs;
Stephane Eranian4788e5b2013-11-12 17:58:50 +0100750};
751
Kan Liang4b6e2572016-03-19 00:20:50 -0700752static const struct intel_rapl_init_fun snb_rapl_init __initconst = {
753 .apply_quirk = false,
754 .cntr_mask = RAPL_IDX_CLN,
755 .attrs = rapl_events_cln_attr,
756};
757
758static const struct intel_rapl_init_fun hsx_rapl_init __initconst = {
759 .apply_quirk = true,
760 .cntr_mask = RAPL_IDX_SRV,
761 .attrs = rapl_events_srv_attr,
762};
763
764static const struct intel_rapl_init_fun hsw_rapl_init __initconst = {
765 .apply_quirk = false,
766 .cntr_mask = RAPL_IDX_HSW,
767 .attrs = rapl_events_hsw_attr,
768};
769
770static const struct intel_rapl_init_fun snbep_rapl_init __initconst = {
771 .apply_quirk = false,
772 .cntr_mask = RAPL_IDX_SRV,
773 .attrs = rapl_events_srv_attr,
774};
775
776static const struct intel_rapl_init_fun knl_rapl_init __initconst = {
777 .apply_quirk = true,
778 .cntr_mask = RAPL_IDX_KNL,
779 .attrs = rapl_events_knl_attr,
780};
781
Srinivas Pandruvadadcee75b2016-04-17 15:03:00 -0700782static const struct intel_rapl_init_fun skl_rapl_init __initconst = {
783 .apply_quirk = false,
784 .cntr_mask = RAPL_IDX_SKL_CLN,
785 .attrs = rapl_events_skl_attr,
786};
787
Kan Liang4b6e2572016-03-19 00:20:50 -0700788static const struct x86_cpu_id rapl_cpu_match[] __initconst = {
789 X86_RAPL_MODEL_MATCH(42, snb_rapl_init), /* Sandy Bridge */
790 X86_RAPL_MODEL_MATCH(58, snb_rapl_init), /* Ivy Bridge */
791 X86_RAPL_MODEL_MATCH(63, hsx_rapl_init), /* Haswell-Server */
792 X86_RAPL_MODEL_MATCH(79, hsx_rapl_init), /* Broadwell-Server */
793 X86_RAPL_MODEL_MATCH(60, hsw_rapl_init), /* Haswell */
794 X86_RAPL_MODEL_MATCH(69, hsw_rapl_init), /* Haswell-Celeron */
Ingo Molnar65cbbd02016-04-23 14:12:10 +0200795 X86_RAPL_MODEL_MATCH(70, hsw_rapl_init), /* Haswell GT3e */
Kan Liang4b6e2572016-03-19 00:20:50 -0700796 X86_RAPL_MODEL_MATCH(61, hsw_rapl_init), /* Broadwell */
797 X86_RAPL_MODEL_MATCH(71, hsw_rapl_init), /* Broadwell-H */
798 X86_RAPL_MODEL_MATCH(45, snbep_rapl_init), /* Sandy Bridge-EP */
799 X86_RAPL_MODEL_MATCH(62, snbep_rapl_init), /* IvyTown */
800 X86_RAPL_MODEL_MATCH(87, knl_rapl_init), /* Knights Landing */
Srinivas Pandruvadadcee75b2016-04-17 15:03:00 -0700801 X86_RAPL_MODEL_MATCH(78, skl_rapl_init), /* Skylake */
802 X86_RAPL_MODEL_MATCH(94, skl_rapl_init), /* Skylake H/S */
Kan Liang4b6e2572016-03-19 00:20:50 -0700803 {},
804};
805
806MODULE_DEVICE_TABLE(x86cpu, rapl_cpu_match);
807
Stephane Eranian4788e5b2013-11-12 17:58:50 +0100808static int __init rapl_pmu_init(void)
809{
Kan Liang4b6e2572016-03-19 00:20:50 -0700810 const struct x86_cpu_id *id;
811 struct intel_rapl_init_fun *rapl_init;
812 bool apply_quirk;
Thomas Gleixner7162b8f2016-02-22 22:19:24 +0000813 int ret;
Stephane Eranian4788e5b2013-11-12 17:58:50 +0100814
Kan Liang4b6e2572016-03-19 00:20:50 -0700815 id = x86_match_cpu(rapl_cpu_match);
816 if (!id)
Thomas Gleixner55f28902016-02-22 22:19:21 +0000817 return -ENODEV;
Stephane Eranian4788e5b2013-11-12 17:58:50 +0100818
Kan Liang4b6e2572016-03-19 00:20:50 -0700819 rapl_init = (struct intel_rapl_init_fun *)id->driver_data;
820 apply_quirk = rapl_init->apply_quirk;
821 rapl_cntr_mask = rapl_init->cntr_mask;
822 rapl_pmu_events_group.attrs = rapl_init->attrs;
Thomas Gleixner55f28902016-02-22 22:19:21 +0000823
Borislav Petkov7a869802016-03-08 17:40:41 +0100824 ret = rapl_check_hw_unit(apply_quirk);
Jacob Pan64552392015-03-26 14:28:45 -0700825 if (ret)
826 return ret;
Srivatsa S. Bhatfd537e52014-03-11 02:08:09 +0530827
Thomas Gleixner9de8d682016-02-22 22:19:26 +0000828 ret = init_rapl_pmus();
829 if (ret)
830 return ret;
831
Srivatsa S. Bhatfd537e52014-03-11 02:08:09 +0530832 cpu_notifier_register_begin();
Stephane Eranian4788e5b2013-11-12 17:58:50 +0100833
Thomas Gleixner7162b8f2016-02-22 22:19:24 +0000834 ret = rapl_prepare_cpus();
835 if (ret)
836 goto out;
Stephane Eranian4788e5b2013-11-12 17:58:50 +0100837
Thomas Gleixner9de8d682016-02-22 22:19:26 +0000838 ret = perf_pmu_register(&rapl_pmus->pmu, "power", -1);
Thomas Gleixner512089d2016-02-22 22:19:23 +0000839 if (ret)
Thomas Gleixner55f28902016-02-22 22:19:21 +0000840 goto out;
Stephane Eranian4788e5b2013-11-12 17:58:50 +0100841
Kan Liang4b6e2572016-03-19 00:20:50 -0700842 __register_cpu_notifier(&rapl_cpu_nb);
Thomas Gleixner75c70032016-02-22 22:19:22 +0000843 cpu_notifier_register_done();
Thomas Gleixner512089d2016-02-22 22:19:23 +0000844 rapl_advertise();
Stephane Eranian4788e5b2013-11-12 17:58:50 +0100845 return 0;
Thomas Gleixner55f28902016-02-22 22:19:21 +0000846
847out:
Thomas Gleixner512089d2016-02-22 22:19:23 +0000848 pr_warn("Initialization failed (%d), disabled\n", ret);
Thomas Gleixner55f28902016-02-22 22:19:21 +0000849 cleanup_rapl_pmus();
850 cpu_notifier_register_done();
851 return ret;
Stephane Eranian4788e5b2013-11-12 17:58:50 +0100852}
Kan Liang4b6e2572016-03-19 00:20:50 -0700853module_init(rapl_pmu_init);
854
855static void __exit intel_rapl_exit(void)
856{
857 cpu_notifier_register_begin();
858 __unregister_cpu_notifier(&rapl_cpu_nb);
859 perf_pmu_unregister(&rapl_pmus->pmu);
860 cleanup_rapl_pmus();
861 cpu_notifier_register_done();
862}
863module_exit(intel_rapl_exit);