2 * SPDX-License-Identifier: MIT
4 * Copyright © 2017-2018 Intel Corporation
7 #include <linux/pm_runtime.h>
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_pm.h"
14 #include "gt/intel_gt_regs.h"
15 #include "gt/intel_rc6.h"
16 #include "gt/intel_rps.h"
22 /* Frequency for the sampling timer for events which need it. */
24 #define PERIOD max_t(u64, 10000, NSEC_PER_SEC / FREQUENCY)
26 #define ENGINE_SAMPLE_MASK \
27 (BIT(I915_SAMPLE_BUSY) | \
28 BIT(I915_SAMPLE_WAIT) | \
29 BIT(I915_SAMPLE_SEMA))
31 static cpumask_t i915_pmu_cpumask;
32 static unsigned int i915_pmu_target_cpu = -1;
34 static u8 engine_config_sample(u64 config)
36 return config & I915_PMU_SAMPLE_MASK;
39 static u8 engine_event_sample(struct perf_event *event)
41 return engine_config_sample(event->attr.config);
44 static u8 engine_event_class(struct perf_event *event)
46 return (event->attr.config >> I915_PMU_CLASS_SHIFT) & 0xff;
49 static u8 engine_event_instance(struct perf_event *event)
51 return (event->attr.config >> I915_PMU_SAMPLE_BITS) & 0xff;
54 static bool is_engine_config(u64 config)
56 return config < __I915_PMU_OTHER(0);
59 static unsigned int other_bit(const u64 config)
64 case I915_PMU_ACTUAL_FREQUENCY:
65 val = __I915_PMU_ACTUAL_FREQUENCY_ENABLED;
67 case I915_PMU_REQUESTED_FREQUENCY:
68 val = __I915_PMU_REQUESTED_FREQUENCY_ENABLED;
70 case I915_PMU_RC6_RESIDENCY:
71 val = __I915_PMU_RC6_RESIDENCY_ENABLED;
75 * Events that do not require sampling, or tracking state
76 * transitions between enabled and disabled can be ignored.
81 return I915_ENGINE_SAMPLE_COUNT + val;
84 static unsigned int config_bit(const u64 config)
86 if (is_engine_config(config))
87 return engine_config_sample(config);
89 return other_bit(config);
92 static u64 config_mask(u64 config)
94 return BIT_ULL(config_bit(config));
97 static bool is_engine_event(struct perf_event *event)
99 return is_engine_config(event->attr.config);
102 static unsigned int event_bit(struct perf_event *event)
104 return config_bit(event->attr.config);
107 static bool pmu_needs_timer(struct i915_pmu *pmu, bool gpu_active)
109 struct drm_i915_private *i915 = container_of(pmu, typeof(*i915), pmu);
113 * Only some counters need the sampling timer.
115 * We start with a bitmask of all currently enabled events.
117 enable = pmu->enable;
120 * Mask out all the ones which do not need the timer, or in
121 * other words keep all the ones that could need the timer.
123 enable &= config_mask(I915_PMU_ACTUAL_FREQUENCY) |
124 config_mask(I915_PMU_REQUESTED_FREQUENCY) |
128 * When the GPU is idle per-engine counters do not need to be
129 * running so clear those bits out.
132 enable &= ~ENGINE_SAMPLE_MASK;
134 * Also there is software busyness tracking available we do not
135 * need the timer for I915_SAMPLE_BUSY counter.
137 else if (i915->caps.scheduler & I915_SCHEDULER_CAP_ENGINE_BUSY_STATS)
138 enable &= ~BIT(I915_SAMPLE_BUSY);
141 * If some bits remain it means we need the sampling timer running.
146 static u64 __get_rc6(struct intel_gt *gt)
148 struct drm_i915_private *i915 = gt->i915;
151 val = intel_rc6_residency_ns(>->rc6, GEN6_GT_GFX_RC6);
154 val += intel_rc6_residency_ns(>->rc6, GEN6_GT_GFX_RC6p);
157 val += intel_rc6_residency_ns(>->rc6, GEN6_GT_GFX_RC6pp);
162 static inline s64 ktime_since_raw(const ktime_t kt)
164 return ktime_to_ns(ktime_sub(ktime_get_raw(), kt));
167 static u64 get_rc6(struct intel_gt *gt)
169 struct drm_i915_private *i915 = gt->i915;
170 struct i915_pmu *pmu = &i915->pmu;
175 if (intel_gt_pm_get_if_awake(gt)) {
177 intel_gt_pm_put_async(gt);
181 spin_lock_irqsave(&pmu->lock, flags);
184 pmu->sample[__I915_SAMPLE_RC6].cur = val;
187 * We think we are runtime suspended.
189 * Report the delta from when the device was suspended to now,
190 * on top of the last known real value, as the approximated RC6
193 val = ktime_since_raw(pmu->sleep_last);
194 val += pmu->sample[__I915_SAMPLE_RC6].cur;
197 if (val < pmu->sample[__I915_SAMPLE_RC6_LAST_REPORTED].cur)
198 val = pmu->sample[__I915_SAMPLE_RC6_LAST_REPORTED].cur;
200 pmu->sample[__I915_SAMPLE_RC6_LAST_REPORTED].cur = val;
202 spin_unlock_irqrestore(&pmu->lock, flags);
207 static void init_rc6(struct i915_pmu *pmu)
209 struct drm_i915_private *i915 = container_of(pmu, typeof(*i915), pmu);
210 intel_wakeref_t wakeref;
212 with_intel_runtime_pm(to_gt(i915)->uncore->rpm, wakeref) {
213 pmu->sample[__I915_SAMPLE_RC6].cur = __get_rc6(to_gt(i915));
214 pmu->sample[__I915_SAMPLE_RC6_LAST_REPORTED].cur =
215 pmu->sample[__I915_SAMPLE_RC6].cur;
216 pmu->sleep_last = ktime_get_raw();
220 static void park_rc6(struct drm_i915_private *i915)
222 struct i915_pmu *pmu = &i915->pmu;
224 pmu->sample[__I915_SAMPLE_RC6].cur = __get_rc6(to_gt(i915));
225 pmu->sleep_last = ktime_get_raw();
228 static void __i915_pmu_maybe_start_timer(struct i915_pmu *pmu)
230 if (!pmu->timer_enabled && pmu_needs_timer(pmu, true)) {
231 pmu->timer_enabled = true;
232 pmu->timer_last = ktime_get();
233 hrtimer_start_range_ns(&pmu->timer,
234 ns_to_ktime(PERIOD), 0,
235 HRTIMER_MODE_REL_PINNED);
239 void i915_pmu_gt_parked(struct drm_i915_private *i915)
241 struct i915_pmu *pmu = &i915->pmu;
243 if (!pmu->base.event_init)
246 spin_lock_irq(&pmu->lock);
251 * Signal sampling timer to stop if only engine events are enabled and
254 pmu->timer_enabled = pmu_needs_timer(pmu, false);
256 spin_unlock_irq(&pmu->lock);
259 void i915_pmu_gt_unparked(struct drm_i915_private *i915)
261 struct i915_pmu *pmu = &i915->pmu;
263 if (!pmu->base.event_init)
266 spin_lock_irq(&pmu->lock);
269 * Re-enable sampling timer when GPU goes active.
271 __i915_pmu_maybe_start_timer(pmu);
273 spin_unlock_irq(&pmu->lock);
277 add_sample(struct i915_pmu_sample *sample, u32 val)
282 static bool exclusive_mmio_access(const struct drm_i915_private *i915)
285 * We have to avoid concurrent mmio cache line access on gen7 or
286 * risk a machine hang. For a fun history lesson dig out the old
287 * userspace intel_gpu_top and run it on Ivybridge or Haswell!
289 return GRAPHICS_VER(i915) == 7;
292 static void engine_sample(struct intel_engine_cs *engine, unsigned int period_ns)
294 struct intel_engine_pmu *pmu = &engine->pmu;
298 val = ENGINE_READ_FW(engine, RING_CTL);
299 if (val == 0) /* powerwell off => engine idle */
303 add_sample(&pmu->sample[I915_SAMPLE_WAIT], period_ns);
304 if (val & RING_WAIT_SEMAPHORE)
305 add_sample(&pmu->sample[I915_SAMPLE_SEMA], period_ns);
307 /* No need to sample when busy stats are supported. */
308 if (intel_engine_supports_stats(engine))
312 * While waiting on a semaphore or event, MI_MODE reports the
313 * ring as idle. However, previously using the seqno, and with
314 * execlists sampling, we account for the ring waiting as the
315 * engine being busy. Therefore, we record the sample as being
316 * busy if either waiting or !idle.
318 busy = val & (RING_WAIT_SEMAPHORE | RING_WAIT);
320 val = ENGINE_READ_FW(engine, RING_MI_MODE);
321 busy = !(val & MODE_IDLE);
324 add_sample(&pmu->sample[I915_SAMPLE_BUSY], period_ns);
328 engines_sample(struct intel_gt *gt, unsigned int period_ns)
330 struct drm_i915_private *i915 = gt->i915;
331 struct intel_engine_cs *engine;
332 enum intel_engine_id id;
335 if ((i915->pmu.enable & ENGINE_SAMPLE_MASK) == 0)
338 if (!intel_gt_pm_is_awake(gt))
341 for_each_engine(engine, gt, id) {
342 if (!intel_engine_pm_get_if_awake(engine))
345 if (exclusive_mmio_access(i915)) {
346 spin_lock_irqsave(&engine->uncore->lock, flags);
347 engine_sample(engine, period_ns);
348 spin_unlock_irqrestore(&engine->uncore->lock, flags);
350 engine_sample(engine, period_ns);
353 intel_engine_pm_put_async(engine);
358 add_sample_mult(struct i915_pmu_sample *sample, u32 val, u32 mul)
360 sample->cur += mul_u32_u32(val, mul);
363 static bool frequency_sampling_enabled(struct i915_pmu *pmu)
366 (config_mask(I915_PMU_ACTUAL_FREQUENCY) |
367 config_mask(I915_PMU_REQUESTED_FREQUENCY));
371 frequency_sample(struct intel_gt *gt, unsigned int period_ns)
373 struct drm_i915_private *i915 = gt->i915;
374 struct intel_uncore *uncore = gt->uncore;
375 struct i915_pmu *pmu = &i915->pmu;
376 struct intel_rps *rps = >->rps;
378 if (!frequency_sampling_enabled(pmu))
381 /* Report 0/0 (actual/requested) frequency while parked. */
382 if (!intel_gt_pm_get_if_awake(gt))
385 if (pmu->enable & config_mask(I915_PMU_ACTUAL_FREQUENCY)) {
389 * We take a quick peek here without using forcewake
390 * so that we don't perturb the system under observation
391 * (forcewake => !rc6 => increased power use). We expect
392 * that if the read fails because it is outside of the
393 * mmio power well, then it will return 0 -- in which
394 * case we assume the system is running at the intended
395 * frequency. Fortunately, the read should rarely fail!
397 val = intel_uncore_read_fw(uncore, GEN6_RPSTAT1);
399 val = intel_rps_get_cagf(rps, val);
403 add_sample_mult(&pmu->sample[__I915_SAMPLE_FREQ_ACT],
404 intel_gpu_freq(rps, val), period_ns / 1000);
407 if (pmu->enable & config_mask(I915_PMU_REQUESTED_FREQUENCY)) {
408 add_sample_mult(&pmu->sample[__I915_SAMPLE_FREQ_REQ],
409 intel_rps_get_requested_frequency(rps),
413 intel_gt_pm_put_async(gt);
416 static enum hrtimer_restart i915_sample(struct hrtimer *hrtimer)
418 struct drm_i915_private *i915 =
419 container_of(hrtimer, struct drm_i915_private, pmu.timer);
420 struct i915_pmu *pmu = &i915->pmu;
421 struct intel_gt *gt = to_gt(i915);
422 unsigned int period_ns;
425 if (!READ_ONCE(pmu->timer_enabled))
426 return HRTIMER_NORESTART;
429 period_ns = ktime_to_ns(ktime_sub(now, pmu->timer_last));
430 pmu->timer_last = now;
433 * Strictly speaking the passed in period may not be 100% accurate for
434 * all internal calculation, since some amount of time can be spent on
435 * grabbing the forcewake. However the potential error from timer call-
436 * back delay greatly dominates this so we keep it simple.
438 engines_sample(gt, period_ns);
439 frequency_sample(gt, period_ns);
441 hrtimer_forward(hrtimer, now, ns_to_ktime(PERIOD));
443 return HRTIMER_RESTART;
446 static void i915_pmu_event_destroy(struct perf_event *event)
448 struct drm_i915_private *i915 =
449 container_of(event->pmu, typeof(*i915), pmu.base);
451 drm_WARN_ON(&i915->drm, event->parent);
453 drm_dev_put(&i915->drm);
457 engine_event_status(struct intel_engine_cs *engine,
458 enum drm_i915_pmu_engine_sample sample)
461 case I915_SAMPLE_BUSY:
462 case I915_SAMPLE_WAIT:
464 case I915_SAMPLE_SEMA:
465 if (GRAPHICS_VER(engine->i915) < 6)
476 config_status(struct drm_i915_private *i915, u64 config)
478 struct intel_gt *gt = to_gt(i915);
481 case I915_PMU_ACTUAL_FREQUENCY:
482 if (IS_VALLEYVIEW(i915) || IS_CHERRYVIEW(i915))
483 /* Requires a mutex for sampling! */
486 case I915_PMU_REQUESTED_FREQUENCY:
487 if (GRAPHICS_VER(i915) < 6)
490 case I915_PMU_INTERRUPTS:
492 case I915_PMU_RC6_RESIDENCY:
493 if (!gt->rc6.supported)
496 case I915_PMU_SOFTWARE_GT_AWAKE_TIME:
505 static int engine_event_init(struct perf_event *event)
507 struct drm_i915_private *i915 =
508 container_of(event->pmu, typeof(*i915), pmu.base);
509 struct intel_engine_cs *engine;
511 engine = intel_engine_lookup_user(i915, engine_event_class(event),
512 engine_event_instance(event));
516 return engine_event_status(engine, engine_event_sample(event));
519 static int i915_pmu_event_init(struct perf_event *event)
521 struct drm_i915_private *i915 =
522 container_of(event->pmu, typeof(*i915), pmu.base);
523 struct i915_pmu *pmu = &i915->pmu;
529 if (event->attr.type != event->pmu->type)
532 /* unsupported modes and filters */
533 if (event->attr.sample_period) /* no sampling */
536 if (has_branch_stack(event))
542 /* only allow running on one cpu at a time */
543 if (!cpumask_test_cpu(event->cpu, &i915_pmu_cpumask))
546 if (is_engine_event(event))
547 ret = engine_event_init(event);
549 ret = config_status(i915, event->attr.config);
553 if (!event->parent) {
554 drm_dev_get(&i915->drm);
555 event->destroy = i915_pmu_event_destroy;
561 static u64 __i915_pmu_event_read(struct perf_event *event)
563 struct drm_i915_private *i915 =
564 container_of(event->pmu, typeof(*i915), pmu.base);
565 struct i915_pmu *pmu = &i915->pmu;
568 if (is_engine_event(event)) {
569 u8 sample = engine_event_sample(event);
570 struct intel_engine_cs *engine;
572 engine = intel_engine_lookup_user(i915,
573 engine_event_class(event),
574 engine_event_instance(event));
576 if (drm_WARN_ON_ONCE(&i915->drm, !engine)) {
578 } else if (sample == I915_SAMPLE_BUSY &&
579 intel_engine_supports_stats(engine)) {
582 val = ktime_to_ns(intel_engine_get_busy_time(engine,
585 val = engine->pmu.sample[sample].cur;
588 switch (event->attr.config) {
589 case I915_PMU_ACTUAL_FREQUENCY:
591 div_u64(pmu->sample[__I915_SAMPLE_FREQ_ACT].cur,
592 USEC_PER_SEC /* to MHz */);
594 case I915_PMU_REQUESTED_FREQUENCY:
596 div_u64(pmu->sample[__I915_SAMPLE_FREQ_REQ].cur,
597 USEC_PER_SEC /* to MHz */);
599 case I915_PMU_INTERRUPTS:
600 val = READ_ONCE(pmu->irq_count);
602 case I915_PMU_RC6_RESIDENCY:
603 val = get_rc6(to_gt(i915));
605 case I915_PMU_SOFTWARE_GT_AWAKE_TIME:
606 val = ktime_to_ns(intel_gt_get_awake_time(to_gt(i915)));
614 static void i915_pmu_event_read(struct perf_event *event)
616 struct drm_i915_private *i915 =
617 container_of(event->pmu, typeof(*i915), pmu.base);
618 struct hw_perf_event *hwc = &event->hw;
619 struct i915_pmu *pmu = &i915->pmu;
623 event->hw.state = PERF_HES_STOPPED;
627 prev = local64_read(&hwc->prev_count);
628 new = __i915_pmu_event_read(event);
630 if (local64_cmpxchg(&hwc->prev_count, prev, new) != prev)
633 local64_add(new - prev, &event->count);
636 static void i915_pmu_enable(struct perf_event *event)
638 struct drm_i915_private *i915 =
639 container_of(event->pmu, typeof(*i915), pmu.base);
640 struct i915_pmu *pmu = &i915->pmu;
644 bit = event_bit(event);
648 spin_lock_irqsave(&pmu->lock, flags);
651 * Update the bitmask of enabled events and increment
652 * the event reference counter.
654 BUILD_BUG_ON(ARRAY_SIZE(pmu->enable_count) != I915_PMU_MASK_BITS);
655 GEM_BUG_ON(bit >= ARRAY_SIZE(pmu->enable_count));
656 GEM_BUG_ON(pmu->enable_count[bit] == ~0);
658 pmu->enable |= BIT_ULL(bit);
659 pmu->enable_count[bit]++;
662 * Start the sampling timer if needed and not already enabled.
664 __i915_pmu_maybe_start_timer(pmu);
667 * For per-engine events the bitmask and reference counting
668 * is stored per engine.
670 if (is_engine_event(event)) {
671 u8 sample = engine_event_sample(event);
672 struct intel_engine_cs *engine;
674 engine = intel_engine_lookup_user(i915,
675 engine_event_class(event),
676 engine_event_instance(event));
678 BUILD_BUG_ON(ARRAY_SIZE(engine->pmu.enable_count) !=
679 I915_ENGINE_SAMPLE_COUNT);
680 BUILD_BUG_ON(ARRAY_SIZE(engine->pmu.sample) !=
681 I915_ENGINE_SAMPLE_COUNT);
682 GEM_BUG_ON(sample >= ARRAY_SIZE(engine->pmu.enable_count));
683 GEM_BUG_ON(sample >= ARRAY_SIZE(engine->pmu.sample));
684 GEM_BUG_ON(engine->pmu.enable_count[sample] == ~0);
686 engine->pmu.enable |= BIT(sample);
687 engine->pmu.enable_count[sample]++;
690 spin_unlock_irqrestore(&pmu->lock, flags);
694 * Store the current counter value so we can report the correct delta
695 * for all listeners. Even when the event was already enabled and has
696 * an existing non-zero value.
698 local64_set(&event->hw.prev_count, __i915_pmu_event_read(event));
701 static void i915_pmu_disable(struct perf_event *event)
703 struct drm_i915_private *i915 =
704 container_of(event->pmu, typeof(*i915), pmu.base);
705 unsigned int bit = event_bit(event);
706 struct i915_pmu *pmu = &i915->pmu;
712 spin_lock_irqsave(&pmu->lock, flags);
714 if (is_engine_event(event)) {
715 u8 sample = engine_event_sample(event);
716 struct intel_engine_cs *engine;
718 engine = intel_engine_lookup_user(i915,
719 engine_event_class(event),
720 engine_event_instance(event));
722 GEM_BUG_ON(sample >= ARRAY_SIZE(engine->pmu.enable_count));
723 GEM_BUG_ON(sample >= ARRAY_SIZE(engine->pmu.sample));
724 GEM_BUG_ON(engine->pmu.enable_count[sample] == 0);
727 * Decrement the reference count and clear the enabled
728 * bitmask when the last listener on an event goes away.
730 if (--engine->pmu.enable_count[sample] == 0)
731 engine->pmu.enable &= ~BIT(sample);
734 GEM_BUG_ON(bit >= ARRAY_SIZE(pmu->enable_count));
735 GEM_BUG_ON(pmu->enable_count[bit] == 0);
737 * Decrement the reference count and clear the enabled
738 * bitmask when the last listener on an event goes away.
740 if (--pmu->enable_count[bit] == 0) {
741 pmu->enable &= ~BIT_ULL(bit);
742 pmu->timer_enabled &= pmu_needs_timer(pmu, true);
745 spin_unlock_irqrestore(&pmu->lock, flags);
748 static void i915_pmu_event_start(struct perf_event *event, int flags)
750 struct drm_i915_private *i915 =
751 container_of(event->pmu, typeof(*i915), pmu.base);
752 struct i915_pmu *pmu = &i915->pmu;
757 i915_pmu_enable(event);
761 static void i915_pmu_event_stop(struct perf_event *event, int flags)
763 if (flags & PERF_EF_UPDATE)
764 i915_pmu_event_read(event);
765 i915_pmu_disable(event);
766 event->hw.state = PERF_HES_STOPPED;
769 static int i915_pmu_event_add(struct perf_event *event, int flags)
771 struct drm_i915_private *i915 =
772 container_of(event->pmu, typeof(*i915), pmu.base);
773 struct i915_pmu *pmu = &i915->pmu;
778 if (flags & PERF_EF_START)
779 i915_pmu_event_start(event, flags);
784 static void i915_pmu_event_del(struct perf_event *event, int flags)
786 i915_pmu_event_stop(event, PERF_EF_UPDATE);
789 static int i915_pmu_event_event_idx(struct perf_event *event)
794 struct i915_str_attribute {
795 struct device_attribute attr;
799 static ssize_t i915_pmu_format_show(struct device *dev,
800 struct device_attribute *attr, char *buf)
802 struct i915_str_attribute *eattr;
804 eattr = container_of(attr, struct i915_str_attribute, attr);
805 return sprintf(buf, "%s\n", eattr->str);
808 #define I915_PMU_FORMAT_ATTR(_name, _config) \
809 (&((struct i915_str_attribute[]) { \
810 { .attr = __ATTR(_name, 0444, i915_pmu_format_show, NULL), \
814 static struct attribute *i915_pmu_format_attrs[] = {
815 I915_PMU_FORMAT_ATTR(i915_eventid, "config:0-20"),
819 static const struct attribute_group i915_pmu_format_attr_group = {
821 .attrs = i915_pmu_format_attrs,
824 struct i915_ext_attribute {
825 struct device_attribute attr;
829 static ssize_t i915_pmu_event_show(struct device *dev,
830 struct device_attribute *attr, char *buf)
832 struct i915_ext_attribute *eattr;
834 eattr = container_of(attr, struct i915_ext_attribute, attr);
835 return sprintf(buf, "config=0x%lx\n", eattr->val);
838 static ssize_t cpumask_show(struct device *dev,
839 struct device_attribute *attr, char *buf)
841 return cpumap_print_to_pagebuf(true, buf, &i915_pmu_cpumask);
844 static DEVICE_ATTR_RO(cpumask);
846 static struct attribute *i915_cpumask_attrs[] = {
847 &dev_attr_cpumask.attr,
851 static const struct attribute_group i915_pmu_cpumask_attr_group = {
852 .attrs = i915_cpumask_attrs,
855 #define __event(__config, __name, __unit) \
857 .config = (__config), \
862 #define __engine_event(__sample, __name) \
864 .sample = (__sample), \
868 static struct i915_ext_attribute *
869 add_i915_attr(struct i915_ext_attribute *attr, const char *name, u64 config)
871 sysfs_attr_init(&attr->attr.attr);
872 attr->attr.attr.name = name;
873 attr->attr.attr.mode = 0444;
874 attr->attr.show = i915_pmu_event_show;
880 static struct perf_pmu_events_attr *
881 add_pmu_attr(struct perf_pmu_events_attr *attr, const char *name,
884 sysfs_attr_init(&attr->attr.attr);
885 attr->attr.attr.name = name;
886 attr->attr.attr.mode = 0444;
887 attr->attr.show = perf_event_sysfs_show;
888 attr->event_str = str;
893 static struct attribute **
894 create_event_attributes(struct i915_pmu *pmu)
896 struct drm_i915_private *i915 = container_of(pmu, typeof(*i915), pmu);
897 static const struct {
902 __event(I915_PMU_ACTUAL_FREQUENCY, "actual-frequency", "M"),
903 __event(I915_PMU_REQUESTED_FREQUENCY, "requested-frequency", "M"),
904 __event(I915_PMU_INTERRUPTS, "interrupts", NULL),
905 __event(I915_PMU_RC6_RESIDENCY, "rc6-residency", "ns"),
906 __event(I915_PMU_SOFTWARE_GT_AWAKE_TIME, "software-gt-awake-time", "ns"),
908 static const struct {
909 enum drm_i915_pmu_engine_sample sample;
911 } engine_events[] = {
912 __engine_event(I915_SAMPLE_BUSY, "busy"),
913 __engine_event(I915_SAMPLE_SEMA, "sema"),
914 __engine_event(I915_SAMPLE_WAIT, "wait"),
916 unsigned int count = 0;
917 struct perf_pmu_events_attr *pmu_attr = NULL, *pmu_iter;
918 struct i915_ext_attribute *i915_attr = NULL, *i915_iter;
919 struct attribute **attr = NULL, **attr_iter;
920 struct intel_engine_cs *engine;
923 /* Count how many counters we will be exposing. */
924 for (i = 0; i < ARRAY_SIZE(events); i++) {
925 if (!config_status(i915, events[i].config))
929 for_each_uabi_engine(engine, i915) {
930 for (i = 0; i < ARRAY_SIZE(engine_events); i++) {
931 if (!engine_event_status(engine,
932 engine_events[i].sample))
937 /* Allocate attribute objects and table. */
938 i915_attr = kcalloc(count, sizeof(*i915_attr), GFP_KERNEL);
942 pmu_attr = kcalloc(count, sizeof(*pmu_attr), GFP_KERNEL);
946 /* Max one pointer of each attribute type plus a termination entry. */
947 attr = kcalloc(count * 2 + 1, sizeof(*attr), GFP_KERNEL);
951 i915_iter = i915_attr;
955 /* Initialize supported non-engine counters. */
956 for (i = 0; i < ARRAY_SIZE(events); i++) {
959 if (config_status(i915, events[i].config))
962 str = kstrdup(events[i].name, GFP_KERNEL);
966 *attr_iter++ = &i915_iter->attr.attr;
967 i915_iter = add_i915_attr(i915_iter, str, events[i].config);
969 if (events[i].unit) {
970 str = kasprintf(GFP_KERNEL, "%s.unit", events[i].name);
974 *attr_iter++ = &pmu_iter->attr.attr;
975 pmu_iter = add_pmu_attr(pmu_iter, str, events[i].unit);
979 /* Initialize supported engine counters. */
980 for_each_uabi_engine(engine, i915) {
981 for (i = 0; i < ARRAY_SIZE(engine_events); i++) {
984 if (engine_event_status(engine,
985 engine_events[i].sample))
988 str = kasprintf(GFP_KERNEL, "%s-%s",
989 engine->name, engine_events[i].name);
993 *attr_iter++ = &i915_iter->attr.attr;
995 add_i915_attr(i915_iter, str,
996 __I915_PMU_ENGINE(engine->uabi_class,
997 engine->uabi_instance,
998 engine_events[i].sample));
1000 str = kasprintf(GFP_KERNEL, "%s-%s.unit",
1001 engine->name, engine_events[i].name);
1005 *attr_iter++ = &pmu_iter->attr.attr;
1006 pmu_iter = add_pmu_attr(pmu_iter, str, "ns");
1010 pmu->i915_attr = i915_attr;
1011 pmu->pmu_attr = pmu_attr;
1016 for (attr_iter = attr; *attr_iter; attr_iter++)
1017 kfree((*attr_iter)->name);
1027 static void free_event_attributes(struct i915_pmu *pmu)
1029 struct attribute **attr_iter = pmu->events_attr_group.attrs;
1031 for (; *attr_iter; attr_iter++)
1032 kfree((*attr_iter)->name);
1034 kfree(pmu->events_attr_group.attrs);
1035 kfree(pmu->i915_attr);
1036 kfree(pmu->pmu_attr);
1038 pmu->events_attr_group.attrs = NULL;
1039 pmu->i915_attr = NULL;
1040 pmu->pmu_attr = NULL;
1043 static int i915_pmu_cpu_online(unsigned int cpu, struct hlist_node *node)
1045 struct i915_pmu *pmu = hlist_entry_safe(node, typeof(*pmu), cpuhp.node);
1047 GEM_BUG_ON(!pmu->base.event_init);
1049 /* Select the first online CPU as a designated reader. */
1050 if (cpumask_empty(&i915_pmu_cpumask))
1051 cpumask_set_cpu(cpu, &i915_pmu_cpumask);
1056 static int i915_pmu_cpu_offline(unsigned int cpu, struct hlist_node *node)
1058 struct i915_pmu *pmu = hlist_entry_safe(node, typeof(*pmu), cpuhp.node);
1059 unsigned int target = i915_pmu_target_cpu;
1061 GEM_BUG_ON(!pmu->base.event_init);
1064 * Unregistering an instance generates a CPU offline event which we must
1065 * ignore to avoid incorrectly modifying the shared i915_pmu_cpumask.
1070 if (cpumask_test_and_clear_cpu(cpu, &i915_pmu_cpumask)) {
1071 target = cpumask_any_but(topology_sibling_cpumask(cpu), cpu);
1073 /* Migrate events if there is a valid target */
1074 if (target < nr_cpu_ids) {
1075 cpumask_set_cpu(target, &i915_pmu_cpumask);
1076 i915_pmu_target_cpu = target;
1080 if (target < nr_cpu_ids && target != pmu->cpuhp.cpu) {
1081 perf_pmu_migrate_context(&pmu->base, cpu, target);
1082 pmu->cpuhp.cpu = target;
1088 static enum cpuhp_state cpuhp_slot = CPUHP_INVALID;
1090 int i915_pmu_init(void)
1094 ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN,
1095 "perf/x86/intel/i915:online",
1096 i915_pmu_cpu_online,
1097 i915_pmu_cpu_offline);
1099 pr_notice("Failed to setup cpuhp state for i915 PMU! (%d)\n",
1107 void i915_pmu_exit(void)
1109 if (cpuhp_slot != CPUHP_INVALID)
1110 cpuhp_remove_multi_state(cpuhp_slot);
1113 static int i915_pmu_register_cpuhp_state(struct i915_pmu *pmu)
1115 if (cpuhp_slot == CPUHP_INVALID)
1118 return cpuhp_state_add_instance(cpuhp_slot, &pmu->cpuhp.node);
1121 static void i915_pmu_unregister_cpuhp_state(struct i915_pmu *pmu)
1123 cpuhp_state_remove_instance(cpuhp_slot, &pmu->cpuhp.node);
1126 static bool is_igp(struct drm_i915_private *i915)
1128 struct pci_dev *pdev = to_pci_dev(i915->drm.dev);
1130 /* IGP is 0000:00:02.0 */
1131 return pci_domain_nr(pdev->bus) == 0 &&
1132 pdev->bus->number == 0 &&
1133 PCI_SLOT(pdev->devfn) == 2 &&
1134 PCI_FUNC(pdev->devfn) == 0;
1137 void i915_pmu_register(struct drm_i915_private *i915)
1139 struct i915_pmu *pmu = &i915->pmu;
1140 const struct attribute_group *attr_groups[] = {
1141 &i915_pmu_format_attr_group,
1142 &pmu->events_attr_group,
1143 &i915_pmu_cpumask_attr_group,
1149 if (GRAPHICS_VER(i915) <= 2) {
1150 drm_info(&i915->drm, "PMU not supported for this GPU.");
1154 spin_lock_init(&pmu->lock);
1155 hrtimer_init(&pmu->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1156 pmu->timer.function = i915_sample;
1157 pmu->cpuhp.cpu = -1;
1160 if (!is_igp(i915)) {
1161 pmu->name = kasprintf(GFP_KERNEL,
1163 dev_name(i915->drm.dev));
1165 /* tools/perf reserves colons as special. */
1166 strreplace((char *)pmu->name, ':', '_');
1174 pmu->events_attr_group.name = "events";
1175 pmu->events_attr_group.attrs = create_event_attributes(pmu);
1176 if (!pmu->events_attr_group.attrs)
1179 pmu->base.attr_groups = kmemdup(attr_groups, sizeof(attr_groups),
1181 if (!pmu->base.attr_groups)
1184 pmu->base.module = THIS_MODULE;
1185 pmu->base.task_ctx_nr = perf_invalid_context;
1186 pmu->base.event_init = i915_pmu_event_init;
1187 pmu->base.add = i915_pmu_event_add;
1188 pmu->base.del = i915_pmu_event_del;
1189 pmu->base.start = i915_pmu_event_start;
1190 pmu->base.stop = i915_pmu_event_stop;
1191 pmu->base.read = i915_pmu_event_read;
1192 pmu->base.event_idx = i915_pmu_event_event_idx;
1194 ret = perf_pmu_register(&pmu->base, pmu->name, -1);
1198 ret = i915_pmu_register_cpuhp_state(pmu);
1205 perf_pmu_unregister(&pmu->base);
1207 kfree(pmu->base.attr_groups);
1209 pmu->base.event_init = NULL;
1210 free_event_attributes(pmu);
1215 drm_notice(&i915->drm, "Failed to register PMU!\n");
1218 void i915_pmu_unregister(struct drm_i915_private *i915)
1220 struct i915_pmu *pmu = &i915->pmu;
1222 if (!pmu->base.event_init)
1226 * "Disconnect" the PMU callbacks - since all are atomic synchronize_rcu
1227 * ensures all currently executing ones will have exited before we
1228 * proceed with unregistration.
1233 hrtimer_cancel(&pmu->timer);
1235 i915_pmu_unregister_cpuhp_state(pmu);
1237 perf_pmu_unregister(&pmu->base);
1238 pmu->base.event_init = NULL;
1239 kfree(pmu->base.attr_groups);
1242 free_event_attributes(pmu);