xref: /openbmc/linux/drivers/gpu/drm/i915/i915_pmu.c (revision 651e7d48577ae28572b9aa1807a1331d1cd2b61f)
1b46a33e2STvrtko Ursulin /*
2058a9b43SMichal Wajdeczko  * SPDX-License-Identifier: MIT
3b46a33e2STvrtko Ursulin  *
4058a9b43SMichal Wajdeczko  * Copyright © 2017-2018 Intel Corporation
5b46a33e2STvrtko Ursulin  */
6b46a33e2STvrtko Ursulin 
73b4ed2e2SVincent Guittot #include <linux/pm_runtime.h>
8112ed2d3SChris Wilson 
9112ed2d3SChris Wilson #include "gt/intel_engine.h"
1051fbd8deSChris Wilson #include "gt/intel_engine_pm.h"
11750e76b4SChris Wilson #include "gt/intel_engine_user.h"
1251fbd8deSChris Wilson #include "gt/intel_gt_pm.h"
13c1132367SAndi Shyti #include "gt/intel_rc6.h"
143e7abf81SAndi Shyti #include "gt/intel_rps.h"
15112ed2d3SChris Wilson 
16058a9b43SMichal Wajdeczko #include "i915_drv.h"
17ecbb5fb7SJani Nikula #include "i915_pmu.h"
18ecbb5fb7SJani Nikula #include "intel_pm.h"
19b46a33e2STvrtko Ursulin 
20b46a33e2STvrtko Ursulin /* Frequency for the sampling timer for events which need it. */
21b46a33e2STvrtko Ursulin #define FREQUENCY 200
22b46a33e2STvrtko Ursulin #define PERIOD max_t(u64, 10000, NSEC_PER_SEC / FREQUENCY)
23b46a33e2STvrtko Ursulin 
24b46a33e2STvrtko Ursulin #define ENGINE_SAMPLE_MASK \
25b46a33e2STvrtko Ursulin 	(BIT(I915_SAMPLE_BUSY) | \
26b46a33e2STvrtko Ursulin 	 BIT(I915_SAMPLE_WAIT) | \
27b46a33e2STvrtko Ursulin 	 BIT(I915_SAMPLE_SEMA))
28b46a33e2STvrtko Ursulin 
29141a0895SChris Wilson static cpumask_t i915_pmu_cpumask;
30537f9c84STvrtko Ursulin static unsigned int i915_pmu_target_cpu = -1;
31b46a33e2STvrtko Ursulin 
32b46a33e2STvrtko Ursulin static u8 engine_config_sample(u64 config)
33b46a33e2STvrtko Ursulin {
34b46a33e2STvrtko Ursulin 	return config & I915_PMU_SAMPLE_MASK;
35b46a33e2STvrtko Ursulin }
36b46a33e2STvrtko Ursulin 
37b46a33e2STvrtko Ursulin static u8 engine_event_sample(struct perf_event *event)
38b46a33e2STvrtko Ursulin {
39b46a33e2STvrtko Ursulin 	return engine_config_sample(event->attr.config);
40b46a33e2STvrtko Ursulin }
41b46a33e2STvrtko Ursulin 
42b46a33e2STvrtko Ursulin static u8 engine_event_class(struct perf_event *event)
43b46a33e2STvrtko Ursulin {
44b46a33e2STvrtko Ursulin 	return (event->attr.config >> I915_PMU_CLASS_SHIFT) & 0xff;
45b46a33e2STvrtko Ursulin }
46b46a33e2STvrtko Ursulin 
47b46a33e2STvrtko Ursulin static u8 engine_event_instance(struct perf_event *event)
48b46a33e2STvrtko Ursulin {
49b46a33e2STvrtko Ursulin 	return (event->attr.config >> I915_PMU_SAMPLE_BITS) & 0xff;
50b46a33e2STvrtko Ursulin }
51b46a33e2STvrtko Ursulin 
52b46a33e2STvrtko Ursulin static bool is_engine_config(u64 config)
53b46a33e2STvrtko Ursulin {
54b46a33e2STvrtko Ursulin 	return config < __I915_PMU_OTHER(0);
55b46a33e2STvrtko Ursulin }
56b46a33e2STvrtko Ursulin 
57348fb0cbSTvrtko Ursulin static unsigned int other_bit(const u64 config)
58348fb0cbSTvrtko Ursulin {
59348fb0cbSTvrtko Ursulin 	unsigned int val;
60348fb0cbSTvrtko Ursulin 
61348fb0cbSTvrtko Ursulin 	switch (config) {
62348fb0cbSTvrtko Ursulin 	case I915_PMU_ACTUAL_FREQUENCY:
63348fb0cbSTvrtko Ursulin 		val =  __I915_PMU_ACTUAL_FREQUENCY_ENABLED;
64348fb0cbSTvrtko Ursulin 		break;
65348fb0cbSTvrtko Ursulin 	case I915_PMU_REQUESTED_FREQUENCY:
66348fb0cbSTvrtko Ursulin 		val = __I915_PMU_REQUESTED_FREQUENCY_ENABLED;
67348fb0cbSTvrtko Ursulin 		break;
68348fb0cbSTvrtko Ursulin 	case I915_PMU_RC6_RESIDENCY:
69348fb0cbSTvrtko Ursulin 		val = __I915_PMU_RC6_RESIDENCY_ENABLED;
70348fb0cbSTvrtko Ursulin 		break;
71348fb0cbSTvrtko Ursulin 	default:
72348fb0cbSTvrtko Ursulin 		/*
73348fb0cbSTvrtko Ursulin 		 * Events that do not require sampling, or tracking state
74348fb0cbSTvrtko Ursulin 		 * transitions between enabled and disabled can be ignored.
75348fb0cbSTvrtko Ursulin 		 */
76348fb0cbSTvrtko Ursulin 		return -1;
77348fb0cbSTvrtko Ursulin 	}
78348fb0cbSTvrtko Ursulin 
79348fb0cbSTvrtko Ursulin 	return I915_ENGINE_SAMPLE_COUNT + val;
80348fb0cbSTvrtko Ursulin }
81348fb0cbSTvrtko Ursulin 
82348fb0cbSTvrtko Ursulin static unsigned int config_bit(const u64 config)
83b46a33e2STvrtko Ursulin {
84b46a33e2STvrtko Ursulin 	if (is_engine_config(config))
85b46a33e2STvrtko Ursulin 		return engine_config_sample(config);
86b46a33e2STvrtko Ursulin 	else
87348fb0cbSTvrtko Ursulin 		return other_bit(config);
88b46a33e2STvrtko Ursulin }
89b46a33e2STvrtko Ursulin 
90348fb0cbSTvrtko Ursulin static u64 config_mask(u64 config)
91b46a33e2STvrtko Ursulin {
92348fb0cbSTvrtko Ursulin 	return BIT_ULL(config_bit(config));
93b46a33e2STvrtko Ursulin }
94b46a33e2STvrtko Ursulin 
95b46a33e2STvrtko Ursulin static bool is_engine_event(struct perf_event *event)
96b46a33e2STvrtko Ursulin {
97b46a33e2STvrtko Ursulin 	return is_engine_config(event->attr.config);
98b46a33e2STvrtko Ursulin }
99b46a33e2STvrtko Ursulin 
100348fb0cbSTvrtko Ursulin static unsigned int event_bit(struct perf_event *event)
101b46a33e2STvrtko Ursulin {
102348fb0cbSTvrtko Ursulin 	return config_bit(event->attr.config);
103b46a33e2STvrtko Ursulin }
104b46a33e2STvrtko Ursulin 
105908091c8STvrtko Ursulin static bool pmu_needs_timer(struct i915_pmu *pmu, bool gpu_active)
106feff0dc6STvrtko Ursulin {
107908091c8STvrtko Ursulin 	struct drm_i915_private *i915 = container_of(pmu, typeof(*i915), pmu);
108348fb0cbSTvrtko Ursulin 	u32 enable;
109feff0dc6STvrtko Ursulin 
110feff0dc6STvrtko Ursulin 	/*
111feff0dc6STvrtko Ursulin 	 * Only some counters need the sampling timer.
112feff0dc6STvrtko Ursulin 	 *
113feff0dc6STvrtko Ursulin 	 * We start with a bitmask of all currently enabled events.
114feff0dc6STvrtko Ursulin 	 */
115908091c8STvrtko Ursulin 	enable = pmu->enable;
116feff0dc6STvrtko Ursulin 
117feff0dc6STvrtko Ursulin 	/*
118feff0dc6STvrtko Ursulin 	 * Mask out all the ones which do not need the timer, or in
119feff0dc6STvrtko Ursulin 	 * other words keep all the ones that could need the timer.
120feff0dc6STvrtko Ursulin 	 */
121348fb0cbSTvrtko Ursulin 	enable &= config_mask(I915_PMU_ACTUAL_FREQUENCY) |
122348fb0cbSTvrtko Ursulin 		  config_mask(I915_PMU_REQUESTED_FREQUENCY) |
123feff0dc6STvrtko Ursulin 		  ENGINE_SAMPLE_MASK;
124feff0dc6STvrtko Ursulin 
125feff0dc6STvrtko Ursulin 	/*
126feff0dc6STvrtko Ursulin 	 * When the GPU is idle per-engine counters do not need to be
127feff0dc6STvrtko Ursulin 	 * running so clear those bits out.
128feff0dc6STvrtko Ursulin 	 */
129feff0dc6STvrtko Ursulin 	if (!gpu_active)
130feff0dc6STvrtko Ursulin 		enable &= ~ENGINE_SAMPLE_MASK;
131b3add01eSTvrtko Ursulin 	/*
132b3add01eSTvrtko Ursulin 	 * Also there is software busyness tracking available we do not
133b3add01eSTvrtko Ursulin 	 * need the timer for I915_SAMPLE_BUSY counter.
134b3add01eSTvrtko Ursulin 	 */
135bf73fc0fSChris Wilson 	else if (i915->caps.scheduler & I915_SCHEDULER_CAP_ENGINE_BUSY_STATS)
136b3add01eSTvrtko Ursulin 		enable &= ~BIT(I915_SAMPLE_BUSY);
137feff0dc6STvrtko Ursulin 
138feff0dc6STvrtko Ursulin 	/*
139feff0dc6STvrtko Ursulin 	 * If some bits remain it means we need the sampling timer running.
140feff0dc6STvrtko Ursulin 	 */
141feff0dc6STvrtko Ursulin 	return enable;
142feff0dc6STvrtko Ursulin }
143feff0dc6STvrtko Ursulin 
144c1132367SAndi Shyti static u64 __get_rc6(struct intel_gt *gt)
14516ffe73cSChris Wilson {
14616ffe73cSChris Wilson 	struct drm_i915_private *i915 = gt->i915;
14716ffe73cSChris Wilson 	u64 val;
14816ffe73cSChris Wilson 
149c1132367SAndi Shyti 	val = intel_rc6_residency_ns(&gt->rc6,
15016ffe73cSChris Wilson 				     IS_VALLEYVIEW(i915) ?
15116ffe73cSChris Wilson 				     VLV_GT_RENDER_RC6 :
15216ffe73cSChris Wilson 				     GEN6_GT_GFX_RC6);
15316ffe73cSChris Wilson 
15416ffe73cSChris Wilson 	if (HAS_RC6p(i915))
155c1132367SAndi Shyti 		val += intel_rc6_residency_ns(&gt->rc6, GEN6_GT_GFX_RC6p);
15616ffe73cSChris Wilson 
15716ffe73cSChris Wilson 	if (HAS_RC6pp(i915))
158c1132367SAndi Shyti 		val += intel_rc6_residency_ns(&gt->rc6, GEN6_GT_GFX_RC6pp);
15916ffe73cSChris Wilson 
16016ffe73cSChris Wilson 	return val;
16116ffe73cSChris Wilson }
16216ffe73cSChris Wilson 
163c51c29fbSTvrtko Ursulin static inline s64 ktime_since_raw(const ktime_t kt)
16416ffe73cSChris Wilson {
165c51c29fbSTvrtko Ursulin 	return ktime_to_ns(ktime_sub(ktime_get_raw(), kt));
16616ffe73cSChris Wilson }
16716ffe73cSChris Wilson 
168df6a4205STvrtko Ursulin static u64 get_rc6(struct intel_gt *gt)
16916ffe73cSChris Wilson {
170df6a4205STvrtko Ursulin 	struct drm_i915_private *i915 = gt->i915;
171df6a4205STvrtko Ursulin 	struct i915_pmu *pmu = &i915->pmu;
172df6a4205STvrtko Ursulin 	unsigned long flags;
173df6a4205STvrtko Ursulin 	bool awake = false;
17416ffe73cSChris Wilson 	u64 val;
17516ffe73cSChris Wilson 
176df6a4205STvrtko Ursulin 	if (intel_gt_pm_get_if_awake(gt)) {
177df6a4205STvrtko Ursulin 		val = __get_rc6(gt);
178df6a4205STvrtko Ursulin 		intel_gt_pm_put_async(gt);
179df6a4205STvrtko Ursulin 		awake = true;
180df6a4205STvrtko Ursulin 	}
181df6a4205STvrtko Ursulin 
182df6a4205STvrtko Ursulin 	spin_lock_irqsave(&pmu->lock, flags);
183df6a4205STvrtko Ursulin 
184df6a4205STvrtko Ursulin 	if (awake) {
185df6a4205STvrtko Ursulin 		pmu->sample[__I915_SAMPLE_RC6].cur = val;
186df6a4205STvrtko Ursulin 	} else {
18716ffe73cSChris Wilson 		/*
18816ffe73cSChris Wilson 		 * We think we are runtime suspended.
18916ffe73cSChris Wilson 		 *
19016ffe73cSChris Wilson 		 * Report the delta from when the device was suspended to now,
19116ffe73cSChris Wilson 		 * on top of the last known real value, as the approximated RC6
19216ffe73cSChris Wilson 		 * counter value.
19316ffe73cSChris Wilson 		 */
194c51c29fbSTvrtko Ursulin 		val = ktime_since_raw(pmu->sleep_last);
19516ffe73cSChris Wilson 		val += pmu->sample[__I915_SAMPLE_RC6].cur;
19616ffe73cSChris Wilson 	}
19716ffe73cSChris Wilson 
198df6a4205STvrtko Ursulin 	if (val < pmu->sample[__I915_SAMPLE_RC6_LAST_REPORTED].cur)
199df6a4205STvrtko Ursulin 		val = pmu->sample[__I915_SAMPLE_RC6_LAST_REPORTED].cur;
20016ffe73cSChris Wilson 	else
201df6a4205STvrtko Ursulin 		pmu->sample[__I915_SAMPLE_RC6_LAST_REPORTED].cur = val;
20216ffe73cSChris Wilson 
20316ffe73cSChris Wilson 	spin_unlock_irqrestore(&pmu->lock, flags);
20416ffe73cSChris Wilson 
20516ffe73cSChris Wilson 	return val;
20616ffe73cSChris Wilson }
20716ffe73cSChris Wilson 
208dbe13ae1STvrtko Ursulin static void init_rc6(struct i915_pmu *pmu)
209dbe13ae1STvrtko Ursulin {
210dbe13ae1STvrtko Ursulin 	struct drm_i915_private *i915 = container_of(pmu, typeof(*i915), pmu);
211dbe13ae1STvrtko Ursulin 	intel_wakeref_t wakeref;
212dbe13ae1STvrtko Ursulin 
213dbe13ae1STvrtko Ursulin 	with_intel_runtime_pm(i915->gt.uncore->rpm, wakeref) {
214dbe13ae1STvrtko Ursulin 		pmu->sample[__I915_SAMPLE_RC6].cur = __get_rc6(&i915->gt);
215dbe13ae1STvrtko Ursulin 		pmu->sample[__I915_SAMPLE_RC6_LAST_REPORTED].cur =
216dbe13ae1STvrtko Ursulin 					pmu->sample[__I915_SAMPLE_RC6].cur;
217c51c29fbSTvrtko Ursulin 		pmu->sleep_last = ktime_get_raw();
218dbe13ae1STvrtko Ursulin 	}
219dbe13ae1STvrtko Ursulin }
220dbe13ae1STvrtko Ursulin 
22116ffe73cSChris Wilson static void park_rc6(struct drm_i915_private *i915)
222feff0dc6STvrtko Ursulin {
223908091c8STvrtko Ursulin 	struct i915_pmu *pmu = &i915->pmu;
224908091c8STvrtko Ursulin 
225df6a4205STvrtko Ursulin 	pmu->sample[__I915_SAMPLE_RC6].cur = __get_rc6(&i915->gt);
226c51c29fbSTvrtko Ursulin 	pmu->sleep_last = ktime_get_raw();
227feff0dc6STvrtko Ursulin }
228feff0dc6STvrtko Ursulin 
229908091c8STvrtko Ursulin static void __i915_pmu_maybe_start_timer(struct i915_pmu *pmu)
230feff0dc6STvrtko Ursulin {
231908091c8STvrtko Ursulin 	if (!pmu->timer_enabled && pmu_needs_timer(pmu, true)) {
232908091c8STvrtko Ursulin 		pmu->timer_enabled = true;
233908091c8STvrtko Ursulin 		pmu->timer_last = ktime_get();
234908091c8STvrtko Ursulin 		hrtimer_start_range_ns(&pmu->timer,
235feff0dc6STvrtko Ursulin 				       ns_to_ktime(PERIOD), 0,
236feff0dc6STvrtko Ursulin 				       HRTIMER_MODE_REL_PINNED);
237feff0dc6STvrtko Ursulin 	}
238feff0dc6STvrtko Ursulin }
239feff0dc6STvrtko Ursulin 
24016ffe73cSChris Wilson void i915_pmu_gt_parked(struct drm_i915_private *i915)
24116ffe73cSChris Wilson {
24216ffe73cSChris Wilson 	struct i915_pmu *pmu = &i915->pmu;
24316ffe73cSChris Wilson 
24416ffe73cSChris Wilson 	if (!pmu->base.event_init)
24516ffe73cSChris Wilson 		return;
24616ffe73cSChris Wilson 
24716ffe73cSChris Wilson 	spin_lock_irq(&pmu->lock);
24816ffe73cSChris Wilson 
24916ffe73cSChris Wilson 	park_rc6(i915);
25016ffe73cSChris Wilson 
25116ffe73cSChris Wilson 	/*
25216ffe73cSChris Wilson 	 * Signal sampling timer to stop if only engine events are enabled and
25316ffe73cSChris Wilson 	 * GPU went idle.
25416ffe73cSChris Wilson 	 */
25516ffe73cSChris Wilson 	pmu->timer_enabled = pmu_needs_timer(pmu, false);
25616ffe73cSChris Wilson 
25716ffe73cSChris Wilson 	spin_unlock_irq(&pmu->lock);
25816ffe73cSChris Wilson }
25916ffe73cSChris Wilson 
260feff0dc6STvrtko Ursulin void i915_pmu_gt_unparked(struct drm_i915_private *i915)
261feff0dc6STvrtko Ursulin {
262908091c8STvrtko Ursulin 	struct i915_pmu *pmu = &i915->pmu;
263908091c8STvrtko Ursulin 
264908091c8STvrtko Ursulin 	if (!pmu->base.event_init)
265feff0dc6STvrtko Ursulin 		return;
266feff0dc6STvrtko Ursulin 
267908091c8STvrtko Ursulin 	spin_lock_irq(&pmu->lock);
26816ffe73cSChris Wilson 
269feff0dc6STvrtko Ursulin 	/*
270feff0dc6STvrtko Ursulin 	 * Re-enable sampling timer when GPU goes active.
271feff0dc6STvrtko Ursulin 	 */
272908091c8STvrtko Ursulin 	__i915_pmu_maybe_start_timer(pmu);
27316ffe73cSChris Wilson 
274908091c8STvrtko Ursulin 	spin_unlock_irq(&pmu->lock);
275feff0dc6STvrtko Ursulin }
276feff0dc6STvrtko Ursulin 
277b46a33e2STvrtko Ursulin static void
2789f473ecfSTvrtko Ursulin add_sample(struct i915_pmu_sample *sample, u32 val)
279b46a33e2STvrtko Ursulin {
2809f473ecfSTvrtko Ursulin 	sample->cur += val;
281b46a33e2STvrtko Ursulin }
282b46a33e2STvrtko Ursulin 
283d79e1bd6SChris Wilson static bool exclusive_mmio_access(const struct drm_i915_private *i915)
284d79e1bd6SChris Wilson {
285d79e1bd6SChris Wilson 	/*
286d79e1bd6SChris Wilson 	 * We have to avoid concurrent mmio cache line access on gen7 or
287d79e1bd6SChris Wilson 	 * risk a machine hang. For a fun history lesson dig out the old
288d79e1bd6SChris Wilson 	 * userspace intel_gpu_top and run it on Ivybridge or Haswell!
289d79e1bd6SChris Wilson 	 */
290*651e7d48SLucas De Marchi 	return GRAPHICS_VER(i915) == 7;
291d79e1bd6SChris Wilson }
292d79e1bd6SChris Wilson 
2936ec81b82SArnd Bergmann static void engine_sample(struct intel_engine_cs *engine, unsigned int period_ns)
294b46a33e2STvrtko Ursulin {
295d0aa694bSChris Wilson 	struct intel_engine_pmu *pmu = &engine->pmu;
296d0aa694bSChris Wilson 	bool busy;
297b46a33e2STvrtko Ursulin 	u32 val;
298b46a33e2STvrtko Ursulin 
29928fba096STvrtko Ursulin 	val = ENGINE_READ_FW(engine, RING_CTL);
300d0aa694bSChris Wilson 	if (val == 0) /* powerwell off => engine idle */
3016ec81b82SArnd Bergmann 		return;
302b46a33e2STvrtko Ursulin 
3039f473ecfSTvrtko Ursulin 	if (val & RING_WAIT)
304d0aa694bSChris Wilson 		add_sample(&pmu->sample[I915_SAMPLE_WAIT], period_ns);
3059f473ecfSTvrtko Ursulin 	if (val & RING_WAIT_SEMAPHORE)
306d0aa694bSChris Wilson 		add_sample(&pmu->sample[I915_SAMPLE_SEMA], period_ns);
307b46a33e2STvrtko Ursulin 
30854fc577dSTvrtko Ursulin 	/* No need to sample when busy stats are supported. */
30954fc577dSTvrtko Ursulin 	if (intel_engine_supports_stats(engine))
3106ec81b82SArnd Bergmann 		return;
31154fc577dSTvrtko Ursulin 
312d0aa694bSChris Wilson 	/*
313d0aa694bSChris Wilson 	 * While waiting on a semaphore or event, MI_MODE reports the
314d0aa694bSChris Wilson 	 * ring as idle. However, previously using the seqno, and with
315d0aa694bSChris Wilson 	 * execlists sampling, we account for the ring waiting as the
316d0aa694bSChris Wilson 	 * engine being busy. Therefore, we record the sample as being
317d0aa694bSChris Wilson 	 * busy if either waiting or !idle.
318d0aa694bSChris Wilson 	 */
319d0aa694bSChris Wilson 	busy = val & (RING_WAIT_SEMAPHORE | RING_WAIT);
320d0aa694bSChris Wilson 	if (!busy) {
32128fba096STvrtko Ursulin 		val = ENGINE_READ_FW(engine, RING_MI_MODE);
322d0aa694bSChris Wilson 		busy = !(val & MODE_IDLE);
323d0aa694bSChris Wilson 	}
324d0aa694bSChris Wilson 	if (busy)
325d0aa694bSChris Wilson 		add_sample(&pmu->sample[I915_SAMPLE_BUSY], period_ns);
3266ec81b82SArnd Bergmann }
327b46a33e2STvrtko Ursulin 
3286ec81b82SArnd Bergmann static void
3296ec81b82SArnd Bergmann engines_sample(struct intel_gt *gt, unsigned int period_ns)
3306ec81b82SArnd Bergmann {
3316ec81b82SArnd Bergmann 	struct drm_i915_private *i915 = gt->i915;
3326ec81b82SArnd Bergmann 	struct intel_engine_cs *engine;
3336ec81b82SArnd Bergmann 	enum intel_engine_id id;
3346ec81b82SArnd Bergmann 	unsigned long flags;
3356ec81b82SArnd Bergmann 
3366ec81b82SArnd Bergmann 	if ((i915->pmu.enable & ENGINE_SAMPLE_MASK) == 0)
3376ec81b82SArnd Bergmann 		return;
3386ec81b82SArnd Bergmann 
3396ec81b82SArnd Bergmann 	if (!intel_gt_pm_is_awake(gt))
3406ec81b82SArnd Bergmann 		return;
3416ec81b82SArnd Bergmann 
3426ec81b82SArnd Bergmann 	for_each_engine(engine, gt, id) {
3436ec81b82SArnd Bergmann 		if (!intel_engine_pm_get_if_awake(engine))
3446ec81b82SArnd Bergmann 			continue;
3456ec81b82SArnd Bergmann 
3466ec81b82SArnd Bergmann 		if (exclusive_mmio_access(i915)) {
3476ec81b82SArnd Bergmann 			spin_lock_irqsave(&engine->uncore->lock, flags);
3486ec81b82SArnd Bergmann 			engine_sample(engine, period_ns);
3496ec81b82SArnd Bergmann 			spin_unlock_irqrestore(&engine->uncore->lock, flags);
3506ec81b82SArnd Bergmann 		} else {
3516ec81b82SArnd Bergmann 			engine_sample(engine, period_ns);
3526ec81b82SArnd Bergmann 		}
3536ec81b82SArnd Bergmann 
35407779a76SChris Wilson 		intel_engine_pm_put_async(engine);
35551fbd8deSChris Wilson 	}
356b46a33e2STvrtko Ursulin }
357b46a33e2STvrtko Ursulin 
3589f473ecfSTvrtko Ursulin static void
3599f473ecfSTvrtko Ursulin add_sample_mult(struct i915_pmu_sample *sample, u32 val, u32 mul)
3609f473ecfSTvrtko Ursulin {
3619f473ecfSTvrtko Ursulin 	sample->cur += mul_u32_u32(val, mul);
3629f473ecfSTvrtko Ursulin }
3639f473ecfSTvrtko Ursulin 
364b66ecd04STvrtko Ursulin static bool frequency_sampling_enabled(struct i915_pmu *pmu)
365b66ecd04STvrtko Ursulin {
366b66ecd04STvrtko Ursulin 	return pmu->enable &
367348fb0cbSTvrtko Ursulin 	       (config_mask(I915_PMU_ACTUAL_FREQUENCY) |
368348fb0cbSTvrtko Ursulin 		config_mask(I915_PMU_REQUESTED_FREQUENCY));
369b66ecd04STvrtko Ursulin }
370b66ecd04STvrtko Ursulin 
3719f473ecfSTvrtko Ursulin static void
37208ce5c64STvrtko Ursulin frequency_sample(struct intel_gt *gt, unsigned int period_ns)
373b46a33e2STvrtko Ursulin {
37408ce5c64STvrtko Ursulin 	struct drm_i915_private *i915 = gt->i915;
37508ce5c64STvrtko Ursulin 	struct intel_uncore *uncore = gt->uncore;
37608ce5c64STvrtko Ursulin 	struct i915_pmu *pmu = &i915->pmu;
3773e7abf81SAndi Shyti 	struct intel_rps *rps = &gt->rps;
37808ce5c64STvrtko Ursulin 
379b66ecd04STvrtko Ursulin 	if (!frequency_sampling_enabled(pmu))
380b66ecd04STvrtko Ursulin 		return;
381b66ecd04STvrtko Ursulin 
382b66ecd04STvrtko Ursulin 	/* Report 0/0 (actual/requested) frequency while parked. */
383b66ecd04STvrtko Ursulin 	if (!intel_gt_pm_get_if_awake(gt))
384b66ecd04STvrtko Ursulin 		return;
385b66ecd04STvrtko Ursulin 
386348fb0cbSTvrtko Ursulin 	if (pmu->enable & config_mask(I915_PMU_ACTUAL_FREQUENCY)) {
387b46a33e2STvrtko Ursulin 		u32 val;
388b46a33e2STvrtko Ursulin 
389c1c82d26SChris Wilson 		/*
390c1c82d26SChris Wilson 		 * We take a quick peek here without using forcewake
391c1c82d26SChris Wilson 		 * so that we don't perturb the system under observation
392c1c82d26SChris Wilson 		 * (forcewake => !rc6 => increased power use). We expect
393c1c82d26SChris Wilson 		 * that if the read fails because it is outside of the
394c1c82d26SChris Wilson 		 * mmio power well, then it will return 0 -- in which
395c1c82d26SChris Wilson 		 * case we assume the system is running at the intended
396c1c82d26SChris Wilson 		 * frequency. Fortunately, the read should rarely fail!
397c1c82d26SChris Wilson 		 */
398b66ecd04STvrtko Ursulin 		val = intel_uncore_read_fw(uncore, GEN6_RPSTAT1);
399b66ecd04STvrtko Ursulin 		if (val)
400e03512edSAndi Shyti 			val = intel_rps_get_cagf(rps, val);
401b66ecd04STvrtko Ursulin 		else
402b66ecd04STvrtko Ursulin 			val = rps->cur_freq;
403b46a33e2STvrtko Ursulin 
40408ce5c64STvrtko Ursulin 		add_sample_mult(&pmu->sample[__I915_SAMPLE_FREQ_ACT],
405b66ecd04STvrtko Ursulin 				intel_gpu_freq(rps, val), period_ns / 1000);
406b46a33e2STvrtko Ursulin 	}
407b46a33e2STvrtko Ursulin 
408348fb0cbSTvrtko Ursulin 	if (pmu->enable & config_mask(I915_PMU_REQUESTED_FREQUENCY)) {
40908ce5c64STvrtko Ursulin 		add_sample_mult(&pmu->sample[__I915_SAMPLE_FREQ_REQ],
4103e7abf81SAndi Shyti 				intel_gpu_freq(rps, rps->cur_freq),
4119f473ecfSTvrtko Ursulin 				period_ns / 1000);
412b46a33e2STvrtko Ursulin 	}
413b66ecd04STvrtko Ursulin 
414b66ecd04STvrtko Ursulin 	intel_gt_pm_put_async(gt);
415b46a33e2STvrtko Ursulin }
416b46a33e2STvrtko Ursulin 
417b46a33e2STvrtko Ursulin static enum hrtimer_restart i915_sample(struct hrtimer *hrtimer)
418b46a33e2STvrtko Ursulin {
419b46a33e2STvrtko Ursulin 	struct drm_i915_private *i915 =
420b46a33e2STvrtko Ursulin 		container_of(hrtimer, struct drm_i915_private, pmu.timer);
421908091c8STvrtko Ursulin 	struct i915_pmu *pmu = &i915->pmu;
42208ce5c64STvrtko Ursulin 	struct intel_gt *gt = &i915->gt;
4239f473ecfSTvrtko Ursulin 	unsigned int period_ns;
4249f473ecfSTvrtko Ursulin 	ktime_t now;
425b46a33e2STvrtko Ursulin 
426908091c8STvrtko Ursulin 	if (!READ_ONCE(pmu->timer_enabled))
427b46a33e2STvrtko Ursulin 		return HRTIMER_NORESTART;
428b46a33e2STvrtko Ursulin 
4299f473ecfSTvrtko Ursulin 	now = ktime_get();
430908091c8STvrtko Ursulin 	period_ns = ktime_to_ns(ktime_sub(now, pmu->timer_last));
431908091c8STvrtko Ursulin 	pmu->timer_last = now;
432b46a33e2STvrtko Ursulin 
4339f473ecfSTvrtko Ursulin 	/*
4349f473ecfSTvrtko Ursulin 	 * Strictly speaking the passed in period may not be 100% accurate for
4359f473ecfSTvrtko Ursulin 	 * all internal calculation, since some amount of time can be spent on
4369f473ecfSTvrtko Ursulin 	 * grabbing the forcewake. However the potential error from timer call-
4379f473ecfSTvrtko Ursulin 	 * back delay greatly dominates this so we keep it simple.
4389f473ecfSTvrtko Ursulin 	 */
43908ce5c64STvrtko Ursulin 	engines_sample(gt, period_ns);
44008ce5c64STvrtko Ursulin 	frequency_sample(gt, period_ns);
4419f473ecfSTvrtko Ursulin 
4429f473ecfSTvrtko Ursulin 	hrtimer_forward(hrtimer, now, ns_to_ktime(PERIOD));
4439f473ecfSTvrtko Ursulin 
444b46a33e2STvrtko Ursulin 	return HRTIMER_RESTART;
445b46a33e2STvrtko Ursulin }
446b46a33e2STvrtko Ursulin 
447b46a33e2STvrtko Ursulin static void i915_pmu_event_destroy(struct perf_event *event)
448b46a33e2STvrtko Ursulin {
449bf07f6ebSPankaj Bharadiya 	struct drm_i915_private *i915 =
450bf07f6ebSPankaj Bharadiya 		container_of(event->pmu, typeof(*i915), pmu.base);
451bf07f6ebSPankaj Bharadiya 
452bf07f6ebSPankaj Bharadiya 	drm_WARN_ON(&i915->drm, event->parent);
453b00bccb3STvrtko Ursulin 
454b00bccb3STvrtko Ursulin 	drm_dev_put(&i915->drm);
455b46a33e2STvrtko Ursulin }
456b46a33e2STvrtko Ursulin 
457109ec558STvrtko Ursulin static int
458109ec558STvrtko Ursulin engine_event_status(struct intel_engine_cs *engine,
459109ec558STvrtko Ursulin 		    enum drm_i915_pmu_engine_sample sample)
460b46a33e2STvrtko Ursulin {
461109ec558STvrtko Ursulin 	switch (sample) {
462b46a33e2STvrtko Ursulin 	case I915_SAMPLE_BUSY:
463b46a33e2STvrtko Ursulin 	case I915_SAMPLE_WAIT:
464b46a33e2STvrtko Ursulin 		break;
465b46a33e2STvrtko Ursulin 	case I915_SAMPLE_SEMA:
466*651e7d48SLucas De Marchi 		if (GRAPHICS_VER(engine->i915) < 6)
467b46a33e2STvrtko Ursulin 			return -ENODEV;
468b46a33e2STvrtko Ursulin 		break;
469b46a33e2STvrtko Ursulin 	default:
470b46a33e2STvrtko Ursulin 		return -ENOENT;
471b46a33e2STvrtko Ursulin 	}
472b46a33e2STvrtko Ursulin 
473b46a33e2STvrtko Ursulin 	return 0;
474b46a33e2STvrtko Ursulin }
475b46a33e2STvrtko Ursulin 
476109ec558STvrtko Ursulin static int
477109ec558STvrtko Ursulin config_status(struct drm_i915_private *i915, u64 config)
478109ec558STvrtko Ursulin {
479109ec558STvrtko Ursulin 	switch (config) {
480109ec558STvrtko Ursulin 	case I915_PMU_ACTUAL_FREQUENCY:
481109ec558STvrtko Ursulin 		if (IS_VALLEYVIEW(i915) || IS_CHERRYVIEW(i915))
482109ec558STvrtko Ursulin 			/* Requires a mutex for sampling! */
483109ec558STvrtko Ursulin 			return -ENODEV;
484df561f66SGustavo A. R. Silva 		fallthrough;
485109ec558STvrtko Ursulin 	case I915_PMU_REQUESTED_FREQUENCY:
486*651e7d48SLucas De Marchi 		if (GRAPHICS_VER(i915) < 6)
487109ec558STvrtko Ursulin 			return -ENODEV;
488109ec558STvrtko Ursulin 		break;
489109ec558STvrtko Ursulin 	case I915_PMU_INTERRUPTS:
490109ec558STvrtko Ursulin 		break;
491109ec558STvrtko Ursulin 	case I915_PMU_RC6_RESIDENCY:
492109ec558STvrtko Ursulin 		if (!HAS_RC6(i915))
493109ec558STvrtko Ursulin 			return -ENODEV;
494109ec558STvrtko Ursulin 		break;
4958c3b1ba0SChris Wilson 	case I915_PMU_SOFTWARE_GT_AWAKE_TIME:
4968c3b1ba0SChris Wilson 		break;
497109ec558STvrtko Ursulin 	default:
498109ec558STvrtko Ursulin 		return -ENOENT;
499109ec558STvrtko Ursulin 	}
500109ec558STvrtko Ursulin 
501109ec558STvrtko Ursulin 	return 0;
502109ec558STvrtko Ursulin }
503109ec558STvrtko Ursulin 
504109ec558STvrtko Ursulin static int engine_event_init(struct perf_event *event)
505109ec558STvrtko Ursulin {
506109ec558STvrtko Ursulin 	struct drm_i915_private *i915 =
507109ec558STvrtko Ursulin 		container_of(event->pmu, typeof(*i915), pmu.base);
508109ec558STvrtko Ursulin 	struct intel_engine_cs *engine;
509109ec558STvrtko Ursulin 
510109ec558STvrtko Ursulin 	engine = intel_engine_lookup_user(i915, engine_event_class(event),
511109ec558STvrtko Ursulin 					  engine_event_instance(event));
512109ec558STvrtko Ursulin 	if (!engine)
513109ec558STvrtko Ursulin 		return -ENODEV;
514109ec558STvrtko Ursulin 
515426d0073SChris Wilson 	return engine_event_status(engine, engine_event_sample(event));
516109ec558STvrtko Ursulin }
517109ec558STvrtko Ursulin 
518b46a33e2STvrtko Ursulin static int i915_pmu_event_init(struct perf_event *event)
519b46a33e2STvrtko Ursulin {
520b46a33e2STvrtko Ursulin 	struct drm_i915_private *i915 =
521b46a33e2STvrtko Ursulin 		container_of(event->pmu, typeof(*i915), pmu.base);
522b00bccb3STvrtko Ursulin 	struct i915_pmu *pmu = &i915->pmu;
5230426c046STvrtko Ursulin 	int ret;
524b46a33e2STvrtko Ursulin 
525b00bccb3STvrtko Ursulin 	if (pmu->closed)
526b00bccb3STvrtko Ursulin 		return -ENODEV;
527b00bccb3STvrtko Ursulin 
528b46a33e2STvrtko Ursulin 	if (event->attr.type != event->pmu->type)
529b46a33e2STvrtko Ursulin 		return -ENOENT;
530b46a33e2STvrtko Ursulin 
531b46a33e2STvrtko Ursulin 	/* unsupported modes and filters */
532b46a33e2STvrtko Ursulin 	if (event->attr.sample_period) /* no sampling */
533b46a33e2STvrtko Ursulin 		return -EINVAL;
534b46a33e2STvrtko Ursulin 
535b46a33e2STvrtko Ursulin 	if (has_branch_stack(event))
536b46a33e2STvrtko Ursulin 		return -EOPNOTSUPP;
537b46a33e2STvrtko Ursulin 
538b46a33e2STvrtko Ursulin 	if (event->cpu < 0)
539b46a33e2STvrtko Ursulin 		return -EINVAL;
540b46a33e2STvrtko Ursulin 
5410426c046STvrtko Ursulin 	/* only allow running on one cpu at a time */
5420426c046STvrtko Ursulin 	if (!cpumask_test_cpu(event->cpu, &i915_pmu_cpumask))
54300a79722STvrtko Ursulin 		return -EINVAL;
544b46a33e2STvrtko Ursulin 
545109ec558STvrtko Ursulin 	if (is_engine_event(event))
546b46a33e2STvrtko Ursulin 		ret = engine_event_init(event);
547109ec558STvrtko Ursulin 	else
548109ec558STvrtko Ursulin 		ret = config_status(i915, event->attr.config);
549b46a33e2STvrtko Ursulin 	if (ret)
550b46a33e2STvrtko Ursulin 		return ret;
551b46a33e2STvrtko Ursulin 
552b00bccb3STvrtko Ursulin 	if (!event->parent) {
553b00bccb3STvrtko Ursulin 		drm_dev_get(&i915->drm);
554b46a33e2STvrtko Ursulin 		event->destroy = i915_pmu_event_destroy;
555b00bccb3STvrtko Ursulin 	}
556b46a33e2STvrtko Ursulin 
557b46a33e2STvrtko Ursulin 	return 0;
558b46a33e2STvrtko Ursulin }
559b46a33e2STvrtko Ursulin 
560ad055fb8STvrtko Ursulin static u64 __i915_pmu_event_read(struct perf_event *event)
561b46a33e2STvrtko Ursulin {
562b46a33e2STvrtko Ursulin 	struct drm_i915_private *i915 =
563b46a33e2STvrtko Ursulin 		container_of(event->pmu, typeof(*i915), pmu.base);
564908091c8STvrtko Ursulin 	struct i915_pmu *pmu = &i915->pmu;
565b46a33e2STvrtko Ursulin 	u64 val = 0;
566b46a33e2STvrtko Ursulin 
567b46a33e2STvrtko Ursulin 	if (is_engine_event(event)) {
568b46a33e2STvrtko Ursulin 		u8 sample = engine_event_sample(event);
569b46a33e2STvrtko Ursulin 		struct intel_engine_cs *engine;
570b46a33e2STvrtko Ursulin 
571b46a33e2STvrtko Ursulin 		engine = intel_engine_lookup_user(i915,
572b46a33e2STvrtko Ursulin 						  engine_event_class(event),
573b46a33e2STvrtko Ursulin 						  engine_event_instance(event));
574b46a33e2STvrtko Ursulin 
57548a1b8d4SPankaj Bharadiya 		if (drm_WARN_ON_ONCE(&i915->drm, !engine)) {
576b46a33e2STvrtko Ursulin 			/* Do nothing */
577b3add01eSTvrtko Ursulin 		} else if (sample == I915_SAMPLE_BUSY &&
578b2f78cdaSTvrtko Ursulin 			   intel_engine_supports_stats(engine)) {
579810b7ee3SChris Wilson 			ktime_t unused;
580810b7ee3SChris Wilson 
581810b7ee3SChris Wilson 			val = ktime_to_ns(intel_engine_get_busy_time(engine,
582810b7ee3SChris Wilson 								     &unused));
583b46a33e2STvrtko Ursulin 		} else {
584b46a33e2STvrtko Ursulin 			val = engine->pmu.sample[sample].cur;
585b46a33e2STvrtko Ursulin 		}
586b46a33e2STvrtko Ursulin 	} else {
587b46a33e2STvrtko Ursulin 		switch (event->attr.config) {
588b46a33e2STvrtko Ursulin 		case I915_PMU_ACTUAL_FREQUENCY:
589b46a33e2STvrtko Ursulin 			val =
590908091c8STvrtko Ursulin 			   div_u64(pmu->sample[__I915_SAMPLE_FREQ_ACT].cur,
5919f473ecfSTvrtko Ursulin 				   USEC_PER_SEC /* to MHz */);
592b46a33e2STvrtko Ursulin 			break;
593b46a33e2STvrtko Ursulin 		case I915_PMU_REQUESTED_FREQUENCY:
594b46a33e2STvrtko Ursulin 			val =
595908091c8STvrtko Ursulin 			   div_u64(pmu->sample[__I915_SAMPLE_FREQ_REQ].cur,
5969f473ecfSTvrtko Ursulin 				   USEC_PER_SEC /* to MHz */);
597b46a33e2STvrtko Ursulin 			break;
5980cd4684dSTvrtko Ursulin 		case I915_PMU_INTERRUPTS:
5999c6508b9SThomas Gleixner 			val = READ_ONCE(pmu->irq_count);
6000cd4684dSTvrtko Ursulin 			break;
6016060b6aeSTvrtko Ursulin 		case I915_PMU_RC6_RESIDENCY:
602518ea582STvrtko Ursulin 			val = get_rc6(&i915->gt);
6036060b6aeSTvrtko Ursulin 			break;
6048c3b1ba0SChris Wilson 		case I915_PMU_SOFTWARE_GT_AWAKE_TIME:
6058c3b1ba0SChris Wilson 			val = ktime_to_ns(intel_gt_get_awake_time(&i915->gt));
6068c3b1ba0SChris Wilson 			break;
607b46a33e2STvrtko Ursulin 		}
608b46a33e2STvrtko Ursulin 	}
609b46a33e2STvrtko Ursulin 
610b46a33e2STvrtko Ursulin 	return val;
611b46a33e2STvrtko Ursulin }
612b46a33e2STvrtko Ursulin 
613b46a33e2STvrtko Ursulin static void i915_pmu_event_read(struct perf_event *event)
614b46a33e2STvrtko Ursulin {
615b00bccb3STvrtko Ursulin 	struct drm_i915_private *i915 =
616b00bccb3STvrtko Ursulin 		container_of(event->pmu, typeof(*i915), pmu.base);
617b46a33e2STvrtko Ursulin 	struct hw_perf_event *hwc = &event->hw;
618b00bccb3STvrtko Ursulin 	struct i915_pmu *pmu = &i915->pmu;
619b46a33e2STvrtko Ursulin 	u64 prev, new;
620b46a33e2STvrtko Ursulin 
621b00bccb3STvrtko Ursulin 	if (pmu->closed) {
622b00bccb3STvrtko Ursulin 		event->hw.state = PERF_HES_STOPPED;
623b00bccb3STvrtko Ursulin 		return;
624b00bccb3STvrtko Ursulin 	}
625b46a33e2STvrtko Ursulin again:
626b46a33e2STvrtko Ursulin 	prev = local64_read(&hwc->prev_count);
627ad055fb8STvrtko Ursulin 	new = __i915_pmu_event_read(event);
628b46a33e2STvrtko Ursulin 
629b46a33e2STvrtko Ursulin 	if (local64_cmpxchg(&hwc->prev_count, prev, new) != prev)
630b46a33e2STvrtko Ursulin 		goto again;
631b46a33e2STvrtko Ursulin 
632b46a33e2STvrtko Ursulin 	local64_add(new - prev, &event->count);
633b46a33e2STvrtko Ursulin }
634b46a33e2STvrtko Ursulin 
635b46a33e2STvrtko Ursulin static void i915_pmu_enable(struct perf_event *event)
636b46a33e2STvrtko Ursulin {
637b46a33e2STvrtko Ursulin 	struct drm_i915_private *i915 =
638b46a33e2STvrtko Ursulin 		container_of(event->pmu, typeof(*i915), pmu.base);
639908091c8STvrtko Ursulin 	struct i915_pmu *pmu = &i915->pmu;
640b46a33e2STvrtko Ursulin 	unsigned long flags;
641348fb0cbSTvrtko Ursulin 	unsigned int bit;
642b46a33e2STvrtko Ursulin 
643348fb0cbSTvrtko Ursulin 	bit = event_bit(event);
644348fb0cbSTvrtko Ursulin 	if (bit == -1)
645348fb0cbSTvrtko Ursulin 		goto update;
646348fb0cbSTvrtko Ursulin 
647908091c8STvrtko Ursulin 	spin_lock_irqsave(&pmu->lock, flags);
648b46a33e2STvrtko Ursulin 
649b46a33e2STvrtko Ursulin 	/*
650b46a33e2STvrtko Ursulin 	 * Update the bitmask of enabled events and increment
651b46a33e2STvrtko Ursulin 	 * the event reference counter.
652b46a33e2STvrtko Ursulin 	 */
653908091c8STvrtko Ursulin 	BUILD_BUG_ON(ARRAY_SIZE(pmu->enable_count) != I915_PMU_MASK_BITS);
654908091c8STvrtko Ursulin 	GEM_BUG_ON(bit >= ARRAY_SIZE(pmu->enable_count));
655908091c8STvrtko Ursulin 	GEM_BUG_ON(pmu->enable_count[bit] == ~0);
656f4e9894bSChris Wilson 
657908091c8STvrtko Ursulin 	pmu->enable |= BIT_ULL(bit);
658908091c8STvrtko Ursulin 	pmu->enable_count[bit]++;
659b46a33e2STvrtko Ursulin 
660b46a33e2STvrtko Ursulin 	/*
661feff0dc6STvrtko Ursulin 	 * Start the sampling timer if needed and not already enabled.
662feff0dc6STvrtko Ursulin 	 */
663908091c8STvrtko Ursulin 	__i915_pmu_maybe_start_timer(pmu);
664feff0dc6STvrtko Ursulin 
665feff0dc6STvrtko Ursulin 	/*
666b46a33e2STvrtko Ursulin 	 * For per-engine events the bitmask and reference counting
667b46a33e2STvrtko Ursulin 	 * is stored per engine.
668b46a33e2STvrtko Ursulin 	 */
669b46a33e2STvrtko Ursulin 	if (is_engine_event(event)) {
670b46a33e2STvrtko Ursulin 		u8 sample = engine_event_sample(event);
671b46a33e2STvrtko Ursulin 		struct intel_engine_cs *engine;
672b46a33e2STvrtko Ursulin 
673b46a33e2STvrtko Ursulin 		engine = intel_engine_lookup_user(i915,
674b46a33e2STvrtko Ursulin 						  engine_event_class(event),
675b46a33e2STvrtko Ursulin 						  engine_event_instance(event));
676b46a33e2STvrtko Ursulin 
67726a11deeSTvrtko Ursulin 		BUILD_BUG_ON(ARRAY_SIZE(engine->pmu.enable_count) !=
67826a11deeSTvrtko Ursulin 			     I915_ENGINE_SAMPLE_COUNT);
67926a11deeSTvrtko Ursulin 		BUILD_BUG_ON(ARRAY_SIZE(engine->pmu.sample) !=
68026a11deeSTvrtko Ursulin 			     I915_ENGINE_SAMPLE_COUNT);
68126a11deeSTvrtko Ursulin 		GEM_BUG_ON(sample >= ARRAY_SIZE(engine->pmu.enable_count));
68226a11deeSTvrtko Ursulin 		GEM_BUG_ON(sample >= ARRAY_SIZE(engine->pmu.sample));
683b46a33e2STvrtko Ursulin 		GEM_BUG_ON(engine->pmu.enable_count[sample] == ~0);
68426a11deeSTvrtko Ursulin 
68526a11deeSTvrtko Ursulin 		engine->pmu.enable |= BIT(sample);
686b2f78cdaSTvrtko Ursulin 		engine->pmu.enable_count[sample]++;
687b46a33e2STvrtko Ursulin 	}
688b46a33e2STvrtko Ursulin 
689908091c8STvrtko Ursulin 	spin_unlock_irqrestore(&pmu->lock, flags);
690ad055fb8STvrtko Ursulin 
691348fb0cbSTvrtko Ursulin update:
692b46a33e2STvrtko Ursulin 	/*
693b46a33e2STvrtko Ursulin 	 * Store the current counter value so we can report the correct delta
694b46a33e2STvrtko Ursulin 	 * for all listeners. Even when the event was already enabled and has
695b46a33e2STvrtko Ursulin 	 * an existing non-zero value.
696b46a33e2STvrtko Ursulin 	 */
697ad055fb8STvrtko Ursulin 	local64_set(&event->hw.prev_count, __i915_pmu_event_read(event));
698b46a33e2STvrtko Ursulin }
699b46a33e2STvrtko Ursulin 
700b46a33e2STvrtko Ursulin static void i915_pmu_disable(struct perf_event *event)
701b46a33e2STvrtko Ursulin {
702b46a33e2STvrtko Ursulin 	struct drm_i915_private *i915 =
703b46a33e2STvrtko Ursulin 		container_of(event->pmu, typeof(*i915), pmu.base);
704348fb0cbSTvrtko Ursulin 	unsigned int bit = event_bit(event);
705908091c8STvrtko Ursulin 	struct i915_pmu *pmu = &i915->pmu;
706b46a33e2STvrtko Ursulin 	unsigned long flags;
707b46a33e2STvrtko Ursulin 
708348fb0cbSTvrtko Ursulin 	if (bit == -1)
709348fb0cbSTvrtko Ursulin 		return;
710348fb0cbSTvrtko Ursulin 
711908091c8STvrtko Ursulin 	spin_lock_irqsave(&pmu->lock, flags);
712b46a33e2STvrtko Ursulin 
713b46a33e2STvrtko Ursulin 	if (is_engine_event(event)) {
714b46a33e2STvrtko Ursulin 		u8 sample = engine_event_sample(event);
715b46a33e2STvrtko Ursulin 		struct intel_engine_cs *engine;
716b46a33e2STvrtko Ursulin 
717b46a33e2STvrtko Ursulin 		engine = intel_engine_lookup_user(i915,
718b46a33e2STvrtko Ursulin 						  engine_event_class(event),
719b46a33e2STvrtko Ursulin 						  engine_event_instance(event));
72026a11deeSTvrtko Ursulin 
72126a11deeSTvrtko Ursulin 		GEM_BUG_ON(sample >= ARRAY_SIZE(engine->pmu.enable_count));
72226a11deeSTvrtko Ursulin 		GEM_BUG_ON(sample >= ARRAY_SIZE(engine->pmu.sample));
723b46a33e2STvrtko Ursulin 		GEM_BUG_ON(engine->pmu.enable_count[sample] == 0);
72426a11deeSTvrtko Ursulin 
725b46a33e2STvrtko Ursulin 		/*
726b46a33e2STvrtko Ursulin 		 * Decrement the reference count and clear the enabled
727b46a33e2STvrtko Ursulin 		 * bitmask when the last listener on an event goes away.
728b46a33e2STvrtko Ursulin 		 */
729b2f78cdaSTvrtko Ursulin 		if (--engine->pmu.enable_count[sample] == 0)
730b46a33e2STvrtko Ursulin 			engine->pmu.enable &= ~BIT(sample);
731b46a33e2STvrtko Ursulin 	}
732b46a33e2STvrtko Ursulin 
733908091c8STvrtko Ursulin 	GEM_BUG_ON(bit >= ARRAY_SIZE(pmu->enable_count));
734908091c8STvrtko Ursulin 	GEM_BUG_ON(pmu->enable_count[bit] == 0);
735b46a33e2STvrtko Ursulin 	/*
736b46a33e2STvrtko Ursulin 	 * Decrement the reference count and clear the enabled
737b46a33e2STvrtko Ursulin 	 * bitmask when the last listener on an event goes away.
738b46a33e2STvrtko Ursulin 	 */
739908091c8STvrtko Ursulin 	if (--pmu->enable_count[bit] == 0) {
740908091c8STvrtko Ursulin 		pmu->enable &= ~BIT_ULL(bit);
741908091c8STvrtko Ursulin 		pmu->timer_enabled &= pmu_needs_timer(pmu, true);
742feff0dc6STvrtko Ursulin 	}
743b46a33e2STvrtko Ursulin 
744908091c8STvrtko Ursulin 	spin_unlock_irqrestore(&pmu->lock, flags);
745b46a33e2STvrtko Ursulin }
746b46a33e2STvrtko Ursulin 
747b46a33e2STvrtko Ursulin static void i915_pmu_event_start(struct perf_event *event, int flags)
748b46a33e2STvrtko Ursulin {
749b00bccb3STvrtko Ursulin 	struct drm_i915_private *i915 =
750b00bccb3STvrtko Ursulin 		container_of(event->pmu, typeof(*i915), pmu.base);
751b00bccb3STvrtko Ursulin 	struct i915_pmu *pmu = &i915->pmu;
752b00bccb3STvrtko Ursulin 
753b00bccb3STvrtko Ursulin 	if (pmu->closed)
754b00bccb3STvrtko Ursulin 		return;
755b00bccb3STvrtko Ursulin 
756b46a33e2STvrtko Ursulin 	i915_pmu_enable(event);
757b46a33e2STvrtko Ursulin 	event->hw.state = 0;
758b46a33e2STvrtko Ursulin }
759b46a33e2STvrtko Ursulin 
760b46a33e2STvrtko Ursulin static void i915_pmu_event_stop(struct perf_event *event, int flags)
761b46a33e2STvrtko Ursulin {
762b46a33e2STvrtko Ursulin 	if (flags & PERF_EF_UPDATE)
763b46a33e2STvrtko Ursulin 		i915_pmu_event_read(event);
764b46a33e2STvrtko Ursulin 	i915_pmu_disable(event);
765b46a33e2STvrtko Ursulin 	event->hw.state = PERF_HES_STOPPED;
766b46a33e2STvrtko Ursulin }
767b46a33e2STvrtko Ursulin 
768b46a33e2STvrtko Ursulin static int i915_pmu_event_add(struct perf_event *event, int flags)
769b46a33e2STvrtko Ursulin {
770b00bccb3STvrtko Ursulin 	struct drm_i915_private *i915 =
771b00bccb3STvrtko Ursulin 		container_of(event->pmu, typeof(*i915), pmu.base);
772b00bccb3STvrtko Ursulin 	struct i915_pmu *pmu = &i915->pmu;
773b00bccb3STvrtko Ursulin 
774b00bccb3STvrtko Ursulin 	if (pmu->closed)
775b00bccb3STvrtko Ursulin 		return -ENODEV;
776b00bccb3STvrtko Ursulin 
777b46a33e2STvrtko Ursulin 	if (flags & PERF_EF_START)
778b46a33e2STvrtko Ursulin 		i915_pmu_event_start(event, flags);
779b46a33e2STvrtko Ursulin 
780b46a33e2STvrtko Ursulin 	return 0;
781b46a33e2STvrtko Ursulin }
782b46a33e2STvrtko Ursulin 
783b46a33e2STvrtko Ursulin static void i915_pmu_event_del(struct perf_event *event, int flags)
784b46a33e2STvrtko Ursulin {
785b46a33e2STvrtko Ursulin 	i915_pmu_event_stop(event, PERF_EF_UPDATE);
786b46a33e2STvrtko Ursulin }
787b46a33e2STvrtko Ursulin 
788b46a33e2STvrtko Ursulin static int i915_pmu_event_event_idx(struct perf_event *event)
789b46a33e2STvrtko Ursulin {
790b46a33e2STvrtko Ursulin 	return 0;
791b46a33e2STvrtko Ursulin }
792b46a33e2STvrtko Ursulin 
793b7d3aabfSChris Wilson struct i915_str_attribute {
794b7d3aabfSChris Wilson 	struct device_attribute attr;
795b7d3aabfSChris Wilson 	const char *str;
796b7d3aabfSChris Wilson };
797b7d3aabfSChris Wilson 
798b46a33e2STvrtko Ursulin static ssize_t i915_pmu_format_show(struct device *dev,
799b46a33e2STvrtko Ursulin 				    struct device_attribute *attr, char *buf)
800b46a33e2STvrtko Ursulin {
801b7d3aabfSChris Wilson 	struct i915_str_attribute *eattr;
802b46a33e2STvrtko Ursulin 
803b7d3aabfSChris Wilson 	eattr = container_of(attr, struct i915_str_attribute, attr);
804b7d3aabfSChris Wilson 	return sprintf(buf, "%s\n", eattr->str);
805b46a33e2STvrtko Ursulin }
806b46a33e2STvrtko Ursulin 
807b46a33e2STvrtko Ursulin #define I915_PMU_FORMAT_ATTR(_name, _config) \
808b7d3aabfSChris Wilson 	(&((struct i915_str_attribute[]) { \
809b46a33e2STvrtko Ursulin 		{ .attr = __ATTR(_name, 0444, i915_pmu_format_show, NULL), \
810b7d3aabfSChris Wilson 		  .str = _config, } \
811b46a33e2STvrtko Ursulin 	})[0].attr.attr)
812b46a33e2STvrtko Ursulin 
813b46a33e2STvrtko Ursulin static struct attribute *i915_pmu_format_attrs[] = {
814b46a33e2STvrtko Ursulin 	I915_PMU_FORMAT_ATTR(i915_eventid, "config:0-20"),
815b46a33e2STvrtko Ursulin 	NULL,
816b46a33e2STvrtko Ursulin };
817b46a33e2STvrtko Ursulin 
818b46a33e2STvrtko Ursulin static const struct attribute_group i915_pmu_format_attr_group = {
819b46a33e2STvrtko Ursulin 	.name = "format",
820b46a33e2STvrtko Ursulin 	.attrs = i915_pmu_format_attrs,
821b46a33e2STvrtko Ursulin };
822b46a33e2STvrtko Ursulin 
823b7d3aabfSChris Wilson struct i915_ext_attribute {
824b7d3aabfSChris Wilson 	struct device_attribute attr;
825b7d3aabfSChris Wilson 	unsigned long val;
826b7d3aabfSChris Wilson };
827b7d3aabfSChris Wilson 
828b46a33e2STvrtko Ursulin static ssize_t i915_pmu_event_show(struct device *dev,
829b46a33e2STvrtko Ursulin 				   struct device_attribute *attr, char *buf)
830b46a33e2STvrtko Ursulin {
831b7d3aabfSChris Wilson 	struct i915_ext_attribute *eattr;
832b46a33e2STvrtko Ursulin 
833b7d3aabfSChris Wilson 	eattr = container_of(attr, struct i915_ext_attribute, attr);
834b7d3aabfSChris Wilson 	return sprintf(buf, "config=0x%lx\n", eattr->val);
835b46a33e2STvrtko Ursulin }
836b46a33e2STvrtko Ursulin 
837b46a33e2STvrtko Ursulin static ssize_t
838b46a33e2STvrtko Ursulin i915_pmu_get_attr_cpumask(struct device *dev,
839b46a33e2STvrtko Ursulin 			  struct device_attribute *attr,
840b46a33e2STvrtko Ursulin 			  char *buf)
841b46a33e2STvrtko Ursulin {
842b46a33e2STvrtko Ursulin 	return cpumap_print_to_pagebuf(true, buf, &i915_pmu_cpumask);
843b46a33e2STvrtko Ursulin }
844b46a33e2STvrtko Ursulin 
845b46a33e2STvrtko Ursulin static DEVICE_ATTR(cpumask, 0444, i915_pmu_get_attr_cpumask, NULL);
846b46a33e2STvrtko Ursulin 
847b46a33e2STvrtko Ursulin static struct attribute *i915_cpumask_attrs[] = {
848b46a33e2STvrtko Ursulin 	&dev_attr_cpumask.attr,
849b46a33e2STvrtko Ursulin 	NULL,
850b46a33e2STvrtko Ursulin };
851b46a33e2STvrtko Ursulin 
852109ec558STvrtko Ursulin static const struct attribute_group i915_pmu_cpumask_attr_group = {
853b46a33e2STvrtko Ursulin 	.attrs = i915_cpumask_attrs,
854b46a33e2STvrtko Ursulin };
855b46a33e2STvrtko Ursulin 
856109ec558STvrtko Ursulin #define __event(__config, __name, __unit) \
857109ec558STvrtko Ursulin { \
858109ec558STvrtko Ursulin 	.config = (__config), \
859109ec558STvrtko Ursulin 	.name = (__name), \
860109ec558STvrtko Ursulin 	.unit = (__unit), \
861109ec558STvrtko Ursulin }
862109ec558STvrtko Ursulin 
863109ec558STvrtko Ursulin #define __engine_event(__sample, __name) \
864109ec558STvrtko Ursulin { \
865109ec558STvrtko Ursulin 	.sample = (__sample), \
866109ec558STvrtko Ursulin 	.name = (__name), \
867109ec558STvrtko Ursulin }
868109ec558STvrtko Ursulin 
869109ec558STvrtko Ursulin static struct i915_ext_attribute *
870109ec558STvrtko Ursulin add_i915_attr(struct i915_ext_attribute *attr, const char *name, u64 config)
871109ec558STvrtko Ursulin {
8722bbba4e9SChris Wilson 	sysfs_attr_init(&attr->attr.attr);
873109ec558STvrtko Ursulin 	attr->attr.attr.name = name;
874109ec558STvrtko Ursulin 	attr->attr.attr.mode = 0444;
875109ec558STvrtko Ursulin 	attr->attr.show = i915_pmu_event_show;
876109ec558STvrtko Ursulin 	attr->val = config;
877109ec558STvrtko Ursulin 
878109ec558STvrtko Ursulin 	return ++attr;
879109ec558STvrtko Ursulin }
880109ec558STvrtko Ursulin 
881109ec558STvrtko Ursulin static struct perf_pmu_events_attr *
882109ec558STvrtko Ursulin add_pmu_attr(struct perf_pmu_events_attr *attr, const char *name,
883109ec558STvrtko Ursulin 	     const char *str)
884109ec558STvrtko Ursulin {
8852bbba4e9SChris Wilson 	sysfs_attr_init(&attr->attr.attr);
886109ec558STvrtko Ursulin 	attr->attr.attr.name = name;
887109ec558STvrtko Ursulin 	attr->attr.attr.mode = 0444;
888109ec558STvrtko Ursulin 	attr->attr.show = perf_event_sysfs_show;
889109ec558STvrtko Ursulin 	attr->event_str = str;
890109ec558STvrtko Ursulin 
891109ec558STvrtko Ursulin 	return ++attr;
892109ec558STvrtko Ursulin }
893109ec558STvrtko Ursulin 
894109ec558STvrtko Ursulin static struct attribute **
895908091c8STvrtko Ursulin create_event_attributes(struct i915_pmu *pmu)
896109ec558STvrtko Ursulin {
897908091c8STvrtko Ursulin 	struct drm_i915_private *i915 = container_of(pmu, typeof(*i915), pmu);
898109ec558STvrtko Ursulin 	static const struct {
899109ec558STvrtko Ursulin 		u64 config;
900109ec558STvrtko Ursulin 		const char *name;
901109ec558STvrtko Ursulin 		const char *unit;
902109ec558STvrtko Ursulin 	} events[] = {
903e88866efSChris Wilson 		__event(I915_PMU_ACTUAL_FREQUENCY, "actual-frequency", "M"),
904e88866efSChris Wilson 		__event(I915_PMU_REQUESTED_FREQUENCY, "requested-frequency", "M"),
905109ec558STvrtko Ursulin 		__event(I915_PMU_INTERRUPTS, "interrupts", NULL),
906109ec558STvrtko Ursulin 		__event(I915_PMU_RC6_RESIDENCY, "rc6-residency", "ns"),
9078c3b1ba0SChris Wilson 		__event(I915_PMU_SOFTWARE_GT_AWAKE_TIME, "software-gt-awake-time", "ns"),
908109ec558STvrtko Ursulin 	};
909109ec558STvrtko Ursulin 	static const struct {
910109ec558STvrtko Ursulin 		enum drm_i915_pmu_engine_sample sample;
911109ec558STvrtko Ursulin 		char *name;
912109ec558STvrtko Ursulin 	} engine_events[] = {
913109ec558STvrtko Ursulin 		__engine_event(I915_SAMPLE_BUSY, "busy"),
914109ec558STvrtko Ursulin 		__engine_event(I915_SAMPLE_SEMA, "sema"),
915109ec558STvrtko Ursulin 		__engine_event(I915_SAMPLE_WAIT, "wait"),
916109ec558STvrtko Ursulin 	};
917109ec558STvrtko Ursulin 	unsigned int count = 0;
918109ec558STvrtko Ursulin 	struct perf_pmu_events_attr *pmu_attr = NULL, *pmu_iter;
919109ec558STvrtko Ursulin 	struct i915_ext_attribute *i915_attr = NULL, *i915_iter;
920109ec558STvrtko Ursulin 	struct attribute **attr = NULL, **attr_iter;
921109ec558STvrtko Ursulin 	struct intel_engine_cs *engine;
922109ec558STvrtko Ursulin 	unsigned int i;
923109ec558STvrtko Ursulin 
924109ec558STvrtko Ursulin 	/* Count how many counters we will be exposing. */
925109ec558STvrtko Ursulin 	for (i = 0; i < ARRAY_SIZE(events); i++) {
926109ec558STvrtko Ursulin 		if (!config_status(i915, events[i].config))
927109ec558STvrtko Ursulin 			count++;
928109ec558STvrtko Ursulin 	}
929109ec558STvrtko Ursulin 
930750e76b4SChris Wilson 	for_each_uabi_engine(engine, i915) {
931109ec558STvrtko Ursulin 		for (i = 0; i < ARRAY_SIZE(engine_events); i++) {
932109ec558STvrtko Ursulin 			if (!engine_event_status(engine,
933109ec558STvrtko Ursulin 						 engine_events[i].sample))
934109ec558STvrtko Ursulin 				count++;
935109ec558STvrtko Ursulin 		}
936109ec558STvrtko Ursulin 	}
937109ec558STvrtko Ursulin 
938109ec558STvrtko Ursulin 	/* Allocate attribute objects and table. */
939dd5fec87STvrtko Ursulin 	i915_attr = kcalloc(count, sizeof(*i915_attr), GFP_KERNEL);
940109ec558STvrtko Ursulin 	if (!i915_attr)
941109ec558STvrtko Ursulin 		goto err_alloc;
942109ec558STvrtko Ursulin 
943dd5fec87STvrtko Ursulin 	pmu_attr = kcalloc(count, sizeof(*pmu_attr), GFP_KERNEL);
944109ec558STvrtko Ursulin 	if (!pmu_attr)
945109ec558STvrtko Ursulin 		goto err_alloc;
946109ec558STvrtko Ursulin 
947109ec558STvrtko Ursulin 	/* Max one pointer of each attribute type plus a termination entry. */
948dd5fec87STvrtko Ursulin 	attr = kcalloc(count * 2 + 1, sizeof(*attr), GFP_KERNEL);
949109ec558STvrtko Ursulin 	if (!attr)
950109ec558STvrtko Ursulin 		goto err_alloc;
951109ec558STvrtko Ursulin 
952109ec558STvrtko Ursulin 	i915_iter = i915_attr;
953109ec558STvrtko Ursulin 	pmu_iter = pmu_attr;
954109ec558STvrtko Ursulin 	attr_iter = attr;
955109ec558STvrtko Ursulin 
956109ec558STvrtko Ursulin 	/* Initialize supported non-engine counters. */
957109ec558STvrtko Ursulin 	for (i = 0; i < ARRAY_SIZE(events); i++) {
958109ec558STvrtko Ursulin 		char *str;
959109ec558STvrtko Ursulin 
960109ec558STvrtko Ursulin 		if (config_status(i915, events[i].config))
961109ec558STvrtko Ursulin 			continue;
962109ec558STvrtko Ursulin 
963109ec558STvrtko Ursulin 		str = kstrdup(events[i].name, GFP_KERNEL);
964109ec558STvrtko Ursulin 		if (!str)
965109ec558STvrtko Ursulin 			goto err;
966109ec558STvrtko Ursulin 
967109ec558STvrtko Ursulin 		*attr_iter++ = &i915_iter->attr.attr;
968109ec558STvrtko Ursulin 		i915_iter = add_i915_attr(i915_iter, str, events[i].config);
969109ec558STvrtko Ursulin 
970109ec558STvrtko Ursulin 		if (events[i].unit) {
971109ec558STvrtko Ursulin 			str = kasprintf(GFP_KERNEL, "%s.unit", events[i].name);
972109ec558STvrtko Ursulin 			if (!str)
973109ec558STvrtko Ursulin 				goto err;
974109ec558STvrtko Ursulin 
975109ec558STvrtko Ursulin 			*attr_iter++ = &pmu_iter->attr.attr;
976109ec558STvrtko Ursulin 			pmu_iter = add_pmu_attr(pmu_iter, str, events[i].unit);
977109ec558STvrtko Ursulin 		}
978109ec558STvrtko Ursulin 	}
979109ec558STvrtko Ursulin 
980109ec558STvrtko Ursulin 	/* Initialize supported engine counters. */
981750e76b4SChris Wilson 	for_each_uabi_engine(engine, i915) {
982109ec558STvrtko Ursulin 		for (i = 0; i < ARRAY_SIZE(engine_events); i++) {
983109ec558STvrtko Ursulin 			char *str;
984109ec558STvrtko Ursulin 
985109ec558STvrtko Ursulin 			if (engine_event_status(engine,
986109ec558STvrtko Ursulin 						engine_events[i].sample))
987109ec558STvrtko Ursulin 				continue;
988109ec558STvrtko Ursulin 
989109ec558STvrtko Ursulin 			str = kasprintf(GFP_KERNEL, "%s-%s",
990109ec558STvrtko Ursulin 					engine->name, engine_events[i].name);
991109ec558STvrtko Ursulin 			if (!str)
992109ec558STvrtko Ursulin 				goto err;
993109ec558STvrtko Ursulin 
994109ec558STvrtko Ursulin 			*attr_iter++ = &i915_iter->attr.attr;
995109ec558STvrtko Ursulin 			i915_iter =
996109ec558STvrtko Ursulin 				add_i915_attr(i915_iter, str,
9978810bc56STvrtko Ursulin 					      __I915_PMU_ENGINE(engine->uabi_class,
998750e76b4SChris Wilson 								engine->uabi_instance,
999109ec558STvrtko Ursulin 								engine_events[i].sample));
1000109ec558STvrtko Ursulin 
1001109ec558STvrtko Ursulin 			str = kasprintf(GFP_KERNEL, "%s-%s.unit",
1002109ec558STvrtko Ursulin 					engine->name, engine_events[i].name);
1003109ec558STvrtko Ursulin 			if (!str)
1004109ec558STvrtko Ursulin 				goto err;
1005109ec558STvrtko Ursulin 
1006109ec558STvrtko Ursulin 			*attr_iter++ = &pmu_iter->attr.attr;
1007109ec558STvrtko Ursulin 			pmu_iter = add_pmu_attr(pmu_iter, str, "ns");
1008109ec558STvrtko Ursulin 		}
1009109ec558STvrtko Ursulin 	}
1010109ec558STvrtko Ursulin 
1011908091c8STvrtko Ursulin 	pmu->i915_attr = i915_attr;
1012908091c8STvrtko Ursulin 	pmu->pmu_attr = pmu_attr;
1013109ec558STvrtko Ursulin 
1014109ec558STvrtko Ursulin 	return attr;
1015109ec558STvrtko Ursulin 
1016109ec558STvrtko Ursulin err:;
1017109ec558STvrtko Ursulin 	for (attr_iter = attr; *attr_iter; attr_iter++)
1018109ec558STvrtko Ursulin 		kfree((*attr_iter)->name);
1019109ec558STvrtko Ursulin 
1020109ec558STvrtko Ursulin err_alloc:
1021109ec558STvrtko Ursulin 	kfree(attr);
1022109ec558STvrtko Ursulin 	kfree(i915_attr);
1023109ec558STvrtko Ursulin 	kfree(pmu_attr);
1024109ec558STvrtko Ursulin 
1025109ec558STvrtko Ursulin 	return NULL;
1026109ec558STvrtko Ursulin }
1027109ec558STvrtko Ursulin 
1028908091c8STvrtko Ursulin static void free_event_attributes(struct i915_pmu *pmu)
1029109ec558STvrtko Ursulin {
103046129dc1SMichał Winiarski 	struct attribute **attr_iter = pmu->events_attr_group.attrs;
1031109ec558STvrtko Ursulin 
1032109ec558STvrtko Ursulin 	for (; *attr_iter; attr_iter++)
1033109ec558STvrtko Ursulin 		kfree((*attr_iter)->name);
1034109ec558STvrtko Ursulin 
103546129dc1SMichał Winiarski 	kfree(pmu->events_attr_group.attrs);
1036908091c8STvrtko Ursulin 	kfree(pmu->i915_attr);
1037908091c8STvrtko Ursulin 	kfree(pmu->pmu_attr);
1038109ec558STvrtko Ursulin 
103946129dc1SMichał Winiarski 	pmu->events_attr_group.attrs = NULL;
1040908091c8STvrtko Ursulin 	pmu->i915_attr = NULL;
1041908091c8STvrtko Ursulin 	pmu->pmu_attr = NULL;
1042109ec558STvrtko Ursulin }
1043109ec558STvrtko Ursulin 
1044b46a33e2STvrtko Ursulin static int i915_pmu_cpu_online(unsigned int cpu, struct hlist_node *node)
1045b46a33e2STvrtko Ursulin {
1046f5a179d4SMichał Winiarski 	struct i915_pmu *pmu = hlist_entry_safe(node, typeof(*pmu), cpuhp.node);
1047b46a33e2STvrtko Ursulin 
1048b46a33e2STvrtko Ursulin 	GEM_BUG_ON(!pmu->base.event_init);
1049b46a33e2STvrtko Ursulin 
1050b46a33e2STvrtko Ursulin 	/* Select the first online CPU as a designated reader. */
10510426c046STvrtko Ursulin 	if (!cpumask_weight(&i915_pmu_cpumask))
1052b46a33e2STvrtko Ursulin 		cpumask_set_cpu(cpu, &i915_pmu_cpumask);
1053b46a33e2STvrtko Ursulin 
1054b46a33e2STvrtko Ursulin 	return 0;
1055b46a33e2STvrtko Ursulin }
1056b46a33e2STvrtko Ursulin 
1057b46a33e2STvrtko Ursulin static int i915_pmu_cpu_offline(unsigned int cpu, struct hlist_node *node)
1058b46a33e2STvrtko Ursulin {
1059f5a179d4SMichał Winiarski 	struct i915_pmu *pmu = hlist_entry_safe(node, typeof(*pmu), cpuhp.node);
1060537f9c84STvrtko Ursulin 	unsigned int target = i915_pmu_target_cpu;
1061b46a33e2STvrtko Ursulin 
1062b46a33e2STvrtko Ursulin 	GEM_BUG_ON(!pmu->base.event_init);
1063b46a33e2STvrtko Ursulin 
1064537f9c84STvrtko Ursulin 	/*
1065537f9c84STvrtko Ursulin 	 * Unregistering an instance generates a CPU offline event which we must
1066537f9c84STvrtko Ursulin 	 * ignore to avoid incorrectly modifying the shared i915_pmu_cpumask.
1067537f9c84STvrtko Ursulin 	 */
1068537f9c84STvrtko Ursulin 	if (pmu->closed)
1069537f9c84STvrtko Ursulin 		return 0;
1070537f9c84STvrtko Ursulin 
1071b46a33e2STvrtko Ursulin 	if (cpumask_test_and_clear_cpu(cpu, &i915_pmu_cpumask)) {
1072b46a33e2STvrtko Ursulin 		target = cpumask_any_but(topology_sibling_cpumask(cpu), cpu);
1073537f9c84STvrtko Ursulin 
1074b46a33e2STvrtko Ursulin 		/* Migrate events if there is a valid target */
1075b46a33e2STvrtko Ursulin 		if (target < nr_cpu_ids) {
1076b46a33e2STvrtko Ursulin 			cpumask_set_cpu(target, &i915_pmu_cpumask);
1077537f9c84STvrtko Ursulin 			i915_pmu_target_cpu = target;
1078b46a33e2STvrtko Ursulin 		}
1079b46a33e2STvrtko Ursulin 	}
1080b46a33e2STvrtko Ursulin 
1081537f9c84STvrtko Ursulin 	if (target < nr_cpu_ids && target != pmu->cpuhp.cpu) {
1082537f9c84STvrtko Ursulin 		perf_pmu_migrate_context(&pmu->base, cpu, target);
1083537f9c84STvrtko Ursulin 		pmu->cpuhp.cpu = target;
1084537f9c84STvrtko Ursulin 	}
1085537f9c84STvrtko Ursulin 
1086b46a33e2STvrtko Ursulin 	return 0;
1087b46a33e2STvrtko Ursulin }
1088b46a33e2STvrtko Ursulin 
1089537f9c84STvrtko Ursulin static enum cpuhp_state cpuhp_slot = CPUHP_INVALID;
1090537f9c84STvrtko Ursulin 
1091537f9c84STvrtko Ursulin void i915_pmu_init(void)
1092b46a33e2STvrtko Ursulin {
1093b46a33e2STvrtko Ursulin 	int ret;
1094b46a33e2STvrtko Ursulin 
1095b46a33e2STvrtko Ursulin 	ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN,
1096b46a33e2STvrtko Ursulin 				      "perf/x86/intel/i915:online",
1097b46a33e2STvrtko Ursulin 				      i915_pmu_cpu_online,
1098b46a33e2STvrtko Ursulin 				      i915_pmu_cpu_offline);
1099b46a33e2STvrtko Ursulin 	if (ret < 0)
1100537f9c84STvrtko Ursulin 		pr_notice("Failed to setup cpuhp state for i915 PMU! (%d)\n",
1101537f9c84STvrtko Ursulin 			  ret);
1102537f9c84STvrtko Ursulin 	else
1103537f9c84STvrtko Ursulin 		cpuhp_slot = ret;
1104b46a33e2STvrtko Ursulin }
1105b46a33e2STvrtko Ursulin 
1106537f9c84STvrtko Ursulin void i915_pmu_exit(void)
1107537f9c84STvrtko Ursulin {
1108537f9c84STvrtko Ursulin 	if (cpuhp_slot != CPUHP_INVALID)
1109537f9c84STvrtko Ursulin 		cpuhp_remove_multi_state(cpuhp_slot);
1110537f9c84STvrtko Ursulin }
1111537f9c84STvrtko Ursulin 
1112537f9c84STvrtko Ursulin static int i915_pmu_register_cpuhp_state(struct i915_pmu *pmu)
1113537f9c84STvrtko Ursulin {
1114537f9c84STvrtko Ursulin 	if (cpuhp_slot == CPUHP_INVALID)
1115537f9c84STvrtko Ursulin 		return -EINVAL;
1116537f9c84STvrtko Ursulin 
1117537f9c84STvrtko Ursulin 	return cpuhp_state_add_instance(cpuhp_slot, &pmu->cpuhp.node);
1118b46a33e2STvrtko Ursulin }
1119b46a33e2STvrtko Ursulin 
1120908091c8STvrtko Ursulin static void i915_pmu_unregister_cpuhp_state(struct i915_pmu *pmu)
1121b46a33e2STvrtko Ursulin {
1122537f9c84STvrtko Ursulin 	cpuhp_state_remove_instance(cpuhp_slot, &pmu->cpuhp.node);
1123b46a33e2STvrtko Ursulin }
1124b46a33e2STvrtko Ursulin 
112505488673STvrtko Ursulin static bool is_igp(struct drm_i915_private *i915)
112605488673STvrtko Ursulin {
11278ff5446aSThomas Zimmermann 	struct pci_dev *pdev = to_pci_dev(i915->drm.dev);
112805488673STvrtko Ursulin 
112905488673STvrtko Ursulin 	/* IGP is 0000:00:02.0 */
113005488673STvrtko Ursulin 	return pci_domain_nr(pdev->bus) == 0 &&
113105488673STvrtko Ursulin 	       pdev->bus->number == 0 &&
113205488673STvrtko Ursulin 	       PCI_SLOT(pdev->devfn) == 2 &&
113305488673STvrtko Ursulin 	       PCI_FUNC(pdev->devfn) == 0;
113405488673STvrtko Ursulin }
113505488673STvrtko Ursulin 
1136b46a33e2STvrtko Ursulin void i915_pmu_register(struct drm_i915_private *i915)
1137b46a33e2STvrtko Ursulin {
1138908091c8STvrtko Ursulin 	struct i915_pmu *pmu = &i915->pmu;
113946129dc1SMichał Winiarski 	const struct attribute_group *attr_groups[] = {
114046129dc1SMichał Winiarski 		&i915_pmu_format_attr_group,
114146129dc1SMichał Winiarski 		&pmu->events_attr_group,
114246129dc1SMichał Winiarski 		&i915_pmu_cpumask_attr_group,
114346129dc1SMichał Winiarski 		NULL
114446129dc1SMichał Winiarski 	};
114546129dc1SMichał Winiarski 
1146fb26eee0STvrtko Ursulin 	int ret = -ENOMEM;
1147b46a33e2STvrtko Ursulin 
1148*651e7d48SLucas De Marchi 	if (GRAPHICS_VER(i915) <= 2) {
11491900aba5SJani Nikula 		drm_info(&i915->drm, "PMU not supported for this GPU.");
1150b46a33e2STvrtko Ursulin 		return;
1151b46a33e2STvrtko Ursulin 	}
1152b46a33e2STvrtko Ursulin 
1153908091c8STvrtko Ursulin 	spin_lock_init(&pmu->lock);
1154908091c8STvrtko Ursulin 	hrtimer_init(&pmu->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1155908091c8STvrtko Ursulin 	pmu->timer.function = i915_sample;
1156537f9c84STvrtko Ursulin 	pmu->cpuhp.cpu = -1;
1157dbe13ae1STvrtko Ursulin 	init_rc6(pmu);
1158b46a33e2STvrtko Ursulin 
1159aebf3b52STvrtko Ursulin 	if (!is_igp(i915)) {
116005488673STvrtko Ursulin 		pmu->name = kasprintf(GFP_KERNEL,
1161aebf3b52STvrtko Ursulin 				      "i915_%s",
116205488673STvrtko Ursulin 				      dev_name(i915->drm.dev));
1163aebf3b52STvrtko Ursulin 		if (pmu->name) {
1164aebf3b52STvrtko Ursulin 			/* tools/perf reserves colons as special. */
1165aebf3b52STvrtko Ursulin 			strreplace((char *)pmu->name, ':', '_');
1166aebf3b52STvrtko Ursulin 		}
1167aebf3b52STvrtko Ursulin 	} else {
116805488673STvrtko Ursulin 		pmu->name = "i915";
1169aebf3b52STvrtko Ursulin 	}
117005488673STvrtko Ursulin 	if (!pmu->name)
1171b46a33e2STvrtko Ursulin 		goto err;
1172b46a33e2STvrtko Ursulin 
117346129dc1SMichał Winiarski 	pmu->events_attr_group.name = "events";
117446129dc1SMichał Winiarski 	pmu->events_attr_group.attrs = create_event_attributes(pmu);
117546129dc1SMichał Winiarski 	if (!pmu->events_attr_group.attrs)
1176c442292aSChris Wilson 		goto err_name;
1177c442292aSChris Wilson 
117846129dc1SMichał Winiarski 	pmu->base.attr_groups = kmemdup(attr_groups, sizeof(attr_groups),
117946129dc1SMichał Winiarski 					GFP_KERNEL);
118046129dc1SMichał Winiarski 	if (!pmu->base.attr_groups)
118146129dc1SMichał Winiarski 		goto err_attr;
118246129dc1SMichał Winiarski 
1183df3ab3cbSChris Wilson 	pmu->base.module	= THIS_MODULE;
1184c442292aSChris Wilson 	pmu->base.task_ctx_nr	= perf_invalid_context;
1185c442292aSChris Wilson 	pmu->base.event_init	= i915_pmu_event_init;
1186c442292aSChris Wilson 	pmu->base.add		= i915_pmu_event_add;
1187c442292aSChris Wilson 	pmu->base.del		= i915_pmu_event_del;
1188c442292aSChris Wilson 	pmu->base.start		= i915_pmu_event_start;
1189c442292aSChris Wilson 	pmu->base.stop		= i915_pmu_event_stop;
1190c442292aSChris Wilson 	pmu->base.read		= i915_pmu_event_read;
1191c442292aSChris Wilson 	pmu->base.event_idx	= i915_pmu_event_event_idx;
1192c442292aSChris Wilson 
119305488673STvrtko Ursulin 	ret = perf_pmu_register(&pmu->base, pmu->name, -1);
119405488673STvrtko Ursulin 	if (ret)
119546129dc1SMichał Winiarski 		goto err_groups;
119605488673STvrtko Ursulin 
1197908091c8STvrtko Ursulin 	ret = i915_pmu_register_cpuhp_state(pmu);
1198b46a33e2STvrtko Ursulin 	if (ret)
1199b46a33e2STvrtko Ursulin 		goto err_unreg;
1200b46a33e2STvrtko Ursulin 
1201b46a33e2STvrtko Ursulin 	return;
1202b46a33e2STvrtko Ursulin 
1203b46a33e2STvrtko Ursulin err_unreg:
1204908091c8STvrtko Ursulin 	perf_pmu_unregister(&pmu->base);
120546129dc1SMichał Winiarski err_groups:
120646129dc1SMichał Winiarski 	kfree(pmu->base.attr_groups);
1207c442292aSChris Wilson err_attr:
1208c442292aSChris Wilson 	pmu->base.event_init = NULL;
1209c442292aSChris Wilson 	free_event_attributes(pmu);
121005488673STvrtko Ursulin err_name:
121105488673STvrtko Ursulin 	if (!is_igp(i915))
121205488673STvrtko Ursulin 		kfree(pmu->name);
1213b46a33e2STvrtko Ursulin err:
12141900aba5SJani Nikula 	drm_notice(&i915->drm, "Failed to register PMU!\n");
1215b46a33e2STvrtko Ursulin }
1216b46a33e2STvrtko Ursulin 
1217b46a33e2STvrtko Ursulin void i915_pmu_unregister(struct drm_i915_private *i915)
1218b46a33e2STvrtko Ursulin {
1219908091c8STvrtko Ursulin 	struct i915_pmu *pmu = &i915->pmu;
1220908091c8STvrtko Ursulin 
1221908091c8STvrtko Ursulin 	if (!pmu->base.event_init)
1222b46a33e2STvrtko Ursulin 		return;
1223b46a33e2STvrtko Ursulin 
1224b00bccb3STvrtko Ursulin 	/*
1225b00bccb3STvrtko Ursulin 	 * "Disconnect" the PMU callbacks - since all are atomic synchronize_rcu
1226b00bccb3STvrtko Ursulin 	 * ensures all currently executing ones will have exited before we
1227b00bccb3STvrtko Ursulin 	 * proceed with unregistration.
1228b00bccb3STvrtko Ursulin 	 */
1229b00bccb3STvrtko Ursulin 	pmu->closed = true;
1230b00bccb3STvrtko Ursulin 	synchronize_rcu();
1231b46a33e2STvrtko Ursulin 
1232908091c8STvrtko Ursulin 	hrtimer_cancel(&pmu->timer);
1233b46a33e2STvrtko Ursulin 
1234908091c8STvrtko Ursulin 	i915_pmu_unregister_cpuhp_state(pmu);
1235b46a33e2STvrtko Ursulin 
1236908091c8STvrtko Ursulin 	perf_pmu_unregister(&pmu->base);
1237908091c8STvrtko Ursulin 	pmu->base.event_init = NULL;
123846129dc1SMichał Winiarski 	kfree(pmu->base.attr_groups);
123905488673STvrtko Ursulin 	if (!is_igp(i915))
124005488673STvrtko Ursulin 		kfree(pmu->name);
1241908091c8STvrtko Ursulin 	free_event_attributes(pmu);
1242b46a33e2STvrtko Ursulin }
1243