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"
20 /* Frequency for the sampling timer for events which need it. */
22 #define PERIOD max_t(u64, 10000, NSEC_PER_SEC / FREQUENCY)
24 #define ENGINE_SAMPLE_MASK \
25 (BIT(I915_SAMPLE_BUSY) | \
26 BIT(I915_SAMPLE_WAIT) | \
27 BIT(I915_SAMPLE_SEMA))
29 #define ENGINE_SAMPLE_BITS (1 << I915_PMU_SAMPLE_BITS)
31 static cpumask_t i915_pmu_cpumask;
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 config_enabled_bit(u64 config)
60 if (is_engine_config(config))
61 return engine_config_sample(config);
63 return ENGINE_SAMPLE_BITS + (config - __I915_PMU_OTHER(0));
66 static u64 config_enabled_mask(u64 config)
68 return BIT_ULL(config_enabled_bit(config));
71 static bool is_engine_event(struct perf_event *event)
73 return is_engine_config(event->attr.config);
76 static unsigned int event_enabled_bit(struct perf_event *event)
78 return config_enabled_bit(event->attr.config);
81 static bool pmu_needs_timer(struct i915_pmu *pmu, bool gpu_active)
83 struct drm_i915_private *i915 = container_of(pmu, typeof(*i915), pmu);
87 * Only some counters need the sampling timer.
89 * We start with a bitmask of all currently enabled events.
94 * Mask out all the ones which do not need the timer, or in
95 * other words keep all the ones that could need the timer.
97 enable &= config_enabled_mask(I915_PMU_ACTUAL_FREQUENCY) |
98 config_enabled_mask(I915_PMU_REQUESTED_FREQUENCY) |
102 * When the GPU is idle per-engine counters do not need to be
103 * running so clear those bits out.
106 enable &= ~ENGINE_SAMPLE_MASK;
108 * Also there is software busyness tracking available we do not
109 * need the timer for I915_SAMPLE_BUSY counter.
111 else if (i915->caps.scheduler & I915_SCHEDULER_CAP_ENGINE_BUSY_STATS)
112 enable &= ~BIT(I915_SAMPLE_BUSY);
115 * If some bits remain it means we need the sampling timer running.
120 static u64 __get_rc6(struct intel_gt *gt)
122 struct drm_i915_private *i915 = gt->i915;
125 val = intel_rc6_residency_ns(>->rc6,
126 IS_VALLEYVIEW(i915) ?
131 val += intel_rc6_residency_ns(>->rc6, GEN6_GT_GFX_RC6p);
134 val += intel_rc6_residency_ns(>->rc6, GEN6_GT_GFX_RC6pp);
139 #if IS_ENABLED(CONFIG_PM)
141 static inline s64 ktime_since(const ktime_t kt)
143 return ktime_to_ns(ktime_sub(ktime_get(), kt));
146 static u64 __pmu_estimate_rc6(struct i915_pmu *pmu)
151 * We think we are runtime suspended.
153 * Report the delta from when the device was suspended to now,
154 * on top of the last known real value, as the approximated RC6
157 val = ktime_since(pmu->sleep_last);
158 val += pmu->sample[__I915_SAMPLE_RC6].cur;
160 pmu->sample[__I915_SAMPLE_RC6_ESTIMATED].cur = val;
165 static u64 __pmu_update_rc6(struct i915_pmu *pmu, u64 val)
168 * If we are coming back from being runtime suspended we must
169 * be careful not to report a larger value than returned
172 if (val >= pmu->sample[__I915_SAMPLE_RC6_ESTIMATED].cur) {
173 pmu->sample[__I915_SAMPLE_RC6_ESTIMATED].cur = 0;
174 pmu->sample[__I915_SAMPLE_RC6].cur = val;
176 val = pmu->sample[__I915_SAMPLE_RC6_ESTIMATED].cur;
182 static u64 get_rc6(struct intel_gt *gt)
184 struct drm_i915_private *i915 = gt->i915;
185 struct i915_pmu *pmu = &i915->pmu;
190 if (intel_gt_pm_get_if_awake(gt)) {
195 spin_lock_irqsave(&pmu->lock, flags);
198 val = __pmu_update_rc6(pmu, val);
200 val = __pmu_estimate_rc6(pmu);
202 spin_unlock_irqrestore(&pmu->lock, flags);
207 static void park_rc6(struct drm_i915_private *i915)
209 struct i915_pmu *pmu = &i915->pmu;
211 if (pmu->enable & config_enabled_mask(I915_PMU_RC6_RESIDENCY))
212 __pmu_update_rc6(pmu, __get_rc6(&i915->gt));
214 pmu->sleep_last = ktime_get();
217 static void unpark_rc6(struct drm_i915_private *i915)
219 struct i915_pmu *pmu = &i915->pmu;
221 /* Estimate how long we slept and accumulate that into rc6 counters */
222 if (pmu->enable & config_enabled_mask(I915_PMU_RC6_RESIDENCY))
223 __pmu_estimate_rc6(pmu);
228 static u64 get_rc6(struct intel_gt *gt)
230 return __get_rc6(gt);
233 static void park_rc6(struct drm_i915_private *i915) {}
234 static void unpark_rc6(struct drm_i915_private *i915) {}
238 static void __i915_pmu_maybe_start_timer(struct i915_pmu *pmu)
240 if (!pmu->timer_enabled && pmu_needs_timer(pmu, true)) {
241 pmu->timer_enabled = true;
242 pmu->timer_last = ktime_get();
243 hrtimer_start_range_ns(&pmu->timer,
244 ns_to_ktime(PERIOD), 0,
245 HRTIMER_MODE_REL_PINNED);
249 void i915_pmu_gt_parked(struct drm_i915_private *i915)
251 struct i915_pmu *pmu = &i915->pmu;
253 if (!pmu->base.event_init)
256 spin_lock_irq(&pmu->lock);
261 * Signal sampling timer to stop if only engine events are enabled and
264 pmu->timer_enabled = pmu_needs_timer(pmu, false);
266 spin_unlock_irq(&pmu->lock);
269 void i915_pmu_gt_unparked(struct drm_i915_private *i915)
271 struct i915_pmu *pmu = &i915->pmu;
273 if (!pmu->base.event_init)
276 spin_lock_irq(&pmu->lock);
279 * Re-enable sampling timer when GPU goes active.
281 __i915_pmu_maybe_start_timer(pmu);
285 spin_unlock_irq(&pmu->lock);
289 add_sample(struct i915_pmu_sample *sample, u32 val)
295 engines_sample(struct intel_gt *gt, unsigned int period_ns)
297 struct drm_i915_private *i915 = gt->i915;
298 struct intel_engine_cs *engine;
299 enum intel_engine_id id;
301 if ((i915->pmu.enable & ENGINE_SAMPLE_MASK) == 0)
304 for_each_engine(engine, i915, id) {
305 struct intel_engine_pmu *pmu = &engine->pmu;
310 if (!intel_engine_pm_get_if_awake(engine))
313 spin_lock_irqsave(&engine->uncore->lock, flags);
315 val = ENGINE_READ_FW(engine, RING_CTL);
316 if (val == 0) /* powerwell off => engine idle */
320 add_sample(&pmu->sample[I915_SAMPLE_WAIT], period_ns);
321 if (val & RING_WAIT_SEMAPHORE)
322 add_sample(&pmu->sample[I915_SAMPLE_SEMA], period_ns);
324 /* No need to sample when busy stats are supported. */
325 if (intel_engine_supports_stats(engine))
329 * While waiting on a semaphore or event, MI_MODE reports the
330 * ring as idle. However, previously using the seqno, and with
331 * execlists sampling, we account for the ring waiting as the
332 * engine being busy. Therefore, we record the sample as being
333 * busy if either waiting or !idle.
335 busy = val & (RING_WAIT_SEMAPHORE | RING_WAIT);
337 val = ENGINE_READ_FW(engine, RING_MI_MODE);
338 busy = !(val & MODE_IDLE);
341 add_sample(&pmu->sample[I915_SAMPLE_BUSY], period_ns);
344 spin_unlock_irqrestore(&engine->uncore->lock, flags);
345 intel_engine_pm_put(engine);
350 add_sample_mult(struct i915_pmu_sample *sample, u32 val, u32 mul)
352 sample->cur += mul_u32_u32(val, mul);
356 frequency_sample(struct intel_gt *gt, unsigned int period_ns)
358 struct drm_i915_private *i915 = gt->i915;
359 struct intel_uncore *uncore = gt->uncore;
360 struct i915_pmu *pmu = &i915->pmu;
362 if (pmu->enable & config_enabled_mask(I915_PMU_ACTUAL_FREQUENCY)) {
365 val = i915->gt_pm.rps.cur_freq;
366 if (intel_gt_pm_get_if_awake(gt)) {
367 val = intel_uncore_read_notrace(uncore, GEN6_RPSTAT1);
368 val = intel_get_cagf(i915, val);
372 add_sample_mult(&pmu->sample[__I915_SAMPLE_FREQ_ACT],
373 intel_gpu_freq(i915, val),
377 if (pmu->enable & config_enabled_mask(I915_PMU_REQUESTED_FREQUENCY)) {
378 add_sample_mult(&pmu->sample[__I915_SAMPLE_FREQ_REQ],
379 intel_gpu_freq(i915, i915->gt_pm.rps.cur_freq),
384 static enum hrtimer_restart i915_sample(struct hrtimer *hrtimer)
386 struct drm_i915_private *i915 =
387 container_of(hrtimer, struct drm_i915_private, pmu.timer);
388 struct i915_pmu *pmu = &i915->pmu;
389 struct intel_gt *gt = &i915->gt;
390 unsigned int period_ns;
393 if (!READ_ONCE(pmu->timer_enabled))
394 return HRTIMER_NORESTART;
397 period_ns = ktime_to_ns(ktime_sub(now, pmu->timer_last));
398 pmu->timer_last = now;
401 * Strictly speaking the passed in period may not be 100% accurate for
402 * all internal calculation, since some amount of time can be spent on
403 * grabbing the forcewake. However the potential error from timer call-
404 * back delay greatly dominates this so we keep it simple.
406 engines_sample(gt, period_ns);
407 frequency_sample(gt, period_ns);
409 hrtimer_forward(hrtimer, now, ns_to_ktime(PERIOD));
411 return HRTIMER_RESTART;
414 static u64 count_interrupts(struct drm_i915_private *i915)
416 /* open-coded kstat_irqs() */
417 struct irq_desc *desc = irq_to_desc(i915->drm.pdev->irq);
421 if (!desc || !desc->kstat_irqs)
424 for_each_possible_cpu(cpu)
425 sum += *per_cpu_ptr(desc->kstat_irqs, cpu);
430 static void engine_event_destroy(struct perf_event *event)
432 struct drm_i915_private *i915 =
433 container_of(event->pmu, typeof(*i915), pmu.base);
434 struct intel_engine_cs *engine;
436 engine = intel_engine_lookup_user(i915,
437 engine_event_class(event),
438 engine_event_instance(event));
439 if (WARN_ON_ONCE(!engine))
442 if (engine_event_sample(event) == I915_SAMPLE_BUSY &&
443 intel_engine_supports_stats(engine))
444 intel_disable_engine_stats(engine);
447 static void i915_pmu_event_destroy(struct perf_event *event)
449 WARN_ON(event->parent);
451 if (is_engine_event(event))
452 engine_event_destroy(event);
456 engine_event_status(struct intel_engine_cs *engine,
457 enum drm_i915_pmu_engine_sample sample)
460 case I915_SAMPLE_BUSY:
461 case I915_SAMPLE_WAIT:
463 case I915_SAMPLE_SEMA:
464 if (INTEL_GEN(engine->i915) < 6)
475 config_status(struct drm_i915_private *i915, u64 config)
478 case I915_PMU_ACTUAL_FREQUENCY:
479 if (IS_VALLEYVIEW(i915) || IS_CHERRYVIEW(i915))
480 /* Requires a mutex for sampling! */
483 case I915_PMU_REQUESTED_FREQUENCY:
484 if (INTEL_GEN(i915) < 6)
487 case I915_PMU_INTERRUPTS:
489 case I915_PMU_RC6_RESIDENCY:
500 static int engine_event_init(struct perf_event *event)
502 struct drm_i915_private *i915 =
503 container_of(event->pmu, typeof(*i915), pmu.base);
504 struct intel_engine_cs *engine;
508 engine = intel_engine_lookup_user(i915, engine_event_class(event),
509 engine_event_instance(event));
513 sample = engine_event_sample(event);
514 ret = engine_event_status(engine, sample);
518 if (sample == I915_SAMPLE_BUSY && intel_engine_supports_stats(engine))
519 ret = intel_enable_engine_stats(engine);
524 static int i915_pmu_event_init(struct perf_event *event)
526 struct drm_i915_private *i915 =
527 container_of(event->pmu, typeof(*i915), pmu.base);
530 if (event->attr.type != event->pmu->type)
533 /* unsupported modes and filters */
534 if (event->attr.sample_period) /* no sampling */
537 if (has_branch_stack(event))
543 /* only allow running on one cpu at a time */
544 if (!cpumask_test_cpu(event->cpu, &i915_pmu_cpumask))
547 if (is_engine_event(event))
548 ret = engine_event_init(event);
550 ret = config_status(i915, event->attr.config);
555 event->destroy = i915_pmu_event_destroy;
560 static u64 __i915_pmu_event_read(struct perf_event *event)
562 struct drm_i915_private *i915 =
563 container_of(event->pmu, typeof(*i915), pmu.base);
564 struct i915_pmu *pmu = &i915->pmu;
567 if (is_engine_event(event)) {
568 u8 sample = engine_event_sample(event);
569 struct intel_engine_cs *engine;
571 engine = intel_engine_lookup_user(i915,
572 engine_event_class(event),
573 engine_event_instance(event));
575 if (WARN_ON_ONCE(!engine)) {
577 } else if (sample == I915_SAMPLE_BUSY &&
578 intel_engine_supports_stats(engine)) {
579 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 = count_interrupts(i915);
598 case I915_PMU_RC6_RESIDENCY:
599 val = get_rc6(&i915->gt);
607 static void i915_pmu_event_read(struct perf_event *event)
609 struct hw_perf_event *hwc = &event->hw;
613 prev = local64_read(&hwc->prev_count);
614 new = __i915_pmu_event_read(event);
616 if (local64_cmpxchg(&hwc->prev_count, prev, new) != prev)
619 local64_add(new - prev, &event->count);
622 static void i915_pmu_enable(struct perf_event *event)
624 struct drm_i915_private *i915 =
625 container_of(event->pmu, typeof(*i915), pmu.base);
626 unsigned int bit = event_enabled_bit(event);
627 struct i915_pmu *pmu = &i915->pmu;
630 spin_lock_irqsave(&pmu->lock, flags);
633 * Update the bitmask of enabled events and increment
634 * the event reference counter.
636 BUILD_BUG_ON(ARRAY_SIZE(pmu->enable_count) != I915_PMU_MASK_BITS);
637 GEM_BUG_ON(bit >= ARRAY_SIZE(pmu->enable_count));
638 GEM_BUG_ON(pmu->enable_count[bit] == ~0);
639 pmu->enable |= BIT_ULL(bit);
640 pmu->enable_count[bit]++;
643 * Start the sampling timer if needed and not already enabled.
645 __i915_pmu_maybe_start_timer(pmu);
648 * For per-engine events the bitmask and reference counting
649 * is stored per engine.
651 if (is_engine_event(event)) {
652 u8 sample = engine_event_sample(event);
653 struct intel_engine_cs *engine;
655 engine = intel_engine_lookup_user(i915,
656 engine_event_class(event),
657 engine_event_instance(event));
659 BUILD_BUG_ON(ARRAY_SIZE(engine->pmu.enable_count) !=
660 I915_ENGINE_SAMPLE_COUNT);
661 BUILD_BUG_ON(ARRAY_SIZE(engine->pmu.sample) !=
662 I915_ENGINE_SAMPLE_COUNT);
663 GEM_BUG_ON(sample >= ARRAY_SIZE(engine->pmu.enable_count));
664 GEM_BUG_ON(sample >= ARRAY_SIZE(engine->pmu.sample));
665 GEM_BUG_ON(engine->pmu.enable_count[sample] == ~0);
667 engine->pmu.enable |= BIT(sample);
668 engine->pmu.enable_count[sample]++;
671 spin_unlock_irqrestore(&pmu->lock, flags);
674 * Store the current counter value so we can report the correct delta
675 * for all listeners. Even when the event was already enabled and has
676 * an existing non-zero value.
678 local64_set(&event->hw.prev_count, __i915_pmu_event_read(event));
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);
801 static struct attribute_group i915_pmu_events_attr_group = {
803 /* Patch in attrs at runtime. */
807 i915_pmu_get_attr_cpumask(struct device *dev,
808 struct device_attribute *attr,
811 return cpumap_print_to_pagebuf(true, buf, &i915_pmu_cpumask);
814 static DEVICE_ATTR(cpumask, 0444, i915_pmu_get_attr_cpumask, NULL);
816 static struct attribute *i915_cpumask_attrs[] = {
817 &dev_attr_cpumask.attr,
821 static const struct attribute_group i915_pmu_cpumask_attr_group = {
822 .attrs = i915_cpumask_attrs,
825 static const struct attribute_group *i915_pmu_attr_groups[] = {
826 &i915_pmu_format_attr_group,
827 &i915_pmu_events_attr_group,
828 &i915_pmu_cpumask_attr_group,
832 #define __event(__config, __name, __unit) \
834 .config = (__config), \
839 #define __engine_event(__sample, __name) \
841 .sample = (__sample), \
845 static struct i915_ext_attribute *
846 add_i915_attr(struct i915_ext_attribute *attr, const char *name, u64 config)
848 sysfs_attr_init(&attr->attr.attr);
849 attr->attr.attr.name = name;
850 attr->attr.attr.mode = 0444;
851 attr->attr.show = i915_pmu_event_show;
857 static struct perf_pmu_events_attr *
858 add_pmu_attr(struct perf_pmu_events_attr *attr, const char *name,
861 sysfs_attr_init(&attr->attr.attr);
862 attr->attr.attr.name = name;
863 attr->attr.attr.mode = 0444;
864 attr->attr.show = perf_event_sysfs_show;
865 attr->event_str = str;
870 static struct attribute **
871 create_event_attributes(struct i915_pmu *pmu)
873 struct drm_i915_private *i915 = container_of(pmu, typeof(*i915), pmu);
874 static const struct {
879 __event(I915_PMU_ACTUAL_FREQUENCY, "actual-frequency", "MHz"),
880 __event(I915_PMU_REQUESTED_FREQUENCY, "requested-frequency", "MHz"),
881 __event(I915_PMU_INTERRUPTS, "interrupts", NULL),
882 __event(I915_PMU_RC6_RESIDENCY, "rc6-residency", "ns"),
884 static const struct {
885 enum drm_i915_pmu_engine_sample sample;
887 } engine_events[] = {
888 __engine_event(I915_SAMPLE_BUSY, "busy"),
889 __engine_event(I915_SAMPLE_SEMA, "sema"),
890 __engine_event(I915_SAMPLE_WAIT, "wait"),
892 unsigned int count = 0;
893 struct perf_pmu_events_attr *pmu_attr = NULL, *pmu_iter;
894 struct i915_ext_attribute *i915_attr = NULL, *i915_iter;
895 struct attribute **attr = NULL, **attr_iter;
896 struct intel_engine_cs *engine;
899 /* Count how many counters we will be exposing. */
900 for (i = 0; i < ARRAY_SIZE(events); i++) {
901 if (!config_status(i915, events[i].config))
905 for_each_uabi_engine(engine, i915) {
906 for (i = 0; i < ARRAY_SIZE(engine_events); i++) {
907 if (!engine_event_status(engine,
908 engine_events[i].sample))
913 /* Allocate attribute objects and table. */
914 i915_attr = kcalloc(count, sizeof(*i915_attr), GFP_KERNEL);
918 pmu_attr = kcalloc(count, sizeof(*pmu_attr), GFP_KERNEL);
922 /* Max one pointer of each attribute type plus a termination entry. */
923 attr = kcalloc(count * 2 + 1, sizeof(*attr), GFP_KERNEL);
927 i915_iter = i915_attr;
931 /* Initialize supported non-engine counters. */
932 for (i = 0; i < ARRAY_SIZE(events); i++) {
935 if (config_status(i915, events[i].config))
938 str = kstrdup(events[i].name, GFP_KERNEL);
942 *attr_iter++ = &i915_iter->attr.attr;
943 i915_iter = add_i915_attr(i915_iter, str, events[i].config);
945 if (events[i].unit) {
946 str = kasprintf(GFP_KERNEL, "%s.unit", events[i].name);
950 *attr_iter++ = &pmu_iter->attr.attr;
951 pmu_iter = add_pmu_attr(pmu_iter, str, events[i].unit);
955 /* Initialize supported engine counters. */
956 for_each_uabi_engine(engine, i915) {
957 for (i = 0; i < ARRAY_SIZE(engine_events); i++) {
960 if (engine_event_status(engine,
961 engine_events[i].sample))
964 str = kasprintf(GFP_KERNEL, "%s-%s",
965 engine->name, engine_events[i].name);
969 *attr_iter++ = &i915_iter->attr.attr;
971 add_i915_attr(i915_iter, str,
972 __I915_PMU_ENGINE(engine->uabi_class,
973 engine->uabi_instance,
974 engine_events[i].sample));
976 str = kasprintf(GFP_KERNEL, "%s-%s.unit",
977 engine->name, engine_events[i].name);
981 *attr_iter++ = &pmu_iter->attr.attr;
982 pmu_iter = add_pmu_attr(pmu_iter, str, "ns");
986 pmu->i915_attr = i915_attr;
987 pmu->pmu_attr = pmu_attr;
992 for (attr_iter = attr; *attr_iter; attr_iter++)
993 kfree((*attr_iter)->name);
1003 static void free_event_attributes(struct i915_pmu *pmu)
1005 struct attribute **attr_iter = i915_pmu_events_attr_group.attrs;
1007 for (; *attr_iter; attr_iter++)
1008 kfree((*attr_iter)->name);
1010 kfree(i915_pmu_events_attr_group.attrs);
1011 kfree(pmu->i915_attr);
1012 kfree(pmu->pmu_attr);
1014 i915_pmu_events_attr_group.attrs = NULL;
1015 pmu->i915_attr = NULL;
1016 pmu->pmu_attr = NULL;
1019 static int i915_pmu_cpu_online(unsigned int cpu, struct hlist_node *node)
1021 struct i915_pmu *pmu = hlist_entry_safe(node, typeof(*pmu), node);
1023 GEM_BUG_ON(!pmu->base.event_init);
1025 /* Select the first online CPU as a designated reader. */
1026 if (!cpumask_weight(&i915_pmu_cpumask))
1027 cpumask_set_cpu(cpu, &i915_pmu_cpumask);
1032 static int i915_pmu_cpu_offline(unsigned int cpu, struct hlist_node *node)
1034 struct i915_pmu *pmu = hlist_entry_safe(node, typeof(*pmu), node);
1035 unsigned int target;
1037 GEM_BUG_ON(!pmu->base.event_init);
1039 if (cpumask_test_and_clear_cpu(cpu, &i915_pmu_cpumask)) {
1040 target = cpumask_any_but(topology_sibling_cpumask(cpu), cpu);
1041 /* Migrate events if there is a valid target */
1042 if (target < nr_cpu_ids) {
1043 cpumask_set_cpu(target, &i915_pmu_cpumask);
1044 perf_pmu_migrate_context(&pmu->base, cpu, target);
1051 static enum cpuhp_state cpuhp_slot = CPUHP_INVALID;
1053 static int i915_pmu_register_cpuhp_state(struct i915_pmu *pmu)
1055 enum cpuhp_state slot;
1058 ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN,
1059 "perf/x86/intel/i915:online",
1060 i915_pmu_cpu_online,
1061 i915_pmu_cpu_offline);
1066 ret = cpuhp_state_add_instance(slot, &pmu->node);
1068 cpuhp_remove_multi_state(slot);
1076 static void i915_pmu_unregister_cpuhp_state(struct i915_pmu *pmu)
1078 WARN_ON(cpuhp_slot == CPUHP_INVALID);
1079 WARN_ON(cpuhp_state_remove_instance(cpuhp_slot, &pmu->node));
1080 cpuhp_remove_multi_state(cpuhp_slot);
1083 static bool is_igp(struct drm_i915_private *i915)
1085 struct pci_dev *pdev = i915->drm.pdev;
1087 /* IGP is 0000:00:02.0 */
1088 return pci_domain_nr(pdev->bus) == 0 &&
1089 pdev->bus->number == 0 &&
1090 PCI_SLOT(pdev->devfn) == 2 &&
1091 PCI_FUNC(pdev->devfn) == 0;
1094 void i915_pmu_register(struct drm_i915_private *i915)
1096 struct i915_pmu *pmu = &i915->pmu;
1099 if (INTEL_GEN(i915) <= 2) {
1100 dev_info(i915->drm.dev, "PMU not supported for this GPU.");
1104 i915_pmu_events_attr_group.attrs = create_event_attributes(pmu);
1105 if (!i915_pmu_events_attr_group.attrs) {
1110 pmu->base.attr_groups = i915_pmu_attr_groups;
1111 pmu->base.task_ctx_nr = perf_invalid_context;
1112 pmu->base.event_init = i915_pmu_event_init;
1113 pmu->base.add = i915_pmu_event_add;
1114 pmu->base.del = i915_pmu_event_del;
1115 pmu->base.start = i915_pmu_event_start;
1116 pmu->base.stop = i915_pmu_event_stop;
1117 pmu->base.read = i915_pmu_event_read;
1118 pmu->base.event_idx = i915_pmu_event_event_idx;
1120 spin_lock_init(&pmu->lock);
1121 hrtimer_init(&pmu->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1122 pmu->timer.function = i915_sample;
1125 pmu->name = kasprintf(GFP_KERNEL,
1127 dev_name(i915->drm.dev));
1133 ret = perf_pmu_register(&pmu->base, pmu->name, -1);
1137 ret = i915_pmu_register_cpuhp_state(pmu);
1144 perf_pmu_unregister(&pmu->base);
1149 pmu->base.event_init = NULL;
1150 free_event_attributes(pmu);
1151 DRM_NOTE("Failed to register PMU! (err=%d)\n", ret);
1154 void i915_pmu_unregister(struct drm_i915_private *i915)
1156 struct i915_pmu *pmu = &i915->pmu;
1158 if (!pmu->base.event_init)
1161 WARN_ON(pmu->enable);
1163 hrtimer_cancel(&pmu->timer);
1165 i915_pmu_unregister_cpuhp_state(pmu);
1167 perf_pmu_unregister(&pmu->base);
1168 pmu->base.event_init = NULL;
1171 free_event_attributes(pmu);