blob: 6255ede56174e53ce4889426c2a879598e3a74d8 [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
Richard Cochran8b5b7732016-07-13 17:16:15 +0000558static int rapl_cpu_offline(unsigned int cpu)
Stephane Eranian4788e5b2013-11-12 17:58:50 +0100559{
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))
Richard Cochran8b5b7732016-07-13 17:16:15 +0000565 return 0;
Thomas Gleixner9de8d682016-02-22 22:19:26 +0000566
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 }
Richard Cochran8b5b7732016-07-13 17:16:15 +0000577 return 0;
Stephane Eranian4788e5b2013-11-12 17:58:50 +0100578}
579
Richard Cochran8b5b7732016-07-13 17:16:15 +0000580static int rapl_cpu_online(unsigned int cpu)
Stephane Eranian4788e5b2013-11-12 17:58:50 +0100581{
Thomas Gleixner9de8d682016-02-22 22:19:26 +0000582 struct rapl_pmu *pmu = cpu_to_rapl_pmu(cpu);
583 int target;
Stephane Eranian4788e5b2013-11-12 17:58:50 +0100584
Thomas Gleixner9de8d682016-02-22 22:19:26 +0000585 /*
586 * Check if there is an online cpu in the package which collects rapl
587 * events already.
588 */
589 target = cpumask_any_and(&rapl_cpu_mask, topology_core_cpumask(cpu));
590 if (target < nr_cpu_ids)
Richard Cochran8b5b7732016-07-13 17:16:15 +0000591 return 0;
Thomas Gleixner9de8d682016-02-22 22:19:26 +0000592
Stephane Eranian4788e5b2013-11-12 17:58:50 +0100593 cpumask_set_cpu(cpu, &rapl_cpu_mask);
Thomas Gleixner9de8d682016-02-22 22:19:26 +0000594 pmu->cpu = cpu;
Richard Cochran8b5b7732016-07-13 17:16:15 +0000595 return 0;
Stephane Eranian4788e5b2013-11-12 17:58:50 +0100596}
597
Richard Cochran8b5b7732016-07-13 17:16:15 +0000598static int rapl_cpu_prepare(unsigned int cpu)
Stephane Eranian4788e5b2013-11-12 17:58:50 +0100599{
Thomas Gleixner9de8d682016-02-22 22:19:26 +0000600 struct rapl_pmu *pmu = cpu_to_rapl_pmu(cpu);
Stephane Eranian4788e5b2013-11-12 17:58:50 +0100601
602 if (pmu)
603 return 0;
604
Stephane Eranian4788e5b2013-11-12 17:58:50 +0100605 pmu = kzalloc_node(sizeof(*pmu), GFP_KERNEL, cpu_to_node(cpu));
606 if (!pmu)
Thomas Gleixner9de8d682016-02-22 22:19:26 +0000607 return -ENOMEM;
608
Thomas Gleixnera2087492016-02-22 22:19:25 +0000609 raw_spin_lock_init(&pmu->lock);
Stephane Eranian4788e5b2013-11-12 17:58:50 +0100610 INIT_LIST_HEAD(&pmu->active_list);
Thomas Gleixner9de8d682016-02-22 22:19:26 +0000611 pmu->pmu = &rapl_pmus->pmu;
Thomas Gleixner75c70032016-02-22 22:19:22 +0000612 pmu->timer_interval = ms_to_ktime(rapl_timer_ms);
Thomas Gleixner9de8d682016-02-22 22:19:26 +0000613 pmu->cpu = -1;
Stephane Eranian65661f92013-11-12 17:58:51 +0100614 rapl_hrtimer_init(pmu);
Thomas Gleixner9de8d682016-02-22 22:19:26 +0000615 rapl_pmus->pmus[topology_logical_package_id(cpu)] = pmu;
Stephane Eranian4788e5b2013-11-12 17:58:50 +0100616 return 0;
617}
618
Borislav Petkov7a869802016-03-08 17:40:41 +0100619static int rapl_check_hw_unit(bool apply_quirk)
Jacob Pan64552392015-03-26 14:28:45 -0700620{
621 u64 msr_rapl_power_unit_bits;
622 int i;
623
624 /* protect rdmsrl() to handle virtualization */
625 if (rdmsrl_safe(MSR_RAPL_POWER_UNIT, &msr_rapl_power_unit_bits))
626 return -1;
627 for (i = 0; i < NR_RAPL_DOMAINS; i++)
628 rapl_hw_unit[i] = (msr_rapl_power_unit_bits >> 8) & 0x1FULL;
629
Borislav Petkov7a869802016-03-08 17:40:41 +0100630 /*
631 * DRAM domain on HSW server and KNL has fixed energy unit which can be
632 * different than the unit from power unit MSR. See
633 * "Intel Xeon Processor E5-1600 and E5-2600 v3 Product Families, V2
634 * of 2. Datasheet, September 2014, Reference Number: 330784-001 "
635 */
636 if (apply_quirk)
637 rapl_hw_unit[RAPL_IDX_RAM_NRG_STAT] = 16;
Thomas Gleixner75c70032016-02-22 22:19:22 +0000638
639 /*
640 * Calculate the timer rate:
641 * Use reference of 200W for scaling the timeout to avoid counter
642 * overflows. 200W = 200 Joules/sec
643 * Divide interval by 2 to avoid lockstep (2 * 100)
644 * if hw unit is 32, then we use 2 ms 1/200/2
645 */
646 rapl_timer_ms = 2;
647 if (rapl_hw_unit[0] < 32) {
648 rapl_timer_ms = (1000 / (2 * 100));
649 rapl_timer_ms *= (1ULL << (32 - rapl_hw_unit[0] - 1));
650 }
Jacob Pan64552392015-03-26 14:28:45 -0700651 return 0;
652}
653
Thomas Gleixner512089d2016-02-22 22:19:23 +0000654static void __init rapl_advertise(void)
655{
656 int i;
657
658 pr_info("API unit is 2^-32 Joules, %d fixed counters, %llu ms ovfl timer\n",
659 hweight32(rapl_cntr_mask), rapl_timer_ms);
660
661 for (i = 0; i < NR_RAPL_DOMAINS; i++) {
662 if (rapl_cntr_mask & (1 << i)) {
663 pr_info("hw unit of domain %s 2^-%d Joules\n",
664 rapl_domain_names[i], rapl_hw_unit[i]);
665 }
666 }
667}
668
Kan Liang4b6e2572016-03-19 00:20:50 -0700669static void cleanup_rapl_pmus(void)
Thomas Gleixner55f28902016-02-22 22:19:21 +0000670{
Thomas Gleixner9de8d682016-02-22 22:19:26 +0000671 int i;
Thomas Gleixner55f28902016-02-22 22:19:21 +0000672
Thomas Gleixner9de8d682016-02-22 22:19:26 +0000673 for (i = 0; i < rapl_pmus->maxpkg; i++)
Vincent Stehlé275ae412016-05-24 16:53:49 +0200674 kfree(rapl_pmus->pmus[i]);
Thomas Gleixner9de8d682016-02-22 22:19:26 +0000675 kfree(rapl_pmus);
676}
677
678static int __init init_rapl_pmus(void)
679{
680 int maxpkg = topology_max_packages();
681 size_t size;
682
683 size = sizeof(*rapl_pmus) + maxpkg * sizeof(struct rapl_pmu *);
684 rapl_pmus = kzalloc(size, GFP_KERNEL);
685 if (!rapl_pmus)
686 return -ENOMEM;
687
688 rapl_pmus->maxpkg = maxpkg;
689 rapl_pmus->pmu.attr_groups = rapl_attr_groups;
690 rapl_pmus->pmu.task_ctx_nr = perf_invalid_context;
691 rapl_pmus->pmu.event_init = rapl_pmu_event_init;
692 rapl_pmus->pmu.add = rapl_pmu_event_add;
693 rapl_pmus->pmu.del = rapl_pmu_event_del;
694 rapl_pmus->pmu.start = rapl_pmu_event_start;
695 rapl_pmus->pmu.stop = rapl_pmu_event_stop;
696 rapl_pmus->pmu.read = rapl_pmu_event_read;
697 return 0;
Thomas Gleixner55f28902016-02-22 22:19:21 +0000698}
699
Kan Liang4b6e2572016-03-19 00:20:50 -0700700#define X86_RAPL_MODEL_MATCH(model, init) \
701 { X86_VENDOR_INTEL, 6, model, X86_FEATURE_ANY, (unsigned long)&init }
702
703struct intel_rapl_init_fun {
704 bool apply_quirk;
705 int cntr_mask;
706 struct attribute **attrs;
Stephane Eranian4788e5b2013-11-12 17:58:50 +0100707};
708
Kan Liang4b6e2572016-03-19 00:20:50 -0700709static const struct intel_rapl_init_fun snb_rapl_init __initconst = {
710 .apply_quirk = false,
711 .cntr_mask = RAPL_IDX_CLN,
712 .attrs = rapl_events_cln_attr,
713};
714
715static const struct intel_rapl_init_fun hsx_rapl_init __initconst = {
716 .apply_quirk = true,
717 .cntr_mask = RAPL_IDX_SRV,
718 .attrs = rapl_events_srv_attr,
719};
720
721static const struct intel_rapl_init_fun hsw_rapl_init __initconst = {
722 .apply_quirk = false,
723 .cntr_mask = RAPL_IDX_HSW,
724 .attrs = rapl_events_hsw_attr,
725};
726
727static const struct intel_rapl_init_fun snbep_rapl_init __initconst = {
728 .apply_quirk = false,
729 .cntr_mask = RAPL_IDX_SRV,
730 .attrs = rapl_events_srv_attr,
731};
732
733static const struct intel_rapl_init_fun knl_rapl_init __initconst = {
734 .apply_quirk = true,
735 .cntr_mask = RAPL_IDX_KNL,
736 .attrs = rapl_events_knl_attr,
737};
738
Srinivas Pandruvadadcee75b2016-04-17 15:03:00 -0700739static const struct intel_rapl_init_fun skl_rapl_init __initconst = {
740 .apply_quirk = false,
741 .cntr_mask = RAPL_IDX_SKL_CLN,
742 .attrs = rapl_events_skl_attr,
743};
744
Kan Liang4b6e2572016-03-19 00:20:50 -0700745static const struct x86_cpu_id rapl_cpu_match[] __initconst = {
746 X86_RAPL_MODEL_MATCH(42, snb_rapl_init), /* Sandy Bridge */
Peter Zijlstrac416e5a2016-04-21 15:14:17 +0200747 X86_RAPL_MODEL_MATCH(45, snbep_rapl_init), /* Sandy Bridge-EP */
748
Kan Liang4b6e2572016-03-19 00:20:50 -0700749 X86_RAPL_MODEL_MATCH(58, snb_rapl_init), /* Ivy Bridge */
Peter Zijlstrac416e5a2016-04-21 15:14:17 +0200750 X86_RAPL_MODEL_MATCH(62, snbep_rapl_init), /* IvyTown */
751
Kan Liang4b6e2572016-03-19 00:20:50 -0700752 X86_RAPL_MODEL_MATCH(60, hsw_rapl_init), /* Haswell */
Peter Zijlstrac416e5a2016-04-21 15:14:17 +0200753 X86_RAPL_MODEL_MATCH(63, hsx_rapl_init), /* Haswell-Server */
Kan Liang4b6e2572016-03-19 00:20:50 -0700754 X86_RAPL_MODEL_MATCH(69, hsw_rapl_init), /* Haswell-Celeron */
Ingo Molnar65cbbd02016-04-23 14:12:10 +0200755 X86_RAPL_MODEL_MATCH(70, hsw_rapl_init), /* Haswell GT3e */
Peter Zijlstrac416e5a2016-04-21 15:14:17 +0200756
Kan Liang4b6e2572016-03-19 00:20:50 -0700757 X86_RAPL_MODEL_MATCH(61, hsw_rapl_init), /* Broadwell */
758 X86_RAPL_MODEL_MATCH(71, hsw_rapl_init), /* Broadwell-H */
Peter Zijlstrac416e5a2016-04-21 15:14:17 +0200759 X86_RAPL_MODEL_MATCH(79, hsx_rapl_init), /* Broadwell-Server */
Peter Zijlstra31b84312016-04-21 15:15:47 +0200760 X86_RAPL_MODEL_MATCH(86, hsx_rapl_init), /* Broadwell Xeon D */
Peter Zijlstrac416e5a2016-04-21 15:14:17 +0200761
Kan Liang4b6e2572016-03-19 00:20:50 -0700762 X86_RAPL_MODEL_MATCH(87, knl_rapl_init), /* Knights Landing */
Peter Zijlstrac416e5a2016-04-21 15:14:17 +0200763
Srinivas Pandruvadadcee75b2016-04-17 15:03:00 -0700764 X86_RAPL_MODEL_MATCH(78, skl_rapl_init), /* Skylake */
765 X86_RAPL_MODEL_MATCH(94, skl_rapl_init), /* Skylake H/S */
Kan Liang4b6e2572016-03-19 00:20:50 -0700766 {},
767};
768
769MODULE_DEVICE_TABLE(x86cpu, rapl_cpu_match);
770
Stephane Eranian4788e5b2013-11-12 17:58:50 +0100771static int __init rapl_pmu_init(void)
772{
Kan Liang4b6e2572016-03-19 00:20:50 -0700773 const struct x86_cpu_id *id;
774 struct intel_rapl_init_fun *rapl_init;
775 bool apply_quirk;
Thomas Gleixner7162b8f2016-02-22 22:19:24 +0000776 int ret;
Stephane Eranian4788e5b2013-11-12 17:58:50 +0100777
Kan Liang4b6e2572016-03-19 00:20:50 -0700778 id = x86_match_cpu(rapl_cpu_match);
779 if (!id)
Thomas Gleixner55f28902016-02-22 22:19:21 +0000780 return -ENODEV;
Stephane Eranian4788e5b2013-11-12 17:58:50 +0100781
Kan Liang4b6e2572016-03-19 00:20:50 -0700782 rapl_init = (struct intel_rapl_init_fun *)id->driver_data;
783 apply_quirk = rapl_init->apply_quirk;
784 rapl_cntr_mask = rapl_init->cntr_mask;
785 rapl_pmu_events_group.attrs = rapl_init->attrs;
Thomas Gleixner55f28902016-02-22 22:19:21 +0000786
Borislav Petkov7a869802016-03-08 17:40:41 +0100787 ret = rapl_check_hw_unit(apply_quirk);
Jacob Pan64552392015-03-26 14:28:45 -0700788 if (ret)
789 return ret;
Srivatsa S. Bhatfd537e52014-03-11 02:08:09 +0530790
Thomas Gleixner9de8d682016-02-22 22:19:26 +0000791 ret = init_rapl_pmus();
792 if (ret)
793 return ret;
794
Richard Cochran8b5b7732016-07-13 17:16:15 +0000795 /*
796 * Install callbacks. Core will call them for each online cpu.
797 */
Stephane Eranian4788e5b2013-11-12 17:58:50 +0100798
Richard Cochran8b5b7732016-07-13 17:16:15 +0000799 ret = cpuhp_setup_state(CPUHP_PERF_X86_RAPL_PREP, "PERF_X86_RAPL_PREP",
800 rapl_cpu_prepare, NULL);
Thomas Gleixner7162b8f2016-02-22 22:19:24 +0000801 if (ret)
802 goto out;
Stephane Eranian4788e5b2013-11-12 17:58:50 +0100803
Richard Cochran8b5b7732016-07-13 17:16:15 +0000804 ret = cpuhp_setup_state(CPUHP_AP_PERF_X86_RAPL_ONLINE,
805 "AP_PERF_X86_RAPL_ONLINE",
806 rapl_cpu_online, rapl_cpu_offline);
807 if (ret)
808 goto out1;
809
Thomas Gleixner9de8d682016-02-22 22:19:26 +0000810 ret = perf_pmu_register(&rapl_pmus->pmu, "power", -1);
Thomas Gleixner512089d2016-02-22 22:19:23 +0000811 if (ret)
Richard Cochran8b5b7732016-07-13 17:16:15 +0000812 goto out2;
Stephane Eranian4788e5b2013-11-12 17:58:50 +0100813
Thomas Gleixner512089d2016-02-22 22:19:23 +0000814 rapl_advertise();
Stephane Eranian4788e5b2013-11-12 17:58:50 +0100815 return 0;
Thomas Gleixner55f28902016-02-22 22:19:21 +0000816
Richard Cochran8b5b7732016-07-13 17:16:15 +0000817out2:
818 cpuhp_remove_state(CPUHP_AP_PERF_X86_RAPL_ONLINE);
819out1:
820 cpuhp_remove_state(CPUHP_PERF_X86_RAPL_PREP);
Thomas Gleixner55f28902016-02-22 22:19:21 +0000821out:
Thomas Gleixner512089d2016-02-22 22:19:23 +0000822 pr_warn("Initialization failed (%d), disabled\n", ret);
Thomas Gleixner55f28902016-02-22 22:19:21 +0000823 cleanup_rapl_pmus();
Thomas Gleixner55f28902016-02-22 22:19:21 +0000824 return ret;
Stephane Eranian4788e5b2013-11-12 17:58:50 +0100825}
Kan Liang4b6e2572016-03-19 00:20:50 -0700826module_init(rapl_pmu_init);
827
828static void __exit intel_rapl_exit(void)
829{
Richard Cochran8b5b7732016-07-13 17:16:15 +0000830 cpuhp_remove_state_nocalls(CPUHP_AP_PERF_X86_RAPL_ONLINE);
831 cpuhp_remove_state_nocalls(CPUHP_PERF_X86_RAPL_PREP);
Kan Liang4b6e2572016-03-19 00:20:50 -0700832 perf_pmu_unregister(&rapl_pmus->pmu);
833 cleanup_rapl_pmus();
Kan Liang4b6e2572016-03-19 00:20:50 -0700834}
835module_exit(intel_rapl_exit);