xref: /openbmc/linux/drivers/gpu/drm/i915/i915_pmu.c (revision 447ae316670230d7d29430e2cbf1f5db4f49d14c)
1b46a33e2STvrtko Ursulin /*
2058a9b43SMichal Wajdeczko  * SPDX-License-Identifier: MIT
3b46a33e2STvrtko Ursulin  *
4058a9b43SMichal Wajdeczko  * Copyright © 2017-2018 Intel Corporation
5b46a33e2STvrtko Ursulin  */
6b46a33e2STvrtko Ursulin 
7*447ae316SNicolai Stange #include <linux/irq.h>
8b46a33e2STvrtko Ursulin #include "i915_pmu.h"
9b46a33e2STvrtko Ursulin #include "intel_ringbuffer.h"
10058a9b43SMichal Wajdeczko #include "i915_drv.h"
11b46a33e2STvrtko Ursulin 
12b46a33e2STvrtko Ursulin /* Frequency for the sampling timer for events which need it. */
13b46a33e2STvrtko Ursulin #define FREQUENCY 200
14b46a33e2STvrtko Ursulin #define PERIOD max_t(u64, 10000, NSEC_PER_SEC / FREQUENCY)
15b46a33e2STvrtko Ursulin 
16b46a33e2STvrtko Ursulin #define ENGINE_SAMPLE_MASK \
17b46a33e2STvrtko Ursulin 	(BIT(I915_SAMPLE_BUSY) | \
18b46a33e2STvrtko Ursulin 	 BIT(I915_SAMPLE_WAIT) | \
19b46a33e2STvrtko Ursulin 	 BIT(I915_SAMPLE_SEMA))
20b46a33e2STvrtko Ursulin 
21b46a33e2STvrtko Ursulin #define ENGINE_SAMPLE_BITS (1 << I915_PMU_SAMPLE_BITS)
22b46a33e2STvrtko Ursulin 
23141a0895SChris Wilson static cpumask_t i915_pmu_cpumask;
24b46a33e2STvrtko Ursulin 
25b46a33e2STvrtko Ursulin static u8 engine_config_sample(u64 config)
26b46a33e2STvrtko Ursulin {
27b46a33e2STvrtko Ursulin 	return config & I915_PMU_SAMPLE_MASK;
28b46a33e2STvrtko Ursulin }
29b46a33e2STvrtko Ursulin 
30b46a33e2STvrtko Ursulin static u8 engine_event_sample(struct perf_event *event)
31b46a33e2STvrtko Ursulin {
32b46a33e2STvrtko Ursulin 	return engine_config_sample(event->attr.config);
33b46a33e2STvrtko Ursulin }
34b46a33e2STvrtko Ursulin 
35b46a33e2STvrtko Ursulin static u8 engine_event_class(struct perf_event *event)
36b46a33e2STvrtko Ursulin {
37b46a33e2STvrtko Ursulin 	return (event->attr.config >> I915_PMU_CLASS_SHIFT) & 0xff;
38b46a33e2STvrtko Ursulin }
39b46a33e2STvrtko Ursulin 
40b46a33e2STvrtko Ursulin static u8 engine_event_instance(struct perf_event *event)
41b46a33e2STvrtko Ursulin {
42b46a33e2STvrtko Ursulin 	return (event->attr.config >> I915_PMU_SAMPLE_BITS) & 0xff;
43b46a33e2STvrtko Ursulin }
44b46a33e2STvrtko Ursulin 
45b46a33e2STvrtko Ursulin static bool is_engine_config(u64 config)
46b46a33e2STvrtko Ursulin {
47b46a33e2STvrtko Ursulin 	return config < __I915_PMU_OTHER(0);
48b46a33e2STvrtko Ursulin }
49b46a33e2STvrtko Ursulin 
50b46a33e2STvrtko Ursulin static unsigned int config_enabled_bit(u64 config)
51b46a33e2STvrtko Ursulin {
52b46a33e2STvrtko Ursulin 	if (is_engine_config(config))
53b46a33e2STvrtko Ursulin 		return engine_config_sample(config);
54b46a33e2STvrtko Ursulin 	else
55b46a33e2STvrtko Ursulin 		return ENGINE_SAMPLE_BITS + (config - __I915_PMU_OTHER(0));
56b46a33e2STvrtko Ursulin }
57b46a33e2STvrtko Ursulin 
58b46a33e2STvrtko Ursulin static u64 config_enabled_mask(u64 config)
59b46a33e2STvrtko Ursulin {
60b46a33e2STvrtko Ursulin 	return BIT_ULL(config_enabled_bit(config));
61b46a33e2STvrtko Ursulin }
62b46a33e2STvrtko Ursulin 
63b46a33e2STvrtko Ursulin static bool is_engine_event(struct perf_event *event)
64b46a33e2STvrtko Ursulin {
65b46a33e2STvrtko Ursulin 	return is_engine_config(event->attr.config);
66b46a33e2STvrtko Ursulin }
67b46a33e2STvrtko Ursulin 
68b46a33e2STvrtko Ursulin static unsigned int event_enabled_bit(struct perf_event *event)
69b46a33e2STvrtko Ursulin {
70b46a33e2STvrtko Ursulin 	return config_enabled_bit(event->attr.config);
71b46a33e2STvrtko Ursulin }
72b46a33e2STvrtko Ursulin 
73feff0dc6STvrtko Ursulin static bool pmu_needs_timer(struct drm_i915_private *i915, bool gpu_active)
74feff0dc6STvrtko Ursulin {
75feff0dc6STvrtko Ursulin 	u64 enable;
76feff0dc6STvrtko Ursulin 
77feff0dc6STvrtko Ursulin 	/*
78feff0dc6STvrtko Ursulin 	 * Only some counters need the sampling timer.
79feff0dc6STvrtko Ursulin 	 *
80feff0dc6STvrtko Ursulin 	 * We start with a bitmask of all currently enabled events.
81feff0dc6STvrtko Ursulin 	 */
82feff0dc6STvrtko Ursulin 	enable = i915->pmu.enable;
83feff0dc6STvrtko Ursulin 
84feff0dc6STvrtko Ursulin 	/*
85feff0dc6STvrtko Ursulin 	 * Mask out all the ones which do not need the timer, or in
86feff0dc6STvrtko Ursulin 	 * other words keep all the ones that could need the timer.
87feff0dc6STvrtko Ursulin 	 */
88feff0dc6STvrtko Ursulin 	enable &= config_enabled_mask(I915_PMU_ACTUAL_FREQUENCY) |
89feff0dc6STvrtko Ursulin 		  config_enabled_mask(I915_PMU_REQUESTED_FREQUENCY) |
90feff0dc6STvrtko Ursulin 		  ENGINE_SAMPLE_MASK;
91feff0dc6STvrtko Ursulin 
92feff0dc6STvrtko Ursulin 	/*
93feff0dc6STvrtko Ursulin 	 * When the GPU is idle per-engine counters do not need to be
94feff0dc6STvrtko Ursulin 	 * running so clear those bits out.
95feff0dc6STvrtko Ursulin 	 */
96feff0dc6STvrtko Ursulin 	if (!gpu_active)
97feff0dc6STvrtko Ursulin 		enable &= ~ENGINE_SAMPLE_MASK;
98b3add01eSTvrtko Ursulin 	/*
99b3add01eSTvrtko Ursulin 	 * Also there is software busyness tracking available we do not
100b3add01eSTvrtko Ursulin 	 * need the timer for I915_SAMPLE_BUSY counter.
101cf669b4eSTvrtko Ursulin 	 *
102cf669b4eSTvrtko Ursulin 	 * Use RCS as proxy for all engines.
103b3add01eSTvrtko Ursulin 	 */
104cf669b4eSTvrtko Ursulin 	else if (intel_engine_supports_stats(i915->engine[RCS]))
105b3add01eSTvrtko Ursulin 		enable &= ~BIT(I915_SAMPLE_BUSY);
106feff0dc6STvrtko Ursulin 
107feff0dc6STvrtko Ursulin 	/*
108feff0dc6STvrtko Ursulin 	 * If some bits remain it means we need the sampling timer running.
109feff0dc6STvrtko Ursulin 	 */
110feff0dc6STvrtko Ursulin 	return enable;
111feff0dc6STvrtko Ursulin }
112feff0dc6STvrtko Ursulin 
113feff0dc6STvrtko Ursulin void i915_pmu_gt_parked(struct drm_i915_private *i915)
114feff0dc6STvrtko Ursulin {
115feff0dc6STvrtko Ursulin 	if (!i915->pmu.base.event_init)
116feff0dc6STvrtko Ursulin 		return;
117feff0dc6STvrtko Ursulin 
118feff0dc6STvrtko Ursulin 	spin_lock_irq(&i915->pmu.lock);
119feff0dc6STvrtko Ursulin 	/*
120feff0dc6STvrtko Ursulin 	 * Signal sampling timer to stop if only engine events are enabled and
121feff0dc6STvrtko Ursulin 	 * GPU went idle.
122feff0dc6STvrtko Ursulin 	 */
123feff0dc6STvrtko Ursulin 	i915->pmu.timer_enabled = pmu_needs_timer(i915, false);
124feff0dc6STvrtko Ursulin 	spin_unlock_irq(&i915->pmu.lock);
125feff0dc6STvrtko Ursulin }
126feff0dc6STvrtko Ursulin 
127feff0dc6STvrtko Ursulin static void __i915_pmu_maybe_start_timer(struct drm_i915_private *i915)
128feff0dc6STvrtko Ursulin {
129feff0dc6STvrtko Ursulin 	if (!i915->pmu.timer_enabled && pmu_needs_timer(i915, true)) {
130feff0dc6STvrtko Ursulin 		i915->pmu.timer_enabled = true;
131feff0dc6STvrtko Ursulin 		hrtimer_start_range_ns(&i915->pmu.timer,
132feff0dc6STvrtko Ursulin 				       ns_to_ktime(PERIOD), 0,
133feff0dc6STvrtko Ursulin 				       HRTIMER_MODE_REL_PINNED);
134feff0dc6STvrtko Ursulin 	}
135feff0dc6STvrtko Ursulin }
136feff0dc6STvrtko Ursulin 
137feff0dc6STvrtko Ursulin void i915_pmu_gt_unparked(struct drm_i915_private *i915)
138feff0dc6STvrtko Ursulin {
139feff0dc6STvrtko Ursulin 	if (!i915->pmu.base.event_init)
140feff0dc6STvrtko Ursulin 		return;
141feff0dc6STvrtko Ursulin 
142feff0dc6STvrtko Ursulin 	spin_lock_irq(&i915->pmu.lock);
143feff0dc6STvrtko Ursulin 	/*
144feff0dc6STvrtko Ursulin 	 * Re-enable sampling timer when GPU goes active.
145feff0dc6STvrtko Ursulin 	 */
146feff0dc6STvrtko Ursulin 	__i915_pmu_maybe_start_timer(i915);
147feff0dc6STvrtko Ursulin 	spin_unlock_irq(&i915->pmu.lock);
148feff0dc6STvrtko Ursulin }
149feff0dc6STvrtko Ursulin 
150b46a33e2STvrtko Ursulin static bool grab_forcewake(struct drm_i915_private *i915, bool fw)
151b46a33e2STvrtko Ursulin {
152b46a33e2STvrtko Ursulin 	if (!fw)
153b46a33e2STvrtko Ursulin 		intel_uncore_forcewake_get(i915, FORCEWAKE_ALL);
154b46a33e2STvrtko Ursulin 
155b46a33e2STvrtko Ursulin 	return true;
156b46a33e2STvrtko Ursulin }
157b46a33e2STvrtko Ursulin 
158b46a33e2STvrtko Ursulin static void
159b46a33e2STvrtko Ursulin update_sample(struct i915_pmu_sample *sample, u32 unit, u32 val)
160b46a33e2STvrtko Ursulin {
1618ee4f19cSTvrtko Ursulin 	sample->cur += mul_u32_u32(val, unit);
162b46a33e2STvrtko Ursulin }
163b46a33e2STvrtko Ursulin 
164b46a33e2STvrtko Ursulin static void engines_sample(struct drm_i915_private *dev_priv)
165b46a33e2STvrtko Ursulin {
166b46a33e2STvrtko Ursulin 	struct intel_engine_cs *engine;
167b46a33e2STvrtko Ursulin 	enum intel_engine_id id;
168b46a33e2STvrtko Ursulin 	bool fw = false;
169b46a33e2STvrtko Ursulin 
170b46a33e2STvrtko Ursulin 	if ((dev_priv->pmu.enable & ENGINE_SAMPLE_MASK) == 0)
171b46a33e2STvrtko Ursulin 		return;
172b46a33e2STvrtko Ursulin 
173b46a33e2STvrtko Ursulin 	if (!dev_priv->gt.awake)
174b46a33e2STvrtko Ursulin 		return;
175b46a33e2STvrtko Ursulin 
176b46a33e2STvrtko Ursulin 	if (!intel_runtime_pm_get_if_in_use(dev_priv))
177b46a33e2STvrtko Ursulin 		return;
178b46a33e2STvrtko Ursulin 
179b46a33e2STvrtko Ursulin 	for_each_engine(engine, dev_priv, id) {
180b46a33e2STvrtko Ursulin 		u32 current_seqno = intel_engine_get_seqno(engine);
181b46a33e2STvrtko Ursulin 		u32 last_seqno = intel_engine_last_submit(engine);
182b46a33e2STvrtko Ursulin 		u32 val;
183b46a33e2STvrtko Ursulin 
184b46a33e2STvrtko Ursulin 		val = !i915_seqno_passed(current_seqno, last_seqno);
185b46a33e2STvrtko Ursulin 
186b46a33e2STvrtko Ursulin 		update_sample(&engine->pmu.sample[I915_SAMPLE_BUSY],
187b46a33e2STvrtko Ursulin 			      PERIOD, val);
188b46a33e2STvrtko Ursulin 
189b46a33e2STvrtko Ursulin 		if (val && (engine->pmu.enable &
190b46a33e2STvrtko Ursulin 		    (BIT(I915_SAMPLE_WAIT) | BIT(I915_SAMPLE_SEMA)))) {
191b46a33e2STvrtko Ursulin 			fw = grab_forcewake(dev_priv, fw);
192b46a33e2STvrtko Ursulin 
193b46a33e2STvrtko Ursulin 			val = I915_READ_FW(RING_CTL(engine->mmio_base));
194b46a33e2STvrtko Ursulin 		} else {
195b46a33e2STvrtko Ursulin 			val = 0;
196b46a33e2STvrtko Ursulin 		}
197b46a33e2STvrtko Ursulin 
198b46a33e2STvrtko Ursulin 		update_sample(&engine->pmu.sample[I915_SAMPLE_WAIT],
199b46a33e2STvrtko Ursulin 			      PERIOD, !!(val & RING_WAIT));
200b46a33e2STvrtko Ursulin 
201b46a33e2STvrtko Ursulin 		update_sample(&engine->pmu.sample[I915_SAMPLE_SEMA],
202b46a33e2STvrtko Ursulin 			      PERIOD, !!(val & RING_WAIT_SEMAPHORE));
203b46a33e2STvrtko Ursulin 	}
204b46a33e2STvrtko Ursulin 
205b46a33e2STvrtko Ursulin 	if (fw)
206b46a33e2STvrtko Ursulin 		intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
207b46a33e2STvrtko Ursulin 
208b46a33e2STvrtko Ursulin 	intel_runtime_pm_put(dev_priv);
209b46a33e2STvrtko Ursulin }
210b46a33e2STvrtko Ursulin 
211b46a33e2STvrtko Ursulin static void frequency_sample(struct drm_i915_private *dev_priv)
212b46a33e2STvrtko Ursulin {
213b46a33e2STvrtko Ursulin 	if (dev_priv->pmu.enable &
214b46a33e2STvrtko Ursulin 	    config_enabled_mask(I915_PMU_ACTUAL_FREQUENCY)) {
215b46a33e2STvrtko Ursulin 		u32 val;
216b46a33e2STvrtko Ursulin 
217b46a33e2STvrtko Ursulin 		val = dev_priv->gt_pm.rps.cur_freq;
218b46a33e2STvrtko Ursulin 		if (dev_priv->gt.awake &&
219b46a33e2STvrtko Ursulin 		    intel_runtime_pm_get_if_in_use(dev_priv)) {
220b46a33e2STvrtko Ursulin 			val = intel_get_cagf(dev_priv,
221b46a33e2STvrtko Ursulin 					     I915_READ_NOTRACE(GEN6_RPSTAT1));
222b46a33e2STvrtko Ursulin 			intel_runtime_pm_put(dev_priv);
223b46a33e2STvrtko Ursulin 		}
224b46a33e2STvrtko Ursulin 
225b46a33e2STvrtko Ursulin 		update_sample(&dev_priv->pmu.sample[__I915_SAMPLE_FREQ_ACT],
226b46a33e2STvrtko Ursulin 			      1, intel_gpu_freq(dev_priv, val));
227b46a33e2STvrtko Ursulin 	}
228b46a33e2STvrtko Ursulin 
229b46a33e2STvrtko Ursulin 	if (dev_priv->pmu.enable &
230b46a33e2STvrtko Ursulin 	    config_enabled_mask(I915_PMU_REQUESTED_FREQUENCY)) {
231b46a33e2STvrtko Ursulin 		update_sample(&dev_priv->pmu.sample[__I915_SAMPLE_FREQ_REQ], 1,
232b46a33e2STvrtko Ursulin 			      intel_gpu_freq(dev_priv,
233b46a33e2STvrtko Ursulin 					     dev_priv->gt_pm.rps.cur_freq));
234b46a33e2STvrtko Ursulin 	}
235b46a33e2STvrtko Ursulin }
236b46a33e2STvrtko Ursulin 
237b46a33e2STvrtko Ursulin static enum hrtimer_restart i915_sample(struct hrtimer *hrtimer)
238b46a33e2STvrtko Ursulin {
239b46a33e2STvrtko Ursulin 	struct drm_i915_private *i915 =
240b46a33e2STvrtko Ursulin 		container_of(hrtimer, struct drm_i915_private, pmu.timer);
241b46a33e2STvrtko Ursulin 
2428ee4f19cSTvrtko Ursulin 	if (!READ_ONCE(i915->pmu.timer_enabled))
243b46a33e2STvrtko Ursulin 		return HRTIMER_NORESTART;
244b46a33e2STvrtko Ursulin 
245b46a33e2STvrtko Ursulin 	engines_sample(i915);
246b46a33e2STvrtko Ursulin 	frequency_sample(i915);
247b46a33e2STvrtko Ursulin 
248b46a33e2STvrtko Ursulin 	hrtimer_forward_now(hrtimer, ns_to_ktime(PERIOD));
249b46a33e2STvrtko Ursulin 	return HRTIMER_RESTART;
250b46a33e2STvrtko Ursulin }
251b46a33e2STvrtko Ursulin 
2520cd4684dSTvrtko Ursulin static u64 count_interrupts(struct drm_i915_private *i915)
2530cd4684dSTvrtko Ursulin {
2540cd4684dSTvrtko Ursulin 	/* open-coded kstat_irqs() */
2550cd4684dSTvrtko Ursulin 	struct irq_desc *desc = irq_to_desc(i915->drm.pdev->irq);
2560cd4684dSTvrtko Ursulin 	u64 sum = 0;
2570cd4684dSTvrtko Ursulin 	int cpu;
2580cd4684dSTvrtko Ursulin 
2590cd4684dSTvrtko Ursulin 	if (!desc || !desc->kstat_irqs)
2600cd4684dSTvrtko Ursulin 		return 0;
2610cd4684dSTvrtko Ursulin 
2620cd4684dSTvrtko Ursulin 	for_each_possible_cpu(cpu)
2630cd4684dSTvrtko Ursulin 		sum += *per_cpu_ptr(desc->kstat_irqs, cpu);
2640cd4684dSTvrtko Ursulin 
2650cd4684dSTvrtko Ursulin 	return sum;
2660cd4684dSTvrtko Ursulin }
2670cd4684dSTvrtko Ursulin 
268b2f78cdaSTvrtko Ursulin static void engine_event_destroy(struct perf_event *event)
269b2f78cdaSTvrtko Ursulin {
270b2f78cdaSTvrtko Ursulin 	struct drm_i915_private *i915 =
271b2f78cdaSTvrtko Ursulin 		container_of(event->pmu, typeof(*i915), pmu.base);
272b2f78cdaSTvrtko Ursulin 	struct intel_engine_cs *engine;
273b2f78cdaSTvrtko Ursulin 
274b2f78cdaSTvrtko Ursulin 	engine = intel_engine_lookup_user(i915,
275b2f78cdaSTvrtko Ursulin 					  engine_event_class(event),
276b2f78cdaSTvrtko Ursulin 					  engine_event_instance(event));
277b2f78cdaSTvrtko Ursulin 	if (WARN_ON_ONCE(!engine))
278b2f78cdaSTvrtko Ursulin 		return;
279b2f78cdaSTvrtko Ursulin 
280b2f78cdaSTvrtko Ursulin 	if (engine_event_sample(event) == I915_SAMPLE_BUSY &&
281b2f78cdaSTvrtko Ursulin 	    intel_engine_supports_stats(engine))
282b2f78cdaSTvrtko Ursulin 		intel_disable_engine_stats(engine);
283b2f78cdaSTvrtko Ursulin }
284b2f78cdaSTvrtko Ursulin 
285b46a33e2STvrtko Ursulin static void i915_pmu_event_destroy(struct perf_event *event)
286b46a33e2STvrtko Ursulin {
287b46a33e2STvrtko Ursulin 	WARN_ON(event->parent);
288b2f78cdaSTvrtko Ursulin 
289b2f78cdaSTvrtko Ursulin 	if (is_engine_event(event))
290b2f78cdaSTvrtko Ursulin 		engine_event_destroy(event);
291b46a33e2STvrtko Ursulin }
292b46a33e2STvrtko Ursulin 
293109ec558STvrtko Ursulin static int
294109ec558STvrtko Ursulin engine_event_status(struct intel_engine_cs *engine,
295109ec558STvrtko Ursulin 		    enum drm_i915_pmu_engine_sample sample)
296b46a33e2STvrtko Ursulin {
297109ec558STvrtko Ursulin 	switch (sample) {
298b46a33e2STvrtko Ursulin 	case I915_SAMPLE_BUSY:
299b46a33e2STvrtko Ursulin 	case I915_SAMPLE_WAIT:
300b46a33e2STvrtko Ursulin 		break;
301b46a33e2STvrtko Ursulin 	case I915_SAMPLE_SEMA:
302109ec558STvrtko Ursulin 		if (INTEL_GEN(engine->i915) < 6)
303b46a33e2STvrtko Ursulin 			return -ENODEV;
304b46a33e2STvrtko Ursulin 		break;
305b46a33e2STvrtko Ursulin 	default:
306b46a33e2STvrtko Ursulin 		return -ENOENT;
307b46a33e2STvrtko Ursulin 	}
308b46a33e2STvrtko Ursulin 
309b46a33e2STvrtko Ursulin 	return 0;
310b46a33e2STvrtko Ursulin }
311b46a33e2STvrtko Ursulin 
312109ec558STvrtko Ursulin static int
313109ec558STvrtko Ursulin config_status(struct drm_i915_private *i915, u64 config)
314109ec558STvrtko Ursulin {
315109ec558STvrtko Ursulin 	switch (config) {
316109ec558STvrtko Ursulin 	case I915_PMU_ACTUAL_FREQUENCY:
317109ec558STvrtko Ursulin 		if (IS_VALLEYVIEW(i915) || IS_CHERRYVIEW(i915))
318109ec558STvrtko Ursulin 			/* Requires a mutex for sampling! */
319109ec558STvrtko Ursulin 			return -ENODEV;
320109ec558STvrtko Ursulin 		/* Fall-through. */
321109ec558STvrtko Ursulin 	case I915_PMU_REQUESTED_FREQUENCY:
322109ec558STvrtko Ursulin 		if (INTEL_GEN(i915) < 6)
323109ec558STvrtko Ursulin 			return -ENODEV;
324109ec558STvrtko Ursulin 		break;
325109ec558STvrtko Ursulin 	case I915_PMU_INTERRUPTS:
326109ec558STvrtko Ursulin 		break;
327109ec558STvrtko Ursulin 	case I915_PMU_RC6_RESIDENCY:
328109ec558STvrtko Ursulin 		if (!HAS_RC6(i915))
329109ec558STvrtko Ursulin 			return -ENODEV;
330109ec558STvrtko Ursulin 		break;
331109ec558STvrtko Ursulin 	default:
332109ec558STvrtko Ursulin 		return -ENOENT;
333109ec558STvrtko Ursulin 	}
334109ec558STvrtko Ursulin 
335109ec558STvrtko Ursulin 	return 0;
336109ec558STvrtko Ursulin }
337109ec558STvrtko Ursulin 
338109ec558STvrtko Ursulin static int engine_event_init(struct perf_event *event)
339109ec558STvrtko Ursulin {
340109ec558STvrtko Ursulin 	struct drm_i915_private *i915 =
341109ec558STvrtko Ursulin 		container_of(event->pmu, typeof(*i915), pmu.base);
342109ec558STvrtko Ursulin 	struct intel_engine_cs *engine;
343b2f78cdaSTvrtko Ursulin 	u8 sample;
344b2f78cdaSTvrtko Ursulin 	int ret;
345109ec558STvrtko Ursulin 
346109ec558STvrtko Ursulin 	engine = intel_engine_lookup_user(i915, engine_event_class(event),
347109ec558STvrtko Ursulin 					  engine_event_instance(event));
348109ec558STvrtko Ursulin 	if (!engine)
349109ec558STvrtko Ursulin 		return -ENODEV;
350109ec558STvrtko Ursulin 
351b2f78cdaSTvrtko Ursulin 	sample = engine_event_sample(event);
352b2f78cdaSTvrtko Ursulin 	ret = engine_event_status(engine, sample);
353b2f78cdaSTvrtko Ursulin 	if (ret)
354b2f78cdaSTvrtko Ursulin 		return ret;
355b2f78cdaSTvrtko Ursulin 
356b2f78cdaSTvrtko Ursulin 	if (sample == I915_SAMPLE_BUSY && intel_engine_supports_stats(engine))
357b2f78cdaSTvrtko Ursulin 		ret = intel_enable_engine_stats(engine);
358b2f78cdaSTvrtko Ursulin 
359b2f78cdaSTvrtko Ursulin 	return ret;
360109ec558STvrtko Ursulin }
361109ec558STvrtko Ursulin 
362b46a33e2STvrtko Ursulin static int i915_pmu_event_init(struct perf_event *event)
363b46a33e2STvrtko Ursulin {
364b46a33e2STvrtko Ursulin 	struct drm_i915_private *i915 =
365b46a33e2STvrtko Ursulin 		container_of(event->pmu, typeof(*i915), pmu.base);
3660426c046STvrtko Ursulin 	int ret;
367b46a33e2STvrtko Ursulin 
368b46a33e2STvrtko Ursulin 	if (event->attr.type != event->pmu->type)
369b46a33e2STvrtko Ursulin 		return -ENOENT;
370b46a33e2STvrtko Ursulin 
371b46a33e2STvrtko Ursulin 	/* unsupported modes and filters */
372b46a33e2STvrtko Ursulin 	if (event->attr.sample_period) /* no sampling */
373b46a33e2STvrtko Ursulin 		return -EINVAL;
374b46a33e2STvrtko Ursulin 
375b46a33e2STvrtko Ursulin 	if (has_branch_stack(event))
376b46a33e2STvrtko Ursulin 		return -EOPNOTSUPP;
377b46a33e2STvrtko Ursulin 
378b46a33e2STvrtko Ursulin 	if (event->cpu < 0)
379b46a33e2STvrtko Ursulin 		return -EINVAL;
380b46a33e2STvrtko Ursulin 
3810426c046STvrtko Ursulin 	/* only allow running on one cpu at a time */
3820426c046STvrtko Ursulin 	if (!cpumask_test_cpu(event->cpu, &i915_pmu_cpumask))
38300a79722STvrtko Ursulin 		return -EINVAL;
384b46a33e2STvrtko Ursulin 
385109ec558STvrtko Ursulin 	if (is_engine_event(event))
386b46a33e2STvrtko Ursulin 		ret = engine_event_init(event);
387109ec558STvrtko Ursulin 	else
388109ec558STvrtko Ursulin 		ret = config_status(i915, event->attr.config);
389b46a33e2STvrtko Ursulin 	if (ret)
390b46a33e2STvrtko Ursulin 		return ret;
391b46a33e2STvrtko Ursulin 
392b46a33e2STvrtko Ursulin 	if (!event->parent)
393b46a33e2STvrtko Ursulin 		event->destroy = i915_pmu_event_destroy;
394b46a33e2STvrtko Ursulin 
395b46a33e2STvrtko Ursulin 	return 0;
396b46a33e2STvrtko Ursulin }
397b46a33e2STvrtko Ursulin 
39805273c95SChris Wilson static u64 __get_rc6(struct drm_i915_private *i915)
3991fe699e3STvrtko Ursulin {
4001fe699e3STvrtko Ursulin 	u64 val;
4011fe699e3STvrtko Ursulin 
40205273c95SChris Wilson 	val = intel_rc6_residency_ns(i915,
40305273c95SChris Wilson 				     IS_VALLEYVIEW(i915) ?
4041fe699e3STvrtko Ursulin 				     VLV_GT_RENDER_RC6 :
4051fe699e3STvrtko Ursulin 				     GEN6_GT_GFX_RC6);
4061fe699e3STvrtko Ursulin 
4071fe699e3STvrtko Ursulin 	if (HAS_RC6p(i915))
4081fe699e3STvrtko Ursulin 		val += intel_rc6_residency_ns(i915, GEN6_GT_GFX_RC6p);
4091fe699e3STvrtko Ursulin 
4101fe699e3STvrtko Ursulin 	if (HAS_RC6pp(i915))
4111fe699e3STvrtko Ursulin 		val += intel_rc6_residency_ns(i915, GEN6_GT_GFX_RC6pp);
4121fe699e3STvrtko Ursulin 
41305273c95SChris Wilson 	return val;
41405273c95SChris Wilson }
41505273c95SChris Wilson 
416ad055fb8STvrtko Ursulin static u64 get_rc6(struct drm_i915_private *i915)
41705273c95SChris Wilson {
41805273c95SChris Wilson #if IS_ENABLED(CONFIG_PM)
41905273c95SChris Wilson 	unsigned long flags;
42005273c95SChris Wilson 	u64 val;
42105273c95SChris Wilson 
42205273c95SChris Wilson 	if (intel_runtime_pm_get_if_in_use(i915)) {
42305273c95SChris Wilson 		val = __get_rc6(i915);
4241fe699e3STvrtko Ursulin 		intel_runtime_pm_put(i915);
4251fe699e3STvrtko Ursulin 
4261fe699e3STvrtko Ursulin 		/*
4271fe699e3STvrtko Ursulin 		 * If we are coming back from being runtime suspended we must
4281fe699e3STvrtko Ursulin 		 * be careful not to report a larger value than returned
4291fe699e3STvrtko Ursulin 		 * previously.
4301fe699e3STvrtko Ursulin 		 */
4311fe699e3STvrtko Ursulin 
4321fe699e3STvrtko Ursulin 		spin_lock_irqsave(&i915->pmu.lock, flags);
4331fe699e3STvrtko Ursulin 
4341fe699e3STvrtko Ursulin 		if (val >= i915->pmu.sample[__I915_SAMPLE_RC6_ESTIMATED].cur) {
4351fe699e3STvrtko Ursulin 			i915->pmu.sample[__I915_SAMPLE_RC6_ESTIMATED].cur = 0;
4361fe699e3STvrtko Ursulin 			i915->pmu.sample[__I915_SAMPLE_RC6].cur = val;
4371fe699e3STvrtko Ursulin 		} else {
4381fe699e3STvrtko Ursulin 			val = i915->pmu.sample[__I915_SAMPLE_RC6_ESTIMATED].cur;
4391fe699e3STvrtko Ursulin 		}
4401fe699e3STvrtko Ursulin 
4411fe699e3STvrtko Ursulin 		spin_unlock_irqrestore(&i915->pmu.lock, flags);
4421fe699e3STvrtko Ursulin 	} else {
4431fe699e3STvrtko Ursulin 		struct pci_dev *pdev = i915->drm.pdev;
4441fe699e3STvrtko Ursulin 		struct device *kdev = &pdev->dev;
4451fe699e3STvrtko Ursulin 
4461fe699e3STvrtko Ursulin 		/*
4471fe699e3STvrtko Ursulin 		 * We are runtime suspended.
4481fe699e3STvrtko Ursulin 		 *
4491fe699e3STvrtko Ursulin 		 * Report the delta from when the device was suspended to now,
4501fe699e3STvrtko Ursulin 		 * on top of the last known real value, as the approximated RC6
4511fe699e3STvrtko Ursulin 		 * counter value.
4521fe699e3STvrtko Ursulin 		 */
4531fe699e3STvrtko Ursulin 		spin_lock_irqsave(&i915->pmu.lock, flags);
454ad055fb8STvrtko Ursulin 		spin_lock(&kdev->power.lock);
4551fe699e3STvrtko Ursulin 
4562924bdeeSTvrtko Ursulin 		/*
4572924bdeeSTvrtko Ursulin 		 * After the above branch intel_runtime_pm_get_if_in_use failed
4582924bdeeSTvrtko Ursulin 		 * to get the runtime PM reference we cannot assume we are in
4592924bdeeSTvrtko Ursulin 		 * runtime suspend since we can either: a) race with coming out
4602924bdeeSTvrtko Ursulin 		 * of it before we took the power.lock, or b) there are other
4612924bdeeSTvrtko Ursulin 		 * states than suspended which can bring us here.
4622924bdeeSTvrtko Ursulin 		 *
4632924bdeeSTvrtko Ursulin 		 * We need to double-check that we are indeed currently runtime
4642924bdeeSTvrtko Ursulin 		 * suspended and if not we cannot do better than report the last
4652924bdeeSTvrtko Ursulin 		 * known RC6 value.
4662924bdeeSTvrtko Ursulin 		 */
4672924bdeeSTvrtko Ursulin 		if (kdev->power.runtime_status == RPM_SUSPENDED) {
4681fe699e3STvrtko Ursulin 			if (!i915->pmu.sample[__I915_SAMPLE_RC6_ESTIMATED].cur)
4691fe699e3STvrtko Ursulin 				i915->pmu.suspended_jiffies_last =
4701fe699e3STvrtko Ursulin 						  kdev->power.suspended_jiffies;
4711fe699e3STvrtko Ursulin 
4721fe699e3STvrtko Ursulin 			val = kdev->power.suspended_jiffies -
4731fe699e3STvrtko Ursulin 			      i915->pmu.suspended_jiffies_last;
4741fe699e3STvrtko Ursulin 			val += jiffies - kdev->power.accounting_timestamp;
4751fe699e3STvrtko Ursulin 
4761fe699e3STvrtko Ursulin 			val = jiffies_to_nsecs(val);
4771fe699e3STvrtko Ursulin 			val += i915->pmu.sample[__I915_SAMPLE_RC6].cur;
4781fe699e3STvrtko Ursulin 
4792924bdeeSTvrtko Ursulin 			i915->pmu.sample[__I915_SAMPLE_RC6_ESTIMATED].cur = val;
4802924bdeeSTvrtko Ursulin 		} else if (i915->pmu.sample[__I915_SAMPLE_RC6_ESTIMATED].cur) {
4812924bdeeSTvrtko Ursulin 			val = i915->pmu.sample[__I915_SAMPLE_RC6_ESTIMATED].cur;
4822924bdeeSTvrtko Ursulin 		} else {
4832924bdeeSTvrtko Ursulin 			val = i915->pmu.sample[__I915_SAMPLE_RC6].cur;
4842924bdeeSTvrtko Ursulin 		}
4852924bdeeSTvrtko Ursulin 
4862924bdeeSTvrtko Ursulin 		spin_unlock(&kdev->power.lock);
4871fe699e3STvrtko Ursulin 		spin_unlock_irqrestore(&i915->pmu.lock, flags);
4881fe699e3STvrtko Ursulin 	}
4891fe699e3STvrtko Ursulin 
4901fe699e3STvrtko Ursulin 	return val;
49105273c95SChris Wilson #else
49205273c95SChris Wilson 	return __get_rc6(i915);
49305273c95SChris Wilson #endif
4941fe699e3STvrtko Ursulin }
4951fe699e3STvrtko Ursulin 
496ad055fb8STvrtko Ursulin static u64 __i915_pmu_event_read(struct perf_event *event)
497b46a33e2STvrtko Ursulin {
498b46a33e2STvrtko Ursulin 	struct drm_i915_private *i915 =
499b46a33e2STvrtko Ursulin 		container_of(event->pmu, typeof(*i915), pmu.base);
500b46a33e2STvrtko Ursulin 	u64 val = 0;
501b46a33e2STvrtko Ursulin 
502b46a33e2STvrtko Ursulin 	if (is_engine_event(event)) {
503b46a33e2STvrtko Ursulin 		u8 sample = engine_event_sample(event);
504b46a33e2STvrtko Ursulin 		struct intel_engine_cs *engine;
505b46a33e2STvrtko Ursulin 
506b46a33e2STvrtko Ursulin 		engine = intel_engine_lookup_user(i915,
507b46a33e2STvrtko Ursulin 						  engine_event_class(event),
508b46a33e2STvrtko Ursulin 						  engine_event_instance(event));
509b46a33e2STvrtko Ursulin 
510b46a33e2STvrtko Ursulin 		if (WARN_ON_ONCE(!engine)) {
511b46a33e2STvrtko Ursulin 			/* Do nothing */
512b3add01eSTvrtko Ursulin 		} else if (sample == I915_SAMPLE_BUSY &&
513b2f78cdaSTvrtko Ursulin 			   intel_engine_supports_stats(engine)) {
514b3add01eSTvrtko Ursulin 			val = ktime_to_ns(intel_engine_get_busy_time(engine));
515b46a33e2STvrtko Ursulin 		} else {
516b46a33e2STvrtko Ursulin 			val = engine->pmu.sample[sample].cur;
517b46a33e2STvrtko Ursulin 		}
518b46a33e2STvrtko Ursulin 	} else {
519b46a33e2STvrtko Ursulin 		switch (event->attr.config) {
520b46a33e2STvrtko Ursulin 		case I915_PMU_ACTUAL_FREQUENCY:
521b46a33e2STvrtko Ursulin 			val =
522b46a33e2STvrtko Ursulin 			   div_u64(i915->pmu.sample[__I915_SAMPLE_FREQ_ACT].cur,
523b46a33e2STvrtko Ursulin 				   FREQUENCY);
524b46a33e2STvrtko Ursulin 			break;
525b46a33e2STvrtko Ursulin 		case I915_PMU_REQUESTED_FREQUENCY:
526b46a33e2STvrtko Ursulin 			val =
527b46a33e2STvrtko Ursulin 			   div_u64(i915->pmu.sample[__I915_SAMPLE_FREQ_REQ].cur,
528b46a33e2STvrtko Ursulin 				   FREQUENCY);
529b46a33e2STvrtko Ursulin 			break;
5300cd4684dSTvrtko Ursulin 		case I915_PMU_INTERRUPTS:
5310cd4684dSTvrtko Ursulin 			val = count_interrupts(i915);
5320cd4684dSTvrtko Ursulin 			break;
5336060b6aeSTvrtko Ursulin 		case I915_PMU_RC6_RESIDENCY:
534ad055fb8STvrtko Ursulin 			val = get_rc6(i915);
5356060b6aeSTvrtko Ursulin 			break;
536b46a33e2STvrtko Ursulin 		}
537b46a33e2STvrtko Ursulin 	}
538b46a33e2STvrtko Ursulin 
539b46a33e2STvrtko Ursulin 	return val;
540b46a33e2STvrtko Ursulin }
541b46a33e2STvrtko Ursulin 
542b46a33e2STvrtko Ursulin static void i915_pmu_event_read(struct perf_event *event)
543b46a33e2STvrtko Ursulin {
544b46a33e2STvrtko Ursulin 	struct hw_perf_event *hwc = &event->hw;
545b46a33e2STvrtko Ursulin 	u64 prev, new;
546b46a33e2STvrtko Ursulin 
547b46a33e2STvrtko Ursulin again:
548b46a33e2STvrtko Ursulin 	prev = local64_read(&hwc->prev_count);
549ad055fb8STvrtko Ursulin 	new = __i915_pmu_event_read(event);
550b46a33e2STvrtko Ursulin 
551b46a33e2STvrtko Ursulin 	if (local64_cmpxchg(&hwc->prev_count, prev, new) != prev)
552b46a33e2STvrtko Ursulin 		goto again;
553b46a33e2STvrtko Ursulin 
554b46a33e2STvrtko Ursulin 	local64_add(new - prev, &event->count);
555b46a33e2STvrtko Ursulin }
556b46a33e2STvrtko Ursulin 
557b46a33e2STvrtko Ursulin static void i915_pmu_enable(struct perf_event *event)
558b46a33e2STvrtko Ursulin {
559b46a33e2STvrtko Ursulin 	struct drm_i915_private *i915 =
560b46a33e2STvrtko Ursulin 		container_of(event->pmu, typeof(*i915), pmu.base);
561b46a33e2STvrtko Ursulin 	unsigned int bit = event_enabled_bit(event);
562b46a33e2STvrtko Ursulin 	unsigned long flags;
563b46a33e2STvrtko Ursulin 
564b46a33e2STvrtko Ursulin 	spin_lock_irqsave(&i915->pmu.lock, flags);
565b46a33e2STvrtko Ursulin 
566b46a33e2STvrtko Ursulin 	/*
567b46a33e2STvrtko Ursulin 	 * Update the bitmask of enabled events and increment
568b46a33e2STvrtko Ursulin 	 * the event reference counter.
569b46a33e2STvrtko Ursulin 	 */
570b46a33e2STvrtko Ursulin 	GEM_BUG_ON(bit >= I915_PMU_MASK_BITS);
571b46a33e2STvrtko Ursulin 	GEM_BUG_ON(i915->pmu.enable_count[bit] == ~0);
572b46a33e2STvrtko Ursulin 	i915->pmu.enable |= BIT_ULL(bit);
573b46a33e2STvrtko Ursulin 	i915->pmu.enable_count[bit]++;
574b46a33e2STvrtko Ursulin 
575b46a33e2STvrtko Ursulin 	/*
576feff0dc6STvrtko Ursulin 	 * Start the sampling timer if needed and not already enabled.
577feff0dc6STvrtko Ursulin 	 */
578feff0dc6STvrtko Ursulin 	__i915_pmu_maybe_start_timer(i915);
579feff0dc6STvrtko Ursulin 
580feff0dc6STvrtko Ursulin 	/*
581b46a33e2STvrtko Ursulin 	 * For per-engine events the bitmask and reference counting
582b46a33e2STvrtko Ursulin 	 * is stored per engine.
583b46a33e2STvrtko Ursulin 	 */
584b46a33e2STvrtko Ursulin 	if (is_engine_event(event)) {
585b46a33e2STvrtko Ursulin 		u8 sample = engine_event_sample(event);
586b46a33e2STvrtko Ursulin 		struct intel_engine_cs *engine;
587b46a33e2STvrtko Ursulin 
588b46a33e2STvrtko Ursulin 		engine = intel_engine_lookup_user(i915,
589b46a33e2STvrtko Ursulin 						  engine_event_class(event),
590b46a33e2STvrtko Ursulin 						  engine_event_instance(event));
591b46a33e2STvrtko Ursulin 		GEM_BUG_ON(!engine);
592b46a33e2STvrtko Ursulin 		engine->pmu.enable |= BIT(sample);
593b46a33e2STvrtko Ursulin 
594b46a33e2STvrtko Ursulin 		GEM_BUG_ON(sample >= I915_PMU_SAMPLE_BITS);
595b46a33e2STvrtko Ursulin 		GEM_BUG_ON(engine->pmu.enable_count[sample] == ~0);
596b2f78cdaSTvrtko Ursulin 		engine->pmu.enable_count[sample]++;
597b46a33e2STvrtko Ursulin 	}
598b46a33e2STvrtko Ursulin 
599ad055fb8STvrtko Ursulin 	spin_unlock_irqrestore(&i915->pmu.lock, flags);
600ad055fb8STvrtko Ursulin 
601b46a33e2STvrtko Ursulin 	/*
602b46a33e2STvrtko Ursulin 	 * Store the current counter value so we can report the correct delta
603b46a33e2STvrtko Ursulin 	 * for all listeners. Even when the event was already enabled and has
604b46a33e2STvrtko Ursulin 	 * an existing non-zero value.
605b46a33e2STvrtko Ursulin 	 */
606ad055fb8STvrtko Ursulin 	local64_set(&event->hw.prev_count, __i915_pmu_event_read(event));
607b46a33e2STvrtko Ursulin }
608b46a33e2STvrtko Ursulin 
609b46a33e2STvrtko Ursulin static void i915_pmu_disable(struct perf_event *event)
610b46a33e2STvrtko Ursulin {
611b46a33e2STvrtko Ursulin 	struct drm_i915_private *i915 =
612b46a33e2STvrtko Ursulin 		container_of(event->pmu, typeof(*i915), pmu.base);
613b46a33e2STvrtko Ursulin 	unsigned int bit = event_enabled_bit(event);
614b46a33e2STvrtko Ursulin 	unsigned long flags;
615b46a33e2STvrtko Ursulin 
616b46a33e2STvrtko Ursulin 	spin_lock_irqsave(&i915->pmu.lock, flags);
617b46a33e2STvrtko Ursulin 
618b46a33e2STvrtko Ursulin 	if (is_engine_event(event)) {
619b46a33e2STvrtko Ursulin 		u8 sample = engine_event_sample(event);
620b46a33e2STvrtko Ursulin 		struct intel_engine_cs *engine;
621b46a33e2STvrtko Ursulin 
622b46a33e2STvrtko Ursulin 		engine = intel_engine_lookup_user(i915,
623b46a33e2STvrtko Ursulin 						  engine_event_class(event),
624b46a33e2STvrtko Ursulin 						  engine_event_instance(event));
625b46a33e2STvrtko Ursulin 		GEM_BUG_ON(!engine);
626b46a33e2STvrtko Ursulin 		GEM_BUG_ON(sample >= I915_PMU_SAMPLE_BITS);
627b46a33e2STvrtko Ursulin 		GEM_BUG_ON(engine->pmu.enable_count[sample] == 0);
628b46a33e2STvrtko Ursulin 		/*
629b46a33e2STvrtko Ursulin 		 * Decrement the reference count and clear the enabled
630b46a33e2STvrtko Ursulin 		 * bitmask when the last listener on an event goes away.
631b46a33e2STvrtko Ursulin 		 */
632b2f78cdaSTvrtko Ursulin 		if (--engine->pmu.enable_count[sample] == 0)
633b46a33e2STvrtko Ursulin 			engine->pmu.enable &= ~BIT(sample);
634b46a33e2STvrtko Ursulin 	}
635b46a33e2STvrtko Ursulin 
636b46a33e2STvrtko Ursulin 	GEM_BUG_ON(bit >= I915_PMU_MASK_BITS);
637b46a33e2STvrtko Ursulin 	GEM_BUG_ON(i915->pmu.enable_count[bit] == 0);
638b46a33e2STvrtko Ursulin 	/*
639b46a33e2STvrtko Ursulin 	 * Decrement the reference count and clear the enabled
640b46a33e2STvrtko Ursulin 	 * bitmask when the last listener on an event goes away.
641b46a33e2STvrtko Ursulin 	 */
642feff0dc6STvrtko Ursulin 	if (--i915->pmu.enable_count[bit] == 0) {
643b46a33e2STvrtko Ursulin 		i915->pmu.enable &= ~BIT_ULL(bit);
644feff0dc6STvrtko Ursulin 		i915->pmu.timer_enabled &= pmu_needs_timer(i915, true);
645feff0dc6STvrtko Ursulin 	}
646b46a33e2STvrtko Ursulin 
647b46a33e2STvrtko Ursulin 	spin_unlock_irqrestore(&i915->pmu.lock, flags);
648b46a33e2STvrtko Ursulin }
649b46a33e2STvrtko Ursulin 
650b46a33e2STvrtko Ursulin static void i915_pmu_event_start(struct perf_event *event, int flags)
651b46a33e2STvrtko Ursulin {
652b46a33e2STvrtko Ursulin 	i915_pmu_enable(event);
653b46a33e2STvrtko Ursulin 	event->hw.state = 0;
654b46a33e2STvrtko Ursulin }
655b46a33e2STvrtko Ursulin 
656b46a33e2STvrtko Ursulin static void i915_pmu_event_stop(struct perf_event *event, int flags)
657b46a33e2STvrtko Ursulin {
658b46a33e2STvrtko Ursulin 	if (flags & PERF_EF_UPDATE)
659b46a33e2STvrtko Ursulin 		i915_pmu_event_read(event);
660b46a33e2STvrtko Ursulin 	i915_pmu_disable(event);
661b46a33e2STvrtko Ursulin 	event->hw.state = PERF_HES_STOPPED;
662b46a33e2STvrtko Ursulin }
663b46a33e2STvrtko Ursulin 
664b46a33e2STvrtko Ursulin static int i915_pmu_event_add(struct perf_event *event, int flags)
665b46a33e2STvrtko Ursulin {
666b46a33e2STvrtko Ursulin 	if (flags & PERF_EF_START)
667b46a33e2STvrtko Ursulin 		i915_pmu_event_start(event, flags);
668b46a33e2STvrtko Ursulin 
669b46a33e2STvrtko Ursulin 	return 0;
670b46a33e2STvrtko Ursulin }
671b46a33e2STvrtko Ursulin 
672b46a33e2STvrtko Ursulin static void i915_pmu_event_del(struct perf_event *event, int flags)
673b46a33e2STvrtko Ursulin {
674b46a33e2STvrtko Ursulin 	i915_pmu_event_stop(event, PERF_EF_UPDATE);
675b46a33e2STvrtko Ursulin }
676b46a33e2STvrtko Ursulin 
677b46a33e2STvrtko Ursulin static int i915_pmu_event_event_idx(struct perf_event *event)
678b46a33e2STvrtko Ursulin {
679b46a33e2STvrtko Ursulin 	return 0;
680b46a33e2STvrtko Ursulin }
681b46a33e2STvrtko Ursulin 
682b7d3aabfSChris Wilson struct i915_str_attribute {
683b7d3aabfSChris Wilson 	struct device_attribute attr;
684b7d3aabfSChris Wilson 	const char *str;
685b7d3aabfSChris Wilson };
686b7d3aabfSChris Wilson 
687b46a33e2STvrtko Ursulin static ssize_t i915_pmu_format_show(struct device *dev,
688b46a33e2STvrtko Ursulin 				    struct device_attribute *attr, char *buf)
689b46a33e2STvrtko Ursulin {
690b7d3aabfSChris Wilson 	struct i915_str_attribute *eattr;
691b46a33e2STvrtko Ursulin 
692b7d3aabfSChris Wilson 	eattr = container_of(attr, struct i915_str_attribute, attr);
693b7d3aabfSChris Wilson 	return sprintf(buf, "%s\n", eattr->str);
694b46a33e2STvrtko Ursulin }
695b46a33e2STvrtko Ursulin 
696b46a33e2STvrtko Ursulin #define I915_PMU_FORMAT_ATTR(_name, _config) \
697b7d3aabfSChris Wilson 	(&((struct i915_str_attribute[]) { \
698b46a33e2STvrtko Ursulin 		{ .attr = __ATTR(_name, 0444, i915_pmu_format_show, NULL), \
699b7d3aabfSChris Wilson 		  .str = _config, } \
700b46a33e2STvrtko Ursulin 	})[0].attr.attr)
701b46a33e2STvrtko Ursulin 
702b46a33e2STvrtko Ursulin static struct attribute *i915_pmu_format_attrs[] = {
703b46a33e2STvrtko Ursulin 	I915_PMU_FORMAT_ATTR(i915_eventid, "config:0-20"),
704b46a33e2STvrtko Ursulin 	NULL,
705b46a33e2STvrtko Ursulin };
706b46a33e2STvrtko Ursulin 
707b46a33e2STvrtko Ursulin static const struct attribute_group i915_pmu_format_attr_group = {
708b46a33e2STvrtko Ursulin 	.name = "format",
709b46a33e2STvrtko Ursulin 	.attrs = i915_pmu_format_attrs,
710b46a33e2STvrtko Ursulin };
711b46a33e2STvrtko Ursulin 
712b7d3aabfSChris Wilson struct i915_ext_attribute {
713b7d3aabfSChris Wilson 	struct device_attribute attr;
714b7d3aabfSChris Wilson 	unsigned long val;
715b7d3aabfSChris Wilson };
716b7d3aabfSChris Wilson 
717b46a33e2STvrtko Ursulin static ssize_t i915_pmu_event_show(struct device *dev,
718b46a33e2STvrtko Ursulin 				   struct device_attribute *attr, char *buf)
719b46a33e2STvrtko Ursulin {
720b7d3aabfSChris Wilson 	struct i915_ext_attribute *eattr;
721b46a33e2STvrtko Ursulin 
722b7d3aabfSChris Wilson 	eattr = container_of(attr, struct i915_ext_attribute, attr);
723b7d3aabfSChris Wilson 	return sprintf(buf, "config=0x%lx\n", eattr->val);
724b46a33e2STvrtko Ursulin }
725b46a33e2STvrtko Ursulin 
726109ec558STvrtko Ursulin static struct attribute_group i915_pmu_events_attr_group = {
727b46a33e2STvrtko Ursulin 	.name = "events",
728109ec558STvrtko Ursulin 	/* Patch in attrs at runtime. */
729b46a33e2STvrtko Ursulin };
730b46a33e2STvrtko Ursulin 
731b46a33e2STvrtko Ursulin static ssize_t
732b46a33e2STvrtko Ursulin i915_pmu_get_attr_cpumask(struct device *dev,
733b46a33e2STvrtko Ursulin 			  struct device_attribute *attr,
734b46a33e2STvrtko Ursulin 			  char *buf)
735b46a33e2STvrtko Ursulin {
736b46a33e2STvrtko Ursulin 	return cpumap_print_to_pagebuf(true, buf, &i915_pmu_cpumask);
737b46a33e2STvrtko Ursulin }
738b46a33e2STvrtko Ursulin 
739b46a33e2STvrtko Ursulin static DEVICE_ATTR(cpumask, 0444, i915_pmu_get_attr_cpumask, NULL);
740b46a33e2STvrtko Ursulin 
741b46a33e2STvrtko Ursulin static struct attribute *i915_cpumask_attrs[] = {
742b46a33e2STvrtko Ursulin 	&dev_attr_cpumask.attr,
743b46a33e2STvrtko Ursulin 	NULL,
744b46a33e2STvrtko Ursulin };
745b46a33e2STvrtko Ursulin 
746109ec558STvrtko Ursulin static const struct attribute_group i915_pmu_cpumask_attr_group = {
747b46a33e2STvrtko Ursulin 	.attrs = i915_cpumask_attrs,
748b46a33e2STvrtko Ursulin };
749b46a33e2STvrtko Ursulin 
750b46a33e2STvrtko Ursulin static const struct attribute_group *i915_pmu_attr_groups[] = {
751b46a33e2STvrtko Ursulin 	&i915_pmu_format_attr_group,
752b46a33e2STvrtko Ursulin 	&i915_pmu_events_attr_group,
753b46a33e2STvrtko Ursulin 	&i915_pmu_cpumask_attr_group,
754b46a33e2STvrtko Ursulin 	NULL
755b46a33e2STvrtko Ursulin };
756b46a33e2STvrtko Ursulin 
757109ec558STvrtko Ursulin #define __event(__config, __name, __unit) \
758109ec558STvrtko Ursulin { \
759109ec558STvrtko Ursulin 	.config = (__config), \
760109ec558STvrtko Ursulin 	.name = (__name), \
761109ec558STvrtko Ursulin 	.unit = (__unit), \
762109ec558STvrtko Ursulin }
763109ec558STvrtko Ursulin 
764109ec558STvrtko Ursulin #define __engine_event(__sample, __name) \
765109ec558STvrtko Ursulin { \
766109ec558STvrtko Ursulin 	.sample = (__sample), \
767109ec558STvrtko Ursulin 	.name = (__name), \
768109ec558STvrtko Ursulin }
769109ec558STvrtko Ursulin 
770109ec558STvrtko Ursulin static struct i915_ext_attribute *
771109ec558STvrtko Ursulin add_i915_attr(struct i915_ext_attribute *attr, const char *name, u64 config)
772109ec558STvrtko Ursulin {
7732bbba4e9SChris Wilson 	sysfs_attr_init(&attr->attr.attr);
774109ec558STvrtko Ursulin 	attr->attr.attr.name = name;
775109ec558STvrtko Ursulin 	attr->attr.attr.mode = 0444;
776109ec558STvrtko Ursulin 	attr->attr.show = i915_pmu_event_show;
777109ec558STvrtko Ursulin 	attr->val = config;
778109ec558STvrtko Ursulin 
779109ec558STvrtko Ursulin 	return ++attr;
780109ec558STvrtko Ursulin }
781109ec558STvrtko Ursulin 
782109ec558STvrtko Ursulin static struct perf_pmu_events_attr *
783109ec558STvrtko Ursulin add_pmu_attr(struct perf_pmu_events_attr *attr, const char *name,
784109ec558STvrtko Ursulin 	     const char *str)
785109ec558STvrtko Ursulin {
7862bbba4e9SChris Wilson 	sysfs_attr_init(&attr->attr.attr);
787109ec558STvrtko Ursulin 	attr->attr.attr.name = name;
788109ec558STvrtko Ursulin 	attr->attr.attr.mode = 0444;
789109ec558STvrtko Ursulin 	attr->attr.show = perf_event_sysfs_show;
790109ec558STvrtko Ursulin 	attr->event_str = str;
791109ec558STvrtko Ursulin 
792109ec558STvrtko Ursulin 	return ++attr;
793109ec558STvrtko Ursulin }
794109ec558STvrtko Ursulin 
795109ec558STvrtko Ursulin static struct attribute **
796109ec558STvrtko Ursulin create_event_attributes(struct drm_i915_private *i915)
797109ec558STvrtko Ursulin {
798109ec558STvrtko Ursulin 	static const struct {
799109ec558STvrtko Ursulin 		u64 config;
800109ec558STvrtko Ursulin 		const char *name;
801109ec558STvrtko Ursulin 		const char *unit;
802109ec558STvrtko Ursulin 	} events[] = {
803109ec558STvrtko Ursulin 		__event(I915_PMU_ACTUAL_FREQUENCY, "actual-frequency", "MHz"),
804109ec558STvrtko Ursulin 		__event(I915_PMU_REQUESTED_FREQUENCY, "requested-frequency", "MHz"),
805109ec558STvrtko Ursulin 		__event(I915_PMU_INTERRUPTS, "interrupts", NULL),
806109ec558STvrtko Ursulin 		__event(I915_PMU_RC6_RESIDENCY, "rc6-residency", "ns"),
807109ec558STvrtko Ursulin 	};
808109ec558STvrtko Ursulin 	static const struct {
809109ec558STvrtko Ursulin 		enum drm_i915_pmu_engine_sample sample;
810109ec558STvrtko Ursulin 		char *name;
811109ec558STvrtko Ursulin 	} engine_events[] = {
812109ec558STvrtko Ursulin 		__engine_event(I915_SAMPLE_BUSY, "busy"),
813109ec558STvrtko Ursulin 		__engine_event(I915_SAMPLE_SEMA, "sema"),
814109ec558STvrtko Ursulin 		__engine_event(I915_SAMPLE_WAIT, "wait"),
815109ec558STvrtko Ursulin 	};
816109ec558STvrtko Ursulin 	unsigned int count = 0;
817109ec558STvrtko Ursulin 	struct perf_pmu_events_attr *pmu_attr = NULL, *pmu_iter;
818109ec558STvrtko Ursulin 	struct i915_ext_attribute *i915_attr = NULL, *i915_iter;
819109ec558STvrtko Ursulin 	struct attribute **attr = NULL, **attr_iter;
820109ec558STvrtko Ursulin 	struct intel_engine_cs *engine;
821109ec558STvrtko Ursulin 	enum intel_engine_id id;
822109ec558STvrtko Ursulin 	unsigned int i;
823109ec558STvrtko Ursulin 
824109ec558STvrtko Ursulin 	/* Count how many counters we will be exposing. */
825109ec558STvrtko Ursulin 	for (i = 0; i < ARRAY_SIZE(events); i++) {
826109ec558STvrtko Ursulin 		if (!config_status(i915, events[i].config))
827109ec558STvrtko Ursulin 			count++;
828109ec558STvrtko Ursulin 	}
829109ec558STvrtko Ursulin 
830109ec558STvrtko Ursulin 	for_each_engine(engine, i915, id) {
831109ec558STvrtko Ursulin 		for (i = 0; i < ARRAY_SIZE(engine_events); i++) {
832109ec558STvrtko Ursulin 			if (!engine_event_status(engine,
833109ec558STvrtko Ursulin 						 engine_events[i].sample))
834109ec558STvrtko Ursulin 				count++;
835109ec558STvrtko Ursulin 		}
836109ec558STvrtko Ursulin 	}
837109ec558STvrtko Ursulin 
838109ec558STvrtko Ursulin 	/* Allocate attribute objects and table. */
839dd5fec87STvrtko Ursulin 	i915_attr = kcalloc(count, sizeof(*i915_attr), GFP_KERNEL);
840109ec558STvrtko Ursulin 	if (!i915_attr)
841109ec558STvrtko Ursulin 		goto err_alloc;
842109ec558STvrtko Ursulin 
843dd5fec87STvrtko Ursulin 	pmu_attr = kcalloc(count, sizeof(*pmu_attr), GFP_KERNEL);
844109ec558STvrtko Ursulin 	if (!pmu_attr)
845109ec558STvrtko Ursulin 		goto err_alloc;
846109ec558STvrtko Ursulin 
847109ec558STvrtko Ursulin 	/* Max one pointer of each attribute type plus a termination entry. */
848dd5fec87STvrtko Ursulin 	attr = kcalloc(count * 2 + 1, sizeof(*attr), GFP_KERNEL);
849109ec558STvrtko Ursulin 	if (!attr)
850109ec558STvrtko Ursulin 		goto err_alloc;
851109ec558STvrtko Ursulin 
852109ec558STvrtko Ursulin 	i915_iter = i915_attr;
853109ec558STvrtko Ursulin 	pmu_iter = pmu_attr;
854109ec558STvrtko Ursulin 	attr_iter = attr;
855109ec558STvrtko Ursulin 
856109ec558STvrtko Ursulin 	/* Initialize supported non-engine counters. */
857109ec558STvrtko Ursulin 	for (i = 0; i < ARRAY_SIZE(events); i++) {
858109ec558STvrtko Ursulin 		char *str;
859109ec558STvrtko Ursulin 
860109ec558STvrtko Ursulin 		if (config_status(i915, events[i].config))
861109ec558STvrtko Ursulin 			continue;
862109ec558STvrtko Ursulin 
863109ec558STvrtko Ursulin 		str = kstrdup(events[i].name, GFP_KERNEL);
864109ec558STvrtko Ursulin 		if (!str)
865109ec558STvrtko Ursulin 			goto err;
866109ec558STvrtko Ursulin 
867109ec558STvrtko Ursulin 		*attr_iter++ = &i915_iter->attr.attr;
868109ec558STvrtko Ursulin 		i915_iter = add_i915_attr(i915_iter, str, events[i].config);
869109ec558STvrtko Ursulin 
870109ec558STvrtko Ursulin 		if (events[i].unit) {
871109ec558STvrtko Ursulin 			str = kasprintf(GFP_KERNEL, "%s.unit", events[i].name);
872109ec558STvrtko Ursulin 			if (!str)
873109ec558STvrtko Ursulin 				goto err;
874109ec558STvrtko Ursulin 
875109ec558STvrtko Ursulin 			*attr_iter++ = &pmu_iter->attr.attr;
876109ec558STvrtko Ursulin 			pmu_iter = add_pmu_attr(pmu_iter, str, events[i].unit);
877109ec558STvrtko Ursulin 		}
878109ec558STvrtko Ursulin 	}
879109ec558STvrtko Ursulin 
880109ec558STvrtko Ursulin 	/* Initialize supported engine counters. */
881109ec558STvrtko Ursulin 	for_each_engine(engine, i915, id) {
882109ec558STvrtko Ursulin 		for (i = 0; i < ARRAY_SIZE(engine_events); i++) {
883109ec558STvrtko Ursulin 			char *str;
884109ec558STvrtko Ursulin 
885109ec558STvrtko Ursulin 			if (engine_event_status(engine,
886109ec558STvrtko Ursulin 						engine_events[i].sample))
887109ec558STvrtko Ursulin 				continue;
888109ec558STvrtko Ursulin 
889109ec558STvrtko Ursulin 			str = kasprintf(GFP_KERNEL, "%s-%s",
890109ec558STvrtko Ursulin 					engine->name, engine_events[i].name);
891109ec558STvrtko Ursulin 			if (!str)
892109ec558STvrtko Ursulin 				goto err;
893109ec558STvrtko Ursulin 
894109ec558STvrtko Ursulin 			*attr_iter++ = &i915_iter->attr.attr;
895109ec558STvrtko Ursulin 			i915_iter =
896109ec558STvrtko Ursulin 				add_i915_attr(i915_iter, str,
8978810bc56STvrtko Ursulin 					      __I915_PMU_ENGINE(engine->uabi_class,
898109ec558STvrtko Ursulin 								engine->instance,
899109ec558STvrtko Ursulin 								engine_events[i].sample));
900109ec558STvrtko Ursulin 
901109ec558STvrtko Ursulin 			str = kasprintf(GFP_KERNEL, "%s-%s.unit",
902109ec558STvrtko Ursulin 					engine->name, engine_events[i].name);
903109ec558STvrtko Ursulin 			if (!str)
904109ec558STvrtko Ursulin 				goto err;
905109ec558STvrtko Ursulin 
906109ec558STvrtko Ursulin 			*attr_iter++ = &pmu_iter->attr.attr;
907109ec558STvrtko Ursulin 			pmu_iter = add_pmu_attr(pmu_iter, str, "ns");
908109ec558STvrtko Ursulin 		}
909109ec558STvrtko Ursulin 	}
910109ec558STvrtko Ursulin 
911109ec558STvrtko Ursulin 	i915->pmu.i915_attr = i915_attr;
912109ec558STvrtko Ursulin 	i915->pmu.pmu_attr = pmu_attr;
913109ec558STvrtko Ursulin 
914109ec558STvrtko Ursulin 	return attr;
915109ec558STvrtko Ursulin 
916109ec558STvrtko Ursulin err:;
917109ec558STvrtko Ursulin 	for (attr_iter = attr; *attr_iter; attr_iter++)
918109ec558STvrtko Ursulin 		kfree((*attr_iter)->name);
919109ec558STvrtko Ursulin 
920109ec558STvrtko Ursulin err_alloc:
921109ec558STvrtko Ursulin 	kfree(attr);
922109ec558STvrtko Ursulin 	kfree(i915_attr);
923109ec558STvrtko Ursulin 	kfree(pmu_attr);
924109ec558STvrtko Ursulin 
925109ec558STvrtko Ursulin 	return NULL;
926109ec558STvrtko Ursulin }
927109ec558STvrtko Ursulin 
928109ec558STvrtko Ursulin static void free_event_attributes(struct drm_i915_private *i915)
929109ec558STvrtko Ursulin {
930109ec558STvrtko Ursulin 	struct attribute **attr_iter = i915_pmu_events_attr_group.attrs;
931109ec558STvrtko Ursulin 
932109ec558STvrtko Ursulin 	for (; *attr_iter; attr_iter++)
933109ec558STvrtko Ursulin 		kfree((*attr_iter)->name);
934109ec558STvrtko Ursulin 
935109ec558STvrtko Ursulin 	kfree(i915_pmu_events_attr_group.attrs);
936109ec558STvrtko Ursulin 	kfree(i915->pmu.i915_attr);
937109ec558STvrtko Ursulin 	kfree(i915->pmu.pmu_attr);
938109ec558STvrtko Ursulin 
939109ec558STvrtko Ursulin 	i915_pmu_events_attr_group.attrs = NULL;
940109ec558STvrtko Ursulin 	i915->pmu.i915_attr = NULL;
941109ec558STvrtko Ursulin 	i915->pmu.pmu_attr = NULL;
942109ec558STvrtko Ursulin }
943109ec558STvrtko Ursulin 
944b46a33e2STvrtko Ursulin static int i915_pmu_cpu_online(unsigned int cpu, struct hlist_node *node)
945b46a33e2STvrtko Ursulin {
946b46a33e2STvrtko Ursulin 	struct i915_pmu *pmu = hlist_entry_safe(node, typeof(*pmu), node);
947b46a33e2STvrtko Ursulin 
948b46a33e2STvrtko Ursulin 	GEM_BUG_ON(!pmu->base.event_init);
949b46a33e2STvrtko Ursulin 
950b46a33e2STvrtko Ursulin 	/* Select the first online CPU as a designated reader. */
9510426c046STvrtko Ursulin 	if (!cpumask_weight(&i915_pmu_cpumask))
952b46a33e2STvrtko Ursulin 		cpumask_set_cpu(cpu, &i915_pmu_cpumask);
953b46a33e2STvrtko Ursulin 
954b46a33e2STvrtko Ursulin 	return 0;
955b46a33e2STvrtko Ursulin }
956b46a33e2STvrtko Ursulin 
957b46a33e2STvrtko Ursulin static int i915_pmu_cpu_offline(unsigned int cpu, struct hlist_node *node)
958b46a33e2STvrtko Ursulin {
959b46a33e2STvrtko Ursulin 	struct i915_pmu *pmu = hlist_entry_safe(node, typeof(*pmu), node);
960b46a33e2STvrtko Ursulin 	unsigned int target;
961b46a33e2STvrtko Ursulin 
962b46a33e2STvrtko Ursulin 	GEM_BUG_ON(!pmu->base.event_init);
963b46a33e2STvrtko Ursulin 
964b46a33e2STvrtko Ursulin 	if (cpumask_test_and_clear_cpu(cpu, &i915_pmu_cpumask)) {
965b46a33e2STvrtko Ursulin 		target = cpumask_any_but(topology_sibling_cpumask(cpu), cpu);
966b46a33e2STvrtko Ursulin 		/* Migrate events if there is a valid target */
967b46a33e2STvrtko Ursulin 		if (target < nr_cpu_ids) {
968b46a33e2STvrtko Ursulin 			cpumask_set_cpu(target, &i915_pmu_cpumask);
969b46a33e2STvrtko Ursulin 			perf_pmu_migrate_context(&pmu->base, cpu, target);
970b46a33e2STvrtko Ursulin 		}
971b46a33e2STvrtko Ursulin 	}
972b46a33e2STvrtko Ursulin 
973b46a33e2STvrtko Ursulin 	return 0;
974b46a33e2STvrtko Ursulin }
975b46a33e2STvrtko Ursulin 
976b46a33e2STvrtko Ursulin static enum cpuhp_state cpuhp_slot = CPUHP_INVALID;
977b46a33e2STvrtko Ursulin 
978b46a33e2STvrtko Ursulin static int i915_pmu_register_cpuhp_state(struct drm_i915_private *i915)
979b46a33e2STvrtko Ursulin {
980b46a33e2STvrtko Ursulin 	enum cpuhp_state slot;
981b46a33e2STvrtko Ursulin 	int ret;
982b46a33e2STvrtko Ursulin 
983b46a33e2STvrtko Ursulin 	ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN,
984b46a33e2STvrtko Ursulin 				      "perf/x86/intel/i915:online",
985b46a33e2STvrtko Ursulin 				      i915_pmu_cpu_online,
986b46a33e2STvrtko Ursulin 				      i915_pmu_cpu_offline);
987b46a33e2STvrtko Ursulin 	if (ret < 0)
988b46a33e2STvrtko Ursulin 		return ret;
989b46a33e2STvrtko Ursulin 
990b46a33e2STvrtko Ursulin 	slot = ret;
991b46a33e2STvrtko Ursulin 	ret = cpuhp_state_add_instance(slot, &i915->pmu.node);
992b46a33e2STvrtko Ursulin 	if (ret) {
993b46a33e2STvrtko Ursulin 		cpuhp_remove_multi_state(slot);
994b46a33e2STvrtko Ursulin 		return ret;
995b46a33e2STvrtko Ursulin 	}
996b46a33e2STvrtko Ursulin 
997b46a33e2STvrtko Ursulin 	cpuhp_slot = slot;
998b46a33e2STvrtko Ursulin 	return 0;
999b46a33e2STvrtko Ursulin }
1000b46a33e2STvrtko Ursulin 
1001b46a33e2STvrtko Ursulin static void i915_pmu_unregister_cpuhp_state(struct drm_i915_private *i915)
1002b46a33e2STvrtko Ursulin {
1003b46a33e2STvrtko Ursulin 	WARN_ON(cpuhp_slot == CPUHP_INVALID);
1004b46a33e2STvrtko Ursulin 	WARN_ON(cpuhp_state_remove_instance(cpuhp_slot, &i915->pmu.node));
1005b46a33e2STvrtko Ursulin 	cpuhp_remove_multi_state(cpuhp_slot);
1006b46a33e2STvrtko Ursulin }
1007b46a33e2STvrtko Ursulin 
1008b46a33e2STvrtko Ursulin void i915_pmu_register(struct drm_i915_private *i915)
1009b46a33e2STvrtko Ursulin {
1010b46a33e2STvrtko Ursulin 	int ret;
1011b46a33e2STvrtko Ursulin 
1012b46a33e2STvrtko Ursulin 	if (INTEL_GEN(i915) <= 2) {
1013b46a33e2STvrtko Ursulin 		DRM_INFO("PMU not supported for this GPU.");
1014b46a33e2STvrtko Ursulin 		return;
1015b46a33e2STvrtko Ursulin 	}
1016b46a33e2STvrtko Ursulin 
1017109ec558STvrtko Ursulin 	i915_pmu_events_attr_group.attrs = create_event_attributes(i915);
1018109ec558STvrtko Ursulin 	if (!i915_pmu_events_attr_group.attrs) {
1019109ec558STvrtko Ursulin 		ret = -ENOMEM;
1020109ec558STvrtko Ursulin 		goto err;
1021109ec558STvrtko Ursulin 	}
1022109ec558STvrtko Ursulin 
1023b46a33e2STvrtko Ursulin 	i915->pmu.base.attr_groups	= i915_pmu_attr_groups;
1024b46a33e2STvrtko Ursulin 	i915->pmu.base.task_ctx_nr	= perf_invalid_context;
1025b46a33e2STvrtko Ursulin 	i915->pmu.base.event_init	= i915_pmu_event_init;
1026b46a33e2STvrtko Ursulin 	i915->pmu.base.add		= i915_pmu_event_add;
1027b46a33e2STvrtko Ursulin 	i915->pmu.base.del		= i915_pmu_event_del;
1028b46a33e2STvrtko Ursulin 	i915->pmu.base.start		= i915_pmu_event_start;
1029b46a33e2STvrtko Ursulin 	i915->pmu.base.stop		= i915_pmu_event_stop;
1030b46a33e2STvrtko Ursulin 	i915->pmu.base.read		= i915_pmu_event_read;
1031b46a33e2STvrtko Ursulin 	i915->pmu.base.event_idx	= i915_pmu_event_event_idx;
1032b46a33e2STvrtko Ursulin 
1033b46a33e2STvrtko Ursulin 	spin_lock_init(&i915->pmu.lock);
1034b46a33e2STvrtko Ursulin 	hrtimer_init(&i915->pmu.timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1035b46a33e2STvrtko Ursulin 	i915->pmu.timer.function = i915_sample;
1036b46a33e2STvrtko Ursulin 
1037b46a33e2STvrtko Ursulin 	ret = perf_pmu_register(&i915->pmu.base, "i915", -1);
1038b46a33e2STvrtko Ursulin 	if (ret)
1039b46a33e2STvrtko Ursulin 		goto err;
1040b46a33e2STvrtko Ursulin 
1041b46a33e2STvrtko Ursulin 	ret = i915_pmu_register_cpuhp_state(i915);
1042b46a33e2STvrtko Ursulin 	if (ret)
1043b46a33e2STvrtko Ursulin 		goto err_unreg;
1044b46a33e2STvrtko Ursulin 
1045b46a33e2STvrtko Ursulin 	return;
1046b46a33e2STvrtko Ursulin 
1047b46a33e2STvrtko Ursulin err_unreg:
1048b46a33e2STvrtko Ursulin 	perf_pmu_unregister(&i915->pmu.base);
1049b46a33e2STvrtko Ursulin err:
1050b46a33e2STvrtko Ursulin 	i915->pmu.base.event_init = NULL;
1051109ec558STvrtko Ursulin 	free_event_attributes(i915);
1052b46a33e2STvrtko Ursulin 	DRM_NOTE("Failed to register PMU! (err=%d)\n", ret);
1053b46a33e2STvrtko Ursulin }
1054b46a33e2STvrtko Ursulin 
1055b46a33e2STvrtko Ursulin void i915_pmu_unregister(struct drm_i915_private *i915)
1056b46a33e2STvrtko Ursulin {
1057b46a33e2STvrtko Ursulin 	if (!i915->pmu.base.event_init)
1058b46a33e2STvrtko Ursulin 		return;
1059b46a33e2STvrtko Ursulin 
1060b46a33e2STvrtko Ursulin 	WARN_ON(i915->pmu.enable);
1061b46a33e2STvrtko Ursulin 
1062b46a33e2STvrtko Ursulin 	hrtimer_cancel(&i915->pmu.timer);
1063b46a33e2STvrtko Ursulin 
1064b46a33e2STvrtko Ursulin 	i915_pmu_unregister_cpuhp_state(i915);
1065b46a33e2STvrtko Ursulin 
1066b46a33e2STvrtko Ursulin 	perf_pmu_unregister(&i915->pmu.base);
1067b46a33e2STvrtko Ursulin 	i915->pmu.base.event_init = NULL;
1068109ec558STvrtko Ursulin 	free_event_attributes(i915);
1069b46a33e2STvrtko Ursulin }
1070