2 * SPDX-License-Identifier: MIT
4 * Copyright © 2017-2018 Intel Corporation
8 #include "intel_ringbuffer.h"
11 /* Frequency for the sampling timer for events which need it. */
13 #define PERIOD max_t(u64, 10000, NSEC_PER_SEC / FREQUENCY)
15 #define ENGINE_SAMPLE_MASK \
16 (BIT(I915_SAMPLE_BUSY) | \
17 BIT(I915_SAMPLE_WAIT) | \
18 BIT(I915_SAMPLE_SEMA))
20 #define ENGINE_SAMPLE_BITS (1 << I915_PMU_SAMPLE_BITS)
22 static cpumask_t i915_pmu_cpumask;
24 static u8 engine_config_sample(u64 config)
26 return config & I915_PMU_SAMPLE_MASK;
29 static u8 engine_event_sample(struct perf_event *event)
31 return engine_config_sample(event->attr.config);
34 static u8 engine_event_class(struct perf_event *event)
36 return (event->attr.config >> I915_PMU_CLASS_SHIFT) & 0xff;
39 static u8 engine_event_instance(struct perf_event *event)
41 return (event->attr.config >> I915_PMU_SAMPLE_BITS) & 0xff;
44 static bool is_engine_config(u64 config)
46 return config < __I915_PMU_OTHER(0);
49 static unsigned int config_enabled_bit(u64 config)
51 if (is_engine_config(config))
52 return engine_config_sample(config);
54 return ENGINE_SAMPLE_BITS + (config - __I915_PMU_OTHER(0));
57 static u64 config_enabled_mask(u64 config)
59 return BIT_ULL(config_enabled_bit(config));
62 static bool is_engine_event(struct perf_event *event)
64 return is_engine_config(event->attr.config);
67 static unsigned int event_enabled_bit(struct perf_event *event)
69 return config_enabled_bit(event->attr.config);
72 static bool pmu_needs_timer(struct drm_i915_private *i915, bool gpu_active)
77 * Only some counters need the sampling timer.
79 * We start with a bitmask of all currently enabled events.
81 enable = i915->pmu.enable;
84 * Mask out all the ones which do not need the timer, or in
85 * other words keep all the ones that could need the timer.
87 enable &= config_enabled_mask(I915_PMU_ACTUAL_FREQUENCY) |
88 config_enabled_mask(I915_PMU_REQUESTED_FREQUENCY) |
92 * When the GPU is idle per-engine counters do not need to be
93 * running so clear those bits out.
96 enable &= ~ENGINE_SAMPLE_MASK;
98 * Also there is software busyness tracking available we do not
99 * need the timer for I915_SAMPLE_BUSY counter.
101 * Use RCS as proxy for all engines.
103 else if (intel_engine_supports_stats(i915->engine[RCS]))
104 enable &= ~BIT(I915_SAMPLE_BUSY);
107 * If some bits remain it means we need the sampling timer running.
112 void i915_pmu_gt_parked(struct drm_i915_private *i915)
114 if (!i915->pmu.base.event_init)
117 spin_lock_irq(&i915->pmu.lock);
119 * Signal sampling timer to stop if only engine events are enabled and
122 i915->pmu.timer_enabled = pmu_needs_timer(i915, false);
123 spin_unlock_irq(&i915->pmu.lock);
126 static void __i915_pmu_maybe_start_timer(struct drm_i915_private *i915)
128 if (!i915->pmu.timer_enabled && pmu_needs_timer(i915, true)) {
129 i915->pmu.timer_enabled = true;
130 hrtimer_start_range_ns(&i915->pmu.timer,
131 ns_to_ktime(PERIOD), 0,
132 HRTIMER_MODE_REL_PINNED);
136 void i915_pmu_gt_unparked(struct drm_i915_private *i915)
138 if (!i915->pmu.base.event_init)
141 spin_lock_irq(&i915->pmu.lock);
143 * Re-enable sampling timer when GPU goes active.
145 __i915_pmu_maybe_start_timer(i915);
146 spin_unlock_irq(&i915->pmu.lock);
149 static bool grab_forcewake(struct drm_i915_private *i915, bool fw)
152 intel_uncore_forcewake_get(i915, FORCEWAKE_ALL);
158 update_sample(struct i915_pmu_sample *sample, u32 unit, u32 val)
160 sample->cur += mul_u32_u32(val, unit);
163 static void engines_sample(struct drm_i915_private *dev_priv)
165 struct intel_engine_cs *engine;
166 enum intel_engine_id id;
169 if ((dev_priv->pmu.enable & ENGINE_SAMPLE_MASK) == 0)
172 if (!dev_priv->gt.awake)
175 if (!intel_runtime_pm_get_if_in_use(dev_priv))
178 for_each_engine(engine, dev_priv, id) {
179 u32 current_seqno = intel_engine_get_seqno(engine);
180 u32 last_seqno = intel_engine_last_submit(engine);
183 val = !i915_seqno_passed(current_seqno, last_seqno);
185 update_sample(&engine->pmu.sample[I915_SAMPLE_BUSY],
188 if (val && (engine->pmu.enable &
189 (BIT(I915_SAMPLE_WAIT) | BIT(I915_SAMPLE_SEMA)))) {
190 fw = grab_forcewake(dev_priv, fw);
192 val = I915_READ_FW(RING_CTL(engine->mmio_base));
197 update_sample(&engine->pmu.sample[I915_SAMPLE_WAIT],
198 PERIOD, !!(val & RING_WAIT));
200 update_sample(&engine->pmu.sample[I915_SAMPLE_SEMA],
201 PERIOD, !!(val & RING_WAIT_SEMAPHORE));
205 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
207 intel_runtime_pm_put(dev_priv);
210 static void frequency_sample(struct drm_i915_private *dev_priv)
212 if (dev_priv->pmu.enable &
213 config_enabled_mask(I915_PMU_ACTUAL_FREQUENCY)) {
216 val = dev_priv->gt_pm.rps.cur_freq;
217 if (dev_priv->gt.awake &&
218 intel_runtime_pm_get_if_in_use(dev_priv)) {
219 val = intel_get_cagf(dev_priv,
220 I915_READ_NOTRACE(GEN6_RPSTAT1));
221 intel_runtime_pm_put(dev_priv);
224 update_sample(&dev_priv->pmu.sample[__I915_SAMPLE_FREQ_ACT],
225 1, intel_gpu_freq(dev_priv, val));
228 if (dev_priv->pmu.enable &
229 config_enabled_mask(I915_PMU_REQUESTED_FREQUENCY)) {
230 update_sample(&dev_priv->pmu.sample[__I915_SAMPLE_FREQ_REQ], 1,
231 intel_gpu_freq(dev_priv,
232 dev_priv->gt_pm.rps.cur_freq));
236 static enum hrtimer_restart i915_sample(struct hrtimer *hrtimer)
238 struct drm_i915_private *i915 =
239 container_of(hrtimer, struct drm_i915_private, pmu.timer);
241 if (!READ_ONCE(i915->pmu.timer_enabled))
242 return HRTIMER_NORESTART;
244 engines_sample(i915);
245 frequency_sample(i915);
247 hrtimer_forward_now(hrtimer, ns_to_ktime(PERIOD));
248 return HRTIMER_RESTART;
251 static u64 count_interrupts(struct drm_i915_private *i915)
253 /* open-coded kstat_irqs() */
254 struct irq_desc *desc = irq_to_desc(i915->drm.pdev->irq);
258 if (!desc || !desc->kstat_irqs)
261 for_each_possible_cpu(cpu)
262 sum += *per_cpu_ptr(desc->kstat_irqs, cpu);
267 static void engine_event_destroy(struct perf_event *event)
269 struct drm_i915_private *i915 =
270 container_of(event->pmu, typeof(*i915), pmu.base);
271 struct intel_engine_cs *engine;
273 engine = intel_engine_lookup_user(i915,
274 engine_event_class(event),
275 engine_event_instance(event));
276 if (WARN_ON_ONCE(!engine))
279 if (engine_event_sample(event) == I915_SAMPLE_BUSY &&
280 intel_engine_supports_stats(engine))
281 intel_disable_engine_stats(engine);
284 static void i915_pmu_event_destroy(struct perf_event *event)
286 WARN_ON(event->parent);
288 if (is_engine_event(event))
289 engine_event_destroy(event);
293 engine_event_status(struct intel_engine_cs *engine,
294 enum drm_i915_pmu_engine_sample sample)
297 case I915_SAMPLE_BUSY:
298 case I915_SAMPLE_WAIT:
300 case I915_SAMPLE_SEMA:
301 if (INTEL_GEN(engine->i915) < 6)
312 config_status(struct drm_i915_private *i915, u64 config)
315 case I915_PMU_ACTUAL_FREQUENCY:
316 if (IS_VALLEYVIEW(i915) || IS_CHERRYVIEW(i915))
317 /* Requires a mutex for sampling! */
320 case I915_PMU_REQUESTED_FREQUENCY:
321 if (INTEL_GEN(i915) < 6)
324 case I915_PMU_INTERRUPTS:
326 case I915_PMU_RC6_RESIDENCY:
337 static int engine_event_init(struct perf_event *event)
339 struct drm_i915_private *i915 =
340 container_of(event->pmu, typeof(*i915), pmu.base);
341 struct intel_engine_cs *engine;
345 engine = intel_engine_lookup_user(i915, engine_event_class(event),
346 engine_event_instance(event));
350 sample = engine_event_sample(event);
351 ret = engine_event_status(engine, sample);
355 if (sample == I915_SAMPLE_BUSY && intel_engine_supports_stats(engine))
356 ret = intel_enable_engine_stats(engine);
361 static int i915_pmu_event_init(struct perf_event *event)
363 struct drm_i915_private *i915 =
364 container_of(event->pmu, typeof(*i915), pmu.base);
367 if (event->attr.type != event->pmu->type)
370 /* unsupported modes and filters */
371 if (event->attr.sample_period) /* no sampling */
374 if (has_branch_stack(event))
380 /* only allow running on one cpu at a time */
381 if (!cpumask_test_cpu(event->cpu, &i915_pmu_cpumask))
384 if (is_engine_event(event))
385 ret = engine_event_init(event);
387 ret = config_status(i915, event->attr.config);
392 event->destroy = i915_pmu_event_destroy;
397 static u64 __get_rc6(struct drm_i915_private *i915)
401 val = intel_rc6_residency_ns(i915,
402 IS_VALLEYVIEW(i915) ?
407 val += intel_rc6_residency_ns(i915, GEN6_GT_GFX_RC6p);
410 val += intel_rc6_residency_ns(i915, GEN6_GT_GFX_RC6pp);
415 static u64 get_rc6(struct drm_i915_private *i915)
417 #if IS_ENABLED(CONFIG_PM)
421 if (intel_runtime_pm_get_if_in_use(i915)) {
422 val = __get_rc6(i915);
423 intel_runtime_pm_put(i915);
426 * If we are coming back from being runtime suspended we must
427 * be careful not to report a larger value than returned
431 spin_lock_irqsave(&i915->pmu.lock, flags);
433 if (val >= i915->pmu.sample[__I915_SAMPLE_RC6_ESTIMATED].cur) {
434 i915->pmu.sample[__I915_SAMPLE_RC6_ESTIMATED].cur = 0;
435 i915->pmu.sample[__I915_SAMPLE_RC6].cur = val;
437 val = i915->pmu.sample[__I915_SAMPLE_RC6_ESTIMATED].cur;
440 spin_unlock_irqrestore(&i915->pmu.lock, flags);
442 struct pci_dev *pdev = i915->drm.pdev;
443 struct device *kdev = &pdev->dev;
446 * We are runtime suspended.
448 * Report the delta from when the device was suspended to now,
449 * on top of the last known real value, as the approximated RC6
452 spin_lock_irqsave(&i915->pmu.lock, flags);
453 spin_lock(&kdev->power.lock);
456 * After the above branch intel_runtime_pm_get_if_in_use failed
457 * to get the runtime PM reference we cannot assume we are in
458 * runtime suspend since we can either: a) race with coming out
459 * of it before we took the power.lock, or b) there are other
460 * states than suspended which can bring us here.
462 * We need to double-check that we are indeed currently runtime
463 * suspended and if not we cannot do better than report the last
466 if (kdev->power.runtime_status == RPM_SUSPENDED) {
467 if (!i915->pmu.sample[__I915_SAMPLE_RC6_ESTIMATED].cur)
468 i915->pmu.suspended_jiffies_last =
469 kdev->power.suspended_jiffies;
471 val = kdev->power.suspended_jiffies -
472 i915->pmu.suspended_jiffies_last;
473 val += jiffies - kdev->power.accounting_timestamp;
475 val = jiffies_to_nsecs(val);
476 val += i915->pmu.sample[__I915_SAMPLE_RC6].cur;
478 i915->pmu.sample[__I915_SAMPLE_RC6_ESTIMATED].cur = val;
479 } else if (i915->pmu.sample[__I915_SAMPLE_RC6_ESTIMATED].cur) {
480 val = i915->pmu.sample[__I915_SAMPLE_RC6_ESTIMATED].cur;
482 val = i915->pmu.sample[__I915_SAMPLE_RC6].cur;
485 spin_unlock(&kdev->power.lock);
486 spin_unlock_irqrestore(&i915->pmu.lock, flags);
491 return __get_rc6(i915);
495 static u64 __i915_pmu_event_read(struct perf_event *event)
497 struct drm_i915_private *i915 =
498 container_of(event->pmu, typeof(*i915), pmu.base);
501 if (is_engine_event(event)) {
502 u8 sample = engine_event_sample(event);
503 struct intel_engine_cs *engine;
505 engine = intel_engine_lookup_user(i915,
506 engine_event_class(event),
507 engine_event_instance(event));
509 if (WARN_ON_ONCE(!engine)) {
511 } else if (sample == I915_SAMPLE_BUSY &&
512 intel_engine_supports_stats(engine)) {
513 val = ktime_to_ns(intel_engine_get_busy_time(engine));
515 val = engine->pmu.sample[sample].cur;
518 switch (event->attr.config) {
519 case I915_PMU_ACTUAL_FREQUENCY:
521 div_u64(i915->pmu.sample[__I915_SAMPLE_FREQ_ACT].cur,
524 case I915_PMU_REQUESTED_FREQUENCY:
526 div_u64(i915->pmu.sample[__I915_SAMPLE_FREQ_REQ].cur,
529 case I915_PMU_INTERRUPTS:
530 val = count_interrupts(i915);
532 case I915_PMU_RC6_RESIDENCY:
541 static void i915_pmu_event_read(struct perf_event *event)
543 struct hw_perf_event *hwc = &event->hw;
547 prev = local64_read(&hwc->prev_count);
548 new = __i915_pmu_event_read(event);
550 if (local64_cmpxchg(&hwc->prev_count, prev, new) != prev)
553 local64_add(new - prev, &event->count);
556 static void i915_pmu_enable(struct perf_event *event)
558 struct drm_i915_private *i915 =
559 container_of(event->pmu, typeof(*i915), pmu.base);
560 unsigned int bit = event_enabled_bit(event);
563 spin_lock_irqsave(&i915->pmu.lock, flags);
566 * Update the bitmask of enabled events and increment
567 * the event reference counter.
569 GEM_BUG_ON(bit >= I915_PMU_MASK_BITS);
570 GEM_BUG_ON(i915->pmu.enable_count[bit] == ~0);
571 i915->pmu.enable |= BIT_ULL(bit);
572 i915->pmu.enable_count[bit]++;
575 * Start the sampling timer if needed and not already enabled.
577 __i915_pmu_maybe_start_timer(i915);
580 * For per-engine events the bitmask and reference counting
581 * is stored per engine.
583 if (is_engine_event(event)) {
584 u8 sample = engine_event_sample(event);
585 struct intel_engine_cs *engine;
587 engine = intel_engine_lookup_user(i915,
588 engine_event_class(event),
589 engine_event_instance(event));
591 engine->pmu.enable |= BIT(sample);
593 GEM_BUG_ON(sample >= I915_PMU_SAMPLE_BITS);
594 GEM_BUG_ON(engine->pmu.enable_count[sample] == ~0);
595 engine->pmu.enable_count[sample]++;
598 spin_unlock_irqrestore(&i915->pmu.lock, flags);
601 * Store the current counter value so we can report the correct delta
602 * for all listeners. Even when the event was already enabled and has
603 * an existing non-zero value.
605 local64_set(&event->hw.prev_count, __i915_pmu_event_read(event));
608 static void i915_pmu_disable(struct perf_event *event)
610 struct drm_i915_private *i915 =
611 container_of(event->pmu, typeof(*i915), pmu.base);
612 unsigned int bit = event_enabled_bit(event);
615 spin_lock_irqsave(&i915->pmu.lock, flags);
617 if (is_engine_event(event)) {
618 u8 sample = engine_event_sample(event);
619 struct intel_engine_cs *engine;
621 engine = intel_engine_lookup_user(i915,
622 engine_event_class(event),
623 engine_event_instance(event));
625 GEM_BUG_ON(sample >= I915_PMU_SAMPLE_BITS);
626 GEM_BUG_ON(engine->pmu.enable_count[sample] == 0);
628 * Decrement the reference count and clear the enabled
629 * bitmask when the last listener on an event goes away.
631 if (--engine->pmu.enable_count[sample] == 0)
632 engine->pmu.enable &= ~BIT(sample);
635 GEM_BUG_ON(bit >= I915_PMU_MASK_BITS);
636 GEM_BUG_ON(i915->pmu.enable_count[bit] == 0);
638 * Decrement the reference count and clear the enabled
639 * bitmask when the last listener on an event goes away.
641 if (--i915->pmu.enable_count[bit] == 0) {
642 i915->pmu.enable &= ~BIT_ULL(bit);
643 i915->pmu.timer_enabled &= pmu_needs_timer(i915, true);
646 spin_unlock_irqrestore(&i915->pmu.lock, flags);
649 static void i915_pmu_event_start(struct perf_event *event, int flags)
651 i915_pmu_enable(event);
655 static void i915_pmu_event_stop(struct perf_event *event, int flags)
657 if (flags & PERF_EF_UPDATE)
658 i915_pmu_event_read(event);
659 i915_pmu_disable(event);
660 event->hw.state = PERF_HES_STOPPED;
663 static int i915_pmu_event_add(struct perf_event *event, int flags)
665 if (flags & PERF_EF_START)
666 i915_pmu_event_start(event, flags);
671 static void i915_pmu_event_del(struct perf_event *event, int flags)
673 i915_pmu_event_stop(event, PERF_EF_UPDATE);
676 static int i915_pmu_event_event_idx(struct perf_event *event)
681 struct i915_str_attribute {
682 struct device_attribute attr;
686 static ssize_t i915_pmu_format_show(struct device *dev,
687 struct device_attribute *attr, char *buf)
689 struct i915_str_attribute *eattr;
691 eattr = container_of(attr, struct i915_str_attribute, attr);
692 return sprintf(buf, "%s\n", eattr->str);
695 #define I915_PMU_FORMAT_ATTR(_name, _config) \
696 (&((struct i915_str_attribute[]) { \
697 { .attr = __ATTR(_name, 0444, i915_pmu_format_show, NULL), \
701 static struct attribute *i915_pmu_format_attrs[] = {
702 I915_PMU_FORMAT_ATTR(i915_eventid, "config:0-20"),
706 static const struct attribute_group i915_pmu_format_attr_group = {
708 .attrs = i915_pmu_format_attrs,
711 struct i915_ext_attribute {
712 struct device_attribute attr;
716 static ssize_t i915_pmu_event_show(struct device *dev,
717 struct device_attribute *attr, char *buf)
719 struct i915_ext_attribute *eattr;
721 eattr = container_of(attr, struct i915_ext_attribute, attr);
722 return sprintf(buf, "config=0x%lx\n", eattr->val);
725 static struct attribute_group i915_pmu_events_attr_group = {
727 /* Patch in attrs at runtime. */
731 i915_pmu_get_attr_cpumask(struct device *dev,
732 struct device_attribute *attr,
735 return cpumap_print_to_pagebuf(true, buf, &i915_pmu_cpumask);
738 static DEVICE_ATTR(cpumask, 0444, i915_pmu_get_attr_cpumask, NULL);
740 static struct attribute *i915_cpumask_attrs[] = {
741 &dev_attr_cpumask.attr,
745 static const struct attribute_group i915_pmu_cpumask_attr_group = {
746 .attrs = i915_cpumask_attrs,
749 static const struct attribute_group *i915_pmu_attr_groups[] = {
750 &i915_pmu_format_attr_group,
751 &i915_pmu_events_attr_group,
752 &i915_pmu_cpumask_attr_group,
756 #define __event(__config, __name, __unit) \
758 .config = (__config), \
763 #define __engine_event(__sample, __name) \
765 .sample = (__sample), \
769 static struct i915_ext_attribute *
770 add_i915_attr(struct i915_ext_attribute *attr, const char *name, u64 config)
772 sysfs_attr_init(&attr->attr.attr);
773 attr->attr.attr.name = name;
774 attr->attr.attr.mode = 0444;
775 attr->attr.show = i915_pmu_event_show;
781 static struct perf_pmu_events_attr *
782 add_pmu_attr(struct perf_pmu_events_attr *attr, const char *name,
785 sysfs_attr_init(&attr->attr.attr);
786 attr->attr.attr.name = name;
787 attr->attr.attr.mode = 0444;
788 attr->attr.show = perf_event_sysfs_show;
789 attr->event_str = str;
794 static struct attribute **
795 create_event_attributes(struct drm_i915_private *i915)
797 static const struct {
802 __event(I915_PMU_ACTUAL_FREQUENCY, "actual-frequency", "MHz"),
803 __event(I915_PMU_REQUESTED_FREQUENCY, "requested-frequency", "MHz"),
804 __event(I915_PMU_INTERRUPTS, "interrupts", NULL),
805 __event(I915_PMU_RC6_RESIDENCY, "rc6-residency", "ns"),
807 static const struct {
808 enum drm_i915_pmu_engine_sample sample;
810 } engine_events[] = {
811 __engine_event(I915_SAMPLE_BUSY, "busy"),
812 __engine_event(I915_SAMPLE_SEMA, "sema"),
813 __engine_event(I915_SAMPLE_WAIT, "wait"),
815 unsigned int count = 0;
816 struct perf_pmu_events_attr *pmu_attr = NULL, *pmu_iter;
817 struct i915_ext_attribute *i915_attr = NULL, *i915_iter;
818 struct attribute **attr = NULL, **attr_iter;
819 struct intel_engine_cs *engine;
820 enum intel_engine_id id;
823 /* Count how many counters we will be exposing. */
824 for (i = 0; i < ARRAY_SIZE(events); i++) {
825 if (!config_status(i915, events[i].config))
829 for_each_engine(engine, i915, id) {
830 for (i = 0; i < ARRAY_SIZE(engine_events); i++) {
831 if (!engine_event_status(engine,
832 engine_events[i].sample))
837 /* Allocate attribute objects and table. */
838 i915_attr = kcalloc(count, sizeof(*i915_attr), GFP_KERNEL);
842 pmu_attr = kcalloc(count, sizeof(*pmu_attr), GFP_KERNEL);
846 /* Max one pointer of each attribute type plus a termination entry. */
847 attr = kcalloc(count * 2 + 1, sizeof(*attr), GFP_KERNEL);
851 i915_iter = i915_attr;
855 /* Initialize supported non-engine counters. */
856 for (i = 0; i < ARRAY_SIZE(events); i++) {
859 if (config_status(i915, events[i].config))
862 str = kstrdup(events[i].name, GFP_KERNEL);
866 *attr_iter++ = &i915_iter->attr.attr;
867 i915_iter = add_i915_attr(i915_iter, str, events[i].config);
869 if (events[i].unit) {
870 str = kasprintf(GFP_KERNEL, "%s.unit", events[i].name);
874 *attr_iter++ = &pmu_iter->attr.attr;
875 pmu_iter = add_pmu_attr(pmu_iter, str, events[i].unit);
879 /* Initialize supported engine counters. */
880 for_each_engine(engine, i915, id) {
881 for (i = 0; i < ARRAY_SIZE(engine_events); i++) {
884 if (engine_event_status(engine,
885 engine_events[i].sample))
888 str = kasprintf(GFP_KERNEL, "%s-%s",
889 engine->name, engine_events[i].name);
893 *attr_iter++ = &i915_iter->attr.attr;
895 add_i915_attr(i915_iter, str,
896 __I915_PMU_ENGINE(engine->uabi_class,
898 engine_events[i].sample));
900 str = kasprintf(GFP_KERNEL, "%s-%s.unit",
901 engine->name, engine_events[i].name);
905 *attr_iter++ = &pmu_iter->attr.attr;
906 pmu_iter = add_pmu_attr(pmu_iter, str, "ns");
910 i915->pmu.i915_attr = i915_attr;
911 i915->pmu.pmu_attr = pmu_attr;
916 for (attr_iter = attr; *attr_iter; attr_iter++)
917 kfree((*attr_iter)->name);
927 static void free_event_attributes(struct drm_i915_private *i915)
929 struct attribute **attr_iter = i915_pmu_events_attr_group.attrs;
931 for (; *attr_iter; attr_iter++)
932 kfree((*attr_iter)->name);
934 kfree(i915_pmu_events_attr_group.attrs);
935 kfree(i915->pmu.i915_attr);
936 kfree(i915->pmu.pmu_attr);
938 i915_pmu_events_attr_group.attrs = NULL;
939 i915->pmu.i915_attr = NULL;
940 i915->pmu.pmu_attr = NULL;
943 static int i915_pmu_cpu_online(unsigned int cpu, struct hlist_node *node)
945 struct i915_pmu *pmu = hlist_entry_safe(node, typeof(*pmu), node);
947 GEM_BUG_ON(!pmu->base.event_init);
949 /* Select the first online CPU as a designated reader. */
950 if (!cpumask_weight(&i915_pmu_cpumask))
951 cpumask_set_cpu(cpu, &i915_pmu_cpumask);
956 static int i915_pmu_cpu_offline(unsigned int cpu, struct hlist_node *node)
958 struct i915_pmu *pmu = hlist_entry_safe(node, typeof(*pmu), node);
961 GEM_BUG_ON(!pmu->base.event_init);
963 if (cpumask_test_and_clear_cpu(cpu, &i915_pmu_cpumask)) {
964 target = cpumask_any_but(topology_sibling_cpumask(cpu), cpu);
965 /* Migrate events if there is a valid target */
966 if (target < nr_cpu_ids) {
967 cpumask_set_cpu(target, &i915_pmu_cpumask);
968 perf_pmu_migrate_context(&pmu->base, cpu, target);
975 static enum cpuhp_state cpuhp_slot = CPUHP_INVALID;
977 static int i915_pmu_register_cpuhp_state(struct drm_i915_private *i915)
979 enum cpuhp_state slot;
982 ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN,
983 "perf/x86/intel/i915:online",
985 i915_pmu_cpu_offline);
990 ret = cpuhp_state_add_instance(slot, &i915->pmu.node);
992 cpuhp_remove_multi_state(slot);
1000 static void i915_pmu_unregister_cpuhp_state(struct drm_i915_private *i915)
1002 WARN_ON(cpuhp_slot == CPUHP_INVALID);
1003 WARN_ON(cpuhp_state_remove_instance(cpuhp_slot, &i915->pmu.node));
1004 cpuhp_remove_multi_state(cpuhp_slot);
1007 void i915_pmu_register(struct drm_i915_private *i915)
1011 if (INTEL_GEN(i915) <= 2) {
1012 DRM_INFO("PMU not supported for this GPU.");
1016 i915_pmu_events_attr_group.attrs = create_event_attributes(i915);
1017 if (!i915_pmu_events_attr_group.attrs) {
1022 i915->pmu.base.attr_groups = i915_pmu_attr_groups;
1023 i915->pmu.base.task_ctx_nr = perf_invalid_context;
1024 i915->pmu.base.event_init = i915_pmu_event_init;
1025 i915->pmu.base.add = i915_pmu_event_add;
1026 i915->pmu.base.del = i915_pmu_event_del;
1027 i915->pmu.base.start = i915_pmu_event_start;
1028 i915->pmu.base.stop = i915_pmu_event_stop;
1029 i915->pmu.base.read = i915_pmu_event_read;
1030 i915->pmu.base.event_idx = i915_pmu_event_event_idx;
1032 spin_lock_init(&i915->pmu.lock);
1033 hrtimer_init(&i915->pmu.timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1034 i915->pmu.timer.function = i915_sample;
1036 ret = perf_pmu_register(&i915->pmu.base, "i915", -1);
1040 ret = i915_pmu_register_cpuhp_state(i915);
1047 perf_pmu_unregister(&i915->pmu.base);
1049 i915->pmu.base.event_init = NULL;
1050 free_event_attributes(i915);
1051 DRM_NOTE("Failed to register PMU! (err=%d)\n", ret);
1054 void i915_pmu_unregister(struct drm_i915_private *i915)
1056 if (!i915->pmu.base.event_init)
1059 WARN_ON(i915->pmu.enable);
1061 hrtimer_cancel(&i915->pmu.timer);
1063 i915_pmu_unregister_cpuhp_state(i915);
1065 perf_pmu_unregister(&i915->pmu.base);
1066 i915->pmu.base.event_init = NULL;
1067 free_event_attributes(i915);