blob: 21006e8065a030c085e676dd1d5ce98182d5b8d2 [file] [log] [blame]
Tvrtko Ursulinb46a33e2017-11-21 18:18:45 +00001/*
2 * Copyright © 2017 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 *
23 */
24
25#include <linux/perf_event.h>
26#include <linux/pm_runtime.h>
27
28#include "i915_drv.h"
29#include "i915_pmu.h"
30#include "intel_ringbuffer.h"
31
32/* Frequency for the sampling timer for events which need it. */
33#define FREQUENCY 200
34#define PERIOD max_t(u64, 10000, NSEC_PER_SEC / FREQUENCY)
35
36#define ENGINE_SAMPLE_MASK \
37 (BIT(I915_SAMPLE_BUSY) | \
38 BIT(I915_SAMPLE_WAIT) | \
39 BIT(I915_SAMPLE_SEMA))
40
41#define ENGINE_SAMPLE_BITS (1 << I915_PMU_SAMPLE_BITS)
42
Chris Wilson141a0892017-11-23 12:34:31 +000043static cpumask_t i915_pmu_cpumask;
Tvrtko Ursulinb46a33e2017-11-21 18:18:45 +000044
45static u8 engine_config_sample(u64 config)
46{
47 return config & I915_PMU_SAMPLE_MASK;
48}
49
50static u8 engine_event_sample(struct perf_event *event)
51{
52 return engine_config_sample(event->attr.config);
53}
54
55static u8 engine_event_class(struct perf_event *event)
56{
57 return (event->attr.config >> I915_PMU_CLASS_SHIFT) & 0xff;
58}
59
60static u8 engine_event_instance(struct perf_event *event)
61{
62 return (event->attr.config >> I915_PMU_SAMPLE_BITS) & 0xff;
63}
64
65static bool is_engine_config(u64 config)
66{
67 return config < __I915_PMU_OTHER(0);
68}
69
70static unsigned int config_enabled_bit(u64 config)
71{
72 if (is_engine_config(config))
73 return engine_config_sample(config);
74 else
75 return ENGINE_SAMPLE_BITS + (config - __I915_PMU_OTHER(0));
76}
77
78static u64 config_enabled_mask(u64 config)
79{
80 return BIT_ULL(config_enabled_bit(config));
81}
82
83static bool is_engine_event(struct perf_event *event)
84{
85 return is_engine_config(event->attr.config);
86}
87
88static unsigned int event_enabled_bit(struct perf_event *event)
89{
90 return config_enabled_bit(event->attr.config);
91}
92
Tvrtko Ursulinb3add012017-11-21 18:18:49 +000093static bool supports_busy_stats(struct drm_i915_private *i915)
94{
95 return INTEL_GEN(i915) >= 8;
96}
97
Tvrtko Ursulinfeff0dc2017-11-21 18:18:46 +000098static bool pmu_needs_timer(struct drm_i915_private *i915, bool gpu_active)
99{
100 u64 enable;
101
102 /*
103 * Only some counters need the sampling timer.
104 *
105 * We start with a bitmask of all currently enabled events.
106 */
107 enable = i915->pmu.enable;
108
109 /*
110 * Mask out all the ones which do not need the timer, or in
111 * other words keep all the ones that could need the timer.
112 */
113 enable &= config_enabled_mask(I915_PMU_ACTUAL_FREQUENCY) |
114 config_enabled_mask(I915_PMU_REQUESTED_FREQUENCY) |
115 ENGINE_SAMPLE_MASK;
116
117 /*
118 * When the GPU is idle per-engine counters do not need to be
119 * running so clear those bits out.
120 */
121 if (!gpu_active)
122 enable &= ~ENGINE_SAMPLE_MASK;
Tvrtko Ursulinb3add012017-11-21 18:18:49 +0000123 /*
124 * Also there is software busyness tracking available we do not
125 * need the timer for I915_SAMPLE_BUSY counter.
126 */
127 else if (supports_busy_stats(i915))
128 enable &= ~BIT(I915_SAMPLE_BUSY);
Tvrtko Ursulinfeff0dc2017-11-21 18:18:46 +0000129
130 /*
131 * If some bits remain it means we need the sampling timer running.
132 */
133 return enable;
134}
135
136void i915_pmu_gt_parked(struct drm_i915_private *i915)
137{
138 if (!i915->pmu.base.event_init)
139 return;
140
141 spin_lock_irq(&i915->pmu.lock);
142 /*
143 * Signal sampling timer to stop if only engine events are enabled and
144 * GPU went idle.
145 */
146 i915->pmu.timer_enabled = pmu_needs_timer(i915, false);
147 spin_unlock_irq(&i915->pmu.lock);
148}
149
150static void __i915_pmu_maybe_start_timer(struct drm_i915_private *i915)
151{
152 if (!i915->pmu.timer_enabled && pmu_needs_timer(i915, true)) {
153 i915->pmu.timer_enabled = true;
154 hrtimer_start_range_ns(&i915->pmu.timer,
155 ns_to_ktime(PERIOD), 0,
156 HRTIMER_MODE_REL_PINNED);
157 }
158}
159
160void i915_pmu_gt_unparked(struct drm_i915_private *i915)
161{
162 if (!i915->pmu.base.event_init)
163 return;
164
165 spin_lock_irq(&i915->pmu.lock);
166 /*
167 * Re-enable sampling timer when GPU goes active.
168 */
169 __i915_pmu_maybe_start_timer(i915);
170 spin_unlock_irq(&i915->pmu.lock);
171}
172
Tvrtko Ursulinb46a33e2017-11-21 18:18:45 +0000173static bool grab_forcewake(struct drm_i915_private *i915, bool fw)
174{
175 if (!fw)
176 intel_uncore_forcewake_get(i915, FORCEWAKE_ALL);
177
178 return true;
179}
180
181static void
182update_sample(struct i915_pmu_sample *sample, u32 unit, u32 val)
183{
184 /*
185 * Since we are doing stochastic sampling for these counters,
186 * average the delta with the previous value for better accuracy.
187 */
188 sample->cur += div_u64(mul_u32_u32(sample->prev + val, unit), 2);
189 sample->prev = val;
190}
191
192static void engines_sample(struct drm_i915_private *dev_priv)
193{
194 struct intel_engine_cs *engine;
195 enum intel_engine_id id;
196 bool fw = false;
197
198 if ((dev_priv->pmu.enable & ENGINE_SAMPLE_MASK) == 0)
199 return;
200
201 if (!dev_priv->gt.awake)
202 return;
203
204 if (!intel_runtime_pm_get_if_in_use(dev_priv))
205 return;
206
207 for_each_engine(engine, dev_priv, id) {
208 u32 current_seqno = intel_engine_get_seqno(engine);
209 u32 last_seqno = intel_engine_last_submit(engine);
210 u32 val;
211
212 val = !i915_seqno_passed(current_seqno, last_seqno);
213
214 update_sample(&engine->pmu.sample[I915_SAMPLE_BUSY],
215 PERIOD, val);
216
217 if (val && (engine->pmu.enable &
218 (BIT(I915_SAMPLE_WAIT) | BIT(I915_SAMPLE_SEMA)))) {
219 fw = grab_forcewake(dev_priv, fw);
220
221 val = I915_READ_FW(RING_CTL(engine->mmio_base));
222 } else {
223 val = 0;
224 }
225
226 update_sample(&engine->pmu.sample[I915_SAMPLE_WAIT],
227 PERIOD, !!(val & RING_WAIT));
228
229 update_sample(&engine->pmu.sample[I915_SAMPLE_SEMA],
230 PERIOD, !!(val & RING_WAIT_SEMAPHORE));
231 }
232
233 if (fw)
234 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
235
236 intel_runtime_pm_put(dev_priv);
237}
238
239static void frequency_sample(struct drm_i915_private *dev_priv)
240{
241 if (dev_priv->pmu.enable &
242 config_enabled_mask(I915_PMU_ACTUAL_FREQUENCY)) {
243 u32 val;
244
245 val = dev_priv->gt_pm.rps.cur_freq;
246 if (dev_priv->gt.awake &&
247 intel_runtime_pm_get_if_in_use(dev_priv)) {
248 val = intel_get_cagf(dev_priv,
249 I915_READ_NOTRACE(GEN6_RPSTAT1));
250 intel_runtime_pm_put(dev_priv);
251 }
252
253 update_sample(&dev_priv->pmu.sample[__I915_SAMPLE_FREQ_ACT],
254 1, intel_gpu_freq(dev_priv, val));
255 }
256
257 if (dev_priv->pmu.enable &
258 config_enabled_mask(I915_PMU_REQUESTED_FREQUENCY)) {
259 update_sample(&dev_priv->pmu.sample[__I915_SAMPLE_FREQ_REQ], 1,
260 intel_gpu_freq(dev_priv,
261 dev_priv->gt_pm.rps.cur_freq));
262 }
263}
264
Tvrtko Ursulinfbba5552017-11-23 10:26:54 +0000265static void pmu_init_previous_samples(struct drm_i915_private *i915)
266{
267 struct intel_engine_cs *engine;
268 enum intel_engine_id id;
269 unsigned int i;
270
271 for_each_engine(engine, i915, id) {
272 for (i = 0; i < ARRAY_SIZE(engine->pmu.sample); i++)
273 engine->pmu.sample[i].prev = 0;
274 }
275
276 for (i = 0; i < ARRAY_SIZE(i915->pmu.sample); i++)
277 i915->pmu.sample[i].prev = i915->gt_pm.rps.idle_freq;
278}
279
Tvrtko Ursulinb46a33e2017-11-21 18:18:45 +0000280static enum hrtimer_restart i915_sample(struct hrtimer *hrtimer)
281{
282 struct drm_i915_private *i915 =
283 container_of(hrtimer, struct drm_i915_private, pmu.timer);
284
Tvrtko Ursulinfbba5552017-11-23 10:26:54 +0000285 if (!READ_ONCE(i915->pmu.timer_enabled)) {
286 pmu_init_previous_samples(i915);
287
Tvrtko Ursulinb46a33e2017-11-21 18:18:45 +0000288 return HRTIMER_NORESTART;
Tvrtko Ursulinfbba5552017-11-23 10:26:54 +0000289 }
Tvrtko Ursulinb46a33e2017-11-21 18:18:45 +0000290
291 engines_sample(i915);
292 frequency_sample(i915);
293
294 hrtimer_forward_now(hrtimer, ns_to_ktime(PERIOD));
295 return HRTIMER_RESTART;
296}
297
Tvrtko Ursulin0cd46842017-11-21 18:18:50 +0000298static u64 count_interrupts(struct drm_i915_private *i915)
299{
300 /* open-coded kstat_irqs() */
301 struct irq_desc *desc = irq_to_desc(i915->drm.pdev->irq);
302 u64 sum = 0;
303 int cpu;
304
305 if (!desc || !desc->kstat_irqs)
306 return 0;
307
308 for_each_possible_cpu(cpu)
309 sum += *per_cpu_ptr(desc->kstat_irqs, cpu);
310
311 return sum;
312}
313
Tvrtko Ursulinb46a33e2017-11-21 18:18:45 +0000314static void i915_pmu_event_destroy(struct perf_event *event)
315{
316 WARN_ON(event->parent);
317}
318
319static int engine_event_init(struct perf_event *event)
320{
321 struct drm_i915_private *i915 =
322 container_of(event->pmu, typeof(*i915), pmu.base);
323
324 if (!intel_engine_lookup_user(i915, engine_event_class(event),
325 engine_event_instance(event)))
326 return -ENODEV;
327
328 switch (engine_event_sample(event)) {
329 case I915_SAMPLE_BUSY:
330 case I915_SAMPLE_WAIT:
331 break;
332 case I915_SAMPLE_SEMA:
333 if (INTEL_GEN(i915) < 6)
334 return -ENODEV;
335 break;
336 default:
337 return -ENOENT;
338 }
339
340 return 0;
341}
342
343static int i915_pmu_event_init(struct perf_event *event)
344{
345 struct drm_i915_private *i915 =
346 container_of(event->pmu, typeof(*i915), pmu.base);
347 int cpu, ret;
348
349 if (event->attr.type != event->pmu->type)
350 return -ENOENT;
351
352 /* unsupported modes and filters */
353 if (event->attr.sample_period) /* no sampling */
354 return -EINVAL;
355
356 if (has_branch_stack(event))
357 return -EOPNOTSUPP;
358
359 if (event->cpu < 0)
360 return -EINVAL;
361
362 cpu = cpumask_any_and(&i915_pmu_cpumask,
363 topology_sibling_cpumask(event->cpu));
364 if (cpu >= nr_cpu_ids)
365 return -ENODEV;
366
367 if (is_engine_event(event)) {
368 ret = engine_event_init(event);
369 } else {
370 ret = 0;
371 switch (event->attr.config) {
372 case I915_PMU_ACTUAL_FREQUENCY:
373 if (IS_VALLEYVIEW(i915) || IS_CHERRYVIEW(i915))
374 /* Requires a mutex for sampling! */
375 ret = -ENODEV;
376 case I915_PMU_REQUESTED_FREQUENCY:
377 if (INTEL_GEN(i915) < 6)
378 ret = -ENODEV;
379 break;
Tvrtko Ursulin0cd46842017-11-21 18:18:50 +0000380 case I915_PMU_INTERRUPTS:
381 break;
Tvrtko Ursulin6060b6a2017-11-21 18:18:52 +0000382 case I915_PMU_RC6_RESIDENCY:
383 if (!HAS_RC6(i915))
384 ret = -ENODEV;
385 break;
386 case I915_PMU_RC6p_RESIDENCY:
387 case I915_PMU_RC6pp_RESIDENCY:
388 if (!HAS_RC6p(i915))
389 ret = -ENODEV;
390 break;
Tvrtko Ursulinb46a33e2017-11-21 18:18:45 +0000391 default:
392 ret = -ENOENT;
393 break;
394 }
395 }
396 if (ret)
397 return ret;
398
399 event->cpu = cpu;
400 if (!event->parent)
401 event->destroy = i915_pmu_event_destroy;
402
403 return 0;
404}
405
406static u64 __i915_pmu_event_read(struct perf_event *event)
407{
408 struct drm_i915_private *i915 =
409 container_of(event->pmu, typeof(*i915), pmu.base);
410 u64 val = 0;
411
412 if (is_engine_event(event)) {
413 u8 sample = engine_event_sample(event);
414 struct intel_engine_cs *engine;
415
416 engine = intel_engine_lookup_user(i915,
417 engine_event_class(event),
418 engine_event_instance(event));
419
420 if (WARN_ON_ONCE(!engine)) {
421 /* Do nothing */
Tvrtko Ursulinb3add012017-11-21 18:18:49 +0000422 } else if (sample == I915_SAMPLE_BUSY &&
423 engine->pmu.busy_stats) {
424 val = ktime_to_ns(intel_engine_get_busy_time(engine));
Tvrtko Ursulinb46a33e2017-11-21 18:18:45 +0000425 } else {
426 val = engine->pmu.sample[sample].cur;
427 }
428 } else {
429 switch (event->attr.config) {
430 case I915_PMU_ACTUAL_FREQUENCY:
431 val =
432 div_u64(i915->pmu.sample[__I915_SAMPLE_FREQ_ACT].cur,
433 FREQUENCY);
434 break;
435 case I915_PMU_REQUESTED_FREQUENCY:
436 val =
437 div_u64(i915->pmu.sample[__I915_SAMPLE_FREQ_REQ].cur,
438 FREQUENCY);
439 break;
Tvrtko Ursulin0cd46842017-11-21 18:18:50 +0000440 case I915_PMU_INTERRUPTS:
441 val = count_interrupts(i915);
442 break;
Tvrtko Ursulin6060b6a2017-11-21 18:18:52 +0000443 case I915_PMU_RC6_RESIDENCY:
444 intel_runtime_pm_get(i915);
445 val = intel_rc6_residency_ns(i915,
446 IS_VALLEYVIEW(i915) ?
447 VLV_GT_RENDER_RC6 :
448 GEN6_GT_GFX_RC6);
449 intel_runtime_pm_put(i915);
450 break;
451 case I915_PMU_RC6p_RESIDENCY:
452 intel_runtime_pm_get(i915);
453 val = intel_rc6_residency_ns(i915, GEN6_GT_GFX_RC6p);
454 intel_runtime_pm_put(i915);
455 break;
456 case I915_PMU_RC6pp_RESIDENCY:
457 intel_runtime_pm_get(i915);
458 val = intel_rc6_residency_ns(i915, GEN6_GT_GFX_RC6pp);
459 intel_runtime_pm_put(i915);
460 break;
Tvrtko Ursulinb46a33e2017-11-21 18:18:45 +0000461 }
462 }
463
464 return val;
465}
466
467static void i915_pmu_event_read(struct perf_event *event)
468{
469 struct hw_perf_event *hwc = &event->hw;
470 u64 prev, new;
471
472again:
473 prev = local64_read(&hwc->prev_count);
474 new = __i915_pmu_event_read(event);
475
476 if (local64_cmpxchg(&hwc->prev_count, prev, new) != prev)
477 goto again;
478
479 local64_add(new - prev, &event->count);
480}
481
Tvrtko Ursulinb3add012017-11-21 18:18:49 +0000482static bool engine_needs_busy_stats(struct intel_engine_cs *engine)
483{
484 return supports_busy_stats(engine->i915) &&
485 (engine->pmu.enable & BIT(I915_SAMPLE_BUSY));
486}
487
Tvrtko Ursulinb46a33e2017-11-21 18:18:45 +0000488static void i915_pmu_enable(struct perf_event *event)
489{
490 struct drm_i915_private *i915 =
491 container_of(event->pmu, typeof(*i915), pmu.base);
492 unsigned int bit = event_enabled_bit(event);
493 unsigned long flags;
494
495 spin_lock_irqsave(&i915->pmu.lock, flags);
496
497 /*
Tvrtko Ursulinb46a33e2017-11-21 18:18:45 +0000498 * Update the bitmask of enabled events and increment
499 * the event reference counter.
500 */
501 GEM_BUG_ON(bit >= I915_PMU_MASK_BITS);
502 GEM_BUG_ON(i915->pmu.enable_count[bit] == ~0);
503 i915->pmu.enable |= BIT_ULL(bit);
504 i915->pmu.enable_count[bit]++;
505
506 /*
Tvrtko Ursulinfeff0dc2017-11-21 18:18:46 +0000507 * Start the sampling timer if needed and not already enabled.
508 */
509 __i915_pmu_maybe_start_timer(i915);
510
511 /*
Tvrtko Ursulinb46a33e2017-11-21 18:18:45 +0000512 * For per-engine events the bitmask and reference counting
513 * is stored per engine.
514 */
515 if (is_engine_event(event)) {
516 u8 sample = engine_event_sample(event);
517 struct intel_engine_cs *engine;
518
519 engine = intel_engine_lookup_user(i915,
520 engine_event_class(event),
521 engine_event_instance(event));
522 GEM_BUG_ON(!engine);
523 engine->pmu.enable |= BIT(sample);
524
525 GEM_BUG_ON(sample >= I915_PMU_SAMPLE_BITS);
526 GEM_BUG_ON(engine->pmu.enable_count[sample] == ~0);
Tvrtko Ursulinb3add012017-11-21 18:18:49 +0000527 if (engine->pmu.enable_count[sample]++ == 0) {
528 /*
529 * Enable engine busy stats tracking if needed or
530 * alternatively cancel the scheduled disable.
531 *
532 * If the delayed disable was pending, cancel it and
533 * in this case do not enable since it already is.
534 */
535 if (engine_needs_busy_stats(engine) &&
536 !engine->pmu.busy_stats) {
537 engine->pmu.busy_stats = true;
538 if (!cancel_delayed_work(&engine->pmu.disable_busy_stats))
539 intel_enable_engine_stats(engine);
540 }
541 }
Tvrtko Ursulinb46a33e2017-11-21 18:18:45 +0000542 }
543
544 /*
545 * Store the current counter value so we can report the correct delta
546 * for all listeners. Even when the event was already enabled and has
547 * an existing non-zero value.
548 */
549 local64_set(&event->hw.prev_count, __i915_pmu_event_read(event));
550
551 spin_unlock_irqrestore(&i915->pmu.lock, flags);
552}
553
Tvrtko Ursulinb3add012017-11-21 18:18:49 +0000554static void __disable_busy_stats(struct work_struct *work)
555{
556 struct intel_engine_cs *engine =
557 container_of(work, typeof(*engine), pmu.disable_busy_stats.work);
558
559 intel_disable_engine_stats(engine);
560}
561
Tvrtko Ursulinb46a33e2017-11-21 18:18:45 +0000562static void i915_pmu_disable(struct perf_event *event)
563{
564 struct drm_i915_private *i915 =
565 container_of(event->pmu, typeof(*i915), pmu.base);
566 unsigned int bit = event_enabled_bit(event);
567 unsigned long flags;
568
569 spin_lock_irqsave(&i915->pmu.lock, flags);
570
571 if (is_engine_event(event)) {
572 u8 sample = engine_event_sample(event);
573 struct intel_engine_cs *engine;
574
575 engine = intel_engine_lookup_user(i915,
576 engine_event_class(event),
577 engine_event_instance(event));
578 GEM_BUG_ON(!engine);
579 GEM_BUG_ON(sample >= I915_PMU_SAMPLE_BITS);
580 GEM_BUG_ON(engine->pmu.enable_count[sample] == 0);
581 /*
582 * Decrement the reference count and clear the enabled
583 * bitmask when the last listener on an event goes away.
584 */
Tvrtko Ursulinb3add012017-11-21 18:18:49 +0000585 if (--engine->pmu.enable_count[sample] == 0) {
Tvrtko Ursulinb46a33e2017-11-21 18:18:45 +0000586 engine->pmu.enable &= ~BIT(sample);
Tvrtko Ursulinb3add012017-11-21 18:18:49 +0000587 if (!engine_needs_busy_stats(engine) &&
588 engine->pmu.busy_stats) {
589 engine->pmu.busy_stats = false;
590 /*
591 * We request a delayed disable to handle the
592 * rapid on/off cycles on events, which can
593 * happen when tools like perf stat start, in a
594 * nicer way.
595 *
596 * In addition, this also helps with busy stats
597 * accuracy with background CPU offline/online
598 * migration events.
599 */
600 queue_delayed_work(system_wq,
601 &engine->pmu.disable_busy_stats,
602 round_jiffies_up_relative(HZ));
603 }
604 }
Tvrtko Ursulinb46a33e2017-11-21 18:18:45 +0000605 }
606
607 GEM_BUG_ON(bit >= I915_PMU_MASK_BITS);
608 GEM_BUG_ON(i915->pmu.enable_count[bit] == 0);
609 /*
610 * Decrement the reference count and clear the enabled
611 * bitmask when the last listener on an event goes away.
612 */
Tvrtko Ursulinfeff0dc2017-11-21 18:18:46 +0000613 if (--i915->pmu.enable_count[bit] == 0) {
Tvrtko Ursulinb46a33e2017-11-21 18:18:45 +0000614 i915->pmu.enable &= ~BIT_ULL(bit);
Tvrtko Ursulinfeff0dc2017-11-21 18:18:46 +0000615 i915->pmu.timer_enabled &= pmu_needs_timer(i915, true);
616 }
Tvrtko Ursulinb46a33e2017-11-21 18:18:45 +0000617
618 spin_unlock_irqrestore(&i915->pmu.lock, flags);
619}
620
621static void i915_pmu_event_start(struct perf_event *event, int flags)
622{
623 i915_pmu_enable(event);
624 event->hw.state = 0;
625}
626
627static void i915_pmu_event_stop(struct perf_event *event, int flags)
628{
629 if (flags & PERF_EF_UPDATE)
630 i915_pmu_event_read(event);
631 i915_pmu_disable(event);
632 event->hw.state = PERF_HES_STOPPED;
633}
634
635static int i915_pmu_event_add(struct perf_event *event, int flags)
636{
637 if (flags & PERF_EF_START)
638 i915_pmu_event_start(event, flags);
639
640 return 0;
641}
642
643static void i915_pmu_event_del(struct perf_event *event, int flags)
644{
645 i915_pmu_event_stop(event, PERF_EF_UPDATE);
646}
647
648static int i915_pmu_event_event_idx(struct perf_event *event)
649{
650 return 0;
651}
652
Chris Wilsonb7d3aab2017-11-23 21:17:51 +0000653struct i915_str_attribute {
654 struct device_attribute attr;
655 const char *str;
656};
657
Tvrtko Ursulinb46a33e2017-11-21 18:18:45 +0000658static ssize_t i915_pmu_format_show(struct device *dev,
659 struct device_attribute *attr, char *buf)
660{
Chris Wilsonb7d3aab2017-11-23 21:17:51 +0000661 struct i915_str_attribute *eattr;
Tvrtko Ursulinb46a33e2017-11-21 18:18:45 +0000662
Chris Wilsonb7d3aab2017-11-23 21:17:51 +0000663 eattr = container_of(attr, struct i915_str_attribute, attr);
664 return sprintf(buf, "%s\n", eattr->str);
Tvrtko Ursulinb46a33e2017-11-21 18:18:45 +0000665}
666
667#define I915_PMU_FORMAT_ATTR(_name, _config) \
Chris Wilsonb7d3aab2017-11-23 21:17:51 +0000668 (&((struct i915_str_attribute[]) { \
Tvrtko Ursulinb46a33e2017-11-21 18:18:45 +0000669 { .attr = __ATTR(_name, 0444, i915_pmu_format_show, NULL), \
Chris Wilsonb7d3aab2017-11-23 21:17:51 +0000670 .str = _config, } \
Tvrtko Ursulinb46a33e2017-11-21 18:18:45 +0000671 })[0].attr.attr)
672
673static struct attribute *i915_pmu_format_attrs[] = {
674 I915_PMU_FORMAT_ATTR(i915_eventid, "config:0-20"),
675 NULL,
676};
677
678static const struct attribute_group i915_pmu_format_attr_group = {
679 .name = "format",
680 .attrs = i915_pmu_format_attrs,
681};
682
Chris Wilsonb7d3aab2017-11-23 21:17:51 +0000683struct i915_ext_attribute {
684 struct device_attribute attr;
685 unsigned long val;
686};
687
Tvrtko Ursulinb46a33e2017-11-21 18:18:45 +0000688static ssize_t i915_pmu_event_show(struct device *dev,
689 struct device_attribute *attr, char *buf)
690{
Chris Wilsonb7d3aab2017-11-23 21:17:51 +0000691 struct i915_ext_attribute *eattr;
Tvrtko Ursulinb46a33e2017-11-21 18:18:45 +0000692
Chris Wilsonb7d3aab2017-11-23 21:17:51 +0000693 eattr = container_of(attr, struct i915_ext_attribute, attr);
694 return sprintf(buf, "config=0x%lx\n", eattr->val);
Tvrtko Ursulinb46a33e2017-11-21 18:18:45 +0000695}
696
697#define I915_EVENT_ATTR(_name, _config) \
Chris Wilsonb7d3aab2017-11-23 21:17:51 +0000698 (&((struct i915_ext_attribute[]) { \
Tvrtko Ursulinb46a33e2017-11-21 18:18:45 +0000699 { .attr = __ATTR(_name, 0444, i915_pmu_event_show, NULL), \
Chris Wilsonb7d3aab2017-11-23 21:17:51 +0000700 .val = _config, } \
Tvrtko Ursulinb46a33e2017-11-21 18:18:45 +0000701 })[0].attr.attr)
702
703#define I915_EVENT_STR(_name, _str) \
704 (&((struct perf_pmu_events_attr[]) { \
705 { .attr = __ATTR(_name, 0444, perf_event_sysfs_show, NULL), \
706 .id = 0, \
707 .event_str = _str, } \
708 })[0].attr.attr)
709
710#define I915_EVENT(_name, _config, _unit) \
711 I915_EVENT_ATTR(_name, _config), \
712 I915_EVENT_STR(_name.unit, _unit)
713
714#define I915_ENGINE_EVENT(_name, _class, _instance, _sample) \
715 I915_EVENT_ATTR(_name, __I915_PMU_ENGINE(_class, _instance, _sample)), \
716 I915_EVENT_STR(_name.unit, "ns")
717
718#define I915_ENGINE_EVENTS(_name, _class, _instance) \
719 I915_ENGINE_EVENT(_name##_instance-busy, _class, _instance, I915_SAMPLE_BUSY), \
720 I915_ENGINE_EVENT(_name##_instance-sema, _class, _instance, I915_SAMPLE_SEMA), \
721 I915_ENGINE_EVENT(_name##_instance-wait, _class, _instance, I915_SAMPLE_WAIT)
722
723static struct attribute *i915_pmu_events_attrs[] = {
724 I915_ENGINE_EVENTS(rcs, I915_ENGINE_CLASS_RENDER, 0),
725 I915_ENGINE_EVENTS(bcs, I915_ENGINE_CLASS_COPY, 0),
726 I915_ENGINE_EVENTS(vcs, I915_ENGINE_CLASS_VIDEO, 0),
727 I915_ENGINE_EVENTS(vcs, I915_ENGINE_CLASS_VIDEO, 1),
728 I915_ENGINE_EVENTS(vecs, I915_ENGINE_CLASS_VIDEO_ENHANCE, 0),
729
730 I915_EVENT(actual-frequency, I915_PMU_ACTUAL_FREQUENCY, "MHz"),
731 I915_EVENT(requested-frequency, I915_PMU_REQUESTED_FREQUENCY, "MHz"),
732
Tvrtko Ursulin0cd46842017-11-21 18:18:50 +0000733 I915_EVENT_ATTR(interrupts, I915_PMU_INTERRUPTS),
734
Tvrtko Ursulin6060b6a2017-11-21 18:18:52 +0000735 I915_EVENT(rc6-residency, I915_PMU_RC6_RESIDENCY, "ns"),
736 I915_EVENT(rc6p-residency, I915_PMU_RC6p_RESIDENCY, "ns"),
737 I915_EVENT(rc6pp-residency, I915_PMU_RC6pp_RESIDENCY, "ns"),
738
Tvrtko Ursulinb46a33e2017-11-21 18:18:45 +0000739 NULL,
740};
741
742static const struct attribute_group i915_pmu_events_attr_group = {
743 .name = "events",
744 .attrs = i915_pmu_events_attrs,
745};
746
747static ssize_t
748i915_pmu_get_attr_cpumask(struct device *dev,
749 struct device_attribute *attr,
750 char *buf)
751{
752 return cpumap_print_to_pagebuf(true, buf, &i915_pmu_cpumask);
753}
754
755static DEVICE_ATTR(cpumask, 0444, i915_pmu_get_attr_cpumask, NULL);
756
757static struct attribute *i915_cpumask_attrs[] = {
758 &dev_attr_cpumask.attr,
759 NULL,
760};
761
762static struct attribute_group i915_pmu_cpumask_attr_group = {
763 .attrs = i915_cpumask_attrs,
764};
765
766static const struct attribute_group *i915_pmu_attr_groups[] = {
767 &i915_pmu_format_attr_group,
768 &i915_pmu_events_attr_group,
769 &i915_pmu_cpumask_attr_group,
770 NULL
771};
772
Tvrtko Ursulinb46a33e2017-11-21 18:18:45 +0000773static int i915_pmu_cpu_online(unsigned int cpu, struct hlist_node *node)
774{
775 struct i915_pmu *pmu = hlist_entry_safe(node, typeof(*pmu), node);
776 unsigned int target;
777
778 GEM_BUG_ON(!pmu->base.event_init);
779
780 target = cpumask_any_and(&i915_pmu_cpumask, &i915_pmu_cpumask);
781 /* Select the first online CPU as a designated reader. */
782 if (target >= nr_cpu_ids)
783 cpumask_set_cpu(cpu, &i915_pmu_cpumask);
784
785 return 0;
786}
787
788static int i915_pmu_cpu_offline(unsigned int cpu, struct hlist_node *node)
789{
790 struct i915_pmu *pmu = hlist_entry_safe(node, typeof(*pmu), node);
791 unsigned int target;
792
793 GEM_BUG_ON(!pmu->base.event_init);
794
795 if (cpumask_test_and_clear_cpu(cpu, &i915_pmu_cpumask)) {
796 target = cpumask_any_but(topology_sibling_cpumask(cpu), cpu);
797 /* Migrate events if there is a valid target */
798 if (target < nr_cpu_ids) {
799 cpumask_set_cpu(target, &i915_pmu_cpumask);
800 perf_pmu_migrate_context(&pmu->base, cpu, target);
801 }
802 }
803
804 return 0;
805}
806
807static enum cpuhp_state cpuhp_slot = CPUHP_INVALID;
Tvrtko Ursulinb46a33e2017-11-21 18:18:45 +0000808
809static int i915_pmu_register_cpuhp_state(struct drm_i915_private *i915)
810{
Tvrtko Ursulinb46a33e2017-11-21 18:18:45 +0000811 enum cpuhp_state slot;
812 int ret;
813
814 ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN,
815 "perf/x86/intel/i915:online",
816 i915_pmu_cpu_online,
817 i915_pmu_cpu_offline);
818 if (ret < 0)
819 return ret;
820
821 slot = ret;
822 ret = cpuhp_state_add_instance(slot, &i915->pmu.node);
823 if (ret) {
824 cpuhp_remove_multi_state(slot);
825 return ret;
826 }
827
828 cpuhp_slot = slot;
Tvrtko Ursulinb46a33e2017-11-21 18:18:45 +0000829 return 0;
830}
831
832static void i915_pmu_unregister_cpuhp_state(struct drm_i915_private *i915)
833{
Tvrtko Ursulinb46a33e2017-11-21 18:18:45 +0000834 WARN_ON(cpuhp_slot == CPUHP_INVALID);
835 WARN_ON(cpuhp_state_remove_instance(cpuhp_slot, &i915->pmu.node));
836 cpuhp_remove_multi_state(cpuhp_slot);
Tvrtko Ursulinb46a33e2017-11-21 18:18:45 +0000837}
838
839void i915_pmu_register(struct drm_i915_private *i915)
840{
Tvrtko Ursulinb3add012017-11-21 18:18:49 +0000841 struct intel_engine_cs *engine;
842 enum intel_engine_id id;
Tvrtko Ursulinb46a33e2017-11-21 18:18:45 +0000843 int ret;
844
845 if (INTEL_GEN(i915) <= 2) {
846 DRM_INFO("PMU not supported for this GPU.");
847 return;
848 }
849
850 i915->pmu.base.attr_groups = i915_pmu_attr_groups;
851 i915->pmu.base.task_ctx_nr = perf_invalid_context;
852 i915->pmu.base.event_init = i915_pmu_event_init;
853 i915->pmu.base.add = i915_pmu_event_add;
854 i915->pmu.base.del = i915_pmu_event_del;
855 i915->pmu.base.start = i915_pmu_event_start;
856 i915->pmu.base.stop = i915_pmu_event_stop;
857 i915->pmu.base.read = i915_pmu_event_read;
858 i915->pmu.base.event_idx = i915_pmu_event_event_idx;
859
860 spin_lock_init(&i915->pmu.lock);
861 hrtimer_init(&i915->pmu.timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
862 i915->pmu.timer.function = i915_sample;
863
Tvrtko Ursulinfbba5552017-11-23 10:26:54 +0000864 pmu_init_previous_samples(i915);
865
Tvrtko Ursulinb3add012017-11-21 18:18:49 +0000866 for_each_engine(engine, i915, id)
867 INIT_DELAYED_WORK(&engine->pmu.disable_busy_stats,
868 __disable_busy_stats);
869
Tvrtko Ursulinb46a33e2017-11-21 18:18:45 +0000870 ret = perf_pmu_register(&i915->pmu.base, "i915", -1);
871 if (ret)
872 goto err;
873
874 ret = i915_pmu_register_cpuhp_state(i915);
875 if (ret)
876 goto err_unreg;
877
878 return;
879
880err_unreg:
881 perf_pmu_unregister(&i915->pmu.base);
882err:
883 i915->pmu.base.event_init = NULL;
884 DRM_NOTE("Failed to register PMU! (err=%d)\n", ret);
885}
886
887void i915_pmu_unregister(struct drm_i915_private *i915)
888{
Tvrtko Ursulinb3add012017-11-21 18:18:49 +0000889 struct intel_engine_cs *engine;
890 enum intel_engine_id id;
891
Tvrtko Ursulinb46a33e2017-11-21 18:18:45 +0000892 if (!i915->pmu.base.event_init)
893 return;
894
895 WARN_ON(i915->pmu.enable);
896
897 hrtimer_cancel(&i915->pmu.timer);
898
Tvrtko Ursulinb3add012017-11-21 18:18:49 +0000899 for_each_engine(engine, i915, id) {
900 GEM_BUG_ON(engine->pmu.busy_stats);
901 flush_delayed_work(&engine->pmu.disable_busy_stats);
902 }
903
Tvrtko Ursulinb46a33e2017-11-21 18:18:45 +0000904 i915_pmu_unregister_cpuhp_state(i915);
905
906 perf_pmu_unregister(&i915->pmu.base);
907 i915->pmu.base.event_init = NULL;
908}