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"
11202b1f4cSMatt Roper #include "gt/intel_engine_regs.h"
12750e76b4SChris Wilson #include "gt/intel_engine_user.h"
13e367d3c4STvrtko Ursulin #include "gt/intel_gt.h"
1451fbd8deSChris Wilson #include "gt/intel_gt_pm.h"
150d6419e9SMatt Roper #include "gt/intel_gt_regs.h"
16c1132367SAndi Shyti #include "gt/intel_rc6.h"
173e7abf81SAndi Shyti #include "gt/intel_rps.h"
18112ed2d3SChris Wilson
19058a9b43SMichal Wajdeczko #include "i915_drv.h"
20ecbb5fb7SJani Nikula #include "i915_pmu.h"
21b46a33e2STvrtko Ursulin
22b46a33e2STvrtko Ursulin /* Frequency for the sampling timer for events which need it. */
23b46a33e2STvrtko Ursulin #define FREQUENCY 200
24b46a33e2STvrtko Ursulin #define PERIOD max_t(u64, 10000, NSEC_PER_SEC / FREQUENCY)
25b46a33e2STvrtko Ursulin
26b46a33e2STvrtko Ursulin #define ENGINE_SAMPLE_MASK \
27b46a33e2STvrtko Ursulin (BIT(I915_SAMPLE_BUSY) | \
28b46a33e2STvrtko Ursulin BIT(I915_SAMPLE_WAIT) | \
29b46a33e2STvrtko Ursulin BIT(I915_SAMPLE_SEMA))
30b46a33e2STvrtko Ursulin
31141a0895SChris Wilson static cpumask_t i915_pmu_cpumask;
32537f9c84STvrtko Ursulin static unsigned int i915_pmu_target_cpu = -1;
33b46a33e2STvrtko Ursulin
engine_config_sample(u64 config)34b46a33e2STvrtko Ursulin static u8 engine_config_sample(u64 config)
35b46a33e2STvrtko Ursulin {
36b46a33e2STvrtko Ursulin return config & I915_PMU_SAMPLE_MASK;
37b46a33e2STvrtko Ursulin }
38b46a33e2STvrtko Ursulin
engine_event_sample(struct perf_event * event)39b46a33e2STvrtko Ursulin static u8 engine_event_sample(struct perf_event *event)
40b46a33e2STvrtko Ursulin {
41b46a33e2STvrtko Ursulin return engine_config_sample(event->attr.config);
42b46a33e2STvrtko Ursulin }
43b46a33e2STvrtko Ursulin
engine_event_class(struct perf_event * event)44b46a33e2STvrtko Ursulin static u8 engine_event_class(struct perf_event *event)
45b46a33e2STvrtko Ursulin {
46b46a33e2STvrtko Ursulin return (event->attr.config >> I915_PMU_CLASS_SHIFT) & 0xff;
47b46a33e2STvrtko Ursulin }
48b46a33e2STvrtko Ursulin
engine_event_instance(struct perf_event * event)49b46a33e2STvrtko Ursulin static u8 engine_event_instance(struct perf_event *event)
50b46a33e2STvrtko Ursulin {
51b46a33e2STvrtko Ursulin return (event->attr.config >> I915_PMU_SAMPLE_BITS) & 0xff;
52b46a33e2STvrtko Ursulin }
53b46a33e2STvrtko Ursulin
is_engine_config(const u64 config)54a644fde7STvrtko Ursulin static bool is_engine_config(const u64 config)
55b46a33e2STvrtko Ursulin {
56b46a33e2STvrtko Ursulin return config < __I915_PMU_OTHER(0);
57b46a33e2STvrtko Ursulin }
58b46a33e2STvrtko Ursulin
config_gt_id(const u64 config)59bc4be0a3STvrtko Ursulin static unsigned int config_gt_id(const u64 config)
60bc4be0a3STvrtko Ursulin {
61bc4be0a3STvrtko Ursulin return config >> __I915_PMU_GT_SHIFT;
62bc4be0a3STvrtko Ursulin }
63bc4be0a3STvrtko Ursulin
config_counter(const u64 config)64bc4be0a3STvrtko Ursulin static u64 config_counter(const u64 config)
65bc4be0a3STvrtko Ursulin {
66bc4be0a3STvrtko Ursulin return config & ~(~0ULL << __I915_PMU_GT_SHIFT);
67bc4be0a3STvrtko Ursulin }
68bc4be0a3STvrtko Ursulin
other_bit(const u64 config)69348fb0cbSTvrtko Ursulin static unsigned int other_bit(const u64 config)
70348fb0cbSTvrtko Ursulin {
71348fb0cbSTvrtko Ursulin unsigned int val;
72348fb0cbSTvrtko Ursulin
73bc4be0a3STvrtko Ursulin switch (config_counter(config)) {
74348fb0cbSTvrtko Ursulin case I915_PMU_ACTUAL_FREQUENCY:
75348fb0cbSTvrtko Ursulin val = __I915_PMU_ACTUAL_FREQUENCY_ENABLED;
76348fb0cbSTvrtko Ursulin break;
77348fb0cbSTvrtko Ursulin case I915_PMU_REQUESTED_FREQUENCY:
78348fb0cbSTvrtko Ursulin val = __I915_PMU_REQUESTED_FREQUENCY_ENABLED;
79348fb0cbSTvrtko Ursulin break;
80348fb0cbSTvrtko Ursulin case I915_PMU_RC6_RESIDENCY:
81348fb0cbSTvrtko Ursulin val = __I915_PMU_RC6_RESIDENCY_ENABLED;
82348fb0cbSTvrtko Ursulin break;
83348fb0cbSTvrtko Ursulin default:
84348fb0cbSTvrtko Ursulin /*
85348fb0cbSTvrtko Ursulin * Events that do not require sampling, or tracking state
86348fb0cbSTvrtko Ursulin * transitions between enabled and disabled can be ignored.
87348fb0cbSTvrtko Ursulin */
88348fb0cbSTvrtko Ursulin return -1;
89348fb0cbSTvrtko Ursulin }
90348fb0cbSTvrtko Ursulin
91bc4be0a3STvrtko Ursulin return I915_ENGINE_SAMPLE_COUNT +
92bc4be0a3STvrtko Ursulin config_gt_id(config) * __I915_PMU_TRACKED_EVENT_COUNT +
93bc4be0a3STvrtko Ursulin val;
94348fb0cbSTvrtko Ursulin }
95348fb0cbSTvrtko Ursulin
config_bit(const u64 config)96348fb0cbSTvrtko Ursulin static unsigned int config_bit(const u64 config)
97b46a33e2STvrtko Ursulin {
98b46a33e2STvrtko Ursulin if (is_engine_config(config))
99b46a33e2STvrtko Ursulin return engine_config_sample(config);
100b46a33e2STvrtko Ursulin else
101348fb0cbSTvrtko Ursulin return other_bit(config);
102b46a33e2STvrtko Ursulin }
103b46a33e2STvrtko Ursulin
config_mask(const u64 config)104a644fde7STvrtko Ursulin static u32 config_mask(const u64 config)
105b46a33e2STvrtko Ursulin {
106a644fde7STvrtko Ursulin unsigned int bit = config_bit(config);
107a644fde7STvrtko Ursulin
108a644fde7STvrtko Ursulin if (__builtin_constant_p(config))
109a644fde7STvrtko Ursulin BUILD_BUG_ON(bit >
110a644fde7STvrtko Ursulin BITS_PER_TYPE(typeof_member(struct i915_pmu,
111a644fde7STvrtko Ursulin enable)) - 1);
112a644fde7STvrtko Ursulin else
113a644fde7STvrtko Ursulin WARN_ON_ONCE(bit >
114a644fde7STvrtko Ursulin BITS_PER_TYPE(typeof_member(struct i915_pmu,
115a644fde7STvrtko Ursulin enable)) - 1);
116a644fde7STvrtko Ursulin
117a644fde7STvrtko Ursulin return BIT(config_bit(config));
118b46a33e2STvrtko Ursulin }
119b46a33e2STvrtko Ursulin
is_engine_event(struct perf_event * event)120b46a33e2STvrtko Ursulin static bool is_engine_event(struct perf_event *event)
121b46a33e2STvrtko Ursulin {
122b46a33e2STvrtko Ursulin return is_engine_config(event->attr.config);
123b46a33e2STvrtko Ursulin }
124b46a33e2STvrtko Ursulin
event_bit(struct perf_event * event)125348fb0cbSTvrtko Ursulin static unsigned int event_bit(struct perf_event *event)
126b46a33e2STvrtko Ursulin {
127348fb0cbSTvrtko Ursulin return config_bit(event->attr.config);
128b46a33e2STvrtko Ursulin }
129b46a33e2STvrtko Ursulin
frequency_enabled_mask(void)130bc4be0a3STvrtko Ursulin static u32 frequency_enabled_mask(void)
131bc4be0a3STvrtko Ursulin {
132bc4be0a3STvrtko Ursulin unsigned int i;
133bc4be0a3STvrtko Ursulin u32 mask = 0;
134bc4be0a3STvrtko Ursulin
135419491eaSMatt Atwood for (i = 0; i < I915_PMU_MAX_GT; i++)
136bc4be0a3STvrtko Ursulin mask |= config_mask(__I915_PMU_ACTUAL_FREQUENCY(i)) |
137bc4be0a3STvrtko Ursulin config_mask(__I915_PMU_REQUESTED_FREQUENCY(i));
138bc4be0a3STvrtko Ursulin
139bc4be0a3STvrtko Ursulin return mask;
140bc4be0a3STvrtko Ursulin }
141bc4be0a3STvrtko Ursulin
pmu_needs_timer(struct i915_pmu * pmu)142ab129025SAshutosh Dixit static bool pmu_needs_timer(struct i915_pmu *pmu)
143feff0dc6STvrtko Ursulin {
144908091c8STvrtko Ursulin struct drm_i915_private *i915 = container_of(pmu, typeof(*i915), pmu);
145348fb0cbSTvrtko Ursulin u32 enable;
146feff0dc6STvrtko Ursulin
147feff0dc6STvrtko Ursulin /*
148feff0dc6STvrtko Ursulin * Only some counters need the sampling timer.
149feff0dc6STvrtko Ursulin *
150feff0dc6STvrtko Ursulin * We start with a bitmask of all currently enabled events.
151feff0dc6STvrtko Ursulin */
152908091c8STvrtko Ursulin enable = pmu->enable;
153feff0dc6STvrtko Ursulin
154feff0dc6STvrtko Ursulin /*
155feff0dc6STvrtko Ursulin * Mask out all the ones which do not need the timer, or in
156feff0dc6STvrtko Ursulin * other words keep all the ones that could need the timer.
157feff0dc6STvrtko Ursulin */
158bc4be0a3STvrtko Ursulin enable &= frequency_enabled_mask() | ENGINE_SAMPLE_MASK;
159feff0dc6STvrtko Ursulin
160feff0dc6STvrtko Ursulin /*
161b3add01eSTvrtko Ursulin * Also there is software busyness tracking available we do not
162b3add01eSTvrtko Ursulin * need the timer for I915_SAMPLE_BUSY counter.
163b3add01eSTvrtko Ursulin */
164ab129025SAshutosh Dixit if (i915->caps.scheduler & I915_SCHEDULER_CAP_ENGINE_BUSY_STATS)
165b3add01eSTvrtko Ursulin enable &= ~BIT(I915_SAMPLE_BUSY);
166feff0dc6STvrtko Ursulin
167feff0dc6STvrtko Ursulin /*
168feff0dc6STvrtko Ursulin * If some bits remain it means we need the sampling timer running.
169feff0dc6STvrtko Ursulin */
170feff0dc6STvrtko Ursulin return enable;
171feff0dc6STvrtko Ursulin }
172feff0dc6STvrtko Ursulin
__get_rc6(struct intel_gt * gt)173c1132367SAndi Shyti static u64 __get_rc6(struct intel_gt *gt)
17416ffe73cSChris Wilson {
17516ffe73cSChris Wilson struct drm_i915_private *i915 = gt->i915;
17616ffe73cSChris Wilson u64 val;
17716ffe73cSChris Wilson
17878d0b455SAshutosh Dixit val = intel_rc6_residency_ns(>->rc6, INTEL_RC6_RES_RC6);
17916ffe73cSChris Wilson
18016ffe73cSChris Wilson if (HAS_RC6p(i915))
18178d0b455SAshutosh Dixit val += intel_rc6_residency_ns(>->rc6, INTEL_RC6_RES_RC6p);
18216ffe73cSChris Wilson
18316ffe73cSChris Wilson if (HAS_RC6pp(i915))
18478d0b455SAshutosh Dixit val += intel_rc6_residency_ns(>->rc6, INTEL_RC6_RES_RC6pp);
18516ffe73cSChris Wilson
18616ffe73cSChris Wilson return val;
18716ffe73cSChris Wilson }
18816ffe73cSChris Wilson
ktime_since_raw(const ktime_t kt)189c51c29fbSTvrtko Ursulin static inline s64 ktime_since_raw(const ktime_t kt)
19016ffe73cSChris Wilson {
191c51c29fbSTvrtko Ursulin return ktime_to_ns(ktime_sub(ktime_get_raw(), kt));
19216ffe73cSChris Wilson }
19316ffe73cSChris Wilson
read_sample(struct i915_pmu * pmu,unsigned int gt_id,int sample)194bc4be0a3STvrtko Ursulin static u64 read_sample(struct i915_pmu *pmu, unsigned int gt_id, int sample)
195bc4be0a3STvrtko Ursulin {
1968ed0753bSAshutosh Dixit return pmu->sample[gt_id][sample].cur;
197bc4be0a3STvrtko Ursulin }
198bc4be0a3STvrtko Ursulin
199bc4be0a3STvrtko Ursulin static void
store_sample(struct i915_pmu * pmu,unsigned int gt_id,int sample,u64 val)200bc4be0a3STvrtko Ursulin store_sample(struct i915_pmu *pmu, unsigned int gt_id, int sample, u64 val)
201bc4be0a3STvrtko Ursulin {
2028ed0753bSAshutosh Dixit pmu->sample[gt_id][sample].cur = val;
203bc4be0a3STvrtko Ursulin }
204bc4be0a3STvrtko Ursulin
205bc4be0a3STvrtko Ursulin static void
add_sample_mult(struct i915_pmu * pmu,unsigned int gt_id,int sample,u32 val,u32 mul)206bc4be0a3STvrtko Ursulin add_sample_mult(struct i915_pmu *pmu, unsigned int gt_id, int sample, u32 val, u32 mul)
207bc4be0a3STvrtko Ursulin {
2088ed0753bSAshutosh Dixit pmu->sample[gt_id][sample].cur += mul_u32_u32(val, mul);
209bc4be0a3STvrtko Ursulin }
210bc4be0a3STvrtko Ursulin
get_rc6(struct intel_gt * gt)211df6a4205STvrtko Ursulin static u64 get_rc6(struct intel_gt *gt)
21216ffe73cSChris Wilson {
213df6a4205STvrtko Ursulin struct drm_i915_private *i915 = gt->i915;
214bc4be0a3STvrtko Ursulin const unsigned int gt_id = gt->info.id;
215df6a4205STvrtko Ursulin struct i915_pmu *pmu = &i915->pmu;
216df6a4205STvrtko Ursulin unsigned long flags;
217df6a4205STvrtko Ursulin bool awake = false;
21816ffe73cSChris Wilson u64 val;
21916ffe73cSChris Wilson
220df6a4205STvrtko Ursulin if (intel_gt_pm_get_if_awake(gt)) {
221df6a4205STvrtko Ursulin val = __get_rc6(gt);
222df6a4205STvrtko Ursulin intel_gt_pm_put_async(gt);
223df6a4205STvrtko Ursulin awake = true;
224df6a4205STvrtko Ursulin }
225df6a4205STvrtko Ursulin
226df6a4205STvrtko Ursulin spin_lock_irqsave(&pmu->lock, flags);
227df6a4205STvrtko Ursulin
228df6a4205STvrtko Ursulin if (awake) {
229bc4be0a3STvrtko Ursulin store_sample(pmu, gt_id, __I915_SAMPLE_RC6, val);
230df6a4205STvrtko Ursulin } else {
23116ffe73cSChris Wilson /*
23216ffe73cSChris Wilson * We think we are runtime suspended.
23316ffe73cSChris Wilson *
23416ffe73cSChris Wilson * Report the delta from when the device was suspended to now,
23516ffe73cSChris Wilson * on top of the last known real value, as the approximated RC6
23616ffe73cSChris Wilson * counter value.
23716ffe73cSChris Wilson */
238bc4be0a3STvrtko Ursulin val = ktime_since_raw(pmu->sleep_last[gt_id]);
239bc4be0a3STvrtko Ursulin val += read_sample(pmu, gt_id, __I915_SAMPLE_RC6);
24016ffe73cSChris Wilson }
24116ffe73cSChris Wilson
242bc4be0a3STvrtko Ursulin if (val < read_sample(pmu, gt_id, __I915_SAMPLE_RC6_LAST_REPORTED))
243bc4be0a3STvrtko Ursulin val = read_sample(pmu, gt_id, __I915_SAMPLE_RC6_LAST_REPORTED);
24416ffe73cSChris Wilson else
245bc4be0a3STvrtko Ursulin store_sample(pmu, gt_id, __I915_SAMPLE_RC6_LAST_REPORTED, val);
24616ffe73cSChris Wilson
24716ffe73cSChris Wilson spin_unlock_irqrestore(&pmu->lock, flags);
24816ffe73cSChris Wilson
24916ffe73cSChris Wilson return val;
25016ffe73cSChris Wilson }
25116ffe73cSChris Wilson
init_rc6(struct i915_pmu * pmu)252dbe13ae1STvrtko Ursulin static void init_rc6(struct i915_pmu *pmu)
253dbe13ae1STvrtko Ursulin {
254dbe13ae1STvrtko Ursulin struct drm_i915_private *i915 = container_of(pmu, typeof(*i915), pmu);
255bc4be0a3STvrtko Ursulin struct intel_gt *gt;
256bc4be0a3STvrtko Ursulin unsigned int i;
257bc4be0a3STvrtko Ursulin
258bc4be0a3STvrtko Ursulin for_each_gt(gt, i915, i) {
259dbe13ae1STvrtko Ursulin intel_wakeref_t wakeref;
260dbe13ae1STvrtko Ursulin
261bc4be0a3STvrtko Ursulin with_intel_runtime_pm(gt->uncore->rpm, wakeref) {
262bc4be0a3STvrtko Ursulin u64 val = __get_rc6(gt);
263bc4be0a3STvrtko Ursulin
264bc4be0a3STvrtko Ursulin store_sample(pmu, i, __I915_SAMPLE_RC6, val);
265bc4be0a3STvrtko Ursulin store_sample(pmu, i, __I915_SAMPLE_RC6_LAST_REPORTED,
266bc4be0a3STvrtko Ursulin val);
267bc4be0a3STvrtko Ursulin pmu->sleep_last[i] = ktime_get_raw();
268bc4be0a3STvrtko Ursulin }
269dbe13ae1STvrtko Ursulin }
270dbe13ae1STvrtko Ursulin }
271dbe13ae1STvrtko Ursulin
park_rc6(struct intel_gt * gt)272da5d5167STvrtko Ursulin static void park_rc6(struct intel_gt *gt)
273feff0dc6STvrtko Ursulin {
274da5d5167STvrtko Ursulin struct i915_pmu *pmu = >->i915->pmu;
275908091c8STvrtko Ursulin
276bc4be0a3STvrtko Ursulin store_sample(pmu, gt->info.id, __I915_SAMPLE_RC6, __get_rc6(gt));
277bc4be0a3STvrtko Ursulin pmu->sleep_last[gt->info.id] = ktime_get_raw();
278feff0dc6STvrtko Ursulin }
279feff0dc6STvrtko Ursulin
__i915_pmu_maybe_start_timer(struct i915_pmu * pmu)280908091c8STvrtko Ursulin static void __i915_pmu_maybe_start_timer(struct i915_pmu *pmu)
281feff0dc6STvrtko Ursulin {
282ab129025SAshutosh Dixit if (!pmu->timer_enabled && pmu_needs_timer(pmu)) {
283908091c8STvrtko Ursulin pmu->timer_enabled = true;
284908091c8STvrtko Ursulin pmu->timer_last = ktime_get();
285908091c8STvrtko Ursulin hrtimer_start_range_ns(&pmu->timer,
286feff0dc6STvrtko Ursulin ns_to_ktime(PERIOD), 0,
287feff0dc6STvrtko Ursulin HRTIMER_MODE_REL_PINNED);
288feff0dc6STvrtko Ursulin }
289feff0dc6STvrtko Ursulin }
290feff0dc6STvrtko Ursulin
i915_pmu_gt_parked(struct intel_gt * gt)291da5d5167STvrtko Ursulin void i915_pmu_gt_parked(struct intel_gt *gt)
29216ffe73cSChris Wilson {
293da5d5167STvrtko Ursulin struct i915_pmu *pmu = >->i915->pmu;
29416ffe73cSChris Wilson
29516ffe73cSChris Wilson if (!pmu->base.event_init)
29616ffe73cSChris Wilson return;
29716ffe73cSChris Wilson
29816ffe73cSChris Wilson spin_lock_irq(&pmu->lock);
29916ffe73cSChris Wilson
300da5d5167STvrtko Ursulin park_rc6(gt);
30116ffe73cSChris Wilson
30216ffe73cSChris Wilson /*
30316ffe73cSChris Wilson * Signal sampling timer to stop if only engine events are enabled and
30416ffe73cSChris Wilson * GPU went idle.
30516ffe73cSChris Wilson */
306b319cc59STvrtko Ursulin pmu->unparked &= ~BIT(gt->info.id);
307b319cc59STvrtko Ursulin if (pmu->unparked == 0)
308ab129025SAshutosh Dixit pmu->timer_enabled = false;
30916ffe73cSChris Wilson
31016ffe73cSChris Wilson spin_unlock_irq(&pmu->lock);
31116ffe73cSChris Wilson }
31216ffe73cSChris Wilson
i915_pmu_gt_unparked(struct intel_gt * gt)313da5d5167STvrtko Ursulin void i915_pmu_gt_unparked(struct intel_gt *gt)
314feff0dc6STvrtko Ursulin {
315da5d5167STvrtko Ursulin struct i915_pmu *pmu = >->i915->pmu;
316908091c8STvrtko Ursulin
317908091c8STvrtko Ursulin if (!pmu->base.event_init)
318feff0dc6STvrtko Ursulin return;
319feff0dc6STvrtko Ursulin
320908091c8STvrtko Ursulin spin_lock_irq(&pmu->lock);
32116ffe73cSChris Wilson
322feff0dc6STvrtko Ursulin /*
323feff0dc6STvrtko Ursulin * Re-enable sampling timer when GPU goes active.
324feff0dc6STvrtko Ursulin */
325b319cc59STvrtko Ursulin if (pmu->unparked == 0)
326908091c8STvrtko Ursulin __i915_pmu_maybe_start_timer(pmu);
32716ffe73cSChris Wilson
328b319cc59STvrtko Ursulin pmu->unparked |= BIT(gt->info.id);
329b319cc59STvrtko Ursulin
330908091c8STvrtko Ursulin spin_unlock_irq(&pmu->lock);
331feff0dc6STvrtko Ursulin }
332feff0dc6STvrtko Ursulin
333b46a33e2STvrtko Ursulin static void
add_sample(struct i915_pmu_sample * sample,u32 val)3349f473ecfSTvrtko Ursulin add_sample(struct i915_pmu_sample *sample, u32 val)
335b46a33e2STvrtko Ursulin {
3369f473ecfSTvrtko Ursulin sample->cur += val;
337b46a33e2STvrtko Ursulin }
338b46a33e2STvrtko Ursulin
exclusive_mmio_access(const struct drm_i915_private * i915)339d79e1bd6SChris Wilson static bool exclusive_mmio_access(const struct drm_i915_private *i915)
340d79e1bd6SChris Wilson {
341d79e1bd6SChris Wilson /*
342d79e1bd6SChris Wilson * We have to avoid concurrent mmio cache line access on gen7 or
343d79e1bd6SChris Wilson * risk a machine hang. For a fun history lesson dig out the old
344d79e1bd6SChris Wilson * userspace intel_gpu_top and run it on Ivybridge or Haswell!
345d79e1bd6SChris Wilson */
346651e7d48SLucas De Marchi return GRAPHICS_VER(i915) == 7;
347d79e1bd6SChris Wilson }
348d79e1bd6SChris Wilson
engine_sample(struct intel_engine_cs * engine,unsigned int period_ns)3496ec81b82SArnd Bergmann static void engine_sample(struct intel_engine_cs *engine, unsigned int period_ns)
350b46a33e2STvrtko Ursulin {
351d0aa694bSChris Wilson struct intel_engine_pmu *pmu = &engine->pmu;
352d0aa694bSChris Wilson bool busy;
353b46a33e2STvrtko Ursulin u32 val;
354b46a33e2STvrtko Ursulin
35528fba096STvrtko Ursulin val = ENGINE_READ_FW(engine, RING_CTL);
356d0aa694bSChris Wilson if (val == 0) /* powerwell off => engine idle */
3576ec81b82SArnd Bergmann return;
358b46a33e2STvrtko Ursulin
3599f473ecfSTvrtko Ursulin if (val & RING_WAIT)
360d0aa694bSChris Wilson add_sample(&pmu->sample[I915_SAMPLE_WAIT], period_ns);
3619f473ecfSTvrtko Ursulin if (val & RING_WAIT_SEMAPHORE)
362d0aa694bSChris Wilson add_sample(&pmu->sample[I915_SAMPLE_SEMA], period_ns);
363b46a33e2STvrtko Ursulin
36454fc577dSTvrtko Ursulin /* No need to sample when busy stats are supported. */
36554fc577dSTvrtko Ursulin if (intel_engine_supports_stats(engine))
3666ec81b82SArnd Bergmann return;
36754fc577dSTvrtko Ursulin
368d0aa694bSChris Wilson /*
369d0aa694bSChris Wilson * While waiting on a semaphore or event, MI_MODE reports the
370d0aa694bSChris Wilson * ring as idle. However, previously using the seqno, and with
371d0aa694bSChris Wilson * execlists sampling, we account for the ring waiting as the
372d0aa694bSChris Wilson * engine being busy. Therefore, we record the sample as being
373d0aa694bSChris Wilson * busy if either waiting or !idle.
374d0aa694bSChris Wilson */
375d0aa694bSChris Wilson busy = val & (RING_WAIT_SEMAPHORE | RING_WAIT);
376d0aa694bSChris Wilson if (!busy) {
37728fba096STvrtko Ursulin val = ENGINE_READ_FW(engine, RING_MI_MODE);
378d0aa694bSChris Wilson busy = !(val & MODE_IDLE);
379d0aa694bSChris Wilson }
380d0aa694bSChris Wilson if (busy)
381d0aa694bSChris Wilson add_sample(&pmu->sample[I915_SAMPLE_BUSY], period_ns);
3826ec81b82SArnd Bergmann }
383b46a33e2STvrtko Ursulin
3846ec81b82SArnd Bergmann static void
engines_sample(struct intel_gt * gt,unsigned int period_ns)3856ec81b82SArnd Bergmann engines_sample(struct intel_gt *gt, unsigned int period_ns)
3866ec81b82SArnd Bergmann {
3876ec81b82SArnd Bergmann struct drm_i915_private *i915 = gt->i915;
3886ec81b82SArnd Bergmann struct intel_engine_cs *engine;
3896ec81b82SArnd Bergmann enum intel_engine_id id;
3906ec81b82SArnd Bergmann unsigned long flags;
3916ec81b82SArnd Bergmann
3926ec81b82SArnd Bergmann if ((i915->pmu.enable & ENGINE_SAMPLE_MASK) == 0)
3936ec81b82SArnd Bergmann return;
3946ec81b82SArnd Bergmann
3956ec81b82SArnd Bergmann if (!intel_gt_pm_is_awake(gt))
3966ec81b82SArnd Bergmann return;
3976ec81b82SArnd Bergmann
3986ec81b82SArnd Bergmann for_each_engine(engine, gt, id) {
39908322dabSTvrtko Ursulin if (!engine->pmu.enable)
40008322dabSTvrtko Ursulin continue;
40108322dabSTvrtko Ursulin
4026ec81b82SArnd Bergmann if (!intel_engine_pm_get_if_awake(engine))
4036ec81b82SArnd Bergmann continue;
4046ec81b82SArnd Bergmann
4056ec81b82SArnd Bergmann if (exclusive_mmio_access(i915)) {
4066ec81b82SArnd Bergmann spin_lock_irqsave(&engine->uncore->lock, flags);
4076ec81b82SArnd Bergmann engine_sample(engine, period_ns);
4086ec81b82SArnd Bergmann spin_unlock_irqrestore(&engine->uncore->lock, flags);
4096ec81b82SArnd Bergmann } else {
4106ec81b82SArnd Bergmann engine_sample(engine, period_ns);
4116ec81b82SArnd Bergmann }
4126ec81b82SArnd Bergmann
41307779a76SChris Wilson intel_engine_pm_put_async(engine);
41451fbd8deSChris Wilson }
415b46a33e2STvrtko Ursulin }
416b46a33e2STvrtko Ursulin
417bc4be0a3STvrtko Ursulin static bool
frequency_sampling_enabled(struct i915_pmu * pmu,unsigned int gt)418bc4be0a3STvrtko Ursulin frequency_sampling_enabled(struct i915_pmu *pmu, unsigned int gt)
419b66ecd04STvrtko Ursulin {
420b66ecd04STvrtko Ursulin return pmu->enable &
421bc4be0a3STvrtko Ursulin (config_mask(__I915_PMU_ACTUAL_FREQUENCY(gt)) |
422bc4be0a3STvrtko Ursulin config_mask(__I915_PMU_REQUESTED_FREQUENCY(gt)));
423b66ecd04STvrtko Ursulin }
424b66ecd04STvrtko Ursulin
4259f473ecfSTvrtko Ursulin static void
frequency_sample(struct intel_gt * gt,unsigned int period_ns)42608ce5c64STvrtko Ursulin frequency_sample(struct intel_gt *gt, unsigned int period_ns)
427b46a33e2STvrtko Ursulin {
42808ce5c64STvrtko Ursulin struct drm_i915_private *i915 = gt->i915;
429bc4be0a3STvrtko Ursulin const unsigned int gt_id = gt->info.id;
43008ce5c64STvrtko Ursulin struct i915_pmu *pmu = &i915->pmu;
4313e7abf81SAndi Shyti struct intel_rps *rps = >->rps;
43208ce5c64STvrtko Ursulin
433bc4be0a3STvrtko Ursulin if (!frequency_sampling_enabled(pmu, gt_id))
434b66ecd04STvrtko Ursulin return;
435b66ecd04STvrtko Ursulin
436b66ecd04STvrtko Ursulin /* Report 0/0 (actual/requested) frequency while parked. */
437b66ecd04STvrtko Ursulin if (!intel_gt_pm_get_if_awake(gt))
438b66ecd04STvrtko Ursulin return;
439b66ecd04STvrtko Ursulin
440bc4be0a3STvrtko Ursulin if (pmu->enable & config_mask(__I915_PMU_ACTUAL_FREQUENCY(gt_id))) {
441b46a33e2STvrtko Ursulin u32 val;
442b46a33e2STvrtko Ursulin
443c1c82d26SChris Wilson /*
444c1c82d26SChris Wilson * We take a quick peek here without using forcewake
445c1c82d26SChris Wilson * so that we don't perturb the system under observation
446c1c82d26SChris Wilson * (forcewake => !rc6 => increased power use). We expect
447c1c82d26SChris Wilson * that if the read fails because it is outside of the
448c1c82d26SChris Wilson * mmio power well, then it will return 0 -- in which
449c1c82d26SChris Wilson * case we assume the system is running at the intended
450c1c82d26SChris Wilson * frequency. Fortunately, the read should rarely fail!
451c1c82d26SChris Wilson */
45244df42e6SAshutosh Dixit val = intel_rps_read_actual_frequency_fw(rps);
45344df42e6SAshutosh Dixit if (!val)
45444df42e6SAshutosh Dixit val = intel_gpu_freq(rps, rps->cur_freq);
455b46a33e2STvrtko Ursulin
456bc4be0a3STvrtko Ursulin add_sample_mult(pmu, gt_id, __I915_SAMPLE_FREQ_ACT,
45744df42e6SAshutosh Dixit val, period_ns / 1000);
458b46a33e2STvrtko Ursulin }
459b46a33e2STvrtko Ursulin
460bc4be0a3STvrtko Ursulin if (pmu->enable & config_mask(__I915_PMU_REQUESTED_FREQUENCY(gt_id))) {
461bc4be0a3STvrtko Ursulin add_sample_mult(pmu, gt_id, __I915_SAMPLE_FREQ_REQ,
46241e5c17eSVinay Belgaumkar intel_rps_get_requested_frequency(rps),
4639f473ecfSTvrtko Ursulin period_ns / 1000);
464b46a33e2STvrtko Ursulin }
465b66ecd04STvrtko Ursulin
466b66ecd04STvrtko Ursulin intel_gt_pm_put_async(gt);
467b46a33e2STvrtko Ursulin }
468b46a33e2STvrtko Ursulin
i915_sample(struct hrtimer * hrtimer)469b46a33e2STvrtko Ursulin static enum hrtimer_restart i915_sample(struct hrtimer *hrtimer)
470b46a33e2STvrtko Ursulin {
471b46a33e2STvrtko Ursulin struct drm_i915_private *i915 =
472b46a33e2STvrtko Ursulin container_of(hrtimer, struct drm_i915_private, pmu.timer);
473908091c8STvrtko Ursulin struct i915_pmu *pmu = &i915->pmu;
4749f473ecfSTvrtko Ursulin unsigned int period_ns;
475e367d3c4STvrtko Ursulin struct intel_gt *gt;
476e367d3c4STvrtko Ursulin unsigned int i;
4779f473ecfSTvrtko Ursulin ktime_t now;
478b46a33e2STvrtko Ursulin
479908091c8STvrtko Ursulin if (!READ_ONCE(pmu->timer_enabled))
480b46a33e2STvrtko Ursulin return HRTIMER_NORESTART;
481b46a33e2STvrtko Ursulin
4829f473ecfSTvrtko Ursulin now = ktime_get();
483908091c8STvrtko Ursulin period_ns = ktime_to_ns(ktime_sub(now, pmu->timer_last));
484908091c8STvrtko Ursulin pmu->timer_last = now;
485b46a33e2STvrtko Ursulin
4869f473ecfSTvrtko Ursulin /*
4879f473ecfSTvrtko Ursulin * Strictly speaking the passed in period may not be 100% accurate for
4889f473ecfSTvrtko Ursulin * all internal calculation, since some amount of time can be spent on
4899f473ecfSTvrtko Ursulin * grabbing the forcewake. However the potential error from timer call-
4909f473ecfSTvrtko Ursulin * back delay greatly dominates this so we keep it simple.
4919f473ecfSTvrtko Ursulin */
492e367d3c4STvrtko Ursulin
493e367d3c4STvrtko Ursulin for_each_gt(gt, i915, i) {
494b319cc59STvrtko Ursulin if (!(pmu->unparked & BIT(i)))
495b319cc59STvrtko Ursulin continue;
496b319cc59STvrtko Ursulin
49708ce5c64STvrtko Ursulin engines_sample(gt, period_ns);
49808ce5c64STvrtko Ursulin frequency_sample(gt, period_ns);
499e367d3c4STvrtko Ursulin }
5009f473ecfSTvrtko Ursulin
5019f473ecfSTvrtko Ursulin hrtimer_forward(hrtimer, now, ns_to_ktime(PERIOD));
5029f473ecfSTvrtko Ursulin
503b46a33e2STvrtko Ursulin return HRTIMER_RESTART;
504b46a33e2STvrtko Ursulin }
505b46a33e2STvrtko Ursulin
i915_pmu_event_destroy(struct perf_event * event)506b46a33e2STvrtko Ursulin static void i915_pmu_event_destroy(struct perf_event *event)
507b46a33e2STvrtko Ursulin {
508bf07f6ebSPankaj Bharadiya struct drm_i915_private *i915 =
509bf07f6ebSPankaj Bharadiya container_of(event->pmu, typeof(*i915), pmu.base);
510bf07f6ebSPankaj Bharadiya
511bf07f6ebSPankaj Bharadiya drm_WARN_ON(&i915->drm, event->parent);
512b00bccb3STvrtko Ursulin
513b00bccb3STvrtko Ursulin drm_dev_put(&i915->drm);
514b46a33e2STvrtko Ursulin }
515b46a33e2STvrtko Ursulin
516109ec558STvrtko Ursulin static int
engine_event_status(struct intel_engine_cs * engine,enum drm_i915_pmu_engine_sample sample)517109ec558STvrtko Ursulin engine_event_status(struct intel_engine_cs *engine,
518109ec558STvrtko Ursulin enum drm_i915_pmu_engine_sample sample)
519b46a33e2STvrtko Ursulin {
520109ec558STvrtko Ursulin switch (sample) {
521b46a33e2STvrtko Ursulin case I915_SAMPLE_BUSY:
522b46a33e2STvrtko Ursulin case I915_SAMPLE_WAIT:
523b46a33e2STvrtko Ursulin break;
524b46a33e2STvrtko Ursulin case I915_SAMPLE_SEMA:
525651e7d48SLucas De Marchi if (GRAPHICS_VER(engine->i915) < 6)
526b46a33e2STvrtko Ursulin return -ENODEV;
527b46a33e2STvrtko Ursulin break;
528b46a33e2STvrtko Ursulin default:
529b46a33e2STvrtko Ursulin return -ENOENT;
530b46a33e2STvrtko Ursulin }
531b46a33e2STvrtko Ursulin
532b46a33e2STvrtko Ursulin return 0;
533b46a33e2STvrtko Ursulin }
534b46a33e2STvrtko Ursulin
535109ec558STvrtko Ursulin static int
config_status(struct drm_i915_private * i915,u64 config)536109ec558STvrtko Ursulin config_status(struct drm_i915_private *i915, u64 config)
537109ec558STvrtko Ursulin {
5382cbc876dSMichał Winiarski struct intel_gt *gt = to_gt(i915);
539399cd979STvrtko Ursulin
540bc4be0a3STvrtko Ursulin unsigned int gt_id = config_gt_id(config);
541bc4be0a3STvrtko Ursulin unsigned int max_gt_id = HAS_EXTRA_GT_LIST(i915) ? 1 : 0;
542bc4be0a3STvrtko Ursulin
543bc4be0a3STvrtko Ursulin if (gt_id > max_gt_id)
544bc4be0a3STvrtko Ursulin return -ENOENT;
545bc4be0a3STvrtko Ursulin
546bc4be0a3STvrtko Ursulin switch (config_counter(config)) {
547109ec558STvrtko Ursulin case I915_PMU_ACTUAL_FREQUENCY:
548109ec558STvrtko Ursulin if (IS_VALLEYVIEW(i915) || IS_CHERRYVIEW(i915))
549109ec558STvrtko Ursulin /* Requires a mutex for sampling! */
550109ec558STvrtko Ursulin return -ENODEV;
551df561f66SGustavo A. R. Silva fallthrough;
552109ec558STvrtko Ursulin case I915_PMU_REQUESTED_FREQUENCY:
553651e7d48SLucas De Marchi if (GRAPHICS_VER(i915) < 6)
554109ec558STvrtko Ursulin return -ENODEV;
555109ec558STvrtko Ursulin break;
556109ec558STvrtko Ursulin case I915_PMU_INTERRUPTS:
557bc4be0a3STvrtko Ursulin if (gt_id)
558bc4be0a3STvrtko Ursulin return -ENOENT;
559109ec558STvrtko Ursulin break;
560109ec558STvrtko Ursulin case I915_PMU_RC6_RESIDENCY:
561399cd979STvrtko Ursulin if (!gt->rc6.supported)
562109ec558STvrtko Ursulin return -ENODEV;
563109ec558STvrtko Ursulin break;
5648c3b1ba0SChris Wilson case I915_PMU_SOFTWARE_GT_AWAKE_TIME:
5658c3b1ba0SChris Wilson break;
566109ec558STvrtko Ursulin default:
567109ec558STvrtko Ursulin return -ENOENT;
568109ec558STvrtko Ursulin }
569109ec558STvrtko Ursulin
570109ec558STvrtko Ursulin return 0;
571109ec558STvrtko Ursulin }
572109ec558STvrtko Ursulin
engine_event_init(struct perf_event * event)573109ec558STvrtko Ursulin static int engine_event_init(struct perf_event *event)
574109ec558STvrtko Ursulin {
575109ec558STvrtko Ursulin struct drm_i915_private *i915 =
576109ec558STvrtko Ursulin container_of(event->pmu, typeof(*i915), pmu.base);
577109ec558STvrtko Ursulin struct intel_engine_cs *engine;
578109ec558STvrtko Ursulin
579109ec558STvrtko Ursulin engine = intel_engine_lookup_user(i915, engine_event_class(event),
580109ec558STvrtko Ursulin engine_event_instance(event));
581109ec558STvrtko Ursulin if (!engine)
582109ec558STvrtko Ursulin return -ENODEV;
583109ec558STvrtko Ursulin
584426d0073SChris Wilson return engine_event_status(engine, engine_event_sample(event));
585109ec558STvrtko Ursulin }
586109ec558STvrtko Ursulin
i915_pmu_event_init(struct perf_event * event)587b46a33e2STvrtko Ursulin static int i915_pmu_event_init(struct perf_event *event)
588b46a33e2STvrtko Ursulin {
589b46a33e2STvrtko Ursulin struct drm_i915_private *i915 =
590b46a33e2STvrtko Ursulin container_of(event->pmu, typeof(*i915), pmu.base);
591b00bccb3STvrtko Ursulin struct i915_pmu *pmu = &i915->pmu;
5920426c046STvrtko Ursulin int ret;
593b46a33e2STvrtko Ursulin
594b00bccb3STvrtko Ursulin if (pmu->closed)
595b00bccb3STvrtko Ursulin return -ENODEV;
596b00bccb3STvrtko Ursulin
597b46a33e2STvrtko Ursulin if (event->attr.type != event->pmu->type)
598b46a33e2STvrtko Ursulin return -ENOENT;
599b46a33e2STvrtko Ursulin
600b46a33e2STvrtko Ursulin /* unsupported modes and filters */
601b46a33e2STvrtko Ursulin if (event->attr.sample_period) /* no sampling */
602b46a33e2STvrtko Ursulin return -EINVAL;
603b46a33e2STvrtko Ursulin
604b46a33e2STvrtko Ursulin if (has_branch_stack(event))
605b46a33e2STvrtko Ursulin return -EOPNOTSUPP;
606b46a33e2STvrtko Ursulin
607b46a33e2STvrtko Ursulin if (event->cpu < 0)
608b46a33e2STvrtko Ursulin return -EINVAL;
609b46a33e2STvrtko Ursulin
6100426c046STvrtko Ursulin /* only allow running on one cpu at a time */
6110426c046STvrtko Ursulin if (!cpumask_test_cpu(event->cpu, &i915_pmu_cpumask))
61200a79722STvrtko Ursulin return -EINVAL;
613b46a33e2STvrtko Ursulin
614109ec558STvrtko Ursulin if (is_engine_event(event))
615b46a33e2STvrtko Ursulin ret = engine_event_init(event);
616109ec558STvrtko Ursulin else
617109ec558STvrtko Ursulin ret = config_status(i915, event->attr.config);
618b46a33e2STvrtko Ursulin if (ret)
619b46a33e2STvrtko Ursulin return ret;
620b46a33e2STvrtko Ursulin
621b00bccb3STvrtko Ursulin if (!event->parent) {
622b00bccb3STvrtko Ursulin drm_dev_get(&i915->drm);
623b46a33e2STvrtko Ursulin event->destroy = i915_pmu_event_destroy;
624b00bccb3STvrtko Ursulin }
625b46a33e2STvrtko Ursulin
626b46a33e2STvrtko Ursulin return 0;
627b46a33e2STvrtko Ursulin }
628b46a33e2STvrtko Ursulin
__i915_pmu_event_read(struct perf_event * event)629ad055fb8STvrtko Ursulin static u64 __i915_pmu_event_read(struct perf_event *event)
630b46a33e2STvrtko Ursulin {
631b46a33e2STvrtko Ursulin struct drm_i915_private *i915 =
632b46a33e2STvrtko Ursulin container_of(event->pmu, typeof(*i915), pmu.base);
633908091c8STvrtko Ursulin struct i915_pmu *pmu = &i915->pmu;
634b46a33e2STvrtko Ursulin u64 val = 0;
635b46a33e2STvrtko Ursulin
636b46a33e2STvrtko Ursulin if (is_engine_event(event)) {
637b46a33e2STvrtko Ursulin u8 sample = engine_event_sample(event);
638b46a33e2STvrtko Ursulin struct intel_engine_cs *engine;
639b46a33e2STvrtko Ursulin
640b46a33e2STvrtko Ursulin engine = intel_engine_lookup_user(i915,
641b46a33e2STvrtko Ursulin engine_event_class(event),
642b46a33e2STvrtko Ursulin engine_event_instance(event));
643b46a33e2STvrtko Ursulin
64448a1b8d4SPankaj Bharadiya if (drm_WARN_ON_ONCE(&i915->drm, !engine)) {
645b46a33e2STvrtko Ursulin /* Do nothing */
646b3add01eSTvrtko Ursulin } else if (sample == I915_SAMPLE_BUSY &&
647b2f78cdaSTvrtko Ursulin intel_engine_supports_stats(engine)) {
648810b7ee3SChris Wilson ktime_t unused;
649810b7ee3SChris Wilson
650810b7ee3SChris Wilson val = ktime_to_ns(intel_engine_get_busy_time(engine,
651810b7ee3SChris Wilson &unused));
652b46a33e2STvrtko Ursulin } else {
653b46a33e2STvrtko Ursulin val = engine->pmu.sample[sample].cur;
654b46a33e2STvrtko Ursulin }
655b46a33e2STvrtko Ursulin } else {
656bc4be0a3STvrtko Ursulin const unsigned int gt_id = config_gt_id(event->attr.config);
657bc4be0a3STvrtko Ursulin const u64 config = config_counter(event->attr.config);
658bc4be0a3STvrtko Ursulin
659bc4be0a3STvrtko Ursulin switch (config) {
660b46a33e2STvrtko Ursulin case I915_PMU_ACTUAL_FREQUENCY:
661b46a33e2STvrtko Ursulin val =
662bc4be0a3STvrtko Ursulin div_u64(read_sample(pmu, gt_id,
663bc4be0a3STvrtko Ursulin __I915_SAMPLE_FREQ_ACT),
6649f473ecfSTvrtko Ursulin USEC_PER_SEC /* to MHz */);
665b46a33e2STvrtko Ursulin break;
666b46a33e2STvrtko Ursulin case I915_PMU_REQUESTED_FREQUENCY:
667b46a33e2STvrtko Ursulin val =
668bc4be0a3STvrtko Ursulin div_u64(read_sample(pmu, gt_id,
669bc4be0a3STvrtko Ursulin __I915_SAMPLE_FREQ_REQ),
6709f473ecfSTvrtko Ursulin USEC_PER_SEC /* to MHz */);
671b46a33e2STvrtko Ursulin break;
6720cd4684dSTvrtko Ursulin case I915_PMU_INTERRUPTS:
6739c6508b9SThomas Gleixner val = READ_ONCE(pmu->irq_count);
6740cd4684dSTvrtko Ursulin break;
6756060b6aeSTvrtko Ursulin case I915_PMU_RC6_RESIDENCY:
676bc4be0a3STvrtko Ursulin val = get_rc6(i915->gt[gt_id]);
6776060b6aeSTvrtko Ursulin break;
6788c3b1ba0SChris Wilson case I915_PMU_SOFTWARE_GT_AWAKE_TIME:
6792cbc876dSMichał Winiarski val = ktime_to_ns(intel_gt_get_awake_time(to_gt(i915)));
6808c3b1ba0SChris Wilson break;
681b46a33e2STvrtko Ursulin }
682b46a33e2STvrtko Ursulin }
683b46a33e2STvrtko Ursulin
684b46a33e2STvrtko Ursulin return val;
685b46a33e2STvrtko Ursulin }
686b46a33e2STvrtko Ursulin
i915_pmu_event_read(struct perf_event * event)687b46a33e2STvrtko Ursulin static void i915_pmu_event_read(struct perf_event *event)
688b46a33e2STvrtko Ursulin {
689b00bccb3STvrtko Ursulin struct drm_i915_private *i915 =
690b00bccb3STvrtko Ursulin container_of(event->pmu, typeof(*i915), pmu.base);
691b46a33e2STvrtko Ursulin struct hw_perf_event *hwc = &event->hw;
692b00bccb3STvrtko Ursulin struct i915_pmu *pmu = &i915->pmu;
693b46a33e2STvrtko Ursulin u64 prev, new;
694b46a33e2STvrtko Ursulin
695b00bccb3STvrtko Ursulin if (pmu->closed) {
696b00bccb3STvrtko Ursulin event->hw.state = PERF_HES_STOPPED;
697b00bccb3STvrtko Ursulin return;
698b00bccb3STvrtko Ursulin }
699b46a33e2STvrtko Ursulin again:
700b46a33e2STvrtko Ursulin prev = local64_read(&hwc->prev_count);
701ad055fb8STvrtko Ursulin new = __i915_pmu_event_read(event);
702b46a33e2STvrtko Ursulin
703b46a33e2STvrtko Ursulin if (local64_cmpxchg(&hwc->prev_count, prev, new) != prev)
704b46a33e2STvrtko Ursulin goto again;
705b46a33e2STvrtko Ursulin
706b46a33e2STvrtko Ursulin local64_add(new - prev, &event->count);
707b46a33e2STvrtko Ursulin }
708b46a33e2STvrtko Ursulin
i915_pmu_enable(struct perf_event * event)709b46a33e2STvrtko Ursulin static void i915_pmu_enable(struct perf_event *event)
710b46a33e2STvrtko Ursulin {
711b46a33e2STvrtko Ursulin struct drm_i915_private *i915 =
712b46a33e2STvrtko Ursulin container_of(event->pmu, typeof(*i915), pmu.base);
713a644fde7STvrtko Ursulin const unsigned int bit = event_bit(event);
714908091c8STvrtko Ursulin struct i915_pmu *pmu = &i915->pmu;
715b46a33e2STvrtko Ursulin unsigned long flags;
716b46a33e2STvrtko Ursulin
717348fb0cbSTvrtko Ursulin if (bit == -1)
718348fb0cbSTvrtko Ursulin goto update;
719348fb0cbSTvrtko Ursulin
720908091c8STvrtko Ursulin spin_lock_irqsave(&pmu->lock, flags);
721b46a33e2STvrtko Ursulin
722b46a33e2STvrtko Ursulin /*
723b46a33e2STvrtko Ursulin * Update the bitmask of enabled events and increment
724b46a33e2STvrtko Ursulin * the event reference counter.
725b46a33e2STvrtko Ursulin */
726908091c8STvrtko Ursulin BUILD_BUG_ON(ARRAY_SIZE(pmu->enable_count) != I915_PMU_MASK_BITS);
727908091c8STvrtko Ursulin GEM_BUG_ON(bit >= ARRAY_SIZE(pmu->enable_count));
728908091c8STvrtko Ursulin GEM_BUG_ON(pmu->enable_count[bit] == ~0);
729f4e9894bSChris Wilson
730a644fde7STvrtko Ursulin pmu->enable |= BIT(bit);
731908091c8STvrtko Ursulin pmu->enable_count[bit]++;
732b46a33e2STvrtko Ursulin
733b46a33e2STvrtko Ursulin /*
734feff0dc6STvrtko Ursulin * Start the sampling timer if needed and not already enabled.
735feff0dc6STvrtko Ursulin */
736908091c8STvrtko Ursulin __i915_pmu_maybe_start_timer(pmu);
737feff0dc6STvrtko Ursulin
738feff0dc6STvrtko Ursulin /*
739b46a33e2STvrtko Ursulin * For per-engine events the bitmask and reference counting
740b46a33e2STvrtko Ursulin * is stored per engine.
741b46a33e2STvrtko Ursulin */
742b46a33e2STvrtko Ursulin if (is_engine_event(event)) {
743b46a33e2STvrtko Ursulin u8 sample = engine_event_sample(event);
744b46a33e2STvrtko Ursulin struct intel_engine_cs *engine;
745b46a33e2STvrtko Ursulin
746b46a33e2STvrtko Ursulin engine = intel_engine_lookup_user(i915,
747b46a33e2STvrtko Ursulin engine_event_class(event),
748b46a33e2STvrtko Ursulin engine_event_instance(event));
749b46a33e2STvrtko Ursulin
75026a11deeSTvrtko Ursulin BUILD_BUG_ON(ARRAY_SIZE(engine->pmu.enable_count) !=
75126a11deeSTvrtko Ursulin I915_ENGINE_SAMPLE_COUNT);
75226a11deeSTvrtko Ursulin BUILD_BUG_ON(ARRAY_SIZE(engine->pmu.sample) !=
75326a11deeSTvrtko Ursulin I915_ENGINE_SAMPLE_COUNT);
75426a11deeSTvrtko Ursulin GEM_BUG_ON(sample >= ARRAY_SIZE(engine->pmu.enable_count));
75526a11deeSTvrtko Ursulin GEM_BUG_ON(sample >= ARRAY_SIZE(engine->pmu.sample));
756b46a33e2STvrtko Ursulin GEM_BUG_ON(engine->pmu.enable_count[sample] == ~0);
75726a11deeSTvrtko Ursulin
75826a11deeSTvrtko Ursulin engine->pmu.enable |= BIT(sample);
759b2f78cdaSTvrtko Ursulin engine->pmu.enable_count[sample]++;
760b46a33e2STvrtko Ursulin }
761b46a33e2STvrtko Ursulin
762908091c8STvrtko Ursulin spin_unlock_irqrestore(&pmu->lock, flags);
763ad055fb8STvrtko Ursulin
764348fb0cbSTvrtko Ursulin update:
765b46a33e2STvrtko Ursulin /*
766b46a33e2STvrtko Ursulin * Store the current counter value so we can report the correct delta
767b46a33e2STvrtko Ursulin * for all listeners. Even when the event was already enabled and has
768b46a33e2STvrtko Ursulin * an existing non-zero value.
769b46a33e2STvrtko Ursulin */
770ad055fb8STvrtko Ursulin local64_set(&event->hw.prev_count, __i915_pmu_event_read(event));
771b46a33e2STvrtko Ursulin }
772b46a33e2STvrtko Ursulin
i915_pmu_disable(struct perf_event * event)773b46a33e2STvrtko Ursulin static void i915_pmu_disable(struct perf_event *event)
774b46a33e2STvrtko Ursulin {
775b46a33e2STvrtko Ursulin struct drm_i915_private *i915 =
776b46a33e2STvrtko Ursulin container_of(event->pmu, typeof(*i915), pmu.base);
777a644fde7STvrtko Ursulin const unsigned int bit = event_bit(event);
778908091c8STvrtko Ursulin struct i915_pmu *pmu = &i915->pmu;
779b46a33e2STvrtko Ursulin unsigned long flags;
780b46a33e2STvrtko Ursulin
781348fb0cbSTvrtko Ursulin if (bit == -1)
782348fb0cbSTvrtko Ursulin return;
783348fb0cbSTvrtko Ursulin
784908091c8STvrtko Ursulin spin_lock_irqsave(&pmu->lock, flags);
785b46a33e2STvrtko Ursulin
786b46a33e2STvrtko Ursulin if (is_engine_event(event)) {
787b46a33e2STvrtko Ursulin u8 sample = engine_event_sample(event);
788b46a33e2STvrtko Ursulin struct intel_engine_cs *engine;
789b46a33e2STvrtko Ursulin
790b46a33e2STvrtko Ursulin engine = intel_engine_lookup_user(i915,
791b46a33e2STvrtko Ursulin engine_event_class(event),
792b46a33e2STvrtko Ursulin engine_event_instance(event));
79326a11deeSTvrtko Ursulin
79426a11deeSTvrtko Ursulin GEM_BUG_ON(sample >= ARRAY_SIZE(engine->pmu.enable_count));
79526a11deeSTvrtko Ursulin GEM_BUG_ON(sample >= ARRAY_SIZE(engine->pmu.sample));
796b46a33e2STvrtko Ursulin GEM_BUG_ON(engine->pmu.enable_count[sample] == 0);
79726a11deeSTvrtko Ursulin
798b46a33e2STvrtko Ursulin /*
799b46a33e2STvrtko Ursulin * Decrement the reference count and clear the enabled
800b46a33e2STvrtko Ursulin * bitmask when the last listener on an event goes away.
801b46a33e2STvrtko Ursulin */
802b2f78cdaSTvrtko Ursulin if (--engine->pmu.enable_count[sample] == 0)
803b46a33e2STvrtko Ursulin engine->pmu.enable &= ~BIT(sample);
804b46a33e2STvrtko Ursulin }
805b46a33e2STvrtko Ursulin
806908091c8STvrtko Ursulin GEM_BUG_ON(bit >= ARRAY_SIZE(pmu->enable_count));
807908091c8STvrtko Ursulin GEM_BUG_ON(pmu->enable_count[bit] == 0);
808b46a33e2STvrtko Ursulin /*
809b46a33e2STvrtko Ursulin * Decrement the reference count and clear the enabled
810b46a33e2STvrtko Ursulin * bitmask when the last listener on an event goes away.
811b46a33e2STvrtko Ursulin */
812908091c8STvrtko Ursulin if (--pmu->enable_count[bit] == 0) {
813a644fde7STvrtko Ursulin pmu->enable &= ~BIT(bit);
814ab129025SAshutosh Dixit pmu->timer_enabled &= pmu_needs_timer(pmu);
815feff0dc6STvrtko Ursulin }
816b46a33e2STvrtko Ursulin
817908091c8STvrtko Ursulin spin_unlock_irqrestore(&pmu->lock, flags);
818b46a33e2STvrtko Ursulin }
819b46a33e2STvrtko Ursulin
i915_pmu_event_start(struct perf_event * event,int flags)820b46a33e2STvrtko Ursulin static void i915_pmu_event_start(struct perf_event *event, int flags)
821b46a33e2STvrtko Ursulin {
822b00bccb3STvrtko Ursulin struct drm_i915_private *i915 =
823b00bccb3STvrtko Ursulin container_of(event->pmu, typeof(*i915), pmu.base);
824b00bccb3STvrtko Ursulin struct i915_pmu *pmu = &i915->pmu;
825b00bccb3STvrtko Ursulin
826b00bccb3STvrtko Ursulin if (pmu->closed)
827b00bccb3STvrtko Ursulin return;
828b00bccb3STvrtko Ursulin
829b46a33e2STvrtko Ursulin i915_pmu_enable(event);
830b46a33e2STvrtko Ursulin event->hw.state = 0;
831b46a33e2STvrtko Ursulin }
832b46a33e2STvrtko Ursulin
i915_pmu_event_stop(struct perf_event * event,int flags)833b46a33e2STvrtko Ursulin static void i915_pmu_event_stop(struct perf_event *event, int flags)
834b46a33e2STvrtko Ursulin {
835*4cbed770SUmesh Nerlige Ramappa struct drm_i915_private *i915 =
836*4cbed770SUmesh Nerlige Ramappa container_of(event->pmu, typeof(*i915), pmu.base);
837*4cbed770SUmesh Nerlige Ramappa struct i915_pmu *pmu = &i915->pmu;
838*4cbed770SUmesh Nerlige Ramappa
839*4cbed770SUmesh Nerlige Ramappa if (pmu->closed)
840*4cbed770SUmesh Nerlige Ramappa goto out;
841*4cbed770SUmesh Nerlige Ramappa
842b46a33e2STvrtko Ursulin if (flags & PERF_EF_UPDATE)
843b46a33e2STvrtko Ursulin i915_pmu_event_read(event);
844b46a33e2STvrtko Ursulin i915_pmu_disable(event);
845*4cbed770SUmesh Nerlige Ramappa
846*4cbed770SUmesh Nerlige Ramappa out:
847b46a33e2STvrtko Ursulin event->hw.state = PERF_HES_STOPPED;
848b46a33e2STvrtko Ursulin }
849b46a33e2STvrtko Ursulin
i915_pmu_event_add(struct perf_event * event,int flags)850b46a33e2STvrtko Ursulin static int i915_pmu_event_add(struct perf_event *event, int flags)
851b46a33e2STvrtko Ursulin {
852b00bccb3STvrtko Ursulin struct drm_i915_private *i915 =
853b00bccb3STvrtko Ursulin container_of(event->pmu, typeof(*i915), pmu.base);
854b00bccb3STvrtko Ursulin struct i915_pmu *pmu = &i915->pmu;
855b00bccb3STvrtko Ursulin
856b00bccb3STvrtko Ursulin if (pmu->closed)
857b00bccb3STvrtko Ursulin return -ENODEV;
858b00bccb3STvrtko Ursulin
859b46a33e2STvrtko Ursulin if (flags & PERF_EF_START)
860b46a33e2STvrtko Ursulin i915_pmu_event_start(event, flags);
861b46a33e2STvrtko Ursulin
862b46a33e2STvrtko Ursulin return 0;
863b46a33e2STvrtko Ursulin }
864b46a33e2STvrtko Ursulin
i915_pmu_event_del(struct perf_event * event,int flags)865b46a33e2STvrtko Ursulin static void i915_pmu_event_del(struct perf_event *event, int flags)
866b46a33e2STvrtko Ursulin {
867b46a33e2STvrtko Ursulin i915_pmu_event_stop(event, PERF_EF_UPDATE);
868b46a33e2STvrtko Ursulin }
869b46a33e2STvrtko Ursulin
i915_pmu_event_event_idx(struct perf_event * event)870b46a33e2STvrtko Ursulin static int i915_pmu_event_event_idx(struct perf_event *event)
871b46a33e2STvrtko Ursulin {
872b46a33e2STvrtko Ursulin return 0;
873b46a33e2STvrtko Ursulin }
874b46a33e2STvrtko Ursulin
875b7d3aabfSChris Wilson struct i915_str_attribute {
876b7d3aabfSChris Wilson struct device_attribute attr;
877b7d3aabfSChris Wilson const char *str;
878b7d3aabfSChris Wilson };
879b7d3aabfSChris Wilson
i915_pmu_format_show(struct device * dev,struct device_attribute * attr,char * buf)880b46a33e2STvrtko Ursulin static ssize_t i915_pmu_format_show(struct device *dev,
881b46a33e2STvrtko Ursulin struct device_attribute *attr, char *buf)
882b46a33e2STvrtko Ursulin {
883b7d3aabfSChris Wilson struct i915_str_attribute *eattr;
884b46a33e2STvrtko Ursulin
885b7d3aabfSChris Wilson eattr = container_of(attr, struct i915_str_attribute, attr);
886b7d3aabfSChris Wilson return sprintf(buf, "%s\n", eattr->str);
887b46a33e2STvrtko Ursulin }
888b46a33e2STvrtko Ursulin
889b46a33e2STvrtko Ursulin #define I915_PMU_FORMAT_ATTR(_name, _config) \
890b7d3aabfSChris Wilson (&((struct i915_str_attribute[]) { \
891b46a33e2STvrtko Ursulin { .attr = __ATTR(_name, 0444, i915_pmu_format_show, NULL), \
892b7d3aabfSChris Wilson .str = _config, } \
893b46a33e2STvrtko Ursulin })[0].attr.attr)
894b46a33e2STvrtko Ursulin
895b46a33e2STvrtko Ursulin static struct attribute *i915_pmu_format_attrs[] = {
896b46a33e2STvrtko Ursulin I915_PMU_FORMAT_ATTR(i915_eventid, "config:0-20"),
897b46a33e2STvrtko Ursulin NULL,
898b46a33e2STvrtko Ursulin };
899b46a33e2STvrtko Ursulin
900b46a33e2STvrtko Ursulin static const struct attribute_group i915_pmu_format_attr_group = {
901b46a33e2STvrtko Ursulin .name = "format",
902b46a33e2STvrtko Ursulin .attrs = i915_pmu_format_attrs,
903b46a33e2STvrtko Ursulin };
904b46a33e2STvrtko Ursulin
905b7d3aabfSChris Wilson struct i915_ext_attribute {
906b7d3aabfSChris Wilson struct device_attribute attr;
907b7d3aabfSChris Wilson unsigned long val;
908b7d3aabfSChris Wilson };
909b7d3aabfSChris Wilson
i915_pmu_event_show(struct device * dev,struct device_attribute * attr,char * buf)910b46a33e2STvrtko Ursulin static ssize_t i915_pmu_event_show(struct device *dev,
911b46a33e2STvrtko Ursulin struct device_attribute *attr, char *buf)
912b46a33e2STvrtko Ursulin {
913b7d3aabfSChris Wilson struct i915_ext_attribute *eattr;
914b46a33e2STvrtko Ursulin
915b7d3aabfSChris Wilson eattr = container_of(attr, struct i915_ext_attribute, attr);
916b7d3aabfSChris Wilson return sprintf(buf, "config=0x%lx\n", eattr->val);
917b46a33e2STvrtko Ursulin }
918b46a33e2STvrtko Ursulin
cpumask_show(struct device * dev,struct device_attribute * attr,char * buf)919177f30c6SYueHaibing static ssize_t cpumask_show(struct device *dev,
920177f30c6SYueHaibing struct device_attribute *attr, char *buf)
921b46a33e2STvrtko Ursulin {
922b46a33e2STvrtko Ursulin return cpumap_print_to_pagebuf(true, buf, &i915_pmu_cpumask);
923b46a33e2STvrtko Ursulin }
924b46a33e2STvrtko Ursulin
925177f30c6SYueHaibing static DEVICE_ATTR_RO(cpumask);
926b46a33e2STvrtko Ursulin
927b46a33e2STvrtko Ursulin static struct attribute *i915_cpumask_attrs[] = {
928b46a33e2STvrtko Ursulin &dev_attr_cpumask.attr,
929b46a33e2STvrtko Ursulin NULL,
930b46a33e2STvrtko Ursulin };
931b46a33e2STvrtko Ursulin
932109ec558STvrtko Ursulin static const struct attribute_group i915_pmu_cpumask_attr_group = {
933b46a33e2STvrtko Ursulin .attrs = i915_cpumask_attrs,
934b46a33e2STvrtko Ursulin };
935b46a33e2STvrtko Ursulin
936906bd0fbSTvrtko Ursulin #define __event(__counter, __name, __unit) \
937109ec558STvrtko Ursulin { \
938906bd0fbSTvrtko Ursulin .counter = (__counter), \
939109ec558STvrtko Ursulin .name = (__name), \
940109ec558STvrtko Ursulin .unit = (__unit), \
941906bd0fbSTvrtko Ursulin .global = false, \
942906bd0fbSTvrtko Ursulin }
943906bd0fbSTvrtko Ursulin
944906bd0fbSTvrtko Ursulin #define __global_event(__counter, __name, __unit) \
945906bd0fbSTvrtko Ursulin { \
946906bd0fbSTvrtko Ursulin .counter = (__counter), \
947906bd0fbSTvrtko Ursulin .name = (__name), \
948906bd0fbSTvrtko Ursulin .unit = (__unit), \
949906bd0fbSTvrtko Ursulin .global = true, \
950109ec558STvrtko Ursulin }
951109ec558STvrtko Ursulin
952109ec558STvrtko Ursulin #define __engine_event(__sample, __name) \
953109ec558STvrtko Ursulin { \
954109ec558STvrtko Ursulin .sample = (__sample), \
955109ec558STvrtko Ursulin .name = (__name), \
956109ec558STvrtko Ursulin }
957109ec558STvrtko Ursulin
958109ec558STvrtko Ursulin static struct i915_ext_attribute *
add_i915_attr(struct i915_ext_attribute * attr,const char * name,u64 config)959109ec558STvrtko Ursulin add_i915_attr(struct i915_ext_attribute *attr, const char *name, u64 config)
960109ec558STvrtko Ursulin {
9612bbba4e9SChris Wilson sysfs_attr_init(&attr->attr.attr);
962109ec558STvrtko Ursulin attr->attr.attr.name = name;
963109ec558STvrtko Ursulin attr->attr.attr.mode = 0444;
964109ec558STvrtko Ursulin attr->attr.show = i915_pmu_event_show;
965109ec558STvrtko Ursulin attr->val = config;
966109ec558STvrtko Ursulin
967109ec558STvrtko Ursulin return ++attr;
968109ec558STvrtko Ursulin }
969109ec558STvrtko Ursulin
970109ec558STvrtko Ursulin static struct perf_pmu_events_attr *
add_pmu_attr(struct perf_pmu_events_attr * attr,const char * name,const char * str)971109ec558STvrtko Ursulin add_pmu_attr(struct perf_pmu_events_attr *attr, const char *name,
972109ec558STvrtko Ursulin const char *str)
973109ec558STvrtko Ursulin {
9742bbba4e9SChris Wilson sysfs_attr_init(&attr->attr.attr);
975109ec558STvrtko Ursulin attr->attr.attr.name = name;
976109ec558STvrtko Ursulin attr->attr.attr.mode = 0444;
977109ec558STvrtko Ursulin attr->attr.show = perf_event_sysfs_show;
978109ec558STvrtko Ursulin attr->event_str = str;
979109ec558STvrtko Ursulin
980109ec558STvrtko Ursulin return ++attr;
981109ec558STvrtko Ursulin }
982109ec558STvrtko Ursulin
983109ec558STvrtko Ursulin static struct attribute **
create_event_attributes(struct i915_pmu * pmu)984908091c8STvrtko Ursulin create_event_attributes(struct i915_pmu *pmu)
985109ec558STvrtko Ursulin {
986908091c8STvrtko Ursulin struct drm_i915_private *i915 = container_of(pmu, typeof(*i915), pmu);
987109ec558STvrtko Ursulin static const struct {
988906bd0fbSTvrtko Ursulin unsigned int counter;
989109ec558STvrtko Ursulin const char *name;
990109ec558STvrtko Ursulin const char *unit;
991906bd0fbSTvrtko Ursulin bool global;
992109ec558STvrtko Ursulin } events[] = {
993906bd0fbSTvrtko Ursulin __event(0, "actual-frequency", "M"),
994906bd0fbSTvrtko Ursulin __event(1, "requested-frequency", "M"),
995906bd0fbSTvrtko Ursulin __global_event(2, "interrupts", NULL),
996906bd0fbSTvrtko Ursulin __event(3, "rc6-residency", "ns"),
997906bd0fbSTvrtko Ursulin __event(4, "software-gt-awake-time", "ns"),
998109ec558STvrtko Ursulin };
999109ec558STvrtko Ursulin static const struct {
1000109ec558STvrtko Ursulin enum drm_i915_pmu_engine_sample sample;
1001109ec558STvrtko Ursulin char *name;
1002109ec558STvrtko Ursulin } engine_events[] = {
1003109ec558STvrtko Ursulin __engine_event(I915_SAMPLE_BUSY, "busy"),
1004109ec558STvrtko Ursulin __engine_event(I915_SAMPLE_SEMA, "sema"),
1005109ec558STvrtko Ursulin __engine_event(I915_SAMPLE_WAIT, "wait"),
1006109ec558STvrtko Ursulin };
1007109ec558STvrtko Ursulin unsigned int count = 0;
1008109ec558STvrtko Ursulin struct perf_pmu_events_attr *pmu_attr = NULL, *pmu_iter;
1009109ec558STvrtko Ursulin struct i915_ext_attribute *i915_attr = NULL, *i915_iter;
1010109ec558STvrtko Ursulin struct attribute **attr = NULL, **attr_iter;
1011109ec558STvrtko Ursulin struct intel_engine_cs *engine;
1012906bd0fbSTvrtko Ursulin struct intel_gt *gt;
1013906bd0fbSTvrtko Ursulin unsigned int i, j;
1014109ec558STvrtko Ursulin
1015109ec558STvrtko Ursulin /* Count how many counters we will be exposing. */
1016906bd0fbSTvrtko Ursulin for_each_gt(gt, i915, j) {
1017109ec558STvrtko Ursulin for (i = 0; i < ARRAY_SIZE(events); i++) {
1018906bd0fbSTvrtko Ursulin u64 config = ___I915_PMU_OTHER(j, events[i].counter);
1019906bd0fbSTvrtko Ursulin
1020906bd0fbSTvrtko Ursulin if (!config_status(i915, config))
1021109ec558STvrtko Ursulin count++;
1022109ec558STvrtko Ursulin }
1023906bd0fbSTvrtko Ursulin }
1024109ec558STvrtko Ursulin
1025750e76b4SChris Wilson for_each_uabi_engine(engine, i915) {
1026109ec558STvrtko Ursulin for (i = 0; i < ARRAY_SIZE(engine_events); i++) {
1027109ec558STvrtko Ursulin if (!engine_event_status(engine,
1028109ec558STvrtko Ursulin engine_events[i].sample))
1029109ec558STvrtko Ursulin count++;
1030109ec558STvrtko Ursulin }
1031109ec558STvrtko Ursulin }
1032109ec558STvrtko Ursulin
1033109ec558STvrtko Ursulin /* Allocate attribute objects and table. */
1034dd5fec87STvrtko Ursulin i915_attr = kcalloc(count, sizeof(*i915_attr), GFP_KERNEL);
1035109ec558STvrtko Ursulin if (!i915_attr)
1036109ec558STvrtko Ursulin goto err_alloc;
1037109ec558STvrtko Ursulin
1038dd5fec87STvrtko Ursulin pmu_attr = kcalloc(count, sizeof(*pmu_attr), GFP_KERNEL);
1039109ec558STvrtko Ursulin if (!pmu_attr)
1040109ec558STvrtko Ursulin goto err_alloc;
1041109ec558STvrtko Ursulin
1042109ec558STvrtko Ursulin /* Max one pointer of each attribute type plus a termination entry. */
1043dd5fec87STvrtko Ursulin attr = kcalloc(count * 2 + 1, sizeof(*attr), GFP_KERNEL);
1044109ec558STvrtko Ursulin if (!attr)
1045109ec558STvrtko Ursulin goto err_alloc;
1046109ec558STvrtko Ursulin
1047109ec558STvrtko Ursulin i915_iter = i915_attr;
1048109ec558STvrtko Ursulin pmu_iter = pmu_attr;
1049109ec558STvrtko Ursulin attr_iter = attr;
1050109ec558STvrtko Ursulin
1051109ec558STvrtko Ursulin /* Initialize supported non-engine counters. */
1052906bd0fbSTvrtko Ursulin for_each_gt(gt, i915, j) {
1053109ec558STvrtko Ursulin for (i = 0; i < ARRAY_SIZE(events); i++) {
1054906bd0fbSTvrtko Ursulin u64 config = ___I915_PMU_OTHER(j, events[i].counter);
1055109ec558STvrtko Ursulin char *str;
1056109ec558STvrtko Ursulin
1057906bd0fbSTvrtko Ursulin if (config_status(i915, config))
1058109ec558STvrtko Ursulin continue;
1059109ec558STvrtko Ursulin
1060906bd0fbSTvrtko Ursulin if (events[i].global || !HAS_EXTRA_GT_LIST(i915))
1061109ec558STvrtko Ursulin str = kstrdup(events[i].name, GFP_KERNEL);
1062906bd0fbSTvrtko Ursulin else
1063906bd0fbSTvrtko Ursulin str = kasprintf(GFP_KERNEL, "%s-gt%u",
1064906bd0fbSTvrtko Ursulin events[i].name, j);
1065109ec558STvrtko Ursulin if (!str)
1066109ec558STvrtko Ursulin goto err;
1067109ec558STvrtko Ursulin
1068109ec558STvrtko Ursulin *attr_iter++ = &i915_iter->attr.attr;
1069906bd0fbSTvrtko Ursulin i915_iter = add_i915_attr(i915_iter, str, config);
1070109ec558STvrtko Ursulin
1071109ec558STvrtko Ursulin if (events[i].unit) {
1072906bd0fbSTvrtko Ursulin if (events[i].global || !HAS_EXTRA_GT_LIST(i915))
1073906bd0fbSTvrtko Ursulin str = kasprintf(GFP_KERNEL, "%s.unit",
1074906bd0fbSTvrtko Ursulin events[i].name);
1075906bd0fbSTvrtko Ursulin else
1076906bd0fbSTvrtko Ursulin str = kasprintf(GFP_KERNEL, "%s-gt%u.unit",
1077906bd0fbSTvrtko Ursulin events[i].name, j);
1078109ec558STvrtko Ursulin if (!str)
1079109ec558STvrtko Ursulin goto err;
1080109ec558STvrtko Ursulin
1081109ec558STvrtko Ursulin *attr_iter++ = &pmu_iter->attr.attr;
1082906bd0fbSTvrtko Ursulin pmu_iter = add_pmu_attr(pmu_iter, str,
1083906bd0fbSTvrtko Ursulin events[i].unit);
1084906bd0fbSTvrtko Ursulin }
1085109ec558STvrtko Ursulin }
1086109ec558STvrtko Ursulin }
1087109ec558STvrtko Ursulin
1088109ec558STvrtko Ursulin /* Initialize supported engine counters. */
1089750e76b4SChris Wilson for_each_uabi_engine(engine, i915) {
1090109ec558STvrtko Ursulin for (i = 0; i < ARRAY_SIZE(engine_events); i++) {
1091109ec558STvrtko Ursulin char *str;
1092109ec558STvrtko Ursulin
1093109ec558STvrtko Ursulin if (engine_event_status(engine,
1094109ec558STvrtko Ursulin engine_events[i].sample))
1095109ec558STvrtko Ursulin continue;
1096109ec558STvrtko Ursulin
1097109ec558STvrtko Ursulin str = kasprintf(GFP_KERNEL, "%s-%s",
1098109ec558STvrtko Ursulin engine->name, engine_events[i].name);
1099109ec558STvrtko Ursulin if (!str)
1100109ec558STvrtko Ursulin goto err;
1101109ec558STvrtko Ursulin
1102109ec558STvrtko Ursulin *attr_iter++ = &i915_iter->attr.attr;
1103109ec558STvrtko Ursulin i915_iter =
1104109ec558STvrtko Ursulin add_i915_attr(i915_iter, str,
11058810bc56STvrtko Ursulin __I915_PMU_ENGINE(engine->uabi_class,
1106750e76b4SChris Wilson engine->uabi_instance,
1107109ec558STvrtko Ursulin engine_events[i].sample));
1108109ec558STvrtko Ursulin
1109109ec558STvrtko Ursulin str = kasprintf(GFP_KERNEL, "%s-%s.unit",
1110109ec558STvrtko Ursulin engine->name, engine_events[i].name);
1111109ec558STvrtko Ursulin if (!str)
1112109ec558STvrtko Ursulin goto err;
1113109ec558STvrtko Ursulin
1114109ec558STvrtko Ursulin *attr_iter++ = &pmu_iter->attr.attr;
1115109ec558STvrtko Ursulin pmu_iter = add_pmu_attr(pmu_iter, str, "ns");
1116109ec558STvrtko Ursulin }
1117109ec558STvrtko Ursulin }
1118109ec558STvrtko Ursulin
1119908091c8STvrtko Ursulin pmu->i915_attr = i915_attr;
1120908091c8STvrtko Ursulin pmu->pmu_attr = pmu_attr;
1121109ec558STvrtko Ursulin
1122109ec558STvrtko Ursulin return attr;
1123109ec558STvrtko Ursulin
1124109ec558STvrtko Ursulin err:;
1125109ec558STvrtko Ursulin for (attr_iter = attr; *attr_iter; attr_iter++)
1126109ec558STvrtko Ursulin kfree((*attr_iter)->name);
1127109ec558STvrtko Ursulin
1128109ec558STvrtko Ursulin err_alloc:
1129109ec558STvrtko Ursulin kfree(attr);
1130109ec558STvrtko Ursulin kfree(i915_attr);
1131109ec558STvrtko Ursulin kfree(pmu_attr);
1132109ec558STvrtko Ursulin
1133109ec558STvrtko Ursulin return NULL;
1134109ec558STvrtko Ursulin }
1135109ec558STvrtko Ursulin
free_event_attributes(struct i915_pmu * pmu)1136908091c8STvrtko Ursulin static void free_event_attributes(struct i915_pmu *pmu)
1137109ec558STvrtko Ursulin {
113846129dc1SMichał Winiarski struct attribute **attr_iter = pmu->events_attr_group.attrs;
1139109ec558STvrtko Ursulin
1140109ec558STvrtko Ursulin for (; *attr_iter; attr_iter++)
1141109ec558STvrtko Ursulin kfree((*attr_iter)->name);
1142109ec558STvrtko Ursulin
114346129dc1SMichał Winiarski kfree(pmu->events_attr_group.attrs);
1144908091c8STvrtko Ursulin kfree(pmu->i915_attr);
1145908091c8STvrtko Ursulin kfree(pmu->pmu_attr);
1146109ec558STvrtko Ursulin
114746129dc1SMichał Winiarski pmu->events_attr_group.attrs = NULL;
1148908091c8STvrtko Ursulin pmu->i915_attr = NULL;
1149908091c8STvrtko Ursulin pmu->pmu_attr = NULL;
1150109ec558STvrtko Ursulin }
1151109ec558STvrtko Ursulin
i915_pmu_cpu_online(unsigned int cpu,struct hlist_node * node)1152b46a33e2STvrtko Ursulin static int i915_pmu_cpu_online(unsigned int cpu, struct hlist_node *node)
1153b46a33e2STvrtko Ursulin {
1154f5a179d4SMichał Winiarski struct i915_pmu *pmu = hlist_entry_safe(node, typeof(*pmu), cpuhp.node);
1155b46a33e2STvrtko Ursulin
1156b46a33e2STvrtko Ursulin GEM_BUG_ON(!pmu->base.event_init);
1157b46a33e2STvrtko Ursulin
1158b46a33e2STvrtko Ursulin /* Select the first online CPU as a designated reader. */
1159a37e94feSYury Norov if (cpumask_empty(&i915_pmu_cpumask))
1160b46a33e2STvrtko Ursulin cpumask_set_cpu(cpu, &i915_pmu_cpumask);
1161b46a33e2STvrtko Ursulin
1162b46a33e2STvrtko Ursulin return 0;
1163b46a33e2STvrtko Ursulin }
1164b46a33e2STvrtko Ursulin
i915_pmu_cpu_offline(unsigned int cpu,struct hlist_node * node)1165b46a33e2STvrtko Ursulin static int i915_pmu_cpu_offline(unsigned int cpu, struct hlist_node *node)
1166b46a33e2STvrtko Ursulin {
1167f5a179d4SMichał Winiarski struct i915_pmu *pmu = hlist_entry_safe(node, typeof(*pmu), cpuhp.node);
1168537f9c84STvrtko Ursulin unsigned int target = i915_pmu_target_cpu;
1169b46a33e2STvrtko Ursulin
1170b46a33e2STvrtko Ursulin GEM_BUG_ON(!pmu->base.event_init);
1171b46a33e2STvrtko Ursulin
1172537f9c84STvrtko Ursulin /*
1173537f9c84STvrtko Ursulin * Unregistering an instance generates a CPU offline event which we must
1174537f9c84STvrtko Ursulin * ignore to avoid incorrectly modifying the shared i915_pmu_cpumask.
1175537f9c84STvrtko Ursulin */
1176537f9c84STvrtko Ursulin if (pmu->closed)
1177537f9c84STvrtko Ursulin return 0;
1178537f9c84STvrtko Ursulin
1179b46a33e2STvrtko Ursulin if (cpumask_test_and_clear_cpu(cpu, &i915_pmu_cpumask)) {
1180b46a33e2STvrtko Ursulin target = cpumask_any_but(topology_sibling_cpumask(cpu), cpu);
1181537f9c84STvrtko Ursulin
1182b46a33e2STvrtko Ursulin /* Migrate events if there is a valid target */
1183b46a33e2STvrtko Ursulin if (target < nr_cpu_ids) {
1184b46a33e2STvrtko Ursulin cpumask_set_cpu(target, &i915_pmu_cpumask);
1185537f9c84STvrtko Ursulin i915_pmu_target_cpu = target;
1186b46a33e2STvrtko Ursulin }
1187b46a33e2STvrtko Ursulin }
1188b46a33e2STvrtko Ursulin
1189537f9c84STvrtko Ursulin if (target < nr_cpu_ids && target != pmu->cpuhp.cpu) {
1190537f9c84STvrtko Ursulin perf_pmu_migrate_context(&pmu->base, cpu, target);
1191537f9c84STvrtko Ursulin pmu->cpuhp.cpu = target;
1192537f9c84STvrtko Ursulin }
1193537f9c84STvrtko Ursulin
1194b46a33e2STvrtko Ursulin return 0;
1195b46a33e2STvrtko Ursulin }
1196b46a33e2STvrtko Ursulin
1197537f9c84STvrtko Ursulin static enum cpuhp_state cpuhp_slot = CPUHP_INVALID;
1198537f9c84STvrtko Ursulin
i915_pmu_init(void)1199a04ea6aeSJason Ekstrand int i915_pmu_init(void)
1200b46a33e2STvrtko Ursulin {
1201b46a33e2STvrtko Ursulin int ret;
1202b46a33e2STvrtko Ursulin
1203b46a33e2STvrtko Ursulin ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN,
1204b46a33e2STvrtko Ursulin "perf/x86/intel/i915:online",
1205b46a33e2STvrtko Ursulin i915_pmu_cpu_online,
1206b46a33e2STvrtko Ursulin i915_pmu_cpu_offline);
1207b46a33e2STvrtko Ursulin if (ret < 0)
1208537f9c84STvrtko Ursulin pr_notice("Failed to setup cpuhp state for i915 PMU! (%d)\n",
1209537f9c84STvrtko Ursulin ret);
1210537f9c84STvrtko Ursulin else
1211537f9c84STvrtko Ursulin cpuhp_slot = ret;
1212a04ea6aeSJason Ekstrand
1213a04ea6aeSJason Ekstrand return 0;
1214b46a33e2STvrtko Ursulin }
1215b46a33e2STvrtko Ursulin
i915_pmu_exit(void)1216537f9c84STvrtko Ursulin void i915_pmu_exit(void)
1217537f9c84STvrtko Ursulin {
1218537f9c84STvrtko Ursulin if (cpuhp_slot != CPUHP_INVALID)
1219537f9c84STvrtko Ursulin cpuhp_remove_multi_state(cpuhp_slot);
1220537f9c84STvrtko Ursulin }
1221537f9c84STvrtko Ursulin
i915_pmu_register_cpuhp_state(struct i915_pmu * pmu)1222537f9c84STvrtko Ursulin static int i915_pmu_register_cpuhp_state(struct i915_pmu *pmu)
1223537f9c84STvrtko Ursulin {
1224537f9c84STvrtko Ursulin if (cpuhp_slot == CPUHP_INVALID)
1225537f9c84STvrtko Ursulin return -EINVAL;
1226537f9c84STvrtko Ursulin
1227537f9c84STvrtko Ursulin return cpuhp_state_add_instance(cpuhp_slot, &pmu->cpuhp.node);
1228b46a33e2STvrtko Ursulin }
1229b46a33e2STvrtko Ursulin
i915_pmu_unregister_cpuhp_state(struct i915_pmu * pmu)1230908091c8STvrtko Ursulin static void i915_pmu_unregister_cpuhp_state(struct i915_pmu *pmu)
1231b46a33e2STvrtko Ursulin {
1232537f9c84STvrtko Ursulin cpuhp_state_remove_instance(cpuhp_slot, &pmu->cpuhp.node);
1233b46a33e2STvrtko Ursulin }
1234b46a33e2STvrtko Ursulin
is_igp(struct drm_i915_private * i915)123505488673STvrtko Ursulin static bool is_igp(struct drm_i915_private *i915)
123605488673STvrtko Ursulin {
12378ff5446aSThomas Zimmermann struct pci_dev *pdev = to_pci_dev(i915->drm.dev);
123805488673STvrtko Ursulin
123905488673STvrtko Ursulin /* IGP is 0000:00:02.0 */
124005488673STvrtko Ursulin return pci_domain_nr(pdev->bus) == 0 &&
124105488673STvrtko Ursulin pdev->bus->number == 0 &&
124205488673STvrtko Ursulin PCI_SLOT(pdev->devfn) == 2 &&
124305488673STvrtko Ursulin PCI_FUNC(pdev->devfn) == 0;
124405488673STvrtko Ursulin }
124505488673STvrtko Ursulin
i915_pmu_register(struct drm_i915_private * i915)1246b46a33e2STvrtko Ursulin void i915_pmu_register(struct drm_i915_private *i915)
1247b46a33e2STvrtko Ursulin {
1248908091c8STvrtko Ursulin struct i915_pmu *pmu = &i915->pmu;
124946129dc1SMichał Winiarski const struct attribute_group *attr_groups[] = {
125046129dc1SMichał Winiarski &i915_pmu_format_attr_group,
125146129dc1SMichał Winiarski &pmu->events_attr_group,
125246129dc1SMichał Winiarski &i915_pmu_cpumask_attr_group,
125346129dc1SMichał Winiarski NULL
125446129dc1SMichał Winiarski };
125546129dc1SMichał Winiarski
1256fb26eee0STvrtko Ursulin int ret = -ENOMEM;
1257b46a33e2STvrtko Ursulin
1258651e7d48SLucas De Marchi if (GRAPHICS_VER(i915) <= 2) {
12591900aba5SJani Nikula drm_info(&i915->drm, "PMU not supported for this GPU.");
1260b46a33e2STvrtko Ursulin return;
1261b46a33e2STvrtko Ursulin }
1262b46a33e2STvrtko Ursulin
1263908091c8STvrtko Ursulin spin_lock_init(&pmu->lock);
1264908091c8STvrtko Ursulin hrtimer_init(&pmu->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1265908091c8STvrtko Ursulin pmu->timer.function = i915_sample;
1266537f9c84STvrtko Ursulin pmu->cpuhp.cpu = -1;
1267dbe13ae1STvrtko Ursulin init_rc6(pmu);
1268b46a33e2STvrtko Ursulin
1269aebf3b52STvrtko Ursulin if (!is_igp(i915)) {
127005488673STvrtko Ursulin pmu->name = kasprintf(GFP_KERNEL,
1271aebf3b52STvrtko Ursulin "i915_%s",
127205488673STvrtko Ursulin dev_name(i915->drm.dev));
1273aebf3b52STvrtko Ursulin if (pmu->name) {
1274aebf3b52STvrtko Ursulin /* tools/perf reserves colons as special. */
1275aebf3b52STvrtko Ursulin strreplace((char *)pmu->name, ':', '_');
1276aebf3b52STvrtko Ursulin }
1277aebf3b52STvrtko Ursulin } else {
127805488673STvrtko Ursulin pmu->name = "i915";
1279aebf3b52STvrtko Ursulin }
128005488673STvrtko Ursulin if (!pmu->name)
1281b46a33e2STvrtko Ursulin goto err;
1282b46a33e2STvrtko Ursulin
128346129dc1SMichał Winiarski pmu->events_attr_group.name = "events";
128446129dc1SMichał Winiarski pmu->events_attr_group.attrs = create_event_attributes(pmu);
128546129dc1SMichał Winiarski if (!pmu->events_attr_group.attrs)
1286c442292aSChris Wilson goto err_name;
1287c442292aSChris Wilson
128846129dc1SMichał Winiarski pmu->base.attr_groups = kmemdup(attr_groups, sizeof(attr_groups),
128946129dc1SMichał Winiarski GFP_KERNEL);
129046129dc1SMichał Winiarski if (!pmu->base.attr_groups)
129146129dc1SMichał Winiarski goto err_attr;
129246129dc1SMichał Winiarski
1293df3ab3cbSChris Wilson pmu->base.module = THIS_MODULE;
1294c442292aSChris Wilson pmu->base.task_ctx_nr = perf_invalid_context;
1295c442292aSChris Wilson pmu->base.event_init = i915_pmu_event_init;
1296c442292aSChris Wilson pmu->base.add = i915_pmu_event_add;
1297c442292aSChris Wilson pmu->base.del = i915_pmu_event_del;
1298c442292aSChris Wilson pmu->base.start = i915_pmu_event_start;
1299c442292aSChris Wilson pmu->base.stop = i915_pmu_event_stop;
1300c442292aSChris Wilson pmu->base.read = i915_pmu_event_read;
1301c442292aSChris Wilson pmu->base.event_idx = i915_pmu_event_event_idx;
1302c442292aSChris Wilson
130305488673STvrtko Ursulin ret = perf_pmu_register(&pmu->base, pmu->name, -1);
130405488673STvrtko Ursulin if (ret)
130546129dc1SMichał Winiarski goto err_groups;
130605488673STvrtko Ursulin
1307908091c8STvrtko Ursulin ret = i915_pmu_register_cpuhp_state(pmu);
1308b46a33e2STvrtko Ursulin if (ret)
1309b46a33e2STvrtko Ursulin goto err_unreg;
1310b46a33e2STvrtko Ursulin
1311b46a33e2STvrtko Ursulin return;
1312b46a33e2STvrtko Ursulin
1313b46a33e2STvrtko Ursulin err_unreg:
1314908091c8STvrtko Ursulin perf_pmu_unregister(&pmu->base);
131546129dc1SMichał Winiarski err_groups:
131646129dc1SMichał Winiarski kfree(pmu->base.attr_groups);
1317c442292aSChris Wilson err_attr:
1318c442292aSChris Wilson pmu->base.event_init = NULL;
1319c442292aSChris Wilson free_event_attributes(pmu);
132005488673STvrtko Ursulin err_name:
132105488673STvrtko Ursulin if (!is_igp(i915))
132205488673STvrtko Ursulin kfree(pmu->name);
1323b46a33e2STvrtko Ursulin err:
13241900aba5SJani Nikula drm_notice(&i915->drm, "Failed to register PMU!\n");
1325b46a33e2STvrtko Ursulin }
1326b46a33e2STvrtko Ursulin
i915_pmu_unregister(struct drm_i915_private * i915)1327b46a33e2STvrtko Ursulin void i915_pmu_unregister(struct drm_i915_private *i915)
1328b46a33e2STvrtko Ursulin {
1329908091c8STvrtko Ursulin struct i915_pmu *pmu = &i915->pmu;
1330908091c8STvrtko Ursulin
1331908091c8STvrtko Ursulin if (!pmu->base.event_init)
1332b46a33e2STvrtko Ursulin return;
1333b46a33e2STvrtko Ursulin
1334b00bccb3STvrtko Ursulin /*
1335b00bccb3STvrtko Ursulin * "Disconnect" the PMU callbacks - since all are atomic synchronize_rcu
1336b00bccb3STvrtko Ursulin * ensures all currently executing ones will have exited before we
1337b00bccb3STvrtko Ursulin * proceed with unregistration.
1338b00bccb3STvrtko Ursulin */
1339b00bccb3STvrtko Ursulin pmu->closed = true;
1340b00bccb3STvrtko Ursulin synchronize_rcu();
1341b46a33e2STvrtko Ursulin
1342908091c8STvrtko Ursulin hrtimer_cancel(&pmu->timer);
1343b46a33e2STvrtko Ursulin
1344908091c8STvrtko Ursulin i915_pmu_unregister_cpuhp_state(pmu);
1345b46a33e2STvrtko Ursulin
1346908091c8STvrtko Ursulin perf_pmu_unregister(&pmu->base);
1347908091c8STvrtko Ursulin pmu->base.event_init = NULL;
134846129dc1SMichał Winiarski kfree(pmu->base.attr_groups);
134905488673STvrtko Ursulin if (!is_igp(i915))
135005488673STvrtko Ursulin kfree(pmu->name);
1351908091c8STvrtko Ursulin free_event_attributes(pmu);
1352b46a33e2STvrtko Ursulin }
1353