2 * SPDX-License-Identifier: MIT
4 * Copyright © 2017-2018 Intel Corporation
8 #include <linux/pm_runtime.h>
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 #include "gt/intel_rc6.h"
15 #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 #define ENGINE_SAMPLE_BITS (1 << I915_PMU_SAMPLE_BITS)
32 static cpumask_t i915_pmu_cpumask;
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 config_enabled_bit(u64 config)
61 if (is_engine_config(config))
62 return engine_config_sample(config);
64 return ENGINE_SAMPLE_BITS + (config - __I915_PMU_OTHER(0));
67 static u64 config_enabled_mask(u64 config)
69 return BIT_ULL(config_enabled_bit(config));
72 static bool is_engine_event(struct perf_event *event)
74 return is_engine_config(event->attr.config);
77 static unsigned int event_enabled_bit(struct perf_event *event)
79 return config_enabled_bit(event->attr.config);
82 static bool pmu_needs_timer(struct i915_pmu *pmu, bool gpu_active)
84 struct drm_i915_private *i915 = container_of(pmu, typeof(*i915), pmu);
88 * Only some counters need the sampling timer.
90 * We start with a bitmask of all currently enabled events.
95 * Mask out all the ones which do not need the timer, or in
96 * other words keep all the ones that could need the timer.
98 enable &= config_enabled_mask(I915_PMU_ACTUAL_FREQUENCY) |
99 config_enabled_mask(I915_PMU_REQUESTED_FREQUENCY) |
103 * When the GPU is idle per-engine counters do not need to be
104 * running so clear those bits out.
107 enable &= ~ENGINE_SAMPLE_MASK;
109 * Also there is software busyness tracking available we do not
110 * need the timer for I915_SAMPLE_BUSY counter.
112 else if (i915->caps.scheduler & I915_SCHEDULER_CAP_ENGINE_BUSY_STATS)
113 enable &= ~BIT(I915_SAMPLE_BUSY);
116 * If some bits remain it means we need the sampling timer running.
121 static u64 __get_rc6(struct intel_gt *gt)
123 struct drm_i915_private *i915 = gt->i915;
126 val = intel_rc6_residency_ns(>->rc6,
127 IS_VALLEYVIEW(i915) ?
132 val += intel_rc6_residency_ns(>->rc6, GEN6_GT_GFX_RC6p);
135 val += intel_rc6_residency_ns(>->rc6, GEN6_GT_GFX_RC6pp);
140 #if IS_ENABLED(CONFIG_PM)
142 static inline s64 ktime_since(const ktime_t kt)
144 return ktime_to_ns(ktime_sub(ktime_get(), kt));
147 static u64 get_rc6(struct intel_gt *gt)
149 struct drm_i915_private *i915 = gt->i915;
150 struct i915_pmu *pmu = &i915->pmu;
155 if (intel_gt_pm_get_if_awake(gt)) {
157 intel_gt_pm_put_async(gt);
161 spin_lock_irqsave(&pmu->lock, flags);
164 pmu->sample[__I915_SAMPLE_RC6].cur = val;
167 * We think we are runtime suspended.
169 * Report the delta from when the device was suspended to now,
170 * on top of the last known real value, as the approximated RC6
173 val = ktime_since(pmu->sleep_last);
174 val += pmu->sample[__I915_SAMPLE_RC6].cur;
177 if (val < pmu->sample[__I915_SAMPLE_RC6_LAST_REPORTED].cur)
178 val = pmu->sample[__I915_SAMPLE_RC6_LAST_REPORTED].cur;
180 pmu->sample[__I915_SAMPLE_RC6_LAST_REPORTED].cur = val;
182 spin_unlock_irqrestore(&pmu->lock, flags);
187 static void park_rc6(struct drm_i915_private *i915)
189 struct i915_pmu *pmu = &i915->pmu;
191 if (pmu->enable & config_enabled_mask(I915_PMU_RC6_RESIDENCY))
192 pmu->sample[__I915_SAMPLE_RC6].cur = __get_rc6(&i915->gt);
194 pmu->sleep_last = ktime_get();
199 static u64 get_rc6(struct intel_gt *gt)
201 return __get_rc6(gt);
204 static void park_rc6(struct drm_i915_private *i915) {}
208 static void __i915_pmu_maybe_start_timer(struct i915_pmu *pmu)
210 if (!pmu->timer_enabled && pmu_needs_timer(pmu, true)) {
211 pmu->timer_enabled = true;
212 pmu->timer_last = ktime_get();
213 hrtimer_start_range_ns(&pmu->timer,
214 ns_to_ktime(PERIOD), 0,
215 HRTIMER_MODE_REL_PINNED);
219 void i915_pmu_gt_parked(struct drm_i915_private *i915)
221 struct i915_pmu *pmu = &i915->pmu;
223 if (!pmu->base.event_init)
226 spin_lock_irq(&pmu->lock);
231 * Signal sampling timer to stop if only engine events are enabled and
234 pmu->timer_enabled = pmu_needs_timer(pmu, false);
236 spin_unlock_irq(&pmu->lock);
239 void i915_pmu_gt_unparked(struct drm_i915_private *i915)
241 struct i915_pmu *pmu = &i915->pmu;
243 if (!pmu->base.event_init)
246 spin_lock_irq(&pmu->lock);
249 * Re-enable sampling timer when GPU goes active.
251 __i915_pmu_maybe_start_timer(pmu);
253 spin_unlock_irq(&pmu->lock);
257 add_sample(struct i915_pmu_sample *sample, u32 val)
262 static bool exclusive_mmio_access(const struct drm_i915_private *i915)
265 * We have to avoid concurrent mmio cache line access on gen7 or
266 * risk a machine hang. For a fun history lesson dig out the old
267 * userspace intel_gpu_top and run it on Ivybridge or Haswell!
269 return IS_GEN(i915, 7);
272 static void engine_sample(struct intel_engine_cs *engine, unsigned int period_ns)
274 struct intel_engine_pmu *pmu = &engine->pmu;
278 val = ENGINE_READ_FW(engine, RING_CTL);
279 if (val == 0) /* powerwell off => engine idle */
283 add_sample(&pmu->sample[I915_SAMPLE_WAIT], period_ns);
284 if (val & RING_WAIT_SEMAPHORE)
285 add_sample(&pmu->sample[I915_SAMPLE_SEMA], period_ns);
287 /* No need to sample when busy stats are supported. */
288 if (intel_engine_supports_stats(engine))
292 * While waiting on a semaphore or event, MI_MODE reports the
293 * ring as idle. However, previously using the seqno, and with
294 * execlists sampling, we account for the ring waiting as the
295 * engine being busy. Therefore, we record the sample as being
296 * busy if either waiting or !idle.
298 busy = val & (RING_WAIT_SEMAPHORE | RING_WAIT);
300 val = ENGINE_READ_FW(engine, RING_MI_MODE);
301 busy = !(val & MODE_IDLE);
304 add_sample(&pmu->sample[I915_SAMPLE_BUSY], period_ns);
308 engines_sample(struct intel_gt *gt, unsigned int period_ns)
310 struct drm_i915_private *i915 = gt->i915;
311 struct intel_engine_cs *engine;
312 enum intel_engine_id id;
315 if ((i915->pmu.enable & ENGINE_SAMPLE_MASK) == 0)
318 if (!intel_gt_pm_is_awake(gt))
321 for_each_engine(engine, gt, id) {
322 if (!intel_engine_pm_get_if_awake(engine))
325 if (exclusive_mmio_access(i915)) {
326 spin_lock_irqsave(&engine->uncore->lock, flags);
327 engine_sample(engine, period_ns);
328 spin_unlock_irqrestore(&engine->uncore->lock, flags);
330 engine_sample(engine, period_ns);
333 intel_engine_pm_put_async(engine);
338 add_sample_mult(struct i915_pmu_sample *sample, u32 val, u32 mul)
340 sample->cur += mul_u32_u32(val, mul);
343 static bool frequency_sampling_enabled(struct i915_pmu *pmu)
346 (config_enabled_mask(I915_PMU_ACTUAL_FREQUENCY) |
347 config_enabled_mask(I915_PMU_REQUESTED_FREQUENCY));
351 frequency_sample(struct intel_gt *gt, unsigned int period_ns)
353 struct drm_i915_private *i915 = gt->i915;
354 struct intel_uncore *uncore = gt->uncore;
355 struct i915_pmu *pmu = &i915->pmu;
356 struct intel_rps *rps = >->rps;
358 if (!frequency_sampling_enabled(pmu))
361 /* Report 0/0 (actual/requested) frequency while parked. */
362 if (!intel_gt_pm_get_if_awake(gt))
365 if (pmu->enable & config_enabled_mask(I915_PMU_ACTUAL_FREQUENCY)) {
369 * We take a quick peek here without using forcewake
370 * so that we don't perturb the system under observation
371 * (forcewake => !rc6 => increased power use). We expect
372 * that if the read fails because it is outside of the
373 * mmio power well, then it will return 0 -- in which
374 * case we assume the system is running at the intended
375 * frequency. Fortunately, the read should rarely fail!
377 val = intel_uncore_read_fw(uncore, GEN6_RPSTAT1);
379 val = intel_rps_get_cagf(rps, val);
383 add_sample_mult(&pmu->sample[__I915_SAMPLE_FREQ_ACT],
384 intel_gpu_freq(rps, val), period_ns / 1000);
387 if (pmu->enable & config_enabled_mask(I915_PMU_REQUESTED_FREQUENCY)) {
388 add_sample_mult(&pmu->sample[__I915_SAMPLE_FREQ_REQ],
389 intel_gpu_freq(rps, rps->cur_freq),
393 intel_gt_pm_put_async(gt);
396 static enum hrtimer_restart i915_sample(struct hrtimer *hrtimer)
398 struct drm_i915_private *i915 =
399 container_of(hrtimer, struct drm_i915_private, pmu.timer);
400 struct i915_pmu *pmu = &i915->pmu;
401 struct intel_gt *gt = &i915->gt;
402 unsigned int period_ns;
405 if (!READ_ONCE(pmu->timer_enabled))
406 return HRTIMER_NORESTART;
409 period_ns = ktime_to_ns(ktime_sub(now, pmu->timer_last));
410 pmu->timer_last = now;
413 * Strictly speaking the passed in period may not be 100% accurate for
414 * all internal calculation, since some amount of time can be spent on
415 * grabbing the forcewake. However the potential error from timer call-
416 * back delay greatly dominates this so we keep it simple.
418 engines_sample(gt, period_ns);
419 frequency_sample(gt, period_ns);
421 hrtimer_forward(hrtimer, now, ns_to_ktime(PERIOD));
423 return HRTIMER_RESTART;
426 static u64 count_interrupts(struct drm_i915_private *i915)
428 /* open-coded kstat_irqs() */
429 struct irq_desc *desc = irq_to_desc(i915->drm.pdev->irq);
433 if (!desc || !desc->kstat_irqs)
436 for_each_possible_cpu(cpu)
437 sum += *per_cpu_ptr(desc->kstat_irqs, cpu);
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);
451 engine_event_status(struct intel_engine_cs *engine,
452 enum drm_i915_pmu_engine_sample sample)
455 case I915_SAMPLE_BUSY:
456 case I915_SAMPLE_WAIT:
458 case I915_SAMPLE_SEMA:
459 if (INTEL_GEN(engine->i915) < 6)
470 config_status(struct drm_i915_private *i915, u64 config)
473 case I915_PMU_ACTUAL_FREQUENCY:
474 if (IS_VALLEYVIEW(i915) || IS_CHERRYVIEW(i915))
475 /* Requires a mutex for sampling! */
478 case I915_PMU_REQUESTED_FREQUENCY:
479 if (INTEL_GEN(i915) < 6)
482 case I915_PMU_INTERRUPTS:
484 case I915_PMU_RC6_RESIDENCY:
495 static int engine_event_init(struct perf_event *event)
497 struct drm_i915_private *i915 =
498 container_of(event->pmu, typeof(*i915), pmu.base);
499 struct intel_engine_cs *engine;
501 engine = intel_engine_lookup_user(i915, engine_event_class(event),
502 engine_event_instance(event));
506 return engine_event_status(engine, engine_event_sample(event));
509 static int i915_pmu_event_init(struct perf_event *event)
511 struct drm_i915_private *i915 =
512 container_of(event->pmu, typeof(*i915), pmu.base);
515 if (event->attr.type != event->pmu->type)
518 /* unsupported modes and filters */
519 if (event->attr.sample_period) /* no sampling */
522 if (has_branch_stack(event))
528 /* only allow running on one cpu at a time */
529 if (!cpumask_test_cpu(event->cpu, &i915_pmu_cpumask))
532 if (is_engine_event(event))
533 ret = engine_event_init(event);
535 ret = config_status(i915, event->attr.config);
540 event->destroy = i915_pmu_event_destroy;
545 static u64 __i915_pmu_event_read(struct perf_event *event)
547 struct drm_i915_private *i915 =
548 container_of(event->pmu, typeof(*i915), pmu.base);
549 struct i915_pmu *pmu = &i915->pmu;
552 if (is_engine_event(event)) {
553 u8 sample = engine_event_sample(event);
554 struct intel_engine_cs *engine;
556 engine = intel_engine_lookup_user(i915,
557 engine_event_class(event),
558 engine_event_instance(event));
560 if (drm_WARN_ON_ONCE(&i915->drm, !engine)) {
562 } else if (sample == I915_SAMPLE_BUSY &&
563 intel_engine_supports_stats(engine)) {
566 val = ktime_to_ns(intel_engine_get_busy_time(engine,
569 val = engine->pmu.sample[sample].cur;
572 switch (event->attr.config) {
573 case I915_PMU_ACTUAL_FREQUENCY:
575 div_u64(pmu->sample[__I915_SAMPLE_FREQ_ACT].cur,
576 USEC_PER_SEC /* to MHz */);
578 case I915_PMU_REQUESTED_FREQUENCY:
580 div_u64(pmu->sample[__I915_SAMPLE_FREQ_REQ].cur,
581 USEC_PER_SEC /* to MHz */);
583 case I915_PMU_INTERRUPTS:
584 val = count_interrupts(i915);
586 case I915_PMU_RC6_RESIDENCY:
587 val = get_rc6(&i915->gt);
595 static void i915_pmu_event_read(struct perf_event *event)
597 struct hw_perf_event *hwc = &event->hw;
601 prev = local64_read(&hwc->prev_count);
602 new = __i915_pmu_event_read(event);
604 if (local64_cmpxchg(&hwc->prev_count, prev, new) != prev)
607 local64_add(new - prev, &event->count);
610 static void i915_pmu_enable(struct perf_event *event)
612 struct drm_i915_private *i915 =
613 container_of(event->pmu, typeof(*i915), pmu.base);
614 unsigned int bit = event_enabled_bit(event);
615 struct i915_pmu *pmu = &i915->pmu;
616 intel_wakeref_t wakeref;
619 wakeref = intel_runtime_pm_get(&i915->runtime_pm);
620 spin_lock_irqsave(&pmu->lock, flags);
623 * Update the bitmask of enabled events and increment
624 * the event reference counter.
626 BUILD_BUG_ON(ARRAY_SIZE(pmu->enable_count) != I915_PMU_MASK_BITS);
627 GEM_BUG_ON(bit >= ARRAY_SIZE(pmu->enable_count));
628 GEM_BUG_ON(pmu->enable_count[bit] == ~0);
630 if (pmu->enable_count[bit] == 0 &&
631 config_enabled_mask(I915_PMU_RC6_RESIDENCY) & BIT_ULL(bit)) {
632 pmu->sample[__I915_SAMPLE_RC6_LAST_REPORTED].cur = 0;
633 pmu->sample[__I915_SAMPLE_RC6].cur = __get_rc6(&i915->gt);
634 pmu->sleep_last = ktime_get();
637 pmu->enable |= BIT_ULL(bit);
638 pmu->enable_count[bit]++;
641 * Start the sampling timer if needed and not already enabled.
643 __i915_pmu_maybe_start_timer(pmu);
646 * For per-engine events the bitmask and reference counting
647 * is stored per engine.
649 if (is_engine_event(event)) {
650 u8 sample = engine_event_sample(event);
651 struct intel_engine_cs *engine;
653 engine = intel_engine_lookup_user(i915,
654 engine_event_class(event),
655 engine_event_instance(event));
657 BUILD_BUG_ON(ARRAY_SIZE(engine->pmu.enable_count) !=
658 I915_ENGINE_SAMPLE_COUNT);
659 BUILD_BUG_ON(ARRAY_SIZE(engine->pmu.sample) !=
660 I915_ENGINE_SAMPLE_COUNT);
661 GEM_BUG_ON(sample >= ARRAY_SIZE(engine->pmu.enable_count));
662 GEM_BUG_ON(sample >= ARRAY_SIZE(engine->pmu.sample));
663 GEM_BUG_ON(engine->pmu.enable_count[sample] == ~0);
665 engine->pmu.enable |= BIT(sample);
666 engine->pmu.enable_count[sample]++;
669 spin_unlock_irqrestore(&pmu->lock, flags);
672 * Store the current counter value so we can report the correct delta
673 * for all listeners. Even when the event was already enabled and has
674 * an existing non-zero value.
676 local64_set(&event->hw.prev_count, __i915_pmu_event_read(event));
678 intel_runtime_pm_put(&i915->runtime_pm, wakeref);
681 static void i915_pmu_disable(struct perf_event *event)
683 struct drm_i915_private *i915 =
684 container_of(event->pmu, typeof(*i915), pmu.base);
685 unsigned int bit = event_enabled_bit(event);
686 struct i915_pmu *pmu = &i915->pmu;
689 spin_lock_irqsave(&pmu->lock, flags);
691 if (is_engine_event(event)) {
692 u8 sample = engine_event_sample(event);
693 struct intel_engine_cs *engine;
695 engine = intel_engine_lookup_user(i915,
696 engine_event_class(event),
697 engine_event_instance(event));
699 GEM_BUG_ON(sample >= ARRAY_SIZE(engine->pmu.enable_count));
700 GEM_BUG_ON(sample >= ARRAY_SIZE(engine->pmu.sample));
701 GEM_BUG_ON(engine->pmu.enable_count[sample] == 0);
704 * Decrement the reference count and clear the enabled
705 * bitmask when the last listener on an event goes away.
707 if (--engine->pmu.enable_count[sample] == 0)
708 engine->pmu.enable &= ~BIT(sample);
711 GEM_BUG_ON(bit >= ARRAY_SIZE(pmu->enable_count));
712 GEM_BUG_ON(pmu->enable_count[bit] == 0);
714 * Decrement the reference count and clear the enabled
715 * bitmask when the last listener on an event goes away.
717 if (--pmu->enable_count[bit] == 0) {
718 pmu->enable &= ~BIT_ULL(bit);
719 pmu->timer_enabled &= pmu_needs_timer(pmu, true);
722 spin_unlock_irqrestore(&pmu->lock, flags);
725 static void i915_pmu_event_start(struct perf_event *event, int flags)
727 i915_pmu_enable(event);
731 static void i915_pmu_event_stop(struct perf_event *event, int flags)
733 if (flags & PERF_EF_UPDATE)
734 i915_pmu_event_read(event);
735 i915_pmu_disable(event);
736 event->hw.state = PERF_HES_STOPPED;
739 static int i915_pmu_event_add(struct perf_event *event, int flags)
741 if (flags & PERF_EF_START)
742 i915_pmu_event_start(event, flags);
747 static void i915_pmu_event_del(struct perf_event *event, int flags)
749 i915_pmu_event_stop(event, PERF_EF_UPDATE);
752 static int i915_pmu_event_event_idx(struct perf_event *event)
757 struct i915_str_attribute {
758 struct device_attribute attr;
762 static ssize_t i915_pmu_format_show(struct device *dev,
763 struct device_attribute *attr, char *buf)
765 struct i915_str_attribute *eattr;
767 eattr = container_of(attr, struct i915_str_attribute, attr);
768 return sprintf(buf, "%s\n", eattr->str);
771 #define I915_PMU_FORMAT_ATTR(_name, _config) \
772 (&((struct i915_str_attribute[]) { \
773 { .attr = __ATTR(_name, 0444, i915_pmu_format_show, NULL), \
777 static struct attribute *i915_pmu_format_attrs[] = {
778 I915_PMU_FORMAT_ATTR(i915_eventid, "config:0-20"),
782 static const struct attribute_group i915_pmu_format_attr_group = {
784 .attrs = i915_pmu_format_attrs,
787 struct i915_ext_attribute {
788 struct device_attribute attr;
792 static ssize_t i915_pmu_event_show(struct device *dev,
793 struct device_attribute *attr, char *buf)
795 struct i915_ext_attribute *eattr;
797 eattr = container_of(attr, struct i915_ext_attribute, attr);
798 return sprintf(buf, "config=0x%lx\n", eattr->val);
802 i915_pmu_get_attr_cpumask(struct device *dev,
803 struct device_attribute *attr,
806 return cpumap_print_to_pagebuf(true, buf, &i915_pmu_cpumask);
809 static DEVICE_ATTR(cpumask, 0444, i915_pmu_get_attr_cpumask, NULL);
811 static struct attribute *i915_cpumask_attrs[] = {
812 &dev_attr_cpumask.attr,
816 static const struct attribute_group i915_pmu_cpumask_attr_group = {
817 .attrs = i915_cpumask_attrs,
820 #define __event(__config, __name, __unit) \
822 .config = (__config), \
827 #define __engine_event(__sample, __name) \
829 .sample = (__sample), \
833 static struct i915_ext_attribute *
834 add_i915_attr(struct i915_ext_attribute *attr, const char *name, u64 config)
836 sysfs_attr_init(&attr->attr.attr);
837 attr->attr.attr.name = name;
838 attr->attr.attr.mode = 0444;
839 attr->attr.show = i915_pmu_event_show;
845 static struct perf_pmu_events_attr *
846 add_pmu_attr(struct perf_pmu_events_attr *attr, const char *name,
849 sysfs_attr_init(&attr->attr.attr);
850 attr->attr.attr.name = name;
851 attr->attr.attr.mode = 0444;
852 attr->attr.show = perf_event_sysfs_show;
853 attr->event_str = str;
858 static struct attribute **
859 create_event_attributes(struct i915_pmu *pmu)
861 struct drm_i915_private *i915 = container_of(pmu, typeof(*i915), pmu);
862 static const struct {
867 __event(I915_PMU_ACTUAL_FREQUENCY, "actual-frequency", "M"),
868 __event(I915_PMU_REQUESTED_FREQUENCY, "requested-frequency", "M"),
869 __event(I915_PMU_INTERRUPTS, "interrupts", NULL),
870 __event(I915_PMU_RC6_RESIDENCY, "rc6-residency", "ns"),
872 static const struct {
873 enum drm_i915_pmu_engine_sample sample;
875 } engine_events[] = {
876 __engine_event(I915_SAMPLE_BUSY, "busy"),
877 __engine_event(I915_SAMPLE_SEMA, "sema"),
878 __engine_event(I915_SAMPLE_WAIT, "wait"),
880 unsigned int count = 0;
881 struct perf_pmu_events_attr *pmu_attr = NULL, *pmu_iter;
882 struct i915_ext_attribute *i915_attr = NULL, *i915_iter;
883 struct attribute **attr = NULL, **attr_iter;
884 struct intel_engine_cs *engine;
887 /* Count how many counters we will be exposing. */
888 for (i = 0; i < ARRAY_SIZE(events); i++) {
889 if (!config_status(i915, events[i].config))
893 for_each_uabi_engine(engine, i915) {
894 for (i = 0; i < ARRAY_SIZE(engine_events); i++) {
895 if (!engine_event_status(engine,
896 engine_events[i].sample))
901 /* Allocate attribute objects and table. */
902 i915_attr = kcalloc(count, sizeof(*i915_attr), GFP_KERNEL);
906 pmu_attr = kcalloc(count, sizeof(*pmu_attr), GFP_KERNEL);
910 /* Max one pointer of each attribute type plus a termination entry. */
911 attr = kcalloc(count * 2 + 1, sizeof(*attr), GFP_KERNEL);
915 i915_iter = i915_attr;
919 /* Initialize supported non-engine counters. */
920 for (i = 0; i < ARRAY_SIZE(events); i++) {
923 if (config_status(i915, events[i].config))
926 str = kstrdup(events[i].name, GFP_KERNEL);
930 *attr_iter++ = &i915_iter->attr.attr;
931 i915_iter = add_i915_attr(i915_iter, str, events[i].config);
933 if (events[i].unit) {
934 str = kasprintf(GFP_KERNEL, "%s.unit", events[i].name);
938 *attr_iter++ = &pmu_iter->attr.attr;
939 pmu_iter = add_pmu_attr(pmu_iter, str, events[i].unit);
943 /* Initialize supported engine counters. */
944 for_each_uabi_engine(engine, i915) {
945 for (i = 0; i < ARRAY_SIZE(engine_events); i++) {
948 if (engine_event_status(engine,
949 engine_events[i].sample))
952 str = kasprintf(GFP_KERNEL, "%s-%s",
953 engine->name, engine_events[i].name);
957 *attr_iter++ = &i915_iter->attr.attr;
959 add_i915_attr(i915_iter, str,
960 __I915_PMU_ENGINE(engine->uabi_class,
961 engine->uabi_instance,
962 engine_events[i].sample));
964 str = kasprintf(GFP_KERNEL, "%s-%s.unit",
965 engine->name, engine_events[i].name);
969 *attr_iter++ = &pmu_iter->attr.attr;
970 pmu_iter = add_pmu_attr(pmu_iter, str, "ns");
974 pmu->i915_attr = i915_attr;
975 pmu->pmu_attr = pmu_attr;
980 for (attr_iter = attr; *attr_iter; attr_iter++)
981 kfree((*attr_iter)->name);
991 static void free_event_attributes(struct i915_pmu *pmu)
993 struct attribute **attr_iter = pmu->events_attr_group.attrs;
995 for (; *attr_iter; attr_iter++)
996 kfree((*attr_iter)->name);
998 kfree(pmu->events_attr_group.attrs);
999 kfree(pmu->i915_attr);
1000 kfree(pmu->pmu_attr);
1002 pmu->events_attr_group.attrs = NULL;
1003 pmu->i915_attr = NULL;
1004 pmu->pmu_attr = NULL;
1007 static int i915_pmu_cpu_online(unsigned int cpu, struct hlist_node *node)
1009 struct i915_pmu *pmu = hlist_entry_safe(node, typeof(*pmu), cpuhp.node);
1011 GEM_BUG_ON(!pmu->base.event_init);
1013 /* Select the first online CPU as a designated reader. */
1014 if (!cpumask_weight(&i915_pmu_cpumask))
1015 cpumask_set_cpu(cpu, &i915_pmu_cpumask);
1020 static int i915_pmu_cpu_offline(unsigned int cpu, struct hlist_node *node)
1022 struct i915_pmu *pmu = hlist_entry_safe(node, typeof(*pmu), cpuhp.node);
1023 unsigned int target;
1025 GEM_BUG_ON(!pmu->base.event_init);
1027 if (cpumask_test_and_clear_cpu(cpu, &i915_pmu_cpumask)) {
1028 target = cpumask_any_but(topology_sibling_cpumask(cpu), cpu);
1029 /* Migrate events if there is a valid target */
1030 if (target < nr_cpu_ids) {
1031 cpumask_set_cpu(target, &i915_pmu_cpumask);
1032 perf_pmu_migrate_context(&pmu->base, cpu, target);
1039 static int i915_pmu_register_cpuhp_state(struct i915_pmu *pmu)
1041 enum cpuhp_state slot;
1044 ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN,
1045 "perf/x86/intel/i915:online",
1046 i915_pmu_cpu_online,
1047 i915_pmu_cpu_offline);
1052 ret = cpuhp_state_add_instance(slot, &pmu->cpuhp.node);
1054 cpuhp_remove_multi_state(slot);
1058 pmu->cpuhp.slot = slot;
1062 static void i915_pmu_unregister_cpuhp_state(struct i915_pmu *pmu)
1064 struct drm_i915_private *i915 = container_of(pmu, typeof(*i915), pmu);
1066 drm_WARN_ON(&i915->drm, pmu->cpuhp.slot == CPUHP_INVALID);
1067 drm_WARN_ON(&i915->drm, cpuhp_state_remove_instance(pmu->cpuhp.slot, &pmu->cpuhp.node));
1068 cpuhp_remove_multi_state(pmu->cpuhp.slot);
1069 pmu->cpuhp.slot = CPUHP_INVALID;
1072 static bool is_igp(struct drm_i915_private *i915)
1074 struct pci_dev *pdev = i915->drm.pdev;
1076 /* IGP is 0000:00:02.0 */
1077 return pci_domain_nr(pdev->bus) == 0 &&
1078 pdev->bus->number == 0 &&
1079 PCI_SLOT(pdev->devfn) == 2 &&
1080 PCI_FUNC(pdev->devfn) == 0;
1083 void i915_pmu_register(struct drm_i915_private *i915)
1085 struct i915_pmu *pmu = &i915->pmu;
1086 const struct attribute_group *attr_groups[] = {
1087 &i915_pmu_format_attr_group,
1088 &pmu->events_attr_group,
1089 &i915_pmu_cpumask_attr_group,
1095 if (INTEL_GEN(i915) <= 2) {
1096 drm_info(&i915->drm, "PMU not supported for this GPU.");
1100 spin_lock_init(&pmu->lock);
1101 hrtimer_init(&pmu->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1102 pmu->timer.function = i915_sample;
1103 pmu->cpuhp.slot = CPUHP_INVALID;
1105 if (!is_igp(i915)) {
1106 pmu->name = kasprintf(GFP_KERNEL,
1108 dev_name(i915->drm.dev));
1110 /* tools/perf reserves colons as special. */
1111 strreplace((char *)pmu->name, ':', '_');
1119 pmu->events_attr_group.name = "events";
1120 pmu->events_attr_group.attrs = create_event_attributes(pmu);
1121 if (!pmu->events_attr_group.attrs)
1124 pmu->base.attr_groups = kmemdup(attr_groups, sizeof(attr_groups),
1126 if (!pmu->base.attr_groups)
1129 pmu->base.module = THIS_MODULE;
1130 pmu->base.task_ctx_nr = perf_invalid_context;
1131 pmu->base.event_init = i915_pmu_event_init;
1132 pmu->base.add = i915_pmu_event_add;
1133 pmu->base.del = i915_pmu_event_del;
1134 pmu->base.start = i915_pmu_event_start;
1135 pmu->base.stop = i915_pmu_event_stop;
1136 pmu->base.read = i915_pmu_event_read;
1137 pmu->base.event_idx = i915_pmu_event_event_idx;
1139 ret = perf_pmu_register(&pmu->base, pmu->name, -1);
1143 ret = i915_pmu_register_cpuhp_state(pmu);
1150 perf_pmu_unregister(&pmu->base);
1152 kfree(pmu->base.attr_groups);
1154 pmu->base.event_init = NULL;
1155 free_event_attributes(pmu);
1160 drm_notice(&i915->drm, "Failed to register PMU!\n");
1163 void i915_pmu_unregister(struct drm_i915_private *i915)
1165 struct i915_pmu *pmu = &i915->pmu;
1167 if (!pmu->base.event_init)
1170 drm_WARN_ON(&i915->drm, pmu->enable);
1172 hrtimer_cancel(&pmu->timer);
1174 i915_pmu_unregister_cpuhp_state(pmu);
1176 perf_pmu_unregister(&pmu->base);
1177 pmu->base.event_init = NULL;
1178 kfree(pmu->base.attr_groups);
1181 free_event_attributes(pmu);