Loading...
1/*
2 * SPDX-License-Identifier: MIT
3 *
4 * Copyright © 2017-2018 Intel Corporation
5 */
6
7#include <linux/irq.h>
8#include <linux/pm_runtime.h>
9
10#include "gt/intel_engine.h"
11#include "gt/intel_engine_pm.h"
12#include "gt/intel_engine_user.h"
13#include "gt/intel_gt_pm.h"
14
15#include "i915_drv.h"
16#include "i915_pmu.h"
17#include "intel_pm.h"
18
19/* Frequency for the sampling timer for events which need it. */
20#define FREQUENCY 200
21#define PERIOD max_t(u64, 10000, NSEC_PER_SEC / FREQUENCY)
22
23#define ENGINE_SAMPLE_MASK \
24 (BIT(I915_SAMPLE_BUSY) | \
25 BIT(I915_SAMPLE_WAIT) | \
26 BIT(I915_SAMPLE_SEMA))
27
28#define ENGINE_SAMPLE_BITS (1 << I915_PMU_SAMPLE_BITS)
29
30static cpumask_t i915_pmu_cpumask;
31
32static u8 engine_config_sample(u64 config)
33{
34 return config & I915_PMU_SAMPLE_MASK;
35}
36
37static u8 engine_event_sample(struct perf_event *event)
38{
39 return engine_config_sample(event->attr.config);
40}
41
42static u8 engine_event_class(struct perf_event *event)
43{
44 return (event->attr.config >> I915_PMU_CLASS_SHIFT) & 0xff;
45}
46
47static u8 engine_event_instance(struct perf_event *event)
48{
49 return (event->attr.config >> I915_PMU_SAMPLE_BITS) & 0xff;
50}
51
52static bool is_engine_config(u64 config)
53{
54 return config < __I915_PMU_OTHER(0);
55}
56
57static unsigned int config_enabled_bit(u64 config)
58{
59 if (is_engine_config(config))
60 return engine_config_sample(config);
61 else
62 return ENGINE_SAMPLE_BITS + (config - __I915_PMU_OTHER(0));
63}
64
65static u64 config_enabled_mask(u64 config)
66{
67 return BIT_ULL(config_enabled_bit(config));
68}
69
70static bool is_engine_event(struct perf_event *event)
71{
72 return is_engine_config(event->attr.config);
73}
74
75static unsigned int event_enabled_bit(struct perf_event *event)
76{
77 return config_enabled_bit(event->attr.config);
78}
79
80static bool pmu_needs_timer(struct i915_pmu *pmu, bool gpu_active)
81{
82 struct drm_i915_private *i915 = container_of(pmu, typeof(*i915), pmu);
83 u64 enable;
84
85 /*
86 * Only some counters need the sampling timer.
87 *
88 * We start with a bitmask of all currently enabled events.
89 */
90 enable = pmu->enable;
91
92 /*
93 * Mask out all the ones which do not need the timer, or in
94 * other words keep all the ones that could need the timer.
95 */
96 enable &= config_enabled_mask(I915_PMU_ACTUAL_FREQUENCY) |
97 config_enabled_mask(I915_PMU_REQUESTED_FREQUENCY) |
98 ENGINE_SAMPLE_MASK;
99
100 /*
101 * When the GPU is idle per-engine counters do not need to be
102 * running so clear those bits out.
103 */
104 if (!gpu_active)
105 enable &= ~ENGINE_SAMPLE_MASK;
106 /*
107 * Also there is software busyness tracking available we do not
108 * need the timer for I915_SAMPLE_BUSY counter.
109 */
110 else if (i915->caps.scheduler & I915_SCHEDULER_CAP_ENGINE_BUSY_STATS)
111 enable &= ~BIT(I915_SAMPLE_BUSY);
112
113 /*
114 * If some bits remain it means we need the sampling timer running.
115 */
116 return enable;
117}
118
119void i915_pmu_gt_parked(struct drm_i915_private *i915)
120{
121 struct i915_pmu *pmu = &i915->pmu;
122
123 if (!pmu->base.event_init)
124 return;
125
126 spin_lock_irq(&pmu->lock);
127 /*
128 * Signal sampling timer to stop if only engine events are enabled and
129 * GPU went idle.
130 */
131 pmu->timer_enabled = pmu_needs_timer(pmu, false);
132 spin_unlock_irq(&pmu->lock);
133}
134
135static void __i915_pmu_maybe_start_timer(struct i915_pmu *pmu)
136{
137 if (!pmu->timer_enabled && pmu_needs_timer(pmu, true)) {
138 pmu->timer_enabled = true;
139 pmu->timer_last = ktime_get();
140 hrtimer_start_range_ns(&pmu->timer,
141 ns_to_ktime(PERIOD), 0,
142 HRTIMER_MODE_REL_PINNED);
143 }
144}
145
146void i915_pmu_gt_unparked(struct drm_i915_private *i915)
147{
148 struct i915_pmu *pmu = &i915->pmu;
149
150 if (!pmu->base.event_init)
151 return;
152
153 spin_lock_irq(&pmu->lock);
154 /*
155 * Re-enable sampling timer when GPU goes active.
156 */
157 __i915_pmu_maybe_start_timer(pmu);
158 spin_unlock_irq(&pmu->lock);
159}
160
161static void
162add_sample(struct i915_pmu_sample *sample, u32 val)
163{
164 sample->cur += val;
165}
166
167static void
168engines_sample(struct intel_gt *gt, unsigned int period_ns)
169{
170 struct drm_i915_private *i915 = gt->i915;
171 struct intel_engine_cs *engine;
172 enum intel_engine_id id;
173
174 if ((i915->pmu.enable & ENGINE_SAMPLE_MASK) == 0)
175 return;
176
177 for_each_engine(engine, i915, id) {
178 struct intel_engine_pmu *pmu = &engine->pmu;
179 unsigned long flags;
180 bool busy;
181 u32 val;
182
183 if (!intel_engine_pm_get_if_awake(engine))
184 continue;
185
186 spin_lock_irqsave(&engine->uncore->lock, flags);
187
188 val = ENGINE_READ_FW(engine, RING_CTL);
189 if (val == 0) /* powerwell off => engine idle */
190 goto skip;
191
192 if (val & RING_WAIT)
193 add_sample(&pmu->sample[I915_SAMPLE_WAIT], period_ns);
194 if (val & RING_WAIT_SEMAPHORE)
195 add_sample(&pmu->sample[I915_SAMPLE_SEMA], period_ns);
196
197 /*
198 * While waiting on a semaphore or event, MI_MODE reports the
199 * ring as idle. However, previously using the seqno, and with
200 * execlists sampling, we account for the ring waiting as the
201 * engine being busy. Therefore, we record the sample as being
202 * busy if either waiting or !idle.
203 */
204 busy = val & (RING_WAIT_SEMAPHORE | RING_WAIT);
205 if (!busy) {
206 val = ENGINE_READ_FW(engine, RING_MI_MODE);
207 busy = !(val & MODE_IDLE);
208 }
209 if (busy)
210 add_sample(&pmu->sample[I915_SAMPLE_BUSY], period_ns);
211
212skip:
213 spin_unlock_irqrestore(&engine->uncore->lock, flags);
214 intel_engine_pm_put(engine);
215 }
216}
217
218static void
219add_sample_mult(struct i915_pmu_sample *sample, u32 val, u32 mul)
220{
221 sample->cur += mul_u32_u32(val, mul);
222}
223
224static void
225frequency_sample(struct intel_gt *gt, unsigned int period_ns)
226{
227 struct drm_i915_private *i915 = gt->i915;
228 struct intel_uncore *uncore = gt->uncore;
229 struct i915_pmu *pmu = &i915->pmu;
230
231 if (pmu->enable & config_enabled_mask(I915_PMU_ACTUAL_FREQUENCY)) {
232 u32 val;
233
234 val = i915->gt_pm.rps.cur_freq;
235 if (intel_gt_pm_get_if_awake(gt)) {
236 val = intel_uncore_read_notrace(uncore, GEN6_RPSTAT1);
237 val = intel_get_cagf(i915, val);
238 intel_gt_pm_put(gt);
239 }
240
241 add_sample_mult(&pmu->sample[__I915_SAMPLE_FREQ_ACT],
242 intel_gpu_freq(i915, val),
243 period_ns / 1000);
244 }
245
246 if (pmu->enable & config_enabled_mask(I915_PMU_REQUESTED_FREQUENCY)) {
247 add_sample_mult(&pmu->sample[__I915_SAMPLE_FREQ_REQ],
248 intel_gpu_freq(i915, i915->gt_pm.rps.cur_freq),
249 period_ns / 1000);
250 }
251}
252
253static enum hrtimer_restart i915_sample(struct hrtimer *hrtimer)
254{
255 struct drm_i915_private *i915 =
256 container_of(hrtimer, struct drm_i915_private, pmu.timer);
257 struct i915_pmu *pmu = &i915->pmu;
258 struct intel_gt *gt = &i915->gt;
259 unsigned int period_ns;
260 ktime_t now;
261
262 if (!READ_ONCE(pmu->timer_enabled))
263 return HRTIMER_NORESTART;
264
265 now = ktime_get();
266 period_ns = ktime_to_ns(ktime_sub(now, pmu->timer_last));
267 pmu->timer_last = now;
268
269 /*
270 * Strictly speaking the passed in period may not be 100% accurate for
271 * all internal calculation, since some amount of time can be spent on
272 * grabbing the forcewake. However the potential error from timer call-
273 * back delay greatly dominates this so we keep it simple.
274 */
275 engines_sample(gt, period_ns);
276 frequency_sample(gt, period_ns);
277
278 hrtimer_forward(hrtimer, now, ns_to_ktime(PERIOD));
279
280 return HRTIMER_RESTART;
281}
282
283static u64 count_interrupts(struct drm_i915_private *i915)
284{
285 /* open-coded kstat_irqs() */
286 struct irq_desc *desc = irq_to_desc(i915->drm.pdev->irq);
287 u64 sum = 0;
288 int cpu;
289
290 if (!desc || !desc->kstat_irqs)
291 return 0;
292
293 for_each_possible_cpu(cpu)
294 sum += *per_cpu_ptr(desc->kstat_irqs, cpu);
295
296 return sum;
297}
298
299static void engine_event_destroy(struct perf_event *event)
300{
301 struct drm_i915_private *i915 =
302 container_of(event->pmu, typeof(*i915), pmu.base);
303 struct intel_engine_cs *engine;
304
305 engine = intel_engine_lookup_user(i915,
306 engine_event_class(event),
307 engine_event_instance(event));
308 if (WARN_ON_ONCE(!engine))
309 return;
310
311 if (engine_event_sample(event) == I915_SAMPLE_BUSY &&
312 intel_engine_supports_stats(engine))
313 intel_disable_engine_stats(engine);
314}
315
316static void i915_pmu_event_destroy(struct perf_event *event)
317{
318 WARN_ON(event->parent);
319
320 if (is_engine_event(event))
321 engine_event_destroy(event);
322}
323
324static int
325engine_event_status(struct intel_engine_cs *engine,
326 enum drm_i915_pmu_engine_sample sample)
327{
328 switch (sample) {
329 case I915_SAMPLE_BUSY:
330 case I915_SAMPLE_WAIT:
331 break;
332 case I915_SAMPLE_SEMA:
333 if (INTEL_GEN(engine->i915) < 6)
334 return -ENODEV;
335 break;
336 default:
337 return -ENOENT;
338 }
339
340 return 0;
341}
342
343static int
344config_status(struct drm_i915_private *i915, u64 config)
345{
346 switch (config) {
347 case I915_PMU_ACTUAL_FREQUENCY:
348 if (IS_VALLEYVIEW(i915) || IS_CHERRYVIEW(i915))
349 /* Requires a mutex for sampling! */
350 return -ENODEV;
351 /* Fall-through. */
352 case I915_PMU_REQUESTED_FREQUENCY:
353 if (INTEL_GEN(i915) < 6)
354 return -ENODEV;
355 break;
356 case I915_PMU_INTERRUPTS:
357 break;
358 case I915_PMU_RC6_RESIDENCY:
359 if (!HAS_RC6(i915))
360 return -ENODEV;
361 break;
362 default:
363 return -ENOENT;
364 }
365
366 return 0;
367}
368
369static int engine_event_init(struct perf_event *event)
370{
371 struct drm_i915_private *i915 =
372 container_of(event->pmu, typeof(*i915), pmu.base);
373 struct intel_engine_cs *engine;
374 u8 sample;
375 int ret;
376
377 engine = intel_engine_lookup_user(i915, engine_event_class(event),
378 engine_event_instance(event));
379 if (!engine)
380 return -ENODEV;
381
382 sample = engine_event_sample(event);
383 ret = engine_event_status(engine, sample);
384 if (ret)
385 return ret;
386
387 if (sample == I915_SAMPLE_BUSY && intel_engine_supports_stats(engine))
388 ret = intel_enable_engine_stats(engine);
389
390 return ret;
391}
392
393static int i915_pmu_event_init(struct perf_event *event)
394{
395 struct drm_i915_private *i915 =
396 container_of(event->pmu, typeof(*i915), pmu.base);
397 int ret;
398
399 if (event->attr.type != event->pmu->type)
400 return -ENOENT;
401
402 /* unsupported modes and filters */
403 if (event->attr.sample_period) /* no sampling */
404 return -EINVAL;
405
406 if (has_branch_stack(event))
407 return -EOPNOTSUPP;
408
409 if (event->cpu < 0)
410 return -EINVAL;
411
412 /* only allow running on one cpu at a time */
413 if (!cpumask_test_cpu(event->cpu, &i915_pmu_cpumask))
414 return -EINVAL;
415
416 if (is_engine_event(event))
417 ret = engine_event_init(event);
418 else
419 ret = config_status(i915, event->attr.config);
420 if (ret)
421 return ret;
422
423 if (!event->parent)
424 event->destroy = i915_pmu_event_destroy;
425
426 return 0;
427}
428
429static u64 __get_rc6(struct intel_gt *gt)
430{
431 struct drm_i915_private *i915 = gt->i915;
432 u64 val;
433
434 val = intel_rc6_residency_ns(i915,
435 IS_VALLEYVIEW(i915) ?
436 VLV_GT_RENDER_RC6 :
437 GEN6_GT_GFX_RC6);
438
439 if (HAS_RC6p(i915))
440 val += intel_rc6_residency_ns(i915, GEN6_GT_GFX_RC6p);
441
442 if (HAS_RC6pp(i915))
443 val += intel_rc6_residency_ns(i915, GEN6_GT_GFX_RC6pp);
444
445 return val;
446}
447
448static u64 get_rc6(struct intel_gt *gt)
449{
450#if IS_ENABLED(CONFIG_PM)
451 struct drm_i915_private *i915 = gt->i915;
452 struct intel_runtime_pm *rpm = &i915->runtime_pm;
453 struct i915_pmu *pmu = &i915->pmu;
454 intel_wakeref_t wakeref;
455 unsigned long flags;
456 u64 val;
457
458 wakeref = intel_runtime_pm_get_if_in_use(rpm);
459 if (wakeref) {
460 val = __get_rc6(gt);
461 intel_runtime_pm_put(rpm, wakeref);
462
463 /*
464 * If we are coming back from being runtime suspended we must
465 * be careful not to report a larger value than returned
466 * previously.
467 */
468
469 spin_lock_irqsave(&pmu->lock, flags);
470
471 if (val >= pmu->sample[__I915_SAMPLE_RC6_ESTIMATED].cur) {
472 pmu->sample[__I915_SAMPLE_RC6_ESTIMATED].cur = 0;
473 pmu->sample[__I915_SAMPLE_RC6].cur = val;
474 } else {
475 val = pmu->sample[__I915_SAMPLE_RC6_ESTIMATED].cur;
476 }
477
478 spin_unlock_irqrestore(&pmu->lock, flags);
479 } else {
480 struct device *kdev = rpm->kdev;
481
482 /*
483 * We are runtime suspended.
484 *
485 * Report the delta from when the device was suspended to now,
486 * on top of the last known real value, as the approximated RC6
487 * counter value.
488 */
489 spin_lock_irqsave(&pmu->lock, flags);
490
491 /*
492 * After the above branch intel_runtime_pm_get_if_in_use failed
493 * to get the runtime PM reference we cannot assume we are in
494 * runtime suspend since we can either: a) race with coming out
495 * of it before we took the power.lock, or b) there are other
496 * states than suspended which can bring us here.
497 *
498 * We need to double-check that we are indeed currently runtime
499 * suspended and if not we cannot do better than report the last
500 * known RC6 value.
501 */
502 if (pm_runtime_status_suspended(kdev)) {
503 val = pm_runtime_suspended_time(kdev);
504
505 if (!pmu->sample[__I915_SAMPLE_RC6_ESTIMATED].cur)
506 pmu->suspended_time_last = val;
507
508 val -= pmu->suspended_time_last;
509 val += pmu->sample[__I915_SAMPLE_RC6].cur;
510
511 pmu->sample[__I915_SAMPLE_RC6_ESTIMATED].cur = val;
512 } else if (pmu->sample[__I915_SAMPLE_RC6_ESTIMATED].cur) {
513 val = pmu->sample[__I915_SAMPLE_RC6_ESTIMATED].cur;
514 } else {
515 val = pmu->sample[__I915_SAMPLE_RC6].cur;
516 }
517
518 spin_unlock_irqrestore(&pmu->lock, flags);
519 }
520
521 return val;
522#else
523 return __get_rc6(gt);
524#endif
525}
526
527static u64 __i915_pmu_event_read(struct perf_event *event)
528{
529 struct drm_i915_private *i915 =
530 container_of(event->pmu, typeof(*i915), pmu.base);
531 struct i915_pmu *pmu = &i915->pmu;
532 u64 val = 0;
533
534 if (is_engine_event(event)) {
535 u8 sample = engine_event_sample(event);
536 struct intel_engine_cs *engine;
537
538 engine = intel_engine_lookup_user(i915,
539 engine_event_class(event),
540 engine_event_instance(event));
541
542 if (WARN_ON_ONCE(!engine)) {
543 /* Do nothing */
544 } else if (sample == I915_SAMPLE_BUSY &&
545 intel_engine_supports_stats(engine)) {
546 val = ktime_to_ns(intel_engine_get_busy_time(engine));
547 } else {
548 val = engine->pmu.sample[sample].cur;
549 }
550 } else {
551 switch (event->attr.config) {
552 case I915_PMU_ACTUAL_FREQUENCY:
553 val =
554 div_u64(pmu->sample[__I915_SAMPLE_FREQ_ACT].cur,
555 USEC_PER_SEC /* to MHz */);
556 break;
557 case I915_PMU_REQUESTED_FREQUENCY:
558 val =
559 div_u64(pmu->sample[__I915_SAMPLE_FREQ_REQ].cur,
560 USEC_PER_SEC /* to MHz */);
561 break;
562 case I915_PMU_INTERRUPTS:
563 val = count_interrupts(i915);
564 break;
565 case I915_PMU_RC6_RESIDENCY:
566 val = get_rc6(&i915->gt);
567 break;
568 }
569 }
570
571 return val;
572}
573
574static void i915_pmu_event_read(struct perf_event *event)
575{
576 struct hw_perf_event *hwc = &event->hw;
577 u64 prev, new;
578
579again:
580 prev = local64_read(&hwc->prev_count);
581 new = __i915_pmu_event_read(event);
582
583 if (local64_cmpxchg(&hwc->prev_count, prev, new) != prev)
584 goto again;
585
586 local64_add(new - prev, &event->count);
587}
588
589static void i915_pmu_enable(struct perf_event *event)
590{
591 struct drm_i915_private *i915 =
592 container_of(event->pmu, typeof(*i915), pmu.base);
593 unsigned int bit = event_enabled_bit(event);
594 struct i915_pmu *pmu = &i915->pmu;
595 unsigned long flags;
596
597 spin_lock_irqsave(&pmu->lock, flags);
598
599 /*
600 * Update the bitmask of enabled events and increment
601 * the event reference counter.
602 */
603 BUILD_BUG_ON(ARRAY_SIZE(pmu->enable_count) != I915_PMU_MASK_BITS);
604 GEM_BUG_ON(bit >= ARRAY_SIZE(pmu->enable_count));
605 GEM_BUG_ON(pmu->enable_count[bit] == ~0);
606 pmu->enable |= BIT_ULL(bit);
607 pmu->enable_count[bit]++;
608
609 /*
610 * Start the sampling timer if needed and not already enabled.
611 */
612 __i915_pmu_maybe_start_timer(pmu);
613
614 /*
615 * For per-engine events the bitmask and reference counting
616 * is stored per engine.
617 */
618 if (is_engine_event(event)) {
619 u8 sample = engine_event_sample(event);
620 struct intel_engine_cs *engine;
621
622 engine = intel_engine_lookup_user(i915,
623 engine_event_class(event),
624 engine_event_instance(event));
625
626 BUILD_BUG_ON(ARRAY_SIZE(engine->pmu.enable_count) !=
627 I915_ENGINE_SAMPLE_COUNT);
628 BUILD_BUG_ON(ARRAY_SIZE(engine->pmu.sample) !=
629 I915_ENGINE_SAMPLE_COUNT);
630 GEM_BUG_ON(sample >= ARRAY_SIZE(engine->pmu.enable_count));
631 GEM_BUG_ON(sample >= ARRAY_SIZE(engine->pmu.sample));
632 GEM_BUG_ON(engine->pmu.enable_count[sample] == ~0);
633
634 engine->pmu.enable |= BIT(sample);
635 engine->pmu.enable_count[sample]++;
636 }
637
638 spin_unlock_irqrestore(&pmu->lock, flags);
639
640 /*
641 * Store the current counter value so we can report the correct delta
642 * for all listeners. Even when the event was already enabled and has
643 * an existing non-zero value.
644 */
645 local64_set(&event->hw.prev_count, __i915_pmu_event_read(event));
646}
647
648static void i915_pmu_disable(struct perf_event *event)
649{
650 struct drm_i915_private *i915 =
651 container_of(event->pmu, typeof(*i915), pmu.base);
652 unsigned int bit = event_enabled_bit(event);
653 struct i915_pmu *pmu = &i915->pmu;
654 unsigned long flags;
655
656 spin_lock_irqsave(&pmu->lock, flags);
657
658 if (is_engine_event(event)) {
659 u8 sample = engine_event_sample(event);
660 struct intel_engine_cs *engine;
661
662 engine = intel_engine_lookup_user(i915,
663 engine_event_class(event),
664 engine_event_instance(event));
665
666 GEM_BUG_ON(sample >= ARRAY_SIZE(engine->pmu.enable_count));
667 GEM_BUG_ON(sample >= ARRAY_SIZE(engine->pmu.sample));
668 GEM_BUG_ON(engine->pmu.enable_count[sample] == 0);
669
670 /*
671 * Decrement the reference count and clear the enabled
672 * bitmask when the last listener on an event goes away.
673 */
674 if (--engine->pmu.enable_count[sample] == 0)
675 engine->pmu.enable &= ~BIT(sample);
676 }
677
678 GEM_BUG_ON(bit >= ARRAY_SIZE(pmu->enable_count));
679 GEM_BUG_ON(pmu->enable_count[bit] == 0);
680 /*
681 * Decrement the reference count and clear the enabled
682 * bitmask when the last listener on an event goes away.
683 */
684 if (--pmu->enable_count[bit] == 0) {
685 pmu->enable &= ~BIT_ULL(bit);
686 pmu->timer_enabled &= pmu_needs_timer(pmu, true);
687 }
688
689 spin_unlock_irqrestore(&pmu->lock, flags);
690}
691
692static void i915_pmu_event_start(struct perf_event *event, int flags)
693{
694 i915_pmu_enable(event);
695 event->hw.state = 0;
696}
697
698static void i915_pmu_event_stop(struct perf_event *event, int flags)
699{
700 if (flags & PERF_EF_UPDATE)
701 i915_pmu_event_read(event);
702 i915_pmu_disable(event);
703 event->hw.state = PERF_HES_STOPPED;
704}
705
706static int i915_pmu_event_add(struct perf_event *event, int flags)
707{
708 if (flags & PERF_EF_START)
709 i915_pmu_event_start(event, flags);
710
711 return 0;
712}
713
714static void i915_pmu_event_del(struct perf_event *event, int flags)
715{
716 i915_pmu_event_stop(event, PERF_EF_UPDATE);
717}
718
719static int i915_pmu_event_event_idx(struct perf_event *event)
720{
721 return 0;
722}
723
724struct i915_str_attribute {
725 struct device_attribute attr;
726 const char *str;
727};
728
729static ssize_t i915_pmu_format_show(struct device *dev,
730 struct device_attribute *attr, char *buf)
731{
732 struct i915_str_attribute *eattr;
733
734 eattr = container_of(attr, struct i915_str_attribute, attr);
735 return sprintf(buf, "%s\n", eattr->str);
736}
737
738#define I915_PMU_FORMAT_ATTR(_name, _config) \
739 (&((struct i915_str_attribute[]) { \
740 { .attr = __ATTR(_name, 0444, i915_pmu_format_show, NULL), \
741 .str = _config, } \
742 })[0].attr.attr)
743
744static struct attribute *i915_pmu_format_attrs[] = {
745 I915_PMU_FORMAT_ATTR(i915_eventid, "config:0-20"),
746 NULL,
747};
748
749static const struct attribute_group i915_pmu_format_attr_group = {
750 .name = "format",
751 .attrs = i915_pmu_format_attrs,
752};
753
754struct i915_ext_attribute {
755 struct device_attribute attr;
756 unsigned long val;
757};
758
759static ssize_t i915_pmu_event_show(struct device *dev,
760 struct device_attribute *attr, char *buf)
761{
762 struct i915_ext_attribute *eattr;
763
764 eattr = container_of(attr, struct i915_ext_attribute, attr);
765 return sprintf(buf, "config=0x%lx\n", eattr->val);
766}
767
768static struct attribute_group i915_pmu_events_attr_group = {
769 .name = "events",
770 /* Patch in attrs at runtime. */
771};
772
773static ssize_t
774i915_pmu_get_attr_cpumask(struct device *dev,
775 struct device_attribute *attr,
776 char *buf)
777{
778 return cpumap_print_to_pagebuf(true, buf, &i915_pmu_cpumask);
779}
780
781static DEVICE_ATTR(cpumask, 0444, i915_pmu_get_attr_cpumask, NULL);
782
783static struct attribute *i915_cpumask_attrs[] = {
784 &dev_attr_cpumask.attr,
785 NULL,
786};
787
788static const struct attribute_group i915_pmu_cpumask_attr_group = {
789 .attrs = i915_cpumask_attrs,
790};
791
792static const struct attribute_group *i915_pmu_attr_groups[] = {
793 &i915_pmu_format_attr_group,
794 &i915_pmu_events_attr_group,
795 &i915_pmu_cpumask_attr_group,
796 NULL
797};
798
799#define __event(__config, __name, __unit) \
800{ \
801 .config = (__config), \
802 .name = (__name), \
803 .unit = (__unit), \
804}
805
806#define __engine_event(__sample, __name) \
807{ \
808 .sample = (__sample), \
809 .name = (__name), \
810}
811
812static struct i915_ext_attribute *
813add_i915_attr(struct i915_ext_attribute *attr, const char *name, u64 config)
814{
815 sysfs_attr_init(&attr->attr.attr);
816 attr->attr.attr.name = name;
817 attr->attr.attr.mode = 0444;
818 attr->attr.show = i915_pmu_event_show;
819 attr->val = config;
820
821 return ++attr;
822}
823
824static struct perf_pmu_events_attr *
825add_pmu_attr(struct perf_pmu_events_attr *attr, const char *name,
826 const char *str)
827{
828 sysfs_attr_init(&attr->attr.attr);
829 attr->attr.attr.name = name;
830 attr->attr.attr.mode = 0444;
831 attr->attr.show = perf_event_sysfs_show;
832 attr->event_str = str;
833
834 return ++attr;
835}
836
837static struct attribute **
838create_event_attributes(struct i915_pmu *pmu)
839{
840 struct drm_i915_private *i915 = container_of(pmu, typeof(*i915), pmu);
841 static const struct {
842 u64 config;
843 const char *name;
844 const char *unit;
845 } events[] = {
846 __event(I915_PMU_ACTUAL_FREQUENCY, "actual-frequency", "M"),
847 __event(I915_PMU_REQUESTED_FREQUENCY, "requested-frequency", "M"),
848 __event(I915_PMU_INTERRUPTS, "interrupts", NULL),
849 __event(I915_PMU_RC6_RESIDENCY, "rc6-residency", "ns"),
850 };
851 static const struct {
852 enum drm_i915_pmu_engine_sample sample;
853 char *name;
854 } engine_events[] = {
855 __engine_event(I915_SAMPLE_BUSY, "busy"),
856 __engine_event(I915_SAMPLE_SEMA, "sema"),
857 __engine_event(I915_SAMPLE_WAIT, "wait"),
858 };
859 unsigned int count = 0;
860 struct perf_pmu_events_attr *pmu_attr = NULL, *pmu_iter;
861 struct i915_ext_attribute *i915_attr = NULL, *i915_iter;
862 struct attribute **attr = NULL, **attr_iter;
863 struct intel_engine_cs *engine;
864 unsigned int i;
865
866 /* Count how many counters we will be exposing. */
867 for (i = 0; i < ARRAY_SIZE(events); i++) {
868 if (!config_status(i915, events[i].config))
869 count++;
870 }
871
872 for_each_uabi_engine(engine, i915) {
873 for (i = 0; i < ARRAY_SIZE(engine_events); i++) {
874 if (!engine_event_status(engine,
875 engine_events[i].sample))
876 count++;
877 }
878 }
879
880 /* Allocate attribute objects and table. */
881 i915_attr = kcalloc(count, sizeof(*i915_attr), GFP_KERNEL);
882 if (!i915_attr)
883 goto err_alloc;
884
885 pmu_attr = kcalloc(count, sizeof(*pmu_attr), GFP_KERNEL);
886 if (!pmu_attr)
887 goto err_alloc;
888
889 /* Max one pointer of each attribute type plus a termination entry. */
890 attr = kcalloc(count * 2 + 1, sizeof(*attr), GFP_KERNEL);
891 if (!attr)
892 goto err_alloc;
893
894 i915_iter = i915_attr;
895 pmu_iter = pmu_attr;
896 attr_iter = attr;
897
898 /* Initialize supported non-engine counters. */
899 for (i = 0; i < ARRAY_SIZE(events); i++) {
900 char *str;
901
902 if (config_status(i915, events[i].config))
903 continue;
904
905 str = kstrdup(events[i].name, GFP_KERNEL);
906 if (!str)
907 goto err;
908
909 *attr_iter++ = &i915_iter->attr.attr;
910 i915_iter = add_i915_attr(i915_iter, str, events[i].config);
911
912 if (events[i].unit) {
913 str = kasprintf(GFP_KERNEL, "%s.unit", events[i].name);
914 if (!str)
915 goto err;
916
917 *attr_iter++ = &pmu_iter->attr.attr;
918 pmu_iter = add_pmu_attr(pmu_iter, str, events[i].unit);
919 }
920 }
921
922 /* Initialize supported engine counters. */
923 for_each_uabi_engine(engine, i915) {
924 for (i = 0; i < ARRAY_SIZE(engine_events); i++) {
925 char *str;
926
927 if (engine_event_status(engine,
928 engine_events[i].sample))
929 continue;
930
931 str = kasprintf(GFP_KERNEL, "%s-%s",
932 engine->name, engine_events[i].name);
933 if (!str)
934 goto err;
935
936 *attr_iter++ = &i915_iter->attr.attr;
937 i915_iter =
938 add_i915_attr(i915_iter, str,
939 __I915_PMU_ENGINE(engine->uabi_class,
940 engine->uabi_instance,
941 engine_events[i].sample));
942
943 str = kasprintf(GFP_KERNEL, "%s-%s.unit",
944 engine->name, engine_events[i].name);
945 if (!str)
946 goto err;
947
948 *attr_iter++ = &pmu_iter->attr.attr;
949 pmu_iter = add_pmu_attr(pmu_iter, str, "ns");
950 }
951 }
952
953 pmu->i915_attr = i915_attr;
954 pmu->pmu_attr = pmu_attr;
955
956 return attr;
957
958err:;
959 for (attr_iter = attr; *attr_iter; attr_iter++)
960 kfree((*attr_iter)->name);
961
962err_alloc:
963 kfree(attr);
964 kfree(i915_attr);
965 kfree(pmu_attr);
966
967 return NULL;
968}
969
970static void free_event_attributes(struct i915_pmu *pmu)
971{
972 struct attribute **attr_iter = i915_pmu_events_attr_group.attrs;
973
974 for (; *attr_iter; attr_iter++)
975 kfree((*attr_iter)->name);
976
977 kfree(i915_pmu_events_attr_group.attrs);
978 kfree(pmu->i915_attr);
979 kfree(pmu->pmu_attr);
980
981 i915_pmu_events_attr_group.attrs = NULL;
982 pmu->i915_attr = NULL;
983 pmu->pmu_attr = NULL;
984}
985
986static int i915_pmu_cpu_online(unsigned int cpu, struct hlist_node *node)
987{
988 struct i915_pmu *pmu = hlist_entry_safe(node, typeof(*pmu), node);
989
990 GEM_BUG_ON(!pmu->base.event_init);
991
992 /* Select the first online CPU as a designated reader. */
993 if (!cpumask_weight(&i915_pmu_cpumask))
994 cpumask_set_cpu(cpu, &i915_pmu_cpumask);
995
996 return 0;
997}
998
999static int i915_pmu_cpu_offline(unsigned int cpu, struct hlist_node *node)
1000{
1001 struct i915_pmu *pmu = hlist_entry_safe(node, typeof(*pmu), node);
1002 unsigned int target;
1003
1004 GEM_BUG_ON(!pmu->base.event_init);
1005
1006 if (cpumask_test_and_clear_cpu(cpu, &i915_pmu_cpumask)) {
1007 target = cpumask_any_but(topology_sibling_cpumask(cpu), cpu);
1008 /* Migrate events if there is a valid target */
1009 if (target < nr_cpu_ids) {
1010 cpumask_set_cpu(target, &i915_pmu_cpumask);
1011 perf_pmu_migrate_context(&pmu->base, cpu, target);
1012 }
1013 }
1014
1015 return 0;
1016}
1017
1018static enum cpuhp_state cpuhp_slot = CPUHP_INVALID;
1019
1020static int i915_pmu_register_cpuhp_state(struct i915_pmu *pmu)
1021{
1022 enum cpuhp_state slot;
1023 int ret;
1024
1025 ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN,
1026 "perf/x86/intel/i915:online",
1027 i915_pmu_cpu_online,
1028 i915_pmu_cpu_offline);
1029 if (ret < 0)
1030 return ret;
1031
1032 slot = ret;
1033 ret = cpuhp_state_add_instance(slot, &pmu->node);
1034 if (ret) {
1035 cpuhp_remove_multi_state(slot);
1036 return ret;
1037 }
1038
1039 cpuhp_slot = slot;
1040 return 0;
1041}
1042
1043static void i915_pmu_unregister_cpuhp_state(struct i915_pmu *pmu)
1044{
1045 WARN_ON(cpuhp_slot == CPUHP_INVALID);
1046 WARN_ON(cpuhp_state_remove_instance(cpuhp_slot, &pmu->node));
1047 cpuhp_remove_multi_state(cpuhp_slot);
1048}
1049
1050void i915_pmu_register(struct drm_i915_private *i915)
1051{
1052 struct i915_pmu *pmu = &i915->pmu;
1053 int ret;
1054
1055 if (INTEL_GEN(i915) <= 2) {
1056 dev_info(i915->drm.dev, "PMU not supported for this GPU.");
1057 return;
1058 }
1059
1060 i915_pmu_events_attr_group.attrs = create_event_attributes(pmu);
1061 if (!i915_pmu_events_attr_group.attrs) {
1062 ret = -ENOMEM;
1063 goto err;
1064 }
1065
1066 pmu->base.attr_groups = i915_pmu_attr_groups;
1067 pmu->base.task_ctx_nr = perf_invalid_context;
1068 pmu->base.event_init = i915_pmu_event_init;
1069 pmu->base.add = i915_pmu_event_add;
1070 pmu->base.del = i915_pmu_event_del;
1071 pmu->base.start = i915_pmu_event_start;
1072 pmu->base.stop = i915_pmu_event_stop;
1073 pmu->base.read = i915_pmu_event_read;
1074 pmu->base.event_idx = i915_pmu_event_event_idx;
1075
1076 spin_lock_init(&pmu->lock);
1077 hrtimer_init(&pmu->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1078 pmu->timer.function = i915_sample;
1079
1080 ret = perf_pmu_register(&pmu->base, "i915", -1);
1081 if (ret)
1082 goto err;
1083
1084 ret = i915_pmu_register_cpuhp_state(pmu);
1085 if (ret)
1086 goto err_unreg;
1087
1088 return;
1089
1090err_unreg:
1091 perf_pmu_unregister(&pmu->base);
1092err:
1093 pmu->base.event_init = NULL;
1094 free_event_attributes(pmu);
1095 DRM_NOTE("Failed to register PMU! (err=%d)\n", ret);
1096}
1097
1098void i915_pmu_unregister(struct drm_i915_private *i915)
1099{
1100 struct i915_pmu *pmu = &i915->pmu;
1101
1102 if (!pmu->base.event_init)
1103 return;
1104
1105 WARN_ON(pmu->enable);
1106
1107 hrtimer_cancel(&pmu->timer);
1108
1109 i915_pmu_unregister_cpuhp_state(pmu);
1110
1111 perf_pmu_unregister(&pmu->base);
1112 pmu->base.event_init = NULL;
1113 free_event_attributes(pmu);
1114}
1/*
2 * SPDX-License-Identifier: MIT
3 *
4 * Copyright © 2017-2018 Intel Corporation
5 */
6
7#include <linux/pm_runtime.h>
8
9#include "gt/intel_engine.h"
10#include "gt/intel_engine_pm.h"
11#include "gt/intel_engine_regs.h"
12#include "gt/intel_engine_user.h"
13#include "gt/intel_gt.h"
14#include "gt/intel_gt_pm.h"
15#include "gt/intel_gt_regs.h"
16#include "gt/intel_rc6.h"
17#include "gt/intel_rps.h"
18
19#include "i915_drv.h"
20#include "i915_pmu.h"
21
22/* Frequency for the sampling timer for events which need it. */
23#define FREQUENCY 200
24#define PERIOD max_t(u64, 10000, NSEC_PER_SEC / FREQUENCY)
25
26#define ENGINE_SAMPLE_MASK \
27 (BIT(I915_SAMPLE_BUSY) | \
28 BIT(I915_SAMPLE_WAIT) | \
29 BIT(I915_SAMPLE_SEMA))
30
31static cpumask_t i915_pmu_cpumask;
32static unsigned int i915_pmu_target_cpu = -1;
33
34static struct i915_pmu *event_to_pmu(struct perf_event *event)
35{
36 return container_of(event->pmu, struct i915_pmu, base);
37}
38
39static struct drm_i915_private *pmu_to_i915(struct i915_pmu *pmu)
40{
41 return container_of(pmu, struct drm_i915_private, pmu);
42}
43
44static u8 engine_config_sample(u64 config)
45{
46 return config & I915_PMU_SAMPLE_MASK;
47}
48
49static u8 engine_event_sample(struct perf_event *event)
50{
51 return engine_config_sample(event->attr.config);
52}
53
54static u8 engine_event_class(struct perf_event *event)
55{
56 return (event->attr.config >> I915_PMU_CLASS_SHIFT) & 0xff;
57}
58
59static u8 engine_event_instance(struct perf_event *event)
60{
61 return (event->attr.config >> I915_PMU_SAMPLE_BITS) & 0xff;
62}
63
64static bool is_engine_config(const u64 config)
65{
66 return config < __I915_PMU_OTHER(0);
67}
68
69static unsigned int config_gt_id(const u64 config)
70{
71 return config >> __I915_PMU_GT_SHIFT;
72}
73
74static u64 config_counter(const u64 config)
75{
76 return config & ~(~0ULL << __I915_PMU_GT_SHIFT);
77}
78
79static unsigned int other_bit(const u64 config)
80{
81 unsigned int val;
82
83 switch (config_counter(config)) {
84 case I915_PMU_ACTUAL_FREQUENCY:
85 val = __I915_PMU_ACTUAL_FREQUENCY_ENABLED;
86 break;
87 case I915_PMU_REQUESTED_FREQUENCY:
88 val = __I915_PMU_REQUESTED_FREQUENCY_ENABLED;
89 break;
90 case I915_PMU_RC6_RESIDENCY:
91 val = __I915_PMU_RC6_RESIDENCY_ENABLED;
92 break;
93 default:
94 /*
95 * Events that do not require sampling, or tracking state
96 * transitions between enabled and disabled can be ignored.
97 */
98 return -1;
99 }
100
101 return I915_ENGINE_SAMPLE_COUNT +
102 config_gt_id(config) * __I915_PMU_TRACKED_EVENT_COUNT +
103 val;
104}
105
106static unsigned int config_bit(const u64 config)
107{
108 if (is_engine_config(config))
109 return engine_config_sample(config);
110 else
111 return other_bit(config);
112}
113
114static u32 config_mask(const u64 config)
115{
116 unsigned int bit = config_bit(config);
117
118 if (__builtin_constant_p(config))
119 BUILD_BUG_ON(bit >
120 BITS_PER_TYPE(typeof_member(struct i915_pmu,
121 enable)) - 1);
122 else
123 WARN_ON_ONCE(bit >
124 BITS_PER_TYPE(typeof_member(struct i915_pmu,
125 enable)) - 1);
126
127 return BIT(config_bit(config));
128}
129
130static bool is_engine_event(struct perf_event *event)
131{
132 return is_engine_config(event->attr.config);
133}
134
135static unsigned int event_bit(struct perf_event *event)
136{
137 return config_bit(event->attr.config);
138}
139
140static u32 frequency_enabled_mask(void)
141{
142 unsigned int i;
143 u32 mask = 0;
144
145 for (i = 0; i < I915_PMU_MAX_GT; i++)
146 mask |= config_mask(__I915_PMU_ACTUAL_FREQUENCY(i)) |
147 config_mask(__I915_PMU_REQUESTED_FREQUENCY(i));
148
149 return mask;
150}
151
152static bool pmu_needs_timer(struct i915_pmu *pmu)
153{
154 struct drm_i915_private *i915 = pmu_to_i915(pmu);
155 u32 enable;
156
157 /*
158 * Only some counters need the sampling timer.
159 *
160 * We start with a bitmask of all currently enabled events.
161 */
162 enable = pmu->enable;
163
164 /*
165 * Mask out all the ones which do not need the timer, or in
166 * other words keep all the ones that could need the timer.
167 */
168 enable &= frequency_enabled_mask() | ENGINE_SAMPLE_MASK;
169
170 /*
171 * Also there is software busyness tracking available we do not
172 * need the timer for I915_SAMPLE_BUSY counter.
173 */
174 if (i915->caps.scheduler & I915_SCHEDULER_CAP_ENGINE_BUSY_STATS)
175 enable &= ~BIT(I915_SAMPLE_BUSY);
176
177 /*
178 * If some bits remain it means we need the sampling timer running.
179 */
180 return enable;
181}
182
183static u64 __get_rc6(struct intel_gt *gt)
184{
185 struct drm_i915_private *i915 = gt->i915;
186 u64 val;
187
188 val = intel_rc6_residency_ns(>->rc6, INTEL_RC6_RES_RC6);
189
190 if (HAS_RC6p(i915))
191 val += intel_rc6_residency_ns(>->rc6, INTEL_RC6_RES_RC6p);
192
193 if (HAS_RC6pp(i915))
194 val += intel_rc6_residency_ns(>->rc6, INTEL_RC6_RES_RC6pp);
195
196 return val;
197}
198
199static inline s64 ktime_since_raw(const ktime_t kt)
200{
201 return ktime_to_ns(ktime_sub(ktime_get_raw(), kt));
202}
203
204static u64 read_sample(struct i915_pmu *pmu, unsigned int gt_id, int sample)
205{
206 return pmu->sample[gt_id][sample].cur;
207}
208
209static void
210store_sample(struct i915_pmu *pmu, unsigned int gt_id, int sample, u64 val)
211{
212 pmu->sample[gt_id][sample].cur = val;
213}
214
215static void
216add_sample_mult(struct i915_pmu *pmu, unsigned int gt_id, int sample, u32 val, u32 mul)
217{
218 pmu->sample[gt_id][sample].cur += mul_u32_u32(val, mul);
219}
220
221static u64 get_rc6(struct intel_gt *gt)
222{
223 struct drm_i915_private *i915 = gt->i915;
224 const unsigned int gt_id = gt->info.id;
225 struct i915_pmu *pmu = &i915->pmu;
226 intel_wakeref_t wakeref;
227 unsigned long flags;
228 u64 val;
229
230 wakeref = intel_gt_pm_get_if_awake(gt);
231 if (wakeref) {
232 val = __get_rc6(gt);
233 intel_gt_pm_put_async(gt, wakeref);
234 }
235
236 spin_lock_irqsave(&pmu->lock, flags);
237
238 if (wakeref) {
239 store_sample(pmu, gt_id, __I915_SAMPLE_RC6, val);
240 } else {
241 /*
242 * We think we are runtime suspended.
243 *
244 * Report the delta from when the device was suspended to now,
245 * on top of the last known real value, as the approximated RC6
246 * counter value.
247 */
248 val = ktime_since_raw(pmu->sleep_last[gt_id]);
249 val += read_sample(pmu, gt_id, __I915_SAMPLE_RC6);
250 }
251
252 if (val < read_sample(pmu, gt_id, __I915_SAMPLE_RC6_LAST_REPORTED))
253 val = read_sample(pmu, gt_id, __I915_SAMPLE_RC6_LAST_REPORTED);
254 else
255 store_sample(pmu, gt_id, __I915_SAMPLE_RC6_LAST_REPORTED, val);
256
257 spin_unlock_irqrestore(&pmu->lock, flags);
258
259 return val;
260}
261
262static void init_rc6(struct i915_pmu *pmu)
263{
264 struct drm_i915_private *i915 = pmu_to_i915(pmu);
265 struct intel_gt *gt;
266 unsigned int i;
267
268 for_each_gt(gt, i915, i) {
269 intel_wakeref_t wakeref;
270
271 with_intel_runtime_pm(gt->uncore->rpm, wakeref) {
272 u64 val = __get_rc6(gt);
273
274 store_sample(pmu, i, __I915_SAMPLE_RC6, val);
275 store_sample(pmu, i, __I915_SAMPLE_RC6_LAST_REPORTED,
276 val);
277 pmu->sleep_last[i] = ktime_get_raw();
278 }
279 }
280}
281
282static void park_rc6(struct intel_gt *gt)
283{
284 struct i915_pmu *pmu = >->i915->pmu;
285
286 store_sample(pmu, gt->info.id, __I915_SAMPLE_RC6, __get_rc6(gt));
287 pmu->sleep_last[gt->info.id] = ktime_get_raw();
288}
289
290static void __i915_pmu_maybe_start_timer(struct i915_pmu *pmu)
291{
292 if (!pmu->timer_enabled && pmu_needs_timer(pmu)) {
293 pmu->timer_enabled = true;
294 pmu->timer_last = ktime_get();
295 hrtimer_start_range_ns(&pmu->timer,
296 ns_to_ktime(PERIOD), 0,
297 HRTIMER_MODE_REL_PINNED);
298 }
299}
300
301void i915_pmu_gt_parked(struct intel_gt *gt)
302{
303 struct i915_pmu *pmu = >->i915->pmu;
304
305 if (!pmu->base.event_init)
306 return;
307
308 spin_lock_irq(&pmu->lock);
309
310 park_rc6(gt);
311
312 /*
313 * Signal sampling timer to stop if only engine events are enabled and
314 * GPU went idle.
315 */
316 pmu->unparked &= ~BIT(gt->info.id);
317 if (pmu->unparked == 0)
318 pmu->timer_enabled = false;
319
320 spin_unlock_irq(&pmu->lock);
321}
322
323void i915_pmu_gt_unparked(struct intel_gt *gt)
324{
325 struct i915_pmu *pmu = >->i915->pmu;
326
327 if (!pmu->base.event_init)
328 return;
329
330 spin_lock_irq(&pmu->lock);
331
332 /*
333 * Re-enable sampling timer when GPU goes active.
334 */
335 if (pmu->unparked == 0)
336 __i915_pmu_maybe_start_timer(pmu);
337
338 pmu->unparked |= BIT(gt->info.id);
339
340 spin_unlock_irq(&pmu->lock);
341}
342
343static void
344add_sample(struct i915_pmu_sample *sample, u32 val)
345{
346 sample->cur += val;
347}
348
349static bool exclusive_mmio_access(const struct drm_i915_private *i915)
350{
351 /*
352 * We have to avoid concurrent mmio cache line access on gen7 or
353 * risk a machine hang. For a fun history lesson dig out the old
354 * userspace intel_gpu_top and run it on Ivybridge or Haswell!
355 */
356 return GRAPHICS_VER(i915) == 7;
357}
358
359static void engine_sample(struct intel_engine_cs *engine, unsigned int period_ns)
360{
361 struct intel_engine_pmu *pmu = &engine->pmu;
362 bool busy;
363 u32 val;
364
365 val = ENGINE_READ_FW(engine, RING_CTL);
366 if (val == 0) /* powerwell off => engine idle */
367 return;
368
369 if (val & RING_WAIT)
370 add_sample(&pmu->sample[I915_SAMPLE_WAIT], period_ns);
371 if (val & RING_WAIT_SEMAPHORE)
372 add_sample(&pmu->sample[I915_SAMPLE_SEMA], period_ns);
373
374 /* No need to sample when busy stats are supported. */
375 if (intel_engine_supports_stats(engine))
376 return;
377
378 /*
379 * While waiting on a semaphore or event, MI_MODE reports the
380 * ring as idle. However, previously using the seqno, and with
381 * execlists sampling, we account for the ring waiting as the
382 * engine being busy. Therefore, we record the sample as being
383 * busy if either waiting or !idle.
384 */
385 busy = val & (RING_WAIT_SEMAPHORE | RING_WAIT);
386 if (!busy) {
387 val = ENGINE_READ_FW(engine, RING_MI_MODE);
388 busy = !(val & MODE_IDLE);
389 }
390 if (busy)
391 add_sample(&pmu->sample[I915_SAMPLE_BUSY], period_ns);
392}
393
394static void
395engines_sample(struct intel_gt *gt, unsigned int period_ns)
396{
397 struct drm_i915_private *i915 = gt->i915;
398 struct intel_engine_cs *engine;
399 enum intel_engine_id id;
400 unsigned long flags;
401
402 if ((i915->pmu.enable & ENGINE_SAMPLE_MASK) == 0)
403 return;
404
405 if (!intel_gt_pm_is_awake(gt))
406 return;
407
408 for_each_engine(engine, gt, id) {
409 if (!engine->pmu.enable)
410 continue;
411
412 if (!intel_engine_pm_get_if_awake(engine))
413 continue;
414
415 if (exclusive_mmio_access(i915)) {
416 spin_lock_irqsave(&engine->uncore->lock, flags);
417 engine_sample(engine, period_ns);
418 spin_unlock_irqrestore(&engine->uncore->lock, flags);
419 } else {
420 engine_sample(engine, period_ns);
421 }
422
423 intel_engine_pm_put_async(engine);
424 }
425}
426
427static bool
428frequency_sampling_enabled(struct i915_pmu *pmu, unsigned int gt)
429{
430 return pmu->enable &
431 (config_mask(__I915_PMU_ACTUAL_FREQUENCY(gt)) |
432 config_mask(__I915_PMU_REQUESTED_FREQUENCY(gt)));
433}
434
435static void
436frequency_sample(struct intel_gt *gt, unsigned int period_ns)
437{
438 struct drm_i915_private *i915 = gt->i915;
439 const unsigned int gt_id = gt->info.id;
440 struct i915_pmu *pmu = &i915->pmu;
441 struct intel_rps *rps = >->rps;
442 intel_wakeref_t wakeref;
443
444 if (!frequency_sampling_enabled(pmu, gt_id))
445 return;
446
447 /* Report 0/0 (actual/requested) frequency while parked. */
448 wakeref = intel_gt_pm_get_if_awake(gt);
449 if (!wakeref)
450 return;
451
452 if (pmu->enable & config_mask(__I915_PMU_ACTUAL_FREQUENCY(gt_id))) {
453 u32 val;
454
455 /*
456 * We take a quick peek here without using forcewake
457 * so that we don't perturb the system under observation
458 * (forcewake => !rc6 => increased power use). We expect
459 * that if the read fails because it is outside of the
460 * mmio power well, then it will return 0 -- in which
461 * case we assume the system is running at the intended
462 * frequency. Fortunately, the read should rarely fail!
463 */
464 val = intel_rps_read_actual_frequency_fw(rps);
465 if (!val)
466 val = intel_gpu_freq(rps, rps->cur_freq);
467
468 add_sample_mult(pmu, gt_id, __I915_SAMPLE_FREQ_ACT,
469 val, period_ns / 1000);
470 }
471
472 if (pmu->enable & config_mask(__I915_PMU_REQUESTED_FREQUENCY(gt_id))) {
473 add_sample_mult(pmu, gt_id, __I915_SAMPLE_FREQ_REQ,
474 intel_rps_get_requested_frequency(rps),
475 period_ns / 1000);
476 }
477
478 intel_gt_pm_put_async(gt, wakeref);
479}
480
481static enum hrtimer_restart i915_sample(struct hrtimer *hrtimer)
482{
483 struct i915_pmu *pmu = container_of(hrtimer, struct i915_pmu, timer);
484 struct drm_i915_private *i915 = pmu_to_i915(pmu);
485 unsigned int period_ns;
486 struct intel_gt *gt;
487 unsigned int i;
488 ktime_t now;
489
490 if (!READ_ONCE(pmu->timer_enabled))
491 return HRTIMER_NORESTART;
492
493 now = ktime_get();
494 period_ns = ktime_to_ns(ktime_sub(now, pmu->timer_last));
495 pmu->timer_last = now;
496
497 /*
498 * Strictly speaking the passed in period may not be 100% accurate for
499 * all internal calculation, since some amount of time can be spent on
500 * grabbing the forcewake. However the potential error from timer call-
501 * back delay greatly dominates this so we keep it simple.
502 */
503
504 for_each_gt(gt, i915, i) {
505 if (!(pmu->unparked & BIT(i)))
506 continue;
507
508 engines_sample(gt, period_ns);
509 frequency_sample(gt, period_ns);
510 }
511
512 hrtimer_forward(hrtimer, now, ns_to_ktime(PERIOD));
513
514 return HRTIMER_RESTART;
515}
516
517static void i915_pmu_event_destroy(struct perf_event *event)
518{
519 struct i915_pmu *pmu = event_to_pmu(event);
520 struct drm_i915_private *i915 = pmu_to_i915(pmu);
521
522 drm_WARN_ON(&i915->drm, event->parent);
523
524 drm_dev_put(&i915->drm);
525}
526
527static int
528engine_event_status(struct intel_engine_cs *engine,
529 enum drm_i915_pmu_engine_sample sample)
530{
531 switch (sample) {
532 case I915_SAMPLE_BUSY:
533 case I915_SAMPLE_WAIT:
534 break;
535 case I915_SAMPLE_SEMA:
536 if (GRAPHICS_VER(engine->i915) < 6)
537 return -ENODEV;
538 break;
539 default:
540 return -ENOENT;
541 }
542
543 return 0;
544}
545
546static int
547config_status(struct drm_i915_private *i915, u64 config)
548{
549 struct intel_gt *gt = to_gt(i915);
550
551 unsigned int gt_id = config_gt_id(config);
552 unsigned int max_gt_id = HAS_EXTRA_GT_LIST(i915) ? 1 : 0;
553
554 if (gt_id > max_gt_id)
555 return -ENOENT;
556
557 switch (config_counter(config)) {
558 case I915_PMU_ACTUAL_FREQUENCY:
559 if (IS_VALLEYVIEW(i915) || IS_CHERRYVIEW(i915))
560 /* Requires a mutex for sampling! */
561 return -ENODEV;
562 fallthrough;
563 case I915_PMU_REQUESTED_FREQUENCY:
564 if (GRAPHICS_VER(i915) < 6)
565 return -ENODEV;
566 break;
567 case I915_PMU_INTERRUPTS:
568 if (gt_id)
569 return -ENOENT;
570 break;
571 case I915_PMU_RC6_RESIDENCY:
572 if (!gt->rc6.supported)
573 return -ENODEV;
574 break;
575 case I915_PMU_SOFTWARE_GT_AWAKE_TIME:
576 break;
577 default:
578 return -ENOENT;
579 }
580
581 return 0;
582}
583
584static int engine_event_init(struct perf_event *event)
585{
586 struct i915_pmu *pmu = event_to_pmu(event);
587 struct drm_i915_private *i915 = pmu_to_i915(pmu);
588 struct intel_engine_cs *engine;
589
590 engine = intel_engine_lookup_user(i915, engine_event_class(event),
591 engine_event_instance(event));
592 if (!engine)
593 return -ENODEV;
594
595 return engine_event_status(engine, engine_event_sample(event));
596}
597
598static int i915_pmu_event_init(struct perf_event *event)
599{
600 struct i915_pmu *pmu = event_to_pmu(event);
601 struct drm_i915_private *i915 = pmu_to_i915(pmu);
602 int ret;
603
604 if (pmu->closed)
605 return -ENODEV;
606
607 if (event->attr.type != event->pmu->type)
608 return -ENOENT;
609
610 /* unsupported modes and filters */
611 if (event->attr.sample_period) /* no sampling */
612 return -EINVAL;
613
614 if (has_branch_stack(event))
615 return -EOPNOTSUPP;
616
617 if (event->cpu < 0)
618 return -EINVAL;
619
620 /* only allow running on one cpu at a time */
621 if (!cpumask_test_cpu(event->cpu, &i915_pmu_cpumask))
622 return -EINVAL;
623
624 if (is_engine_event(event))
625 ret = engine_event_init(event);
626 else
627 ret = config_status(i915, event->attr.config);
628 if (ret)
629 return ret;
630
631 if (!event->parent) {
632 drm_dev_get(&i915->drm);
633 event->destroy = i915_pmu_event_destroy;
634 }
635
636 return 0;
637}
638
639static u64 __i915_pmu_event_read(struct perf_event *event)
640{
641 struct i915_pmu *pmu = event_to_pmu(event);
642 struct drm_i915_private *i915 = pmu_to_i915(pmu);
643 u64 val = 0;
644
645 if (is_engine_event(event)) {
646 u8 sample = engine_event_sample(event);
647 struct intel_engine_cs *engine;
648
649 engine = intel_engine_lookup_user(i915,
650 engine_event_class(event),
651 engine_event_instance(event));
652
653 if (drm_WARN_ON_ONCE(&i915->drm, !engine)) {
654 /* Do nothing */
655 } else if (sample == I915_SAMPLE_BUSY &&
656 intel_engine_supports_stats(engine)) {
657 ktime_t unused;
658
659 val = ktime_to_ns(intel_engine_get_busy_time(engine,
660 &unused));
661 } else {
662 val = engine->pmu.sample[sample].cur;
663 }
664 } else {
665 const unsigned int gt_id = config_gt_id(event->attr.config);
666 const u64 config = config_counter(event->attr.config);
667
668 switch (config) {
669 case I915_PMU_ACTUAL_FREQUENCY:
670 val =
671 div_u64(read_sample(pmu, gt_id,
672 __I915_SAMPLE_FREQ_ACT),
673 USEC_PER_SEC /* to MHz */);
674 break;
675 case I915_PMU_REQUESTED_FREQUENCY:
676 val =
677 div_u64(read_sample(pmu, gt_id,
678 __I915_SAMPLE_FREQ_REQ),
679 USEC_PER_SEC /* to MHz */);
680 break;
681 case I915_PMU_INTERRUPTS:
682 val = READ_ONCE(pmu->irq_count);
683 break;
684 case I915_PMU_RC6_RESIDENCY:
685 val = get_rc6(i915->gt[gt_id]);
686 break;
687 case I915_PMU_SOFTWARE_GT_AWAKE_TIME:
688 val = ktime_to_ns(intel_gt_get_awake_time(to_gt(i915)));
689 break;
690 }
691 }
692
693 return val;
694}
695
696static void i915_pmu_event_read(struct perf_event *event)
697{
698 struct i915_pmu *pmu = event_to_pmu(event);
699 struct hw_perf_event *hwc = &event->hw;
700 u64 prev, new;
701
702 if (pmu->closed) {
703 event->hw.state = PERF_HES_STOPPED;
704 return;
705 }
706
707 prev = local64_read(&hwc->prev_count);
708 do {
709 new = __i915_pmu_event_read(event);
710 } while (!local64_try_cmpxchg(&hwc->prev_count, &prev, new));
711
712 local64_add(new - prev, &event->count);
713}
714
715static void i915_pmu_enable(struct perf_event *event)
716{
717 struct i915_pmu *pmu = event_to_pmu(event);
718 struct drm_i915_private *i915 = pmu_to_i915(pmu);
719 const unsigned int bit = event_bit(event);
720 unsigned long flags;
721
722 if (bit == -1)
723 goto update;
724
725 spin_lock_irqsave(&pmu->lock, flags);
726
727 /*
728 * Update the bitmask of enabled events and increment
729 * the event reference counter.
730 */
731 BUILD_BUG_ON(ARRAY_SIZE(pmu->enable_count) != I915_PMU_MASK_BITS);
732 GEM_BUG_ON(bit >= ARRAY_SIZE(pmu->enable_count));
733 GEM_BUG_ON(pmu->enable_count[bit] == ~0);
734
735 pmu->enable |= BIT(bit);
736 pmu->enable_count[bit]++;
737
738 /*
739 * Start the sampling timer if needed and not already enabled.
740 */
741 __i915_pmu_maybe_start_timer(pmu);
742
743 /*
744 * For per-engine events the bitmask and reference counting
745 * is stored per engine.
746 */
747 if (is_engine_event(event)) {
748 u8 sample = engine_event_sample(event);
749 struct intel_engine_cs *engine;
750
751 engine = intel_engine_lookup_user(i915,
752 engine_event_class(event),
753 engine_event_instance(event));
754
755 BUILD_BUG_ON(ARRAY_SIZE(engine->pmu.enable_count) !=
756 I915_ENGINE_SAMPLE_COUNT);
757 BUILD_BUG_ON(ARRAY_SIZE(engine->pmu.sample) !=
758 I915_ENGINE_SAMPLE_COUNT);
759 GEM_BUG_ON(sample >= ARRAY_SIZE(engine->pmu.enable_count));
760 GEM_BUG_ON(sample >= ARRAY_SIZE(engine->pmu.sample));
761 GEM_BUG_ON(engine->pmu.enable_count[sample] == ~0);
762
763 engine->pmu.enable |= BIT(sample);
764 engine->pmu.enable_count[sample]++;
765 }
766
767 spin_unlock_irqrestore(&pmu->lock, flags);
768
769update:
770 /*
771 * Store the current counter value so we can report the correct delta
772 * for all listeners. Even when the event was already enabled and has
773 * an existing non-zero value.
774 */
775 local64_set(&event->hw.prev_count, __i915_pmu_event_read(event));
776}
777
778static void i915_pmu_disable(struct perf_event *event)
779{
780 struct i915_pmu *pmu = event_to_pmu(event);
781 struct drm_i915_private *i915 = pmu_to_i915(pmu);
782 const unsigned int bit = event_bit(event);
783 unsigned long flags;
784
785 if (bit == -1)
786 return;
787
788 spin_lock_irqsave(&pmu->lock, flags);
789
790 if (is_engine_event(event)) {
791 u8 sample = engine_event_sample(event);
792 struct intel_engine_cs *engine;
793
794 engine = intel_engine_lookup_user(i915,
795 engine_event_class(event),
796 engine_event_instance(event));
797
798 GEM_BUG_ON(sample >= ARRAY_SIZE(engine->pmu.enable_count));
799 GEM_BUG_ON(sample >= ARRAY_SIZE(engine->pmu.sample));
800 GEM_BUG_ON(engine->pmu.enable_count[sample] == 0);
801
802 /*
803 * Decrement the reference count and clear the enabled
804 * bitmask when the last listener on an event goes away.
805 */
806 if (--engine->pmu.enable_count[sample] == 0)
807 engine->pmu.enable &= ~BIT(sample);
808 }
809
810 GEM_BUG_ON(bit >= ARRAY_SIZE(pmu->enable_count));
811 GEM_BUG_ON(pmu->enable_count[bit] == 0);
812 /*
813 * Decrement the reference count and clear the enabled
814 * bitmask when the last listener on an event goes away.
815 */
816 if (--pmu->enable_count[bit] == 0) {
817 pmu->enable &= ~BIT(bit);
818 pmu->timer_enabled &= pmu_needs_timer(pmu);
819 }
820
821 spin_unlock_irqrestore(&pmu->lock, flags);
822}
823
824static void i915_pmu_event_start(struct perf_event *event, int flags)
825{
826 struct i915_pmu *pmu = event_to_pmu(event);
827
828 if (pmu->closed)
829 return;
830
831 i915_pmu_enable(event);
832 event->hw.state = 0;
833}
834
835static void i915_pmu_event_stop(struct perf_event *event, int flags)
836{
837 struct drm_i915_private *i915 =
838 container_of(event->pmu, typeof(*i915), pmu.base);
839 struct i915_pmu *pmu = &i915->pmu;
840
841 if (pmu->closed)
842 goto out;
843
844 if (flags & PERF_EF_UPDATE)
845 i915_pmu_event_read(event);
846 i915_pmu_disable(event);
847
848out:
849 event->hw.state = PERF_HES_STOPPED;
850}
851
852static int i915_pmu_event_add(struct perf_event *event, int flags)
853{
854 struct i915_pmu *pmu = event_to_pmu(event);
855
856 if (pmu->closed)
857 return -ENODEV;
858
859 if (flags & PERF_EF_START)
860 i915_pmu_event_start(event, flags);
861
862 return 0;
863}
864
865static void i915_pmu_event_del(struct perf_event *event, int flags)
866{
867 i915_pmu_event_stop(event, PERF_EF_UPDATE);
868}
869
870static int i915_pmu_event_event_idx(struct perf_event *event)
871{
872 return 0;
873}
874
875struct i915_str_attribute {
876 struct device_attribute attr;
877 const char *str;
878};
879
880static ssize_t i915_pmu_format_show(struct device *dev,
881 struct device_attribute *attr, char *buf)
882{
883 struct i915_str_attribute *eattr;
884
885 eattr = container_of(attr, struct i915_str_attribute, attr);
886 return sprintf(buf, "%s\n", eattr->str);
887}
888
889#define I915_PMU_FORMAT_ATTR(_name, _config) \
890 (&((struct i915_str_attribute[]) { \
891 { .attr = __ATTR(_name, 0444, i915_pmu_format_show, NULL), \
892 .str = _config, } \
893 })[0].attr.attr)
894
895static struct attribute *i915_pmu_format_attrs[] = {
896 I915_PMU_FORMAT_ATTR(i915_eventid, "config:0-20"),
897 NULL,
898};
899
900static const struct attribute_group i915_pmu_format_attr_group = {
901 .name = "format",
902 .attrs = i915_pmu_format_attrs,
903};
904
905struct i915_ext_attribute {
906 struct device_attribute attr;
907 unsigned long val;
908};
909
910static ssize_t i915_pmu_event_show(struct device *dev,
911 struct device_attribute *attr, char *buf)
912{
913 struct i915_ext_attribute *eattr;
914
915 eattr = container_of(attr, struct i915_ext_attribute, attr);
916 return sprintf(buf, "config=0x%lx\n", eattr->val);
917}
918
919static ssize_t cpumask_show(struct device *dev,
920 struct device_attribute *attr, char *buf)
921{
922 return cpumap_print_to_pagebuf(true, buf, &i915_pmu_cpumask);
923}
924
925static DEVICE_ATTR_RO(cpumask);
926
927static struct attribute *i915_cpumask_attrs[] = {
928 &dev_attr_cpumask.attr,
929 NULL,
930};
931
932static const struct attribute_group i915_pmu_cpumask_attr_group = {
933 .attrs = i915_cpumask_attrs,
934};
935
936#define __event(__counter, __name, __unit) \
937{ \
938 .counter = (__counter), \
939 .name = (__name), \
940 .unit = (__unit), \
941 .global = false, \
942}
943
944#define __global_event(__counter, __name, __unit) \
945{ \
946 .counter = (__counter), \
947 .name = (__name), \
948 .unit = (__unit), \
949 .global = true, \
950}
951
952#define __engine_event(__sample, __name) \
953{ \
954 .sample = (__sample), \
955 .name = (__name), \
956}
957
958static struct i915_ext_attribute *
959add_i915_attr(struct i915_ext_attribute *attr, const char *name, u64 config)
960{
961 sysfs_attr_init(&attr->attr.attr);
962 attr->attr.attr.name = name;
963 attr->attr.attr.mode = 0444;
964 attr->attr.show = i915_pmu_event_show;
965 attr->val = config;
966
967 return ++attr;
968}
969
970static struct perf_pmu_events_attr *
971add_pmu_attr(struct perf_pmu_events_attr *attr, const char *name,
972 const char *str)
973{
974 sysfs_attr_init(&attr->attr.attr);
975 attr->attr.attr.name = name;
976 attr->attr.attr.mode = 0444;
977 attr->attr.show = perf_event_sysfs_show;
978 attr->event_str = str;
979
980 return ++attr;
981}
982
983static struct attribute **
984create_event_attributes(struct i915_pmu *pmu)
985{
986 struct drm_i915_private *i915 = pmu_to_i915(pmu);
987 static const struct {
988 unsigned int counter;
989 const char *name;
990 const char *unit;
991 bool global;
992 } events[] = {
993 __event(0, "actual-frequency", "M"),
994 __event(1, "requested-frequency", "M"),
995 __global_event(2, "interrupts", NULL),
996 __event(3, "rc6-residency", "ns"),
997 __event(4, "software-gt-awake-time", "ns"),
998 };
999 static const struct {
1000 enum drm_i915_pmu_engine_sample sample;
1001 char *name;
1002 } engine_events[] = {
1003 __engine_event(I915_SAMPLE_BUSY, "busy"),
1004 __engine_event(I915_SAMPLE_SEMA, "sema"),
1005 __engine_event(I915_SAMPLE_WAIT, "wait"),
1006 };
1007 unsigned int count = 0;
1008 struct perf_pmu_events_attr *pmu_attr = NULL, *pmu_iter;
1009 struct i915_ext_attribute *i915_attr = NULL, *i915_iter;
1010 struct attribute **attr = NULL, **attr_iter;
1011 struct intel_engine_cs *engine;
1012 struct intel_gt *gt;
1013 unsigned int i, j;
1014
1015 /* Count how many counters we will be exposing. */
1016 for_each_gt(gt, i915, j) {
1017 for (i = 0; i < ARRAY_SIZE(events); i++) {
1018 u64 config = ___I915_PMU_OTHER(j, events[i].counter);
1019
1020 if (!config_status(i915, config))
1021 count++;
1022 }
1023 }
1024
1025 for_each_uabi_engine(engine, i915) {
1026 for (i = 0; i < ARRAY_SIZE(engine_events); i++) {
1027 if (!engine_event_status(engine,
1028 engine_events[i].sample))
1029 count++;
1030 }
1031 }
1032
1033 /* Allocate attribute objects and table. */
1034 i915_attr = kcalloc(count, sizeof(*i915_attr), GFP_KERNEL);
1035 if (!i915_attr)
1036 goto err_alloc;
1037
1038 pmu_attr = kcalloc(count, sizeof(*pmu_attr), GFP_KERNEL);
1039 if (!pmu_attr)
1040 goto err_alloc;
1041
1042 /* Max one pointer of each attribute type plus a termination entry. */
1043 attr = kcalloc(count * 2 + 1, sizeof(*attr), GFP_KERNEL);
1044 if (!attr)
1045 goto err_alloc;
1046
1047 i915_iter = i915_attr;
1048 pmu_iter = pmu_attr;
1049 attr_iter = attr;
1050
1051 /* Initialize supported non-engine counters. */
1052 for_each_gt(gt, i915, j) {
1053 for (i = 0; i < ARRAY_SIZE(events); i++) {
1054 u64 config = ___I915_PMU_OTHER(j, events[i].counter);
1055 char *str;
1056
1057 if (config_status(i915, config))
1058 continue;
1059
1060 if (events[i].global || !HAS_EXTRA_GT_LIST(i915))
1061 str = kstrdup(events[i].name, GFP_KERNEL);
1062 else
1063 str = kasprintf(GFP_KERNEL, "%s-gt%u",
1064 events[i].name, j);
1065 if (!str)
1066 goto err;
1067
1068 *attr_iter++ = &i915_iter->attr.attr;
1069 i915_iter = add_i915_attr(i915_iter, str, config);
1070
1071 if (events[i].unit) {
1072 if (events[i].global || !HAS_EXTRA_GT_LIST(i915))
1073 str = kasprintf(GFP_KERNEL, "%s.unit",
1074 events[i].name);
1075 else
1076 str = kasprintf(GFP_KERNEL, "%s-gt%u.unit",
1077 events[i].name, j);
1078 if (!str)
1079 goto err;
1080
1081 *attr_iter++ = &pmu_iter->attr.attr;
1082 pmu_iter = add_pmu_attr(pmu_iter, str,
1083 events[i].unit);
1084 }
1085 }
1086 }
1087
1088 /* Initialize supported engine counters. */
1089 for_each_uabi_engine(engine, i915) {
1090 for (i = 0; i < ARRAY_SIZE(engine_events); i++) {
1091 char *str;
1092
1093 if (engine_event_status(engine,
1094 engine_events[i].sample))
1095 continue;
1096
1097 str = kasprintf(GFP_KERNEL, "%s-%s",
1098 engine->name, engine_events[i].name);
1099 if (!str)
1100 goto err;
1101
1102 *attr_iter++ = &i915_iter->attr.attr;
1103 i915_iter =
1104 add_i915_attr(i915_iter, str,
1105 __I915_PMU_ENGINE(engine->uabi_class,
1106 engine->uabi_instance,
1107 engine_events[i].sample));
1108
1109 str = kasprintf(GFP_KERNEL, "%s-%s.unit",
1110 engine->name, engine_events[i].name);
1111 if (!str)
1112 goto err;
1113
1114 *attr_iter++ = &pmu_iter->attr.attr;
1115 pmu_iter = add_pmu_attr(pmu_iter, str, "ns");
1116 }
1117 }
1118
1119 pmu->i915_attr = i915_attr;
1120 pmu->pmu_attr = pmu_attr;
1121
1122 return attr;
1123
1124err:;
1125 for (attr_iter = attr; *attr_iter; attr_iter++)
1126 kfree((*attr_iter)->name);
1127
1128err_alloc:
1129 kfree(attr);
1130 kfree(i915_attr);
1131 kfree(pmu_attr);
1132
1133 return NULL;
1134}
1135
1136static void free_event_attributes(struct i915_pmu *pmu)
1137{
1138 struct attribute **attr_iter = pmu->events_attr_group.attrs;
1139
1140 for (; *attr_iter; attr_iter++)
1141 kfree((*attr_iter)->name);
1142
1143 kfree(pmu->events_attr_group.attrs);
1144 kfree(pmu->i915_attr);
1145 kfree(pmu->pmu_attr);
1146
1147 pmu->events_attr_group.attrs = NULL;
1148 pmu->i915_attr = NULL;
1149 pmu->pmu_attr = NULL;
1150}
1151
1152static int i915_pmu_cpu_online(unsigned int cpu, struct hlist_node *node)
1153{
1154 struct i915_pmu *pmu = hlist_entry_safe(node, typeof(*pmu), cpuhp.node);
1155
1156 GEM_BUG_ON(!pmu->base.event_init);
1157
1158 /* Select the first online CPU as a designated reader. */
1159 if (cpumask_empty(&i915_pmu_cpumask))
1160 cpumask_set_cpu(cpu, &i915_pmu_cpumask);
1161
1162 return 0;
1163}
1164
1165static int i915_pmu_cpu_offline(unsigned int cpu, struct hlist_node *node)
1166{
1167 struct i915_pmu *pmu = hlist_entry_safe(node, typeof(*pmu), cpuhp.node);
1168 unsigned int target = i915_pmu_target_cpu;
1169
1170 GEM_BUG_ON(!pmu->base.event_init);
1171
1172 /*
1173 * Unregistering an instance generates a CPU offline event which we must
1174 * ignore to avoid incorrectly modifying the shared i915_pmu_cpumask.
1175 */
1176 if (pmu->closed)
1177 return 0;
1178
1179 if (cpumask_test_and_clear_cpu(cpu, &i915_pmu_cpumask)) {
1180 target = cpumask_any_but(topology_sibling_cpumask(cpu), cpu);
1181
1182 /* Migrate events if there is a valid target */
1183 if (target < nr_cpu_ids) {
1184 cpumask_set_cpu(target, &i915_pmu_cpumask);
1185 i915_pmu_target_cpu = target;
1186 }
1187 }
1188
1189 if (target < nr_cpu_ids && target != pmu->cpuhp.cpu) {
1190 perf_pmu_migrate_context(&pmu->base, cpu, target);
1191 pmu->cpuhp.cpu = target;
1192 }
1193
1194 return 0;
1195}
1196
1197static enum cpuhp_state cpuhp_slot = CPUHP_INVALID;
1198
1199int i915_pmu_init(void)
1200{
1201 int ret;
1202
1203 ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN,
1204 "perf/x86/intel/i915:online",
1205 i915_pmu_cpu_online,
1206 i915_pmu_cpu_offline);
1207 if (ret < 0)
1208 pr_notice("Failed to setup cpuhp state for i915 PMU! (%d)\n",
1209 ret);
1210 else
1211 cpuhp_slot = ret;
1212
1213 return 0;
1214}
1215
1216void i915_pmu_exit(void)
1217{
1218 if (cpuhp_slot != CPUHP_INVALID)
1219 cpuhp_remove_multi_state(cpuhp_slot);
1220}
1221
1222static int i915_pmu_register_cpuhp_state(struct i915_pmu *pmu)
1223{
1224 if (cpuhp_slot == CPUHP_INVALID)
1225 return -EINVAL;
1226
1227 return cpuhp_state_add_instance(cpuhp_slot, &pmu->cpuhp.node);
1228}
1229
1230static void i915_pmu_unregister_cpuhp_state(struct i915_pmu *pmu)
1231{
1232 cpuhp_state_remove_instance(cpuhp_slot, &pmu->cpuhp.node);
1233}
1234
1235static bool is_igp(struct drm_i915_private *i915)
1236{
1237 struct pci_dev *pdev = to_pci_dev(i915->drm.dev);
1238
1239 /* IGP is 0000:00:02.0 */
1240 return pci_domain_nr(pdev->bus) == 0 &&
1241 pdev->bus->number == 0 &&
1242 PCI_SLOT(pdev->devfn) == 2 &&
1243 PCI_FUNC(pdev->devfn) == 0;
1244}
1245
1246void i915_pmu_register(struct drm_i915_private *i915)
1247{
1248 struct i915_pmu *pmu = &i915->pmu;
1249 const struct attribute_group *attr_groups[] = {
1250 &i915_pmu_format_attr_group,
1251 &pmu->events_attr_group,
1252 &i915_pmu_cpumask_attr_group,
1253 NULL
1254 };
1255
1256 int ret = -ENOMEM;
1257
1258 if (GRAPHICS_VER(i915) <= 2) {
1259 drm_info(&i915->drm, "PMU not supported for this GPU.");
1260 return;
1261 }
1262
1263 spin_lock_init(&pmu->lock);
1264 hrtimer_init(&pmu->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1265 pmu->timer.function = i915_sample;
1266 pmu->cpuhp.cpu = -1;
1267 init_rc6(pmu);
1268
1269 if (!is_igp(i915)) {
1270 pmu->name = kasprintf(GFP_KERNEL,
1271 "i915_%s",
1272 dev_name(i915->drm.dev));
1273 if (pmu->name) {
1274 /* tools/perf reserves colons as special. */
1275 strreplace((char *)pmu->name, ':', '_');
1276 }
1277 } else {
1278 pmu->name = "i915";
1279 }
1280 if (!pmu->name)
1281 goto err;
1282
1283 pmu->events_attr_group.name = "events";
1284 pmu->events_attr_group.attrs = create_event_attributes(pmu);
1285 if (!pmu->events_attr_group.attrs)
1286 goto err_name;
1287
1288 pmu->base.attr_groups = kmemdup(attr_groups, sizeof(attr_groups),
1289 GFP_KERNEL);
1290 if (!pmu->base.attr_groups)
1291 goto err_attr;
1292
1293 pmu->base.module = THIS_MODULE;
1294 pmu->base.task_ctx_nr = perf_invalid_context;
1295 pmu->base.event_init = i915_pmu_event_init;
1296 pmu->base.add = i915_pmu_event_add;
1297 pmu->base.del = i915_pmu_event_del;
1298 pmu->base.start = i915_pmu_event_start;
1299 pmu->base.stop = i915_pmu_event_stop;
1300 pmu->base.read = i915_pmu_event_read;
1301 pmu->base.event_idx = i915_pmu_event_event_idx;
1302
1303 ret = perf_pmu_register(&pmu->base, pmu->name, -1);
1304 if (ret)
1305 goto err_groups;
1306
1307 ret = i915_pmu_register_cpuhp_state(pmu);
1308 if (ret)
1309 goto err_unreg;
1310
1311 return;
1312
1313err_unreg:
1314 perf_pmu_unregister(&pmu->base);
1315err_groups:
1316 kfree(pmu->base.attr_groups);
1317err_attr:
1318 pmu->base.event_init = NULL;
1319 free_event_attributes(pmu);
1320err_name:
1321 if (!is_igp(i915))
1322 kfree(pmu->name);
1323err:
1324 drm_notice(&i915->drm, "Failed to register PMU!\n");
1325}
1326
1327void i915_pmu_unregister(struct drm_i915_private *i915)
1328{
1329 struct i915_pmu *pmu = &i915->pmu;
1330
1331 if (!pmu->base.event_init)
1332 return;
1333
1334 /*
1335 * "Disconnect" the PMU callbacks - since all are atomic synchronize_rcu
1336 * ensures all currently executing ones will have exited before we
1337 * proceed with unregistration.
1338 */
1339 pmu->closed = true;
1340 synchronize_rcu();
1341
1342 hrtimer_cancel(&pmu->timer);
1343
1344 i915_pmu_unregister_cpuhp_state(pmu);
1345
1346 perf_pmu_unregister(&pmu->base);
1347 pmu->base.event_init = NULL;
1348 kfree(pmu->base.attr_groups);
1349 if (!is_igp(i915))
1350 kfree(pmu->name);
1351 free_event_attributes(pmu);
1352}