xref: /linux/arch/x86/events/rapl.c (revision f96a974170b749e3a56844e25b31d46a7233b6f6)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Support Intel/AMD RAPL energy consumption counters
4  * Copyright (C) 2013 Google, Inc., Stephane Eranian
5  *
6  * Intel RAPL interface is specified in the IA-32 Manual Vol3b
7  * section 14.7.1 (September 2013)
8  *
9  * AMD RAPL interface for Fam17h is described in the public PPR:
10  * https://bugzilla.kernel.org/show_bug.cgi?id=206537
11  *
12  * RAPL provides more controls than just reporting energy consumption
13  * however here we only expose the 3 energy consumption free running
14  * counters (pp0, pkg, dram).
15  *
16  * Each of those counters increments in a power unit defined by the
17  * RAPL_POWER_UNIT MSR. On SandyBridge, this unit is 1/(2^16) Joules
18  * but it can vary.
19  *
20  * Counter to rapl events mappings:
21  *
22  *  pp0 counter: consumption of all physical cores (power plane 0)
23  * 	  event: rapl_energy_cores
24  *    perf code: 0x1
25  *
26  *  pkg counter: consumption of the whole processor package
27  *	  event: rapl_energy_pkg
28  *    perf code: 0x2
29  *
30  * dram counter: consumption of the dram domain (servers only)
31  *	  event: rapl_energy_dram
32  *    perf code: 0x3
33  *
34  * gpu counter: consumption of the builtin-gpu domain (client only)
35  *	  event: rapl_energy_gpu
36  *    perf code: 0x4
37  *
38  *  psys counter: consumption of the builtin-psys domain (client only)
39  *	  event: rapl_energy_psys
40  *    perf code: 0x5
41  *
42  *  core counter: consumption of a single physical core
43  *	  event: rapl_energy_core (power_core PMU)
44  *    perf code: 0x1
45  *
46  * We manage those counters as free running (read-only). They may be
47  * use simultaneously by other tools, such as turbostat.
48  *
49  * The events only support system-wide mode counting. There is no
50  * sampling support because it does not make sense and is not
51  * supported by the RAPL hardware.
52  *
53  * Because we want to avoid floating-point operations in the kernel,
54  * the events are all reported in fixed point arithmetic (32.32).
55  * Tools must adjust the counts to convert them to Watts using
56  * the duration of the measurement. Tools may use a function such as
57  * ldexp(raw_count, -32);
58  */
59 
60 #define pr_fmt(fmt) "RAPL PMU: " fmt
61 
62 #include <linux/module.h>
63 #include <linux/slab.h>
64 #include <linux/perf_event.h>
65 #include <linux/nospec.h>
66 #include <asm/cpu_device_id.h>
67 #include <asm/intel-family.h>
68 #include "perf_event.h"
69 #include "probe.h"
70 
71 MODULE_DESCRIPTION("Support Intel/AMD RAPL energy consumption counters");
72 MODULE_LICENSE("GPL");
73 
74 /*
75  * RAPL energy status counters
76  */
77 enum perf_rapl_pkg_events {
78 	PERF_RAPL_PP0 = 0,		/* all cores */
79 	PERF_RAPL_PKG,			/* entire package */
80 	PERF_RAPL_RAM,			/* DRAM */
81 	PERF_RAPL_PP1,			/* gpu */
82 	PERF_RAPL_PSYS,			/* psys */
83 
84 	PERF_RAPL_PKG_EVENTS_MAX,
85 	NR_RAPL_PKG_DOMAINS = PERF_RAPL_PKG_EVENTS_MAX,
86 };
87 
88 #define PERF_RAPL_CORE			0		/* single core */
89 #define PERF_RAPL_CORE_EVENTS_MAX	1
90 #define NR_RAPL_CORE_DOMAINS		PERF_RAPL_CORE_EVENTS_MAX
91 
92 static const char *const rapl_pkg_domain_names[NR_RAPL_PKG_DOMAINS] __initconst = {
93 	"pp0-core",
94 	"package",
95 	"dram",
96 	"pp1-gpu",
97 	"psys",
98 };
99 
100 static const char *const rapl_core_domain_name __initconst = "core";
101 
102 /*
103  * event code: LSB 8 bits, passed in attr->config
104  * any other bit is reserved
105  */
106 #define RAPL_EVENT_MASK	0xFFULL
107 #define RAPL_CNTR_WIDTH 32
108 
109 #define RAPL_EVENT_ATTR_STR(_name, v, str)					\
110 static struct perf_pmu_events_attr event_attr_##v = {				\
111 	.attr		= __ATTR(_name, 0444, perf_event_sysfs_show, NULL),	\
112 	.id		= 0,							\
113 	.event_str	= str,							\
114 };
115 
116 /*
117  * RAPL Package energy counter scope:
118  * 1. AMD/HYGON platforms have a per-PKG package energy counter
119  * 2. For Intel platforms
120  *	2.1. CLX-AP is multi-die and its RAPL MSRs are die-scope
121  *	2.2. Other Intel platforms are single die systems so the scope can be
122  *	     considered as either pkg-scope or die-scope, and we are considering
123  *	     them as die-scope.
124  */
125 #define rapl_pkg_pmu_is_pkg_scope()				\
126 	(boot_cpu_data.x86_vendor == X86_VENDOR_AMD ||	\
127 	 boot_cpu_data.x86_vendor == X86_VENDOR_HYGON)
128 
129 struct rapl_pmu {
130 	raw_spinlock_t		lock;
131 	int			n_active;
132 	int			cpu;
133 	struct list_head	active_list;
134 	struct pmu		*pmu;
135 	ktime_t			timer_interval;
136 	struct hrtimer		hrtimer;
137 };
138 
139 struct rapl_pmus {
140 	struct pmu		pmu;
141 	unsigned int		nr_rapl_pmu;
142 	unsigned int		cntr_mask;
143 	struct rapl_pmu		*rapl_pmu[] __counted_by(nr_rapl_pmu);
144 };
145 
146 enum rapl_unit_quirk {
147 	RAPL_UNIT_QUIRK_NONE,
148 	RAPL_UNIT_QUIRK_INTEL_HSW,
149 	RAPL_UNIT_QUIRK_INTEL_SPR,
150 };
151 
152 struct rapl_model {
153 	struct perf_msr *rapl_pkg_msrs;
154 	struct perf_msr *rapl_core_msrs;
155 	unsigned long	pkg_events;
156 	unsigned long	core_events;
157 	unsigned int	msr_power_unit;
158 	enum rapl_unit_quirk	unit_quirk;
159 };
160 
161  /* 1/2^hw_unit Joule */
162 static int rapl_pkg_hw_unit[NR_RAPL_PKG_DOMAINS] __read_mostly;
163 static int rapl_core_hw_unit __read_mostly;
164 static struct rapl_pmus *rapl_pmus_pkg;
165 static struct rapl_pmus *rapl_pmus_core;
166 static u64 rapl_timer_ms;
167 static struct rapl_model *rapl_model;
168 
169 /*
170  * Helper function to get the correct topology id according to the
171  * RAPL PMU scope.
172  */
173 static inline unsigned int get_rapl_pmu_idx(int cpu, int scope)
174 {
175 	/*
176 	 * Returns unsigned int, which converts the '-1' return value
177 	 * (for non-existent mappings in topology map) to UINT_MAX, so
178 	 * the error check in the caller is simplified.
179 	 */
180 	switch (scope) {
181 	case PERF_PMU_SCOPE_PKG:
182 		return topology_logical_package_id(cpu);
183 	case PERF_PMU_SCOPE_DIE:
184 		return topology_logical_die_id(cpu);
185 	case PERF_PMU_SCOPE_CORE:
186 		return topology_logical_core_id(cpu);
187 	default:
188 		return -EINVAL;
189 	}
190 }
191 
192 static inline u64 rapl_read_counter(struct perf_event *event)
193 {
194 	u64 raw;
195 	rdmsrl(event->hw.event_base, raw);
196 	return raw;
197 }
198 
199 static inline u64 rapl_scale(u64 v, struct perf_event *event)
200 {
201 	int hw_unit = rapl_pkg_hw_unit[event->hw.config - 1];
202 
203 	if (event->pmu->scope == PERF_PMU_SCOPE_CORE)
204 		hw_unit = rapl_core_hw_unit;
205 
206 	/*
207 	 * scale delta to smallest unit (1/2^32)
208 	 * users must then scale back: count * 1/(1e9*2^32) to get Joules
209 	 * or use ldexp(count, -32).
210 	 * Watts = Joules/Time delta
211 	 */
212 	return v << (32 - hw_unit);
213 }
214 
215 static u64 rapl_event_update(struct perf_event *event)
216 {
217 	struct hw_perf_event *hwc = &event->hw;
218 	u64 prev_raw_count, new_raw_count;
219 	s64 delta, sdelta;
220 	int shift = RAPL_CNTR_WIDTH;
221 
222 	prev_raw_count = local64_read(&hwc->prev_count);
223 	do {
224 		rdmsrl(event->hw.event_base, new_raw_count);
225 	} while (!local64_try_cmpxchg(&hwc->prev_count,
226 				      &prev_raw_count, new_raw_count));
227 
228 	/*
229 	 * Now we have the new raw value and have updated the prev
230 	 * timestamp already. We can now calculate the elapsed delta
231 	 * (event-)time and add that to the generic event.
232 	 *
233 	 * Careful, not all hw sign-extends above the physical width
234 	 * of the count.
235 	 */
236 	delta = (new_raw_count << shift) - (prev_raw_count << shift);
237 	delta >>= shift;
238 
239 	sdelta = rapl_scale(delta, event);
240 
241 	local64_add(sdelta, &event->count);
242 
243 	return new_raw_count;
244 }
245 
246 static void rapl_start_hrtimer(struct rapl_pmu *pmu)
247 {
248        hrtimer_start(&pmu->hrtimer, pmu->timer_interval,
249 		     HRTIMER_MODE_REL_PINNED);
250 }
251 
252 static enum hrtimer_restart rapl_hrtimer_handle(struct hrtimer *hrtimer)
253 {
254 	struct rapl_pmu *rapl_pmu = container_of(hrtimer, struct rapl_pmu, hrtimer);
255 	struct perf_event *event;
256 	unsigned long flags;
257 
258 	if (!rapl_pmu->n_active)
259 		return HRTIMER_NORESTART;
260 
261 	raw_spin_lock_irqsave(&rapl_pmu->lock, flags);
262 
263 	list_for_each_entry(event, &rapl_pmu->active_list, active_entry)
264 		rapl_event_update(event);
265 
266 	raw_spin_unlock_irqrestore(&rapl_pmu->lock, flags);
267 
268 	hrtimer_forward_now(hrtimer, rapl_pmu->timer_interval);
269 
270 	return HRTIMER_RESTART;
271 }
272 
273 static void rapl_hrtimer_init(struct rapl_pmu *rapl_pmu)
274 {
275 	struct hrtimer *hr = &rapl_pmu->hrtimer;
276 
277 	hrtimer_init(hr, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
278 	hr->function = rapl_hrtimer_handle;
279 }
280 
281 static void __rapl_pmu_event_start(struct rapl_pmu *rapl_pmu,
282 				   struct perf_event *event)
283 {
284 	if (WARN_ON_ONCE(!(event->hw.state & PERF_HES_STOPPED)))
285 		return;
286 
287 	event->hw.state = 0;
288 
289 	list_add_tail(&event->active_entry, &rapl_pmu->active_list);
290 
291 	local64_set(&event->hw.prev_count, rapl_read_counter(event));
292 
293 	rapl_pmu->n_active++;
294 	if (rapl_pmu->n_active == 1)
295 		rapl_start_hrtimer(rapl_pmu);
296 }
297 
298 static void rapl_pmu_event_start(struct perf_event *event, int mode)
299 {
300 	struct rapl_pmu *rapl_pmu = event->pmu_private;
301 	unsigned long flags;
302 
303 	raw_spin_lock_irqsave(&rapl_pmu->lock, flags);
304 	__rapl_pmu_event_start(rapl_pmu, event);
305 	raw_spin_unlock_irqrestore(&rapl_pmu->lock, flags);
306 }
307 
308 static void rapl_pmu_event_stop(struct perf_event *event, int mode)
309 {
310 	struct rapl_pmu *rapl_pmu = event->pmu_private;
311 	struct hw_perf_event *hwc = &event->hw;
312 	unsigned long flags;
313 
314 	raw_spin_lock_irqsave(&rapl_pmu->lock, flags);
315 
316 	/* mark event as deactivated and stopped */
317 	if (!(hwc->state & PERF_HES_STOPPED)) {
318 		WARN_ON_ONCE(rapl_pmu->n_active <= 0);
319 		rapl_pmu->n_active--;
320 		if (rapl_pmu->n_active == 0)
321 			hrtimer_cancel(&rapl_pmu->hrtimer);
322 
323 		list_del(&event->active_entry);
324 
325 		WARN_ON_ONCE(hwc->state & PERF_HES_STOPPED);
326 		hwc->state |= PERF_HES_STOPPED;
327 	}
328 
329 	/* check if update of sw counter is necessary */
330 	if ((mode & PERF_EF_UPDATE) && !(hwc->state & PERF_HES_UPTODATE)) {
331 		/*
332 		 * Drain the remaining delta count out of a event
333 		 * that we are disabling:
334 		 */
335 		rapl_event_update(event);
336 		hwc->state |= PERF_HES_UPTODATE;
337 	}
338 
339 	raw_spin_unlock_irqrestore(&rapl_pmu->lock, flags);
340 }
341 
342 static int rapl_pmu_event_add(struct perf_event *event, int mode)
343 {
344 	struct rapl_pmu *rapl_pmu = event->pmu_private;
345 	struct hw_perf_event *hwc = &event->hw;
346 	unsigned long flags;
347 
348 	raw_spin_lock_irqsave(&rapl_pmu->lock, flags);
349 
350 	hwc->state = PERF_HES_UPTODATE | PERF_HES_STOPPED;
351 
352 	if (mode & PERF_EF_START)
353 		__rapl_pmu_event_start(rapl_pmu, event);
354 
355 	raw_spin_unlock_irqrestore(&rapl_pmu->lock, flags);
356 
357 	return 0;
358 }
359 
360 static void rapl_pmu_event_del(struct perf_event *event, int flags)
361 {
362 	rapl_pmu_event_stop(event, PERF_EF_UPDATE);
363 }
364 
365 static int rapl_pmu_event_init(struct perf_event *event)
366 {
367 	u64 cfg = event->attr.config & RAPL_EVENT_MASK;
368 	int bit, rapl_pmus_scope, ret = 0;
369 	struct rapl_pmu *rapl_pmu;
370 	unsigned int rapl_pmu_idx;
371 	struct rapl_pmus *rapl_pmus;
372 
373 	/* unsupported modes and filters */
374 	if (event->attr.sample_period) /* no sampling */
375 		return -EINVAL;
376 
377 	/* check only supported bits are set */
378 	if (event->attr.config & ~RAPL_EVENT_MASK)
379 		return -EINVAL;
380 
381 	if (event->cpu < 0)
382 		return -EINVAL;
383 
384 	rapl_pmus = container_of(event->pmu, struct rapl_pmus, pmu);
385 	if (!rapl_pmus)
386 		return -EINVAL;
387 	rapl_pmus_scope = rapl_pmus->pmu.scope;
388 
389 	if (rapl_pmus_scope == PERF_PMU_SCOPE_PKG || rapl_pmus_scope == PERF_PMU_SCOPE_DIE) {
390 		/* only look at RAPL package events */
391 		if (event->attr.type != rapl_pmus_pkg->pmu.type)
392 			return -ENOENT;
393 
394 		cfg = array_index_nospec((long)cfg, NR_RAPL_PKG_DOMAINS + 1);
395 		if (!cfg || cfg >= NR_RAPL_PKG_DOMAINS + 1)
396 			return -EINVAL;
397 
398 		bit = cfg - 1;
399 		event->hw.event_base = rapl_model->rapl_pkg_msrs[bit].msr;
400 	} else if (rapl_pmus_scope == PERF_PMU_SCOPE_CORE) {
401 		/* only look at RAPL core events */
402 		if (event->attr.type != rapl_pmus_core->pmu.type)
403 			return -ENOENT;
404 
405 		cfg = array_index_nospec((long)cfg, NR_RAPL_CORE_DOMAINS + 1);
406 		if (!cfg || cfg >= NR_RAPL_PKG_DOMAINS + 1)
407 			return -EINVAL;
408 
409 		bit = cfg - 1;
410 		event->hw.event_base = rapl_model->rapl_core_msrs[bit].msr;
411 	} else
412 		return -EINVAL;
413 
414 	/* check event supported */
415 	if (!(rapl_pmus->cntr_mask & (1 << bit)))
416 		return -EINVAL;
417 
418 	rapl_pmu_idx = get_rapl_pmu_idx(event->cpu, rapl_pmus_scope);
419 	if (rapl_pmu_idx >= rapl_pmus->nr_rapl_pmu)
420 		return -EINVAL;
421 	/* must be done before validate_group */
422 	rapl_pmu = rapl_pmus->rapl_pmu[rapl_pmu_idx];
423 	if (!rapl_pmu)
424 		return -EINVAL;
425 
426 	event->pmu_private = rapl_pmu;
427 	event->hw.config = cfg;
428 	event->hw.idx = bit;
429 
430 	return ret;
431 }
432 
433 static void rapl_pmu_event_read(struct perf_event *event)
434 {
435 	rapl_event_update(event);
436 }
437 
438 RAPL_EVENT_ATTR_STR(energy-cores, rapl_cores, "event=0x01");
439 RAPL_EVENT_ATTR_STR(energy-pkg  ,   rapl_pkg, "event=0x02");
440 RAPL_EVENT_ATTR_STR(energy-ram  ,   rapl_ram, "event=0x03");
441 RAPL_EVENT_ATTR_STR(energy-gpu  ,   rapl_gpu, "event=0x04");
442 RAPL_EVENT_ATTR_STR(energy-psys,   rapl_psys, "event=0x05");
443 RAPL_EVENT_ATTR_STR(energy-core,   rapl_core, "event=0x01");
444 
445 RAPL_EVENT_ATTR_STR(energy-cores.unit, rapl_cores_unit, "Joules");
446 RAPL_EVENT_ATTR_STR(energy-pkg.unit  ,   rapl_pkg_unit, "Joules");
447 RAPL_EVENT_ATTR_STR(energy-ram.unit  ,   rapl_ram_unit, "Joules");
448 RAPL_EVENT_ATTR_STR(energy-gpu.unit  ,   rapl_gpu_unit, "Joules");
449 RAPL_EVENT_ATTR_STR(energy-psys.unit,   rapl_psys_unit, "Joules");
450 RAPL_EVENT_ATTR_STR(energy-core.unit,   rapl_core_unit, "Joules");
451 
452 /*
453  * we compute in 0.23 nJ increments regardless of MSR
454  */
455 RAPL_EVENT_ATTR_STR(energy-cores.scale, rapl_cores_scale, "2.3283064365386962890625e-10");
456 RAPL_EVENT_ATTR_STR(energy-pkg.scale,     rapl_pkg_scale, "2.3283064365386962890625e-10");
457 RAPL_EVENT_ATTR_STR(energy-ram.scale,     rapl_ram_scale, "2.3283064365386962890625e-10");
458 RAPL_EVENT_ATTR_STR(energy-gpu.scale,     rapl_gpu_scale, "2.3283064365386962890625e-10");
459 RAPL_EVENT_ATTR_STR(energy-psys.scale,   rapl_psys_scale, "2.3283064365386962890625e-10");
460 RAPL_EVENT_ATTR_STR(energy-core.scale,   rapl_core_scale, "2.3283064365386962890625e-10");
461 
462 /*
463  * There are no default events, but we need to create
464  * "events" group (with empty attrs) before updating
465  * it with detected events.
466  */
467 static struct attribute *attrs_empty[] = {
468 	NULL,
469 };
470 
471 static struct attribute_group rapl_pmu_events_group = {
472 	.name = "events",
473 	.attrs = attrs_empty,
474 };
475 
476 PMU_FORMAT_ATTR(event, "config:0-7");
477 static struct attribute *rapl_formats_attr[] = {
478 	&format_attr_event.attr,
479 	NULL,
480 };
481 
482 static struct attribute_group rapl_pmu_format_group = {
483 	.name = "format",
484 	.attrs = rapl_formats_attr,
485 };
486 
487 static const struct attribute_group *rapl_attr_groups[] = {
488 	&rapl_pmu_format_group,
489 	&rapl_pmu_events_group,
490 	NULL,
491 };
492 
493 static const struct attribute_group *rapl_core_attr_groups[] = {
494 	&rapl_pmu_format_group,
495 	&rapl_pmu_events_group,
496 	NULL,
497 };
498 
499 static struct attribute *rapl_events_cores[] = {
500 	EVENT_PTR(rapl_cores),
501 	EVENT_PTR(rapl_cores_unit),
502 	EVENT_PTR(rapl_cores_scale),
503 	NULL,
504 };
505 
506 static struct attribute_group rapl_events_cores_group = {
507 	.name  = "events",
508 	.attrs = rapl_events_cores,
509 };
510 
511 static struct attribute *rapl_events_pkg[] = {
512 	EVENT_PTR(rapl_pkg),
513 	EVENT_PTR(rapl_pkg_unit),
514 	EVENT_PTR(rapl_pkg_scale),
515 	NULL,
516 };
517 
518 static struct attribute_group rapl_events_pkg_group = {
519 	.name  = "events",
520 	.attrs = rapl_events_pkg,
521 };
522 
523 static struct attribute *rapl_events_ram[] = {
524 	EVENT_PTR(rapl_ram),
525 	EVENT_PTR(rapl_ram_unit),
526 	EVENT_PTR(rapl_ram_scale),
527 	NULL,
528 };
529 
530 static struct attribute_group rapl_events_ram_group = {
531 	.name  = "events",
532 	.attrs = rapl_events_ram,
533 };
534 
535 static struct attribute *rapl_events_gpu[] = {
536 	EVENT_PTR(rapl_gpu),
537 	EVENT_PTR(rapl_gpu_unit),
538 	EVENT_PTR(rapl_gpu_scale),
539 	NULL,
540 };
541 
542 static struct attribute_group rapl_events_gpu_group = {
543 	.name  = "events",
544 	.attrs = rapl_events_gpu,
545 };
546 
547 static struct attribute *rapl_events_psys[] = {
548 	EVENT_PTR(rapl_psys),
549 	EVENT_PTR(rapl_psys_unit),
550 	EVENT_PTR(rapl_psys_scale),
551 	NULL,
552 };
553 
554 static struct attribute_group rapl_events_psys_group = {
555 	.name  = "events",
556 	.attrs = rapl_events_psys,
557 };
558 
559 static struct attribute *rapl_events_core[] = {
560 	EVENT_PTR(rapl_core),
561 	EVENT_PTR(rapl_core_unit),
562 	EVENT_PTR(rapl_core_scale),
563 	NULL,
564 };
565 
566 static struct attribute_group rapl_events_core_group = {
567 	.name  = "events",
568 	.attrs = rapl_events_core,
569 };
570 
571 static bool test_msr(int idx, void *data)
572 {
573 	return test_bit(idx, (unsigned long *) data);
574 }
575 
576 /* Only lower 32bits of the MSR represents the energy counter */
577 #define RAPL_MSR_MASK 0xFFFFFFFF
578 
579 static struct perf_msr intel_rapl_msrs[] = {
580 	[PERF_RAPL_PP0]  = { MSR_PP0_ENERGY_STATUS,      &rapl_events_cores_group, test_msr, false, RAPL_MSR_MASK },
581 	[PERF_RAPL_PKG]  = { MSR_PKG_ENERGY_STATUS,      &rapl_events_pkg_group,   test_msr, false, RAPL_MSR_MASK },
582 	[PERF_RAPL_RAM]  = { MSR_DRAM_ENERGY_STATUS,     &rapl_events_ram_group,   test_msr, false, RAPL_MSR_MASK },
583 	[PERF_RAPL_PP1]  = { MSR_PP1_ENERGY_STATUS,      &rapl_events_gpu_group,   test_msr, false, RAPL_MSR_MASK },
584 	[PERF_RAPL_PSYS] = { MSR_PLATFORM_ENERGY_STATUS, &rapl_events_psys_group,  test_msr, false, RAPL_MSR_MASK },
585 };
586 
587 static struct perf_msr intel_rapl_spr_msrs[] = {
588 	[PERF_RAPL_PP0]  = { MSR_PP0_ENERGY_STATUS,      &rapl_events_cores_group, test_msr, false, RAPL_MSR_MASK },
589 	[PERF_RAPL_PKG]  = { MSR_PKG_ENERGY_STATUS,      &rapl_events_pkg_group,   test_msr, false, RAPL_MSR_MASK },
590 	[PERF_RAPL_RAM]  = { MSR_DRAM_ENERGY_STATUS,     &rapl_events_ram_group,   test_msr, false, RAPL_MSR_MASK },
591 	[PERF_RAPL_PP1]  = { MSR_PP1_ENERGY_STATUS,      &rapl_events_gpu_group,   test_msr, false, RAPL_MSR_MASK },
592 	[PERF_RAPL_PSYS] = { MSR_PLATFORM_ENERGY_STATUS, &rapl_events_psys_group,  test_msr, true, RAPL_MSR_MASK },
593 };
594 
595 /*
596  * Force to PERF_RAPL_PKG_EVENTS_MAX size due to:
597  * - perf_msr_probe(PERF_RAPL_PKG_EVENTS_MAX)
598  * - want to use same event codes across both architectures
599  */
600 static struct perf_msr amd_rapl_pkg_msrs[] = {
601 	[PERF_RAPL_PP0]  = { 0, &rapl_events_cores_group, NULL, false, 0 },
602 	[PERF_RAPL_PKG]  = { MSR_AMD_PKG_ENERGY_STATUS,  &rapl_events_pkg_group,   test_msr, false, RAPL_MSR_MASK },
603 	[PERF_RAPL_RAM]  = { 0, &rapl_events_ram_group,   NULL, false, 0 },
604 	[PERF_RAPL_PP1]  = { 0, &rapl_events_gpu_group,   NULL, false, 0 },
605 	[PERF_RAPL_PSYS] = { 0, &rapl_events_psys_group,  NULL, false, 0 },
606 };
607 
608 static struct perf_msr amd_rapl_core_msrs[] = {
609 	[PERF_RAPL_CORE] = { MSR_AMD_CORE_ENERGY_STATUS, &rapl_events_core_group,
610 				 test_msr, false, RAPL_MSR_MASK },
611 };
612 
613 static int rapl_check_hw_unit(void)
614 {
615 	u64 msr_rapl_power_unit_bits;
616 	int i;
617 
618 	/* protect rdmsrl() to handle virtualization */
619 	if (rdmsrl_safe(rapl_model->msr_power_unit, &msr_rapl_power_unit_bits))
620 		return -1;
621 	for (i = 0; i < NR_RAPL_PKG_DOMAINS; i++)
622 		rapl_pkg_hw_unit[i] = (msr_rapl_power_unit_bits >> 8) & 0x1FULL;
623 
624 	rapl_core_hw_unit = (msr_rapl_power_unit_bits >> 8) & 0x1FULL;
625 
626 	switch (rapl_model->unit_quirk) {
627 	/*
628 	 * DRAM domain on HSW server and KNL has fixed energy unit which can be
629 	 * different than the unit from power unit MSR. See
630 	 * "Intel Xeon Processor E5-1600 and E5-2600 v3 Product Families, V2
631 	 * of 2. Datasheet, September 2014, Reference Number: 330784-001 "
632 	 */
633 	case RAPL_UNIT_QUIRK_INTEL_HSW:
634 		rapl_pkg_hw_unit[PERF_RAPL_RAM] = 16;
635 		break;
636 	/* SPR uses a fixed energy unit for Psys domain. */
637 	case RAPL_UNIT_QUIRK_INTEL_SPR:
638 		rapl_pkg_hw_unit[PERF_RAPL_PSYS] = 0;
639 		break;
640 	default:
641 		break;
642 	}
643 
644 	/*
645 	 * Calculate the timer rate:
646 	 * Use reference of 200W for scaling the timeout to avoid counter
647 	 * overflows. 200W = 200 Joules/sec
648 	 * Divide interval by 2 to avoid lockstep (2 * 100)
649 	 * if hw unit is 32, then we use 2 ms 1/200/2
650 	 */
651 	rapl_timer_ms = 2;
652 	if (rapl_pkg_hw_unit[0] < 32) {
653 		rapl_timer_ms = (1000 / (2 * 100));
654 		rapl_timer_ms *= (1ULL << (32 - rapl_pkg_hw_unit[0] - 1));
655 	}
656 	return 0;
657 }
658 
659 static void __init rapl_advertise(void)
660 {
661 	int i;
662 	int num_counters = hweight32(rapl_pmus_pkg->cntr_mask);
663 
664 	if (rapl_pmus_core)
665 		num_counters += hweight32(rapl_pmus_core->cntr_mask);
666 
667 	pr_info("API unit is 2^-32 Joules, %d fixed counters, %llu ms ovfl timer\n",
668 		num_counters, rapl_timer_ms);
669 
670 	for (i = 0; i < NR_RAPL_PKG_DOMAINS; i++) {
671 		if (rapl_pmus_pkg->cntr_mask & (1 << i)) {
672 			pr_info("hw unit of domain %s 2^-%d Joules\n",
673 				rapl_pkg_domain_names[i], rapl_pkg_hw_unit[i]);
674 		}
675 	}
676 
677 	if (rapl_pmus_core && (rapl_pmus_core->cntr_mask & (1 << PERF_RAPL_CORE)))
678 		pr_info("hw unit of domain %s 2^-%d Joules\n",
679 			rapl_core_domain_name, rapl_core_hw_unit);
680 }
681 
682 static void cleanup_rapl_pmus(struct rapl_pmus *rapl_pmus)
683 {
684 	int i;
685 
686 	for (i = 0; i < rapl_pmus->nr_rapl_pmu; i++)
687 		kfree(rapl_pmus->rapl_pmu[i]);
688 	kfree(rapl_pmus);
689 }
690 
691 static const struct attribute_group *rapl_attr_update[] = {
692 	&rapl_events_cores_group,
693 	&rapl_events_pkg_group,
694 	&rapl_events_ram_group,
695 	&rapl_events_gpu_group,
696 	&rapl_events_psys_group,
697 	NULL,
698 };
699 
700 static const struct attribute_group *rapl_core_attr_update[] = {
701 	&rapl_events_core_group,
702 	NULL,
703 };
704 
705 static int __init init_rapl_pmu(struct rapl_pmus *rapl_pmus)
706 {
707 	struct rapl_pmu *rapl_pmu;
708 	int idx;
709 
710 	for (idx = 0; idx < rapl_pmus->nr_rapl_pmu; idx++) {
711 		rapl_pmu = kzalloc(sizeof(*rapl_pmu), GFP_KERNEL);
712 		if (!rapl_pmu)
713 			goto free;
714 
715 		raw_spin_lock_init(&rapl_pmu->lock);
716 		INIT_LIST_HEAD(&rapl_pmu->active_list);
717 		rapl_pmu->pmu = &rapl_pmus->pmu;
718 		rapl_pmu->timer_interval = ms_to_ktime(rapl_timer_ms);
719 		rapl_hrtimer_init(rapl_pmu);
720 
721 		rapl_pmus->rapl_pmu[idx] = rapl_pmu;
722 	}
723 
724 	return 0;
725 free:
726 	for (; idx > 0; idx--)
727 		kfree(rapl_pmus->rapl_pmu[idx - 1]);
728 	return -ENOMEM;
729 }
730 
731 static int __init init_rapl_pmus(struct rapl_pmus **rapl_pmus_ptr, int rapl_pmu_scope,
732 				 const struct attribute_group **rapl_attr_groups,
733 				 const struct attribute_group **rapl_attr_update)
734 {
735 	int nr_rapl_pmu = topology_max_packages();
736 	struct rapl_pmus *rapl_pmus;
737 
738 	/*
739 	 * rapl_pmu_scope must be either PKG, DIE or CORE
740 	 */
741 	if (rapl_pmu_scope == PERF_PMU_SCOPE_DIE)
742 		nr_rapl_pmu	*= topology_max_dies_per_package();
743 	else if (rapl_pmu_scope == PERF_PMU_SCOPE_CORE)
744 		nr_rapl_pmu	*= topology_num_cores_per_package();
745 	else if (rapl_pmu_scope != PERF_PMU_SCOPE_PKG)
746 		return -EINVAL;
747 
748 	rapl_pmus = kzalloc(struct_size(rapl_pmus, rapl_pmu, nr_rapl_pmu), GFP_KERNEL);
749 	if (!rapl_pmus)
750 		return -ENOMEM;
751 
752 	*rapl_pmus_ptr = rapl_pmus;
753 
754 	rapl_pmus->nr_rapl_pmu		= nr_rapl_pmu;
755 	rapl_pmus->pmu.attr_groups	= rapl_attr_groups;
756 	rapl_pmus->pmu.attr_update	= rapl_attr_update;
757 	rapl_pmus->pmu.task_ctx_nr	= perf_invalid_context;
758 	rapl_pmus->pmu.event_init	= rapl_pmu_event_init;
759 	rapl_pmus->pmu.add		= rapl_pmu_event_add;
760 	rapl_pmus->pmu.del		= rapl_pmu_event_del;
761 	rapl_pmus->pmu.start		= rapl_pmu_event_start;
762 	rapl_pmus->pmu.stop		= rapl_pmu_event_stop;
763 	rapl_pmus->pmu.read		= rapl_pmu_event_read;
764 	rapl_pmus->pmu.scope		= rapl_pmu_scope;
765 	rapl_pmus->pmu.module		= THIS_MODULE;
766 	rapl_pmus->pmu.capabilities	= PERF_PMU_CAP_NO_EXCLUDE;
767 
768 	return init_rapl_pmu(rapl_pmus);
769 }
770 
771 static struct rapl_model model_snb = {
772 	.pkg_events	= BIT(PERF_RAPL_PP0) |
773 			  BIT(PERF_RAPL_PKG) |
774 			  BIT(PERF_RAPL_PP1),
775 	.msr_power_unit = MSR_RAPL_POWER_UNIT,
776 	.rapl_pkg_msrs	= intel_rapl_msrs,
777 };
778 
779 static struct rapl_model model_snbep = {
780 	.pkg_events	= BIT(PERF_RAPL_PP0) |
781 			  BIT(PERF_RAPL_PKG) |
782 			  BIT(PERF_RAPL_RAM),
783 	.msr_power_unit = MSR_RAPL_POWER_UNIT,
784 	.rapl_pkg_msrs	= intel_rapl_msrs,
785 };
786 
787 static struct rapl_model model_hsw = {
788 	.pkg_events	= BIT(PERF_RAPL_PP0) |
789 			  BIT(PERF_RAPL_PKG) |
790 			  BIT(PERF_RAPL_RAM) |
791 			  BIT(PERF_RAPL_PP1),
792 	.msr_power_unit = MSR_RAPL_POWER_UNIT,
793 	.rapl_pkg_msrs	= intel_rapl_msrs,
794 };
795 
796 static struct rapl_model model_hsx = {
797 	.pkg_events	= BIT(PERF_RAPL_PP0) |
798 			  BIT(PERF_RAPL_PKG) |
799 			  BIT(PERF_RAPL_RAM),
800 	.unit_quirk	= RAPL_UNIT_QUIRK_INTEL_HSW,
801 	.msr_power_unit = MSR_RAPL_POWER_UNIT,
802 	.rapl_pkg_msrs	= intel_rapl_msrs,
803 };
804 
805 static struct rapl_model model_knl = {
806 	.pkg_events	= BIT(PERF_RAPL_PKG) |
807 			  BIT(PERF_RAPL_RAM),
808 	.unit_quirk	= RAPL_UNIT_QUIRK_INTEL_HSW,
809 	.msr_power_unit = MSR_RAPL_POWER_UNIT,
810 	.rapl_pkg_msrs	= intel_rapl_msrs,
811 };
812 
813 static struct rapl_model model_skl = {
814 	.pkg_events	= BIT(PERF_RAPL_PP0) |
815 			  BIT(PERF_RAPL_PKG) |
816 			  BIT(PERF_RAPL_RAM) |
817 			  BIT(PERF_RAPL_PP1) |
818 			  BIT(PERF_RAPL_PSYS),
819 	.msr_power_unit = MSR_RAPL_POWER_UNIT,
820 	.rapl_pkg_msrs      = intel_rapl_msrs,
821 };
822 
823 static struct rapl_model model_spr = {
824 	.pkg_events	= BIT(PERF_RAPL_PP0) |
825 			  BIT(PERF_RAPL_PKG) |
826 			  BIT(PERF_RAPL_RAM) |
827 			  BIT(PERF_RAPL_PSYS),
828 	.unit_quirk	= RAPL_UNIT_QUIRK_INTEL_SPR,
829 	.msr_power_unit = MSR_RAPL_POWER_UNIT,
830 	.rapl_pkg_msrs	= intel_rapl_spr_msrs,
831 };
832 
833 static struct rapl_model model_amd_hygon = {
834 	.pkg_events	= BIT(PERF_RAPL_PKG),
835 	.core_events	= BIT(PERF_RAPL_CORE),
836 	.msr_power_unit = MSR_AMD_RAPL_POWER_UNIT,
837 	.rapl_pkg_msrs	= amd_rapl_pkg_msrs,
838 	.rapl_core_msrs	= amd_rapl_core_msrs,
839 };
840 
841 static const struct x86_cpu_id rapl_model_match[] __initconst = {
842 	X86_MATCH_FEATURE(X86_FEATURE_RAPL,	&model_amd_hygon),
843 	X86_MATCH_VFM(INTEL_SANDYBRIDGE,	&model_snb),
844 	X86_MATCH_VFM(INTEL_SANDYBRIDGE_X,	&model_snbep),
845 	X86_MATCH_VFM(INTEL_IVYBRIDGE,		&model_snb),
846 	X86_MATCH_VFM(INTEL_IVYBRIDGE_X,	&model_snbep),
847 	X86_MATCH_VFM(INTEL_HASWELL,		&model_hsw),
848 	X86_MATCH_VFM(INTEL_HASWELL_X,		&model_hsx),
849 	X86_MATCH_VFM(INTEL_HASWELL_L,		&model_hsw),
850 	X86_MATCH_VFM(INTEL_HASWELL_G,		&model_hsw),
851 	X86_MATCH_VFM(INTEL_BROADWELL,		&model_hsw),
852 	X86_MATCH_VFM(INTEL_BROADWELL_G,	&model_hsw),
853 	X86_MATCH_VFM(INTEL_BROADWELL_X,	&model_hsx),
854 	X86_MATCH_VFM(INTEL_BROADWELL_D,	&model_hsx),
855 	X86_MATCH_VFM(INTEL_XEON_PHI_KNL,	&model_knl),
856 	X86_MATCH_VFM(INTEL_XEON_PHI_KNM,	&model_knl),
857 	X86_MATCH_VFM(INTEL_SKYLAKE_L,		&model_skl),
858 	X86_MATCH_VFM(INTEL_SKYLAKE,		&model_skl),
859 	X86_MATCH_VFM(INTEL_SKYLAKE_X,		&model_hsx),
860 	X86_MATCH_VFM(INTEL_KABYLAKE_L,		&model_skl),
861 	X86_MATCH_VFM(INTEL_KABYLAKE,		&model_skl),
862 	X86_MATCH_VFM(INTEL_CANNONLAKE_L,	&model_skl),
863 	X86_MATCH_VFM(INTEL_ATOM_GOLDMONT,	&model_hsw),
864 	X86_MATCH_VFM(INTEL_ATOM_GOLDMONT_D,	&model_hsw),
865 	X86_MATCH_VFM(INTEL_ATOM_GOLDMONT_PLUS,	&model_hsw),
866 	X86_MATCH_VFM(INTEL_ICELAKE_L,		&model_skl),
867 	X86_MATCH_VFM(INTEL_ICELAKE,		&model_skl),
868 	X86_MATCH_VFM(INTEL_ICELAKE_D,		&model_hsx),
869 	X86_MATCH_VFM(INTEL_ICELAKE_X,		&model_hsx),
870 	X86_MATCH_VFM(INTEL_COMETLAKE_L,	&model_skl),
871 	X86_MATCH_VFM(INTEL_COMETLAKE,		&model_skl),
872 	X86_MATCH_VFM(INTEL_TIGERLAKE_L,	&model_skl),
873 	X86_MATCH_VFM(INTEL_TIGERLAKE,		&model_skl),
874 	X86_MATCH_VFM(INTEL_ALDERLAKE,		&model_skl),
875 	X86_MATCH_VFM(INTEL_ALDERLAKE_L,	&model_skl),
876 	X86_MATCH_VFM(INTEL_ATOM_GRACEMONT,	&model_skl),
877 	X86_MATCH_VFM(INTEL_SAPPHIRERAPIDS_X,	&model_spr),
878 	X86_MATCH_VFM(INTEL_EMERALDRAPIDS_X,	&model_spr),
879 	X86_MATCH_VFM(INTEL_RAPTORLAKE,		&model_skl),
880 	X86_MATCH_VFM(INTEL_RAPTORLAKE_P,	&model_skl),
881 	X86_MATCH_VFM(INTEL_RAPTORLAKE_S,	&model_skl),
882 	X86_MATCH_VFM(INTEL_METEORLAKE,		&model_skl),
883 	X86_MATCH_VFM(INTEL_METEORLAKE_L,	&model_skl),
884 	X86_MATCH_VFM(INTEL_ARROWLAKE_H,	&model_skl),
885 	X86_MATCH_VFM(INTEL_ARROWLAKE,		&model_skl),
886 	X86_MATCH_VFM(INTEL_LUNARLAKE_M,	&model_skl),
887 	{},
888 };
889 MODULE_DEVICE_TABLE(x86cpu, rapl_model_match);
890 
891 static int __init rapl_pmu_init(void)
892 {
893 	const struct x86_cpu_id *id;
894 	int rapl_pkg_pmu_scope = PERF_PMU_SCOPE_DIE;
895 	int ret;
896 
897 	if (rapl_pkg_pmu_is_pkg_scope())
898 		rapl_pkg_pmu_scope = PERF_PMU_SCOPE_PKG;
899 
900 	id = x86_match_cpu(rapl_model_match);
901 	if (!id)
902 		return -ENODEV;
903 
904 	rapl_model = (struct rapl_model *) id->driver_data;
905 
906 	ret = rapl_check_hw_unit();
907 	if (ret)
908 		return ret;
909 
910 	ret = init_rapl_pmus(&rapl_pmus_pkg, rapl_pkg_pmu_scope, rapl_attr_groups,
911 			     rapl_attr_update);
912 	if (ret)
913 		return ret;
914 
915 	rapl_pmus_pkg->cntr_mask = perf_msr_probe(rapl_model->rapl_pkg_msrs,
916 						  PERF_RAPL_PKG_EVENTS_MAX, false,
917 						  (void *) &rapl_model->pkg_events);
918 
919 	ret = perf_pmu_register(&rapl_pmus_pkg->pmu, "power", -1);
920 	if (ret)
921 		goto out;
922 
923 	if (rapl_model->core_events) {
924 		ret = init_rapl_pmus(&rapl_pmus_core, PERF_PMU_SCOPE_CORE,
925 				     rapl_core_attr_groups,
926 				     rapl_core_attr_update);
927 		if (ret) {
928 			pr_warn("power-core PMU initialization failed (%d)\n", ret);
929 			goto core_init_failed;
930 		}
931 
932 		rapl_pmus_core->cntr_mask = perf_msr_probe(rapl_model->rapl_core_msrs,
933 						     PERF_RAPL_CORE_EVENTS_MAX, false,
934 						     (void *) &rapl_model->core_events);
935 
936 		ret = perf_pmu_register(&rapl_pmus_core->pmu, "power_core", -1);
937 		if (ret) {
938 			pr_warn("power-core PMU registration failed (%d)\n", ret);
939 			cleanup_rapl_pmus(rapl_pmus_core);
940 		}
941 	}
942 
943 core_init_failed:
944 	rapl_advertise();
945 	return 0;
946 
947 out:
948 	pr_warn("Initialization failed (%d), disabled\n", ret);
949 	cleanup_rapl_pmus(rapl_pmus_pkg);
950 	return ret;
951 }
952 module_init(rapl_pmu_init);
953 
954 static void __exit intel_rapl_exit(void)
955 {
956 	if (rapl_pmus_core) {
957 		perf_pmu_unregister(&rapl_pmus_core->pmu);
958 		cleanup_rapl_pmus(rapl_pmus_core);
959 	}
960 	perf_pmu_unregister(&rapl_pmus_pkg->pmu);
961 	cleanup_rapl_pmus(rapl_pmus_pkg);
962 }
963 module_exit(intel_rapl_exit);
964