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"
21 /* Frequency for the sampling timer for events which need it. */
23 #define PERIOD max_t(u64, 10000, NSEC_PER_SEC / FREQUENCY)
25 #define ENGINE_SAMPLE_MASK \
26 (BIT(I915_SAMPLE_BUSY) | \
27 BIT(I915_SAMPLE_WAIT) | \
28 BIT(I915_SAMPLE_SEMA))
30 static cpumask_t i915_pmu_cpumask;
31 static unsigned int i915_pmu_target_cpu = -1;
33 static u8 engine_config_sample(u64 config)
35 return config & I915_PMU_SAMPLE_MASK;
38 static u8 engine_event_sample(struct perf_event *event)
40 return engine_config_sample(event->attr.config);
43 static u8 engine_event_class(struct perf_event *event)
45 return (event->attr.config >> I915_PMU_CLASS_SHIFT) & 0xff;
48 static u8 engine_event_instance(struct perf_event *event)
50 return (event->attr.config >> I915_PMU_SAMPLE_BITS) & 0xff;
53 static bool is_engine_config(u64 config)
55 return config < __I915_PMU_OTHER(0);
58 static unsigned int other_bit(const u64 config)
63 case I915_PMU_ACTUAL_FREQUENCY:
64 val = __I915_PMU_ACTUAL_FREQUENCY_ENABLED;
66 case I915_PMU_REQUESTED_FREQUENCY:
67 val = __I915_PMU_REQUESTED_FREQUENCY_ENABLED;
69 case I915_PMU_RC6_RESIDENCY:
70 val = __I915_PMU_RC6_RESIDENCY_ENABLED;
74 * Events that do not require sampling, or tracking state
75 * transitions between enabled and disabled can be ignored.
80 return I915_ENGINE_SAMPLE_COUNT + val;
83 static unsigned int config_bit(const u64 config)
85 if (is_engine_config(config))
86 return engine_config_sample(config);
88 return other_bit(config);
91 static u64 config_mask(u64 config)
93 return BIT_ULL(config_bit(config));
96 static bool is_engine_event(struct perf_event *event)
98 return is_engine_config(event->attr.config);
101 static unsigned int event_bit(struct perf_event *event)
103 return config_bit(event->attr.config);
106 static bool pmu_needs_timer(struct i915_pmu *pmu, bool gpu_active)
108 struct drm_i915_private *i915 = container_of(pmu, typeof(*i915), pmu);
112 * Only some counters need the sampling timer.
114 * We start with a bitmask of all currently enabled events.
116 enable = pmu->enable;
119 * Mask out all the ones which do not need the timer, or in
120 * other words keep all the ones that could need the timer.
122 enable &= config_mask(I915_PMU_ACTUAL_FREQUENCY) |
123 config_mask(I915_PMU_REQUESTED_FREQUENCY) |
127 * When the GPU is idle per-engine counters do not need to be
128 * running so clear those bits out.
131 enable &= ~ENGINE_SAMPLE_MASK;
133 * Also there is software busyness tracking available we do not
134 * need the timer for I915_SAMPLE_BUSY counter.
136 else if (i915->caps.scheduler & I915_SCHEDULER_CAP_ENGINE_BUSY_STATS)
137 enable &= ~BIT(I915_SAMPLE_BUSY);
140 * If some bits remain it means we need the sampling timer running.
145 static u64 __get_rc6(struct intel_gt *gt)
147 struct drm_i915_private *i915 = gt->i915;
150 val = intel_rc6_residency_ns(>->rc6, INTEL_RC6_RES_RC6);
153 val += intel_rc6_residency_ns(>->rc6, INTEL_RC6_RES_RC6p);
156 val += intel_rc6_residency_ns(>->rc6, INTEL_RC6_RES_RC6pp);
161 static inline s64 ktime_since_raw(const ktime_t kt)
163 return ktime_to_ns(ktime_sub(ktime_get_raw(), kt));
166 static u64 get_rc6(struct intel_gt *gt)
168 struct drm_i915_private *i915 = gt->i915;
169 struct i915_pmu *pmu = &i915->pmu;
174 if (intel_gt_pm_get_if_awake(gt)) {
176 intel_gt_pm_put_async(gt);
180 spin_lock_irqsave(&pmu->lock, flags);
183 pmu->sample[__I915_SAMPLE_RC6].cur = val;
186 * We think we are runtime suspended.
188 * Report the delta from when the device was suspended to now,
189 * on top of the last known real value, as the approximated RC6
192 val = ktime_since_raw(pmu->sleep_last);
193 val += pmu->sample[__I915_SAMPLE_RC6].cur;
196 if (val < pmu->sample[__I915_SAMPLE_RC6_LAST_REPORTED].cur)
197 val = pmu->sample[__I915_SAMPLE_RC6_LAST_REPORTED].cur;
199 pmu->sample[__I915_SAMPLE_RC6_LAST_REPORTED].cur = val;
201 spin_unlock_irqrestore(&pmu->lock, flags);
206 static void init_rc6(struct i915_pmu *pmu)
208 struct drm_i915_private *i915 = container_of(pmu, typeof(*i915), pmu);
209 intel_wakeref_t wakeref;
211 with_intel_runtime_pm(to_gt(i915)->uncore->rpm, wakeref) {
212 pmu->sample[__I915_SAMPLE_RC6].cur = __get_rc6(to_gt(i915));
213 pmu->sample[__I915_SAMPLE_RC6_LAST_REPORTED].cur =
214 pmu->sample[__I915_SAMPLE_RC6].cur;
215 pmu->sleep_last = ktime_get_raw();
219 static void park_rc6(struct drm_i915_private *i915)
221 struct i915_pmu *pmu = &i915->pmu;
223 pmu->sample[__I915_SAMPLE_RC6].cur = __get_rc6(to_gt(i915));
224 pmu->sleep_last = ktime_get_raw();
227 static void __i915_pmu_maybe_start_timer(struct i915_pmu *pmu)
229 if (!pmu->timer_enabled && pmu_needs_timer(pmu, true)) {
230 pmu->timer_enabled = true;
231 pmu->timer_last = ktime_get();
232 hrtimer_start_range_ns(&pmu->timer,
233 ns_to_ktime(PERIOD), 0,
234 HRTIMER_MODE_REL_PINNED);
238 void i915_pmu_gt_parked(struct drm_i915_private *i915)
240 struct i915_pmu *pmu = &i915->pmu;
242 if (!pmu->base.event_init)
245 spin_lock_irq(&pmu->lock);
250 * Signal sampling timer to stop if only engine events are enabled and
253 pmu->timer_enabled = pmu_needs_timer(pmu, false);
255 spin_unlock_irq(&pmu->lock);
258 void i915_pmu_gt_unparked(struct drm_i915_private *i915)
260 struct i915_pmu *pmu = &i915->pmu;
262 if (!pmu->base.event_init)
265 spin_lock_irq(&pmu->lock);
268 * Re-enable sampling timer when GPU goes active.
270 __i915_pmu_maybe_start_timer(pmu);
272 spin_unlock_irq(&pmu->lock);
276 add_sample(struct i915_pmu_sample *sample, u32 val)
281 static bool exclusive_mmio_access(const struct drm_i915_private *i915)
284 * We have to avoid concurrent mmio cache line access on gen7 or
285 * risk a machine hang. For a fun history lesson dig out the old
286 * userspace intel_gpu_top and run it on Ivybridge or Haswell!
288 return GRAPHICS_VER(i915) == 7;
291 static void engine_sample(struct intel_engine_cs *engine, unsigned int period_ns)
293 struct intel_engine_pmu *pmu = &engine->pmu;
297 val = ENGINE_READ_FW(engine, RING_CTL);
298 if (val == 0) /* powerwell off => engine idle */
302 add_sample(&pmu->sample[I915_SAMPLE_WAIT], period_ns);
303 if (val & RING_WAIT_SEMAPHORE)
304 add_sample(&pmu->sample[I915_SAMPLE_SEMA], period_ns);
306 /* No need to sample when busy stats are supported. */
307 if (intel_engine_supports_stats(engine))
311 * While waiting on a semaphore or event, MI_MODE reports the
312 * ring as idle. However, previously using the seqno, and with
313 * execlists sampling, we account for the ring waiting as the
314 * engine being busy. Therefore, we record the sample as being
315 * busy if either waiting or !idle.
317 busy = val & (RING_WAIT_SEMAPHORE | RING_WAIT);
319 val = ENGINE_READ_FW(engine, RING_MI_MODE);
320 busy = !(val & MODE_IDLE);
323 add_sample(&pmu->sample[I915_SAMPLE_BUSY], period_ns);
327 engines_sample(struct intel_gt *gt, unsigned int period_ns)
329 struct drm_i915_private *i915 = gt->i915;
330 struct intel_engine_cs *engine;
331 enum intel_engine_id id;
334 if ((i915->pmu.enable & ENGINE_SAMPLE_MASK) == 0)
337 if (!intel_gt_pm_is_awake(gt))
340 for_each_engine(engine, gt, id) {
341 if (!intel_engine_pm_get_if_awake(engine))
344 if (exclusive_mmio_access(i915)) {
345 spin_lock_irqsave(&engine->uncore->lock, flags);
346 engine_sample(engine, period_ns);
347 spin_unlock_irqrestore(&engine->uncore->lock, flags);
349 engine_sample(engine, period_ns);
352 intel_engine_pm_put_async(engine);
357 add_sample_mult(struct i915_pmu_sample *sample, u32 val, u32 mul)
359 sample->cur += mul_u32_u32(val, mul);
362 static bool frequency_sampling_enabled(struct i915_pmu *pmu)
365 (config_mask(I915_PMU_ACTUAL_FREQUENCY) |
366 config_mask(I915_PMU_REQUESTED_FREQUENCY));
370 frequency_sample(struct intel_gt *gt, unsigned int period_ns)
372 struct drm_i915_private *i915 = gt->i915;
373 struct i915_pmu *pmu = &i915->pmu;
374 struct intel_rps *rps = >->rps;
376 if (!frequency_sampling_enabled(pmu))
379 /* Report 0/0 (actual/requested) frequency while parked. */
380 if (!intel_gt_pm_get_if_awake(gt))
383 if (pmu->enable & config_mask(I915_PMU_ACTUAL_FREQUENCY)) {
387 * We take a quick peek here without using forcewake
388 * so that we don't perturb the system under observation
389 * (forcewake => !rc6 => increased power use). We expect
390 * that if the read fails because it is outside of the
391 * mmio power well, then it will return 0 -- in which
392 * case we assume the system is running at the intended
393 * frequency. Fortunately, the read should rarely fail!
395 val = intel_rps_read_actual_frequency_fw(rps);
397 val = intel_gpu_freq(rps, rps->cur_freq);
399 add_sample_mult(&pmu->sample[__I915_SAMPLE_FREQ_ACT],
400 val, period_ns / 1000);
403 if (pmu->enable & config_mask(I915_PMU_REQUESTED_FREQUENCY)) {
404 add_sample_mult(&pmu->sample[__I915_SAMPLE_FREQ_REQ],
405 intel_rps_get_requested_frequency(rps),
409 intel_gt_pm_put_async(gt);
412 static enum hrtimer_restart i915_sample(struct hrtimer *hrtimer)
414 struct drm_i915_private *i915 =
415 container_of(hrtimer, struct drm_i915_private, pmu.timer);
416 struct i915_pmu *pmu = &i915->pmu;
417 struct intel_gt *gt = to_gt(i915);
418 unsigned int period_ns;
421 if (!READ_ONCE(pmu->timer_enabled))
422 return HRTIMER_NORESTART;
425 period_ns = ktime_to_ns(ktime_sub(now, pmu->timer_last));
426 pmu->timer_last = now;
429 * Strictly speaking the passed in period may not be 100% accurate for
430 * all internal calculation, since some amount of time can be spent on
431 * grabbing the forcewake. However the potential error from timer call-
432 * back delay greatly dominates this so we keep it simple.
434 engines_sample(gt, period_ns);
435 frequency_sample(gt, period_ns);
437 hrtimer_forward(hrtimer, now, ns_to_ktime(PERIOD));
439 return HRTIMER_RESTART;
442 static void i915_pmu_event_destroy(struct perf_event *event)
444 struct drm_i915_private *i915 =
445 container_of(event->pmu, typeof(*i915), pmu.base);
447 drm_WARN_ON(&i915->drm, event->parent);
449 drm_dev_put(&i915->drm);
453 engine_event_status(struct intel_engine_cs *engine,
454 enum drm_i915_pmu_engine_sample sample)
457 case I915_SAMPLE_BUSY:
458 case I915_SAMPLE_WAIT:
460 case I915_SAMPLE_SEMA:
461 if (GRAPHICS_VER(engine->i915) < 6)
472 config_status(struct drm_i915_private *i915, u64 config)
474 struct intel_gt *gt = to_gt(i915);
477 case I915_PMU_ACTUAL_FREQUENCY:
478 if (IS_VALLEYVIEW(i915) || IS_CHERRYVIEW(i915))
479 /* Requires a mutex for sampling! */
482 case I915_PMU_REQUESTED_FREQUENCY:
483 if (GRAPHICS_VER(i915) < 6)
486 case I915_PMU_INTERRUPTS:
488 case I915_PMU_RC6_RESIDENCY:
489 if (!gt->rc6.supported)
492 case I915_PMU_SOFTWARE_GT_AWAKE_TIME:
501 static int engine_event_init(struct perf_event *event)
503 struct drm_i915_private *i915 =
504 container_of(event->pmu, typeof(*i915), pmu.base);
505 struct intel_engine_cs *engine;
507 engine = intel_engine_lookup_user(i915, engine_event_class(event),
508 engine_event_instance(event));
512 return engine_event_status(engine, engine_event_sample(event));
515 static int i915_pmu_event_init(struct perf_event *event)
517 struct drm_i915_private *i915 =
518 container_of(event->pmu, typeof(*i915), pmu.base);
519 struct i915_pmu *pmu = &i915->pmu;
525 if (event->attr.type != event->pmu->type)
528 /* unsupported modes and filters */
529 if (event->attr.sample_period) /* no sampling */
532 if (has_branch_stack(event))
538 /* only allow running on one cpu at a time */
539 if (!cpumask_test_cpu(event->cpu, &i915_pmu_cpumask))
542 if (is_engine_event(event))
543 ret = engine_event_init(event);
545 ret = config_status(i915, event->attr.config);
549 if (!event->parent) {
550 drm_dev_get(&i915->drm);
551 event->destroy = i915_pmu_event_destroy;
557 static u64 __i915_pmu_event_read(struct perf_event *event)
559 struct drm_i915_private *i915 =
560 container_of(event->pmu, typeof(*i915), pmu.base);
561 struct i915_pmu *pmu = &i915->pmu;
564 if (is_engine_event(event)) {
565 u8 sample = engine_event_sample(event);
566 struct intel_engine_cs *engine;
568 engine = intel_engine_lookup_user(i915,
569 engine_event_class(event),
570 engine_event_instance(event));
572 if (drm_WARN_ON_ONCE(&i915->drm, !engine)) {
574 } else if (sample == I915_SAMPLE_BUSY &&
575 intel_engine_supports_stats(engine)) {
578 val = ktime_to_ns(intel_engine_get_busy_time(engine,
581 val = engine->pmu.sample[sample].cur;
584 switch (event->attr.config) {
585 case I915_PMU_ACTUAL_FREQUENCY:
587 div_u64(pmu->sample[__I915_SAMPLE_FREQ_ACT].cur,
588 USEC_PER_SEC /* to MHz */);
590 case I915_PMU_REQUESTED_FREQUENCY:
592 div_u64(pmu->sample[__I915_SAMPLE_FREQ_REQ].cur,
593 USEC_PER_SEC /* to MHz */);
595 case I915_PMU_INTERRUPTS:
596 val = READ_ONCE(pmu->irq_count);
598 case I915_PMU_RC6_RESIDENCY:
599 val = get_rc6(to_gt(i915));
601 case I915_PMU_SOFTWARE_GT_AWAKE_TIME:
602 val = ktime_to_ns(intel_gt_get_awake_time(to_gt(i915)));
610 static void i915_pmu_event_read(struct perf_event *event)
612 struct drm_i915_private *i915 =
613 container_of(event->pmu, typeof(*i915), pmu.base);
614 struct hw_perf_event *hwc = &event->hw;
615 struct i915_pmu *pmu = &i915->pmu;
619 event->hw.state = PERF_HES_STOPPED;
623 prev = local64_read(&hwc->prev_count);
624 new = __i915_pmu_event_read(event);
626 if (local64_cmpxchg(&hwc->prev_count, prev, new) != prev)
629 local64_add(new - prev, &event->count);
632 static void i915_pmu_enable(struct perf_event *event)
634 struct drm_i915_private *i915 =
635 container_of(event->pmu, typeof(*i915), pmu.base);
636 struct i915_pmu *pmu = &i915->pmu;
640 bit = event_bit(event);
644 spin_lock_irqsave(&pmu->lock, flags);
647 * Update the bitmask of enabled events and increment
648 * the event reference counter.
650 BUILD_BUG_ON(ARRAY_SIZE(pmu->enable_count) != I915_PMU_MASK_BITS);
651 GEM_BUG_ON(bit >= ARRAY_SIZE(pmu->enable_count));
652 GEM_BUG_ON(pmu->enable_count[bit] == ~0);
654 pmu->enable |= BIT_ULL(bit);
655 pmu->enable_count[bit]++;
658 * Start the sampling timer if needed and not already enabled.
660 __i915_pmu_maybe_start_timer(pmu);
663 * For per-engine events the bitmask and reference counting
664 * is stored per engine.
666 if (is_engine_event(event)) {
667 u8 sample = engine_event_sample(event);
668 struct intel_engine_cs *engine;
670 engine = intel_engine_lookup_user(i915,
671 engine_event_class(event),
672 engine_event_instance(event));
674 BUILD_BUG_ON(ARRAY_SIZE(engine->pmu.enable_count) !=
675 I915_ENGINE_SAMPLE_COUNT);
676 BUILD_BUG_ON(ARRAY_SIZE(engine->pmu.sample) !=
677 I915_ENGINE_SAMPLE_COUNT);
678 GEM_BUG_ON(sample >= ARRAY_SIZE(engine->pmu.enable_count));
679 GEM_BUG_ON(sample >= ARRAY_SIZE(engine->pmu.sample));
680 GEM_BUG_ON(engine->pmu.enable_count[sample] == ~0);
682 engine->pmu.enable |= BIT(sample);
683 engine->pmu.enable_count[sample]++;
686 spin_unlock_irqrestore(&pmu->lock, flags);
690 * Store the current counter value so we can report the correct delta
691 * for all listeners. Even when the event was already enabled and has
692 * an existing non-zero value.
694 local64_set(&event->hw.prev_count, __i915_pmu_event_read(event));
697 static void i915_pmu_disable(struct perf_event *event)
699 struct drm_i915_private *i915 =
700 container_of(event->pmu, typeof(*i915), pmu.base);
701 unsigned int bit = event_bit(event);
702 struct i915_pmu *pmu = &i915->pmu;
708 spin_lock_irqsave(&pmu->lock, flags);
710 if (is_engine_event(event)) {
711 u8 sample = engine_event_sample(event);
712 struct intel_engine_cs *engine;
714 engine = intel_engine_lookup_user(i915,
715 engine_event_class(event),
716 engine_event_instance(event));
718 GEM_BUG_ON(sample >= ARRAY_SIZE(engine->pmu.enable_count));
719 GEM_BUG_ON(sample >= ARRAY_SIZE(engine->pmu.sample));
720 GEM_BUG_ON(engine->pmu.enable_count[sample] == 0);
723 * Decrement the reference count and clear the enabled
724 * bitmask when the last listener on an event goes away.
726 if (--engine->pmu.enable_count[sample] == 0)
727 engine->pmu.enable &= ~BIT(sample);
730 GEM_BUG_ON(bit >= ARRAY_SIZE(pmu->enable_count));
731 GEM_BUG_ON(pmu->enable_count[bit] == 0);
733 * Decrement the reference count and clear the enabled
734 * bitmask when the last listener on an event goes away.
736 if (--pmu->enable_count[bit] == 0) {
737 pmu->enable &= ~BIT_ULL(bit);
738 pmu->timer_enabled &= pmu_needs_timer(pmu, true);
741 spin_unlock_irqrestore(&pmu->lock, flags);
744 static void i915_pmu_event_start(struct perf_event *event, int flags)
746 struct drm_i915_private *i915 =
747 container_of(event->pmu, typeof(*i915), pmu.base);
748 struct i915_pmu *pmu = &i915->pmu;
753 i915_pmu_enable(event);
757 static void i915_pmu_event_stop(struct perf_event *event, int flags)
759 if (flags & PERF_EF_UPDATE)
760 i915_pmu_event_read(event);
761 i915_pmu_disable(event);
762 event->hw.state = PERF_HES_STOPPED;
765 static int i915_pmu_event_add(struct perf_event *event, int flags)
767 struct drm_i915_private *i915 =
768 container_of(event->pmu, typeof(*i915), pmu.base);
769 struct i915_pmu *pmu = &i915->pmu;
774 if (flags & PERF_EF_START)
775 i915_pmu_event_start(event, flags);
780 static void i915_pmu_event_del(struct perf_event *event, int flags)
782 i915_pmu_event_stop(event, PERF_EF_UPDATE);
785 static int i915_pmu_event_event_idx(struct perf_event *event)
790 struct i915_str_attribute {
791 struct device_attribute attr;
795 static ssize_t i915_pmu_format_show(struct device *dev,
796 struct device_attribute *attr, char *buf)
798 struct i915_str_attribute *eattr;
800 eattr = container_of(attr, struct i915_str_attribute, attr);
801 return sprintf(buf, "%s\n", eattr->str);
804 #define I915_PMU_FORMAT_ATTR(_name, _config) \
805 (&((struct i915_str_attribute[]) { \
806 { .attr = __ATTR(_name, 0444, i915_pmu_format_show, NULL), \
810 static struct attribute *i915_pmu_format_attrs[] = {
811 I915_PMU_FORMAT_ATTR(i915_eventid, "config:0-20"),
815 static const struct attribute_group i915_pmu_format_attr_group = {
817 .attrs = i915_pmu_format_attrs,
820 struct i915_ext_attribute {
821 struct device_attribute attr;
825 static ssize_t i915_pmu_event_show(struct device *dev,
826 struct device_attribute *attr, char *buf)
828 struct i915_ext_attribute *eattr;
830 eattr = container_of(attr, struct i915_ext_attribute, attr);
831 return sprintf(buf, "config=0x%lx\n", eattr->val);
834 static ssize_t cpumask_show(struct device *dev,
835 struct device_attribute *attr, char *buf)
837 return cpumap_print_to_pagebuf(true, buf, &i915_pmu_cpumask);
840 static DEVICE_ATTR_RO(cpumask);
842 static struct attribute *i915_cpumask_attrs[] = {
843 &dev_attr_cpumask.attr,
847 static const struct attribute_group i915_pmu_cpumask_attr_group = {
848 .attrs = i915_cpumask_attrs,
851 #define __event(__config, __name, __unit) \
853 .config = (__config), \
858 #define __engine_event(__sample, __name) \
860 .sample = (__sample), \
864 static struct i915_ext_attribute *
865 add_i915_attr(struct i915_ext_attribute *attr, const char *name, u64 config)
867 sysfs_attr_init(&attr->attr.attr);
868 attr->attr.attr.name = name;
869 attr->attr.attr.mode = 0444;
870 attr->attr.show = i915_pmu_event_show;
876 static struct perf_pmu_events_attr *
877 add_pmu_attr(struct perf_pmu_events_attr *attr, const char *name,
880 sysfs_attr_init(&attr->attr.attr);
881 attr->attr.attr.name = name;
882 attr->attr.attr.mode = 0444;
883 attr->attr.show = perf_event_sysfs_show;
884 attr->event_str = str;
889 static struct attribute **
890 create_event_attributes(struct i915_pmu *pmu)
892 struct drm_i915_private *i915 = container_of(pmu, typeof(*i915), pmu);
893 static const struct {
898 __event(I915_PMU_ACTUAL_FREQUENCY, "actual-frequency", "M"),
899 __event(I915_PMU_REQUESTED_FREQUENCY, "requested-frequency", "M"),
900 __event(I915_PMU_INTERRUPTS, "interrupts", NULL),
901 __event(I915_PMU_RC6_RESIDENCY, "rc6-residency", "ns"),
902 __event(I915_PMU_SOFTWARE_GT_AWAKE_TIME, "software-gt-awake-time", "ns"),
904 static const struct {
905 enum drm_i915_pmu_engine_sample sample;
907 } engine_events[] = {
908 __engine_event(I915_SAMPLE_BUSY, "busy"),
909 __engine_event(I915_SAMPLE_SEMA, "sema"),
910 __engine_event(I915_SAMPLE_WAIT, "wait"),
912 unsigned int count = 0;
913 struct perf_pmu_events_attr *pmu_attr = NULL, *pmu_iter;
914 struct i915_ext_attribute *i915_attr = NULL, *i915_iter;
915 struct attribute **attr = NULL, **attr_iter;
916 struct intel_engine_cs *engine;
919 /* Count how many counters we will be exposing. */
920 for (i = 0; i < ARRAY_SIZE(events); i++) {
921 if (!config_status(i915, events[i].config))
925 for_each_uabi_engine(engine, i915) {
926 for (i = 0; i < ARRAY_SIZE(engine_events); i++) {
927 if (!engine_event_status(engine,
928 engine_events[i].sample))
933 /* Allocate attribute objects and table. */
934 i915_attr = kcalloc(count, sizeof(*i915_attr), GFP_KERNEL);
938 pmu_attr = kcalloc(count, sizeof(*pmu_attr), GFP_KERNEL);
942 /* Max one pointer of each attribute type plus a termination entry. */
943 attr = kcalloc(count * 2 + 1, sizeof(*attr), GFP_KERNEL);
947 i915_iter = i915_attr;
951 /* Initialize supported non-engine counters. */
952 for (i = 0; i < ARRAY_SIZE(events); i++) {
955 if (config_status(i915, events[i].config))
958 str = kstrdup(events[i].name, GFP_KERNEL);
962 *attr_iter++ = &i915_iter->attr.attr;
963 i915_iter = add_i915_attr(i915_iter, str, events[i].config);
965 if (events[i].unit) {
966 str = kasprintf(GFP_KERNEL, "%s.unit", events[i].name);
970 *attr_iter++ = &pmu_iter->attr.attr;
971 pmu_iter = add_pmu_attr(pmu_iter, str, events[i].unit);
975 /* Initialize supported engine counters. */
976 for_each_uabi_engine(engine, i915) {
977 for (i = 0; i < ARRAY_SIZE(engine_events); i++) {
980 if (engine_event_status(engine,
981 engine_events[i].sample))
984 str = kasprintf(GFP_KERNEL, "%s-%s",
985 engine->name, engine_events[i].name);
989 *attr_iter++ = &i915_iter->attr.attr;
991 add_i915_attr(i915_iter, str,
992 __I915_PMU_ENGINE(engine->uabi_class,
993 engine->uabi_instance,
994 engine_events[i].sample));
996 str = kasprintf(GFP_KERNEL, "%s-%s.unit",
997 engine->name, engine_events[i].name);
1001 *attr_iter++ = &pmu_iter->attr.attr;
1002 pmu_iter = add_pmu_attr(pmu_iter, str, "ns");
1006 pmu->i915_attr = i915_attr;
1007 pmu->pmu_attr = pmu_attr;
1012 for (attr_iter = attr; *attr_iter; attr_iter++)
1013 kfree((*attr_iter)->name);
1023 static void free_event_attributes(struct i915_pmu *pmu)
1025 struct attribute **attr_iter = pmu->events_attr_group.attrs;
1027 for (; *attr_iter; attr_iter++)
1028 kfree((*attr_iter)->name);
1030 kfree(pmu->events_attr_group.attrs);
1031 kfree(pmu->i915_attr);
1032 kfree(pmu->pmu_attr);
1034 pmu->events_attr_group.attrs = NULL;
1035 pmu->i915_attr = NULL;
1036 pmu->pmu_attr = NULL;
1039 static int i915_pmu_cpu_online(unsigned int cpu, struct hlist_node *node)
1041 struct i915_pmu *pmu = hlist_entry_safe(node, typeof(*pmu), cpuhp.node);
1043 GEM_BUG_ON(!pmu->base.event_init);
1045 /* Select the first online CPU as a designated reader. */
1046 if (cpumask_empty(&i915_pmu_cpumask))
1047 cpumask_set_cpu(cpu, &i915_pmu_cpumask);
1052 static int i915_pmu_cpu_offline(unsigned int cpu, struct hlist_node *node)
1054 struct i915_pmu *pmu = hlist_entry_safe(node, typeof(*pmu), cpuhp.node);
1055 unsigned int target = i915_pmu_target_cpu;
1057 GEM_BUG_ON(!pmu->base.event_init);
1060 * Unregistering an instance generates a CPU offline event which we must
1061 * ignore to avoid incorrectly modifying the shared i915_pmu_cpumask.
1066 if (cpumask_test_and_clear_cpu(cpu, &i915_pmu_cpumask)) {
1067 target = cpumask_any_but(topology_sibling_cpumask(cpu), cpu);
1069 /* Migrate events if there is a valid target */
1070 if (target < nr_cpu_ids) {
1071 cpumask_set_cpu(target, &i915_pmu_cpumask);
1072 i915_pmu_target_cpu = target;
1076 if (target < nr_cpu_ids && target != pmu->cpuhp.cpu) {
1077 perf_pmu_migrate_context(&pmu->base, cpu, target);
1078 pmu->cpuhp.cpu = target;
1084 static enum cpuhp_state cpuhp_slot = CPUHP_INVALID;
1086 int i915_pmu_init(void)
1090 ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN,
1091 "perf/x86/intel/i915:online",
1092 i915_pmu_cpu_online,
1093 i915_pmu_cpu_offline);
1095 pr_notice("Failed to setup cpuhp state for i915 PMU! (%d)\n",
1103 void i915_pmu_exit(void)
1105 if (cpuhp_slot != CPUHP_INVALID)
1106 cpuhp_remove_multi_state(cpuhp_slot);
1109 static int i915_pmu_register_cpuhp_state(struct i915_pmu *pmu)
1111 if (cpuhp_slot == CPUHP_INVALID)
1114 return cpuhp_state_add_instance(cpuhp_slot, &pmu->cpuhp.node);
1117 static void i915_pmu_unregister_cpuhp_state(struct i915_pmu *pmu)
1119 cpuhp_state_remove_instance(cpuhp_slot, &pmu->cpuhp.node);
1122 static bool is_igp(struct drm_i915_private *i915)
1124 struct pci_dev *pdev = to_pci_dev(i915->drm.dev);
1126 /* IGP is 0000:00:02.0 */
1127 return pci_domain_nr(pdev->bus) == 0 &&
1128 pdev->bus->number == 0 &&
1129 PCI_SLOT(pdev->devfn) == 2 &&
1130 PCI_FUNC(pdev->devfn) == 0;
1133 void i915_pmu_register(struct drm_i915_private *i915)
1135 struct i915_pmu *pmu = &i915->pmu;
1136 const struct attribute_group *attr_groups[] = {
1137 &i915_pmu_format_attr_group,
1138 &pmu->events_attr_group,
1139 &i915_pmu_cpumask_attr_group,
1145 if (GRAPHICS_VER(i915) <= 2) {
1146 drm_info(&i915->drm, "PMU not supported for this GPU.");
1150 spin_lock_init(&pmu->lock);
1151 hrtimer_init(&pmu->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1152 pmu->timer.function = i915_sample;
1153 pmu->cpuhp.cpu = -1;
1156 if (!is_igp(i915)) {
1157 pmu->name = kasprintf(GFP_KERNEL,
1159 dev_name(i915->drm.dev));
1161 /* tools/perf reserves colons as special. */
1162 strreplace((char *)pmu->name, ':', '_');
1170 pmu->events_attr_group.name = "events";
1171 pmu->events_attr_group.attrs = create_event_attributes(pmu);
1172 if (!pmu->events_attr_group.attrs)
1175 pmu->base.attr_groups = kmemdup(attr_groups, sizeof(attr_groups),
1177 if (!pmu->base.attr_groups)
1180 pmu->base.module = THIS_MODULE;
1181 pmu->base.task_ctx_nr = perf_invalid_context;
1182 pmu->base.event_init = i915_pmu_event_init;
1183 pmu->base.add = i915_pmu_event_add;
1184 pmu->base.del = i915_pmu_event_del;
1185 pmu->base.start = i915_pmu_event_start;
1186 pmu->base.stop = i915_pmu_event_stop;
1187 pmu->base.read = i915_pmu_event_read;
1188 pmu->base.event_idx = i915_pmu_event_event_idx;
1190 ret = perf_pmu_register(&pmu->base, pmu->name, -1);
1194 ret = i915_pmu_register_cpuhp_state(pmu);
1201 perf_pmu_unregister(&pmu->base);
1203 kfree(pmu->base.attr_groups);
1205 pmu->base.event_init = NULL;
1206 free_event_attributes(pmu);
1211 drm_notice(&i915->drm, "Failed to register PMU!\n");
1214 void i915_pmu_unregister(struct drm_i915_private *i915)
1216 struct i915_pmu *pmu = &i915->pmu;
1218 if (!pmu->base.event_init)
1222 * "Disconnect" the PMU callbacks - since all are atomic synchronize_rcu
1223 * ensures all currently executing ones will have exited before we
1224 * proceed with unregistration.
1229 hrtimer_cancel(&pmu->timer);
1231 i915_pmu_unregister_cpuhp_state(pmu);
1233 perf_pmu_unregister(&pmu->base);
1234 pmu->base.event_init = NULL;
1235 kfree(pmu->base.attr_groups);
1238 free_event_attributes(pmu);