1b46a33e2STvrtko Ursulin /* 2058a9b43SMichal Wajdeczko * SPDX-License-Identifier: MIT 3b46a33e2STvrtko Ursulin * 4058a9b43SMichal Wajdeczko * Copyright © 2017-2018 Intel Corporation 5b46a33e2STvrtko Ursulin */ 6b46a33e2STvrtko Ursulin 7447ae316SNicolai Stange #include <linux/irq.h> 83b4ed2e2SVincent Guittot #include <linux/pm_runtime.h> 9112ed2d3SChris Wilson 10112ed2d3SChris Wilson #include "gt/intel_engine.h" 1151fbd8deSChris Wilson #include "gt/intel_engine_pm.h" 12750e76b4SChris Wilson #include "gt/intel_engine_user.h" 1351fbd8deSChris Wilson #include "gt/intel_gt_pm.h" 14c1132367SAndi Shyti #include "gt/intel_rc6.h" 153e7abf81SAndi Shyti #include "gt/intel_rps.h" 16112ed2d3SChris Wilson 17058a9b43SMichal Wajdeczko #include "i915_drv.h" 18ecbb5fb7SJani Nikula #include "i915_pmu.h" 19ecbb5fb7SJani Nikula #include "intel_pm.h" 20b46a33e2STvrtko Ursulin 21b46a33e2STvrtko Ursulin /* Frequency for the sampling timer for events which need it. */ 22b46a33e2STvrtko Ursulin #define FREQUENCY 200 23b46a33e2STvrtko Ursulin #define PERIOD max_t(u64, 10000, NSEC_PER_SEC / FREQUENCY) 24b46a33e2STvrtko Ursulin 25b46a33e2STvrtko Ursulin #define ENGINE_SAMPLE_MASK \ 26b46a33e2STvrtko Ursulin (BIT(I915_SAMPLE_BUSY) | \ 27b46a33e2STvrtko Ursulin BIT(I915_SAMPLE_WAIT) | \ 28b46a33e2STvrtko Ursulin BIT(I915_SAMPLE_SEMA)) 29b46a33e2STvrtko Ursulin 30b46a33e2STvrtko Ursulin #define ENGINE_SAMPLE_BITS (1 << I915_PMU_SAMPLE_BITS) 31b46a33e2STvrtko Ursulin 32141a0895SChris Wilson static cpumask_t i915_pmu_cpumask; 33b46a33e2STvrtko Ursulin 34b46a33e2STvrtko Ursulin static u8 engine_config_sample(u64 config) 35b46a33e2STvrtko Ursulin { 36b46a33e2STvrtko Ursulin return config & I915_PMU_SAMPLE_MASK; 37b46a33e2STvrtko Ursulin } 38b46a33e2STvrtko Ursulin 39b46a33e2STvrtko Ursulin static u8 engine_event_sample(struct perf_event *event) 40b46a33e2STvrtko Ursulin { 41b46a33e2STvrtko Ursulin return engine_config_sample(event->attr.config); 42b46a33e2STvrtko Ursulin } 43b46a33e2STvrtko Ursulin 44b46a33e2STvrtko Ursulin static u8 engine_event_class(struct perf_event *event) 45b46a33e2STvrtko Ursulin { 46b46a33e2STvrtko Ursulin return (event->attr.config >> I915_PMU_CLASS_SHIFT) & 0xff; 47b46a33e2STvrtko Ursulin } 48b46a33e2STvrtko Ursulin 49b46a33e2STvrtko Ursulin static u8 engine_event_instance(struct perf_event *event) 50b46a33e2STvrtko Ursulin { 51b46a33e2STvrtko Ursulin return (event->attr.config >> I915_PMU_SAMPLE_BITS) & 0xff; 52b46a33e2STvrtko Ursulin } 53b46a33e2STvrtko Ursulin 54b46a33e2STvrtko Ursulin static bool is_engine_config(u64 config) 55b46a33e2STvrtko Ursulin { 56b46a33e2STvrtko Ursulin return config < __I915_PMU_OTHER(0); 57b46a33e2STvrtko Ursulin } 58b46a33e2STvrtko Ursulin 59b46a33e2STvrtko Ursulin static unsigned int config_enabled_bit(u64 config) 60b46a33e2STvrtko Ursulin { 61b46a33e2STvrtko Ursulin if (is_engine_config(config)) 62b46a33e2STvrtko Ursulin return engine_config_sample(config); 63b46a33e2STvrtko Ursulin else 64b46a33e2STvrtko Ursulin return ENGINE_SAMPLE_BITS + (config - __I915_PMU_OTHER(0)); 65b46a33e2STvrtko Ursulin } 66b46a33e2STvrtko Ursulin 67b46a33e2STvrtko Ursulin static u64 config_enabled_mask(u64 config) 68b46a33e2STvrtko Ursulin { 69b46a33e2STvrtko Ursulin return BIT_ULL(config_enabled_bit(config)); 70b46a33e2STvrtko Ursulin } 71b46a33e2STvrtko Ursulin 72b46a33e2STvrtko Ursulin static bool is_engine_event(struct perf_event *event) 73b46a33e2STvrtko Ursulin { 74b46a33e2STvrtko Ursulin return is_engine_config(event->attr.config); 75b46a33e2STvrtko Ursulin } 76b46a33e2STvrtko Ursulin 77b46a33e2STvrtko Ursulin static unsigned int event_enabled_bit(struct perf_event *event) 78b46a33e2STvrtko Ursulin { 79b46a33e2STvrtko Ursulin return config_enabled_bit(event->attr.config); 80b46a33e2STvrtko Ursulin } 81b46a33e2STvrtko Ursulin 82908091c8STvrtko Ursulin static bool pmu_needs_timer(struct i915_pmu *pmu, bool gpu_active) 83feff0dc6STvrtko Ursulin { 84908091c8STvrtko Ursulin struct drm_i915_private *i915 = container_of(pmu, typeof(*i915), pmu); 85feff0dc6STvrtko Ursulin u64 enable; 86feff0dc6STvrtko Ursulin 87feff0dc6STvrtko Ursulin /* 88feff0dc6STvrtko Ursulin * Only some counters need the sampling timer. 89feff0dc6STvrtko Ursulin * 90feff0dc6STvrtko Ursulin * We start with a bitmask of all currently enabled events. 91feff0dc6STvrtko Ursulin */ 92908091c8STvrtko Ursulin enable = pmu->enable; 93feff0dc6STvrtko Ursulin 94feff0dc6STvrtko Ursulin /* 95feff0dc6STvrtko Ursulin * Mask out all the ones which do not need the timer, or in 96feff0dc6STvrtko Ursulin * other words keep all the ones that could need the timer. 97feff0dc6STvrtko Ursulin */ 98feff0dc6STvrtko Ursulin enable &= config_enabled_mask(I915_PMU_ACTUAL_FREQUENCY) | 99feff0dc6STvrtko Ursulin config_enabled_mask(I915_PMU_REQUESTED_FREQUENCY) | 100feff0dc6STvrtko Ursulin ENGINE_SAMPLE_MASK; 101feff0dc6STvrtko Ursulin 102feff0dc6STvrtko Ursulin /* 103feff0dc6STvrtko Ursulin * When the GPU is idle per-engine counters do not need to be 104feff0dc6STvrtko Ursulin * running so clear those bits out. 105feff0dc6STvrtko Ursulin */ 106feff0dc6STvrtko Ursulin if (!gpu_active) 107feff0dc6STvrtko Ursulin enable &= ~ENGINE_SAMPLE_MASK; 108b3add01eSTvrtko Ursulin /* 109b3add01eSTvrtko Ursulin * Also there is software busyness tracking available we do not 110b3add01eSTvrtko Ursulin * need the timer for I915_SAMPLE_BUSY counter. 111b3add01eSTvrtko Ursulin */ 112bf73fc0fSChris Wilson else if (i915->caps.scheduler & I915_SCHEDULER_CAP_ENGINE_BUSY_STATS) 113b3add01eSTvrtko Ursulin enable &= ~BIT(I915_SAMPLE_BUSY); 114feff0dc6STvrtko Ursulin 115feff0dc6STvrtko Ursulin /* 116feff0dc6STvrtko Ursulin * If some bits remain it means we need the sampling timer running. 117feff0dc6STvrtko Ursulin */ 118feff0dc6STvrtko Ursulin return enable; 119feff0dc6STvrtko Ursulin } 120feff0dc6STvrtko Ursulin 121c1132367SAndi Shyti static u64 __get_rc6(struct intel_gt *gt) 12216ffe73cSChris Wilson { 12316ffe73cSChris Wilson struct drm_i915_private *i915 = gt->i915; 12416ffe73cSChris Wilson u64 val; 12516ffe73cSChris Wilson 126c1132367SAndi Shyti val = intel_rc6_residency_ns(>->rc6, 12716ffe73cSChris Wilson IS_VALLEYVIEW(i915) ? 12816ffe73cSChris Wilson VLV_GT_RENDER_RC6 : 12916ffe73cSChris Wilson GEN6_GT_GFX_RC6); 13016ffe73cSChris Wilson 13116ffe73cSChris Wilson if (HAS_RC6p(i915)) 132c1132367SAndi Shyti val += intel_rc6_residency_ns(>->rc6, GEN6_GT_GFX_RC6p); 13316ffe73cSChris Wilson 13416ffe73cSChris Wilson if (HAS_RC6pp(i915)) 135c1132367SAndi Shyti val += intel_rc6_residency_ns(>->rc6, GEN6_GT_GFX_RC6pp); 13616ffe73cSChris Wilson 13716ffe73cSChris Wilson return val; 13816ffe73cSChris Wilson } 13916ffe73cSChris Wilson 14016ffe73cSChris Wilson #if IS_ENABLED(CONFIG_PM) 14116ffe73cSChris Wilson 14216ffe73cSChris Wilson static inline s64 ktime_since(const ktime_t kt) 14316ffe73cSChris Wilson { 14416ffe73cSChris Wilson return ktime_to_ns(ktime_sub(ktime_get(), kt)); 14516ffe73cSChris Wilson } 14616ffe73cSChris Wilson 147df6a4205STvrtko Ursulin static u64 get_rc6(struct intel_gt *gt) 14816ffe73cSChris Wilson { 149df6a4205STvrtko Ursulin struct drm_i915_private *i915 = gt->i915; 150df6a4205STvrtko Ursulin struct i915_pmu *pmu = &i915->pmu; 151df6a4205STvrtko Ursulin unsigned long flags; 152df6a4205STvrtko Ursulin bool awake = false; 15316ffe73cSChris Wilson u64 val; 15416ffe73cSChris Wilson 155df6a4205STvrtko Ursulin if (intel_gt_pm_get_if_awake(gt)) { 156df6a4205STvrtko Ursulin val = __get_rc6(gt); 157df6a4205STvrtko Ursulin intel_gt_pm_put_async(gt); 158df6a4205STvrtko Ursulin awake = true; 159df6a4205STvrtko Ursulin } 160df6a4205STvrtko Ursulin 161df6a4205STvrtko Ursulin spin_lock_irqsave(&pmu->lock, flags); 162df6a4205STvrtko Ursulin 163df6a4205STvrtko Ursulin if (awake) { 164df6a4205STvrtko Ursulin pmu->sample[__I915_SAMPLE_RC6].cur = val; 165df6a4205STvrtko Ursulin } else { 16616ffe73cSChris Wilson /* 16716ffe73cSChris Wilson * We think we are runtime suspended. 16816ffe73cSChris Wilson * 16916ffe73cSChris Wilson * Report the delta from when the device was suspended to now, 17016ffe73cSChris Wilson * on top of the last known real value, as the approximated RC6 17116ffe73cSChris Wilson * counter value. 17216ffe73cSChris Wilson */ 17316ffe73cSChris Wilson val = ktime_since(pmu->sleep_last); 17416ffe73cSChris Wilson val += pmu->sample[__I915_SAMPLE_RC6].cur; 17516ffe73cSChris Wilson } 17616ffe73cSChris Wilson 177df6a4205STvrtko Ursulin if (val < pmu->sample[__I915_SAMPLE_RC6_LAST_REPORTED].cur) 178df6a4205STvrtko Ursulin val = pmu->sample[__I915_SAMPLE_RC6_LAST_REPORTED].cur; 17916ffe73cSChris Wilson else 180df6a4205STvrtko Ursulin pmu->sample[__I915_SAMPLE_RC6_LAST_REPORTED].cur = val; 18116ffe73cSChris Wilson 18216ffe73cSChris Wilson spin_unlock_irqrestore(&pmu->lock, flags); 18316ffe73cSChris Wilson 18416ffe73cSChris Wilson return val; 18516ffe73cSChris Wilson } 18616ffe73cSChris Wilson 18716ffe73cSChris Wilson static void park_rc6(struct drm_i915_private *i915) 188feff0dc6STvrtko Ursulin { 189908091c8STvrtko Ursulin struct i915_pmu *pmu = &i915->pmu; 190908091c8STvrtko Ursulin 19116ffe73cSChris Wilson if (pmu->enable & config_enabled_mask(I915_PMU_RC6_RESIDENCY)) 192df6a4205STvrtko Ursulin pmu->sample[__I915_SAMPLE_RC6].cur = __get_rc6(&i915->gt); 193feff0dc6STvrtko Ursulin 19416ffe73cSChris Wilson pmu->sleep_last = ktime_get(); 195feff0dc6STvrtko Ursulin } 196feff0dc6STvrtko Ursulin 19716ffe73cSChris Wilson #else 19816ffe73cSChris Wilson 19916ffe73cSChris Wilson static u64 get_rc6(struct intel_gt *gt) 20016ffe73cSChris Wilson { 20116ffe73cSChris Wilson return __get_rc6(gt); 20216ffe73cSChris Wilson } 20316ffe73cSChris Wilson 20416ffe73cSChris Wilson static void park_rc6(struct drm_i915_private *i915) {} 20516ffe73cSChris Wilson 20616ffe73cSChris Wilson #endif 20716ffe73cSChris Wilson 208908091c8STvrtko Ursulin static void __i915_pmu_maybe_start_timer(struct i915_pmu *pmu) 209feff0dc6STvrtko Ursulin { 210908091c8STvrtko Ursulin if (!pmu->timer_enabled && pmu_needs_timer(pmu, true)) { 211908091c8STvrtko Ursulin pmu->timer_enabled = true; 212908091c8STvrtko Ursulin pmu->timer_last = ktime_get(); 213908091c8STvrtko Ursulin hrtimer_start_range_ns(&pmu->timer, 214feff0dc6STvrtko Ursulin ns_to_ktime(PERIOD), 0, 215feff0dc6STvrtko Ursulin HRTIMER_MODE_REL_PINNED); 216feff0dc6STvrtko Ursulin } 217feff0dc6STvrtko Ursulin } 218feff0dc6STvrtko Ursulin 21916ffe73cSChris Wilson void i915_pmu_gt_parked(struct drm_i915_private *i915) 22016ffe73cSChris Wilson { 22116ffe73cSChris Wilson struct i915_pmu *pmu = &i915->pmu; 22216ffe73cSChris Wilson 22316ffe73cSChris Wilson if (!pmu->base.event_init) 22416ffe73cSChris Wilson return; 22516ffe73cSChris Wilson 22616ffe73cSChris Wilson spin_lock_irq(&pmu->lock); 22716ffe73cSChris Wilson 22816ffe73cSChris Wilson park_rc6(i915); 22916ffe73cSChris Wilson 23016ffe73cSChris Wilson /* 23116ffe73cSChris Wilson * Signal sampling timer to stop if only engine events are enabled and 23216ffe73cSChris Wilson * GPU went idle. 23316ffe73cSChris Wilson */ 23416ffe73cSChris Wilson pmu->timer_enabled = pmu_needs_timer(pmu, false); 23516ffe73cSChris Wilson 23616ffe73cSChris Wilson spin_unlock_irq(&pmu->lock); 23716ffe73cSChris Wilson } 23816ffe73cSChris Wilson 239feff0dc6STvrtko Ursulin void i915_pmu_gt_unparked(struct drm_i915_private *i915) 240feff0dc6STvrtko Ursulin { 241908091c8STvrtko Ursulin struct i915_pmu *pmu = &i915->pmu; 242908091c8STvrtko Ursulin 243908091c8STvrtko Ursulin if (!pmu->base.event_init) 244feff0dc6STvrtko Ursulin return; 245feff0dc6STvrtko Ursulin 246908091c8STvrtko Ursulin spin_lock_irq(&pmu->lock); 24716ffe73cSChris Wilson 248feff0dc6STvrtko Ursulin /* 249feff0dc6STvrtko Ursulin * Re-enable sampling timer when GPU goes active. 250feff0dc6STvrtko Ursulin */ 251908091c8STvrtko Ursulin __i915_pmu_maybe_start_timer(pmu); 25216ffe73cSChris Wilson 253908091c8STvrtko Ursulin spin_unlock_irq(&pmu->lock); 254feff0dc6STvrtko Ursulin } 255feff0dc6STvrtko Ursulin 256b46a33e2STvrtko Ursulin static void 2579f473ecfSTvrtko Ursulin add_sample(struct i915_pmu_sample *sample, u32 val) 258b46a33e2STvrtko Ursulin { 2599f473ecfSTvrtko Ursulin sample->cur += val; 260b46a33e2STvrtko Ursulin } 261b46a33e2STvrtko Ursulin 262d79e1bd6SChris Wilson static bool exclusive_mmio_access(const struct drm_i915_private *i915) 263d79e1bd6SChris Wilson { 264d79e1bd6SChris Wilson /* 265d79e1bd6SChris Wilson * We have to avoid concurrent mmio cache line access on gen7 or 266d79e1bd6SChris Wilson * risk a machine hang. For a fun history lesson dig out the old 267d79e1bd6SChris Wilson * userspace intel_gpu_top and run it on Ivybridge or Haswell! 268d79e1bd6SChris Wilson */ 269d79e1bd6SChris Wilson return IS_GEN(i915, 7); 270d79e1bd6SChris Wilson } 271d79e1bd6SChris Wilson 2729f473ecfSTvrtko Ursulin static void 27308ce5c64STvrtko Ursulin engines_sample(struct intel_gt *gt, unsigned int period_ns) 274b46a33e2STvrtko Ursulin { 27508ce5c64STvrtko Ursulin struct drm_i915_private *i915 = gt->i915; 276b46a33e2STvrtko Ursulin struct intel_engine_cs *engine; 277b46a33e2STvrtko Ursulin enum intel_engine_id id; 278b46a33e2STvrtko Ursulin 27928fba096STvrtko Ursulin if ((i915->pmu.enable & ENGINE_SAMPLE_MASK) == 0) 280b46a33e2STvrtko Ursulin return; 281b46a33e2STvrtko Ursulin 282edb1ecadSChris Wilson if (!intel_gt_pm_is_awake(gt)) 283edb1ecadSChris Wilson return; 284edb1ecadSChris Wilson 285c6e07adaSChris Wilson for_each_engine(engine, gt, id) { 286d0aa694bSChris Wilson struct intel_engine_pmu *pmu = &engine->pmu; 287d79e1bd6SChris Wilson spinlock_t *mmio_lock; 28851fbd8deSChris Wilson unsigned long flags; 289d0aa694bSChris Wilson bool busy; 290b46a33e2STvrtko Ursulin u32 val; 291b46a33e2STvrtko Ursulin 29251fbd8deSChris Wilson if (!intel_engine_pm_get_if_awake(engine)) 29351fbd8deSChris Wilson continue; 29451fbd8deSChris Wilson 295d79e1bd6SChris Wilson mmio_lock = NULL; 296d79e1bd6SChris Wilson if (exclusive_mmio_access(i915)) 297d79e1bd6SChris Wilson mmio_lock = &engine->uncore->lock; 298d79e1bd6SChris Wilson 299d79e1bd6SChris Wilson if (unlikely(mmio_lock)) 300d79e1bd6SChris Wilson spin_lock_irqsave(mmio_lock, flags); 30151fbd8deSChris Wilson 30228fba096STvrtko Ursulin val = ENGINE_READ_FW(engine, RING_CTL); 303d0aa694bSChris Wilson if (val == 0) /* powerwell off => engine idle */ 30451fbd8deSChris Wilson goto skip; 305b46a33e2STvrtko Ursulin 3069f473ecfSTvrtko Ursulin if (val & RING_WAIT) 307d0aa694bSChris Wilson add_sample(&pmu->sample[I915_SAMPLE_WAIT], period_ns); 3089f473ecfSTvrtko Ursulin if (val & RING_WAIT_SEMAPHORE) 309d0aa694bSChris Wilson add_sample(&pmu->sample[I915_SAMPLE_SEMA], period_ns); 310b46a33e2STvrtko Ursulin 31154fc577dSTvrtko Ursulin /* No need to sample when busy stats are supported. */ 31254fc577dSTvrtko Ursulin if (intel_engine_supports_stats(engine)) 31354fc577dSTvrtko Ursulin goto skip; 31454fc577dSTvrtko Ursulin 315d0aa694bSChris Wilson /* 316d0aa694bSChris Wilson * While waiting on a semaphore or event, MI_MODE reports the 317d0aa694bSChris Wilson * ring as idle. However, previously using the seqno, and with 318d0aa694bSChris Wilson * execlists sampling, we account for the ring waiting as the 319d0aa694bSChris Wilson * engine being busy. Therefore, we record the sample as being 320d0aa694bSChris Wilson * busy if either waiting or !idle. 321d0aa694bSChris Wilson */ 322d0aa694bSChris Wilson busy = val & (RING_WAIT_SEMAPHORE | RING_WAIT); 323d0aa694bSChris Wilson if (!busy) { 32428fba096STvrtko Ursulin val = ENGINE_READ_FW(engine, RING_MI_MODE); 325d0aa694bSChris Wilson busy = !(val & MODE_IDLE); 326d0aa694bSChris Wilson } 327d0aa694bSChris Wilson if (busy) 328d0aa694bSChris Wilson add_sample(&pmu->sample[I915_SAMPLE_BUSY], period_ns); 329b46a33e2STvrtko Ursulin 33051fbd8deSChris Wilson skip: 331d79e1bd6SChris Wilson if (unlikely(mmio_lock)) 332d79e1bd6SChris Wilson spin_unlock_irqrestore(mmio_lock, flags); 33307779a76SChris Wilson intel_engine_pm_put_async(engine); 33451fbd8deSChris Wilson } 335b46a33e2STvrtko Ursulin } 336b46a33e2STvrtko Ursulin 3379f473ecfSTvrtko Ursulin static void 3389f473ecfSTvrtko Ursulin add_sample_mult(struct i915_pmu_sample *sample, u32 val, u32 mul) 3399f473ecfSTvrtko Ursulin { 3409f473ecfSTvrtko Ursulin sample->cur += mul_u32_u32(val, mul); 3419f473ecfSTvrtko Ursulin } 3429f473ecfSTvrtko Ursulin 343b66ecd04STvrtko Ursulin static bool frequency_sampling_enabled(struct i915_pmu *pmu) 344b66ecd04STvrtko Ursulin { 345b66ecd04STvrtko Ursulin return pmu->enable & 346b66ecd04STvrtko Ursulin (config_enabled_mask(I915_PMU_ACTUAL_FREQUENCY) | 347b66ecd04STvrtko Ursulin config_enabled_mask(I915_PMU_REQUESTED_FREQUENCY)); 348b66ecd04STvrtko Ursulin } 349b66ecd04STvrtko Ursulin 3509f473ecfSTvrtko Ursulin static void 35108ce5c64STvrtko Ursulin frequency_sample(struct intel_gt *gt, unsigned int period_ns) 352b46a33e2STvrtko Ursulin { 35308ce5c64STvrtko Ursulin struct drm_i915_private *i915 = gt->i915; 35408ce5c64STvrtko Ursulin struct intel_uncore *uncore = gt->uncore; 35508ce5c64STvrtko Ursulin struct i915_pmu *pmu = &i915->pmu; 3563e7abf81SAndi Shyti struct intel_rps *rps = >->rps; 35708ce5c64STvrtko Ursulin 358b66ecd04STvrtko Ursulin if (!frequency_sampling_enabled(pmu)) 359b66ecd04STvrtko Ursulin return; 360b66ecd04STvrtko Ursulin 361b66ecd04STvrtko Ursulin /* Report 0/0 (actual/requested) frequency while parked. */ 362b66ecd04STvrtko Ursulin if (!intel_gt_pm_get_if_awake(gt)) 363b66ecd04STvrtko Ursulin return; 364b66ecd04STvrtko Ursulin 36508ce5c64STvrtko Ursulin if (pmu->enable & config_enabled_mask(I915_PMU_ACTUAL_FREQUENCY)) { 366b46a33e2STvrtko Ursulin u32 val; 367b46a33e2STvrtko Ursulin 368c1c82d26SChris Wilson /* 369c1c82d26SChris Wilson * We take a quick peek here without using forcewake 370c1c82d26SChris Wilson * so that we don't perturb the system under observation 371c1c82d26SChris Wilson * (forcewake => !rc6 => increased power use). We expect 372c1c82d26SChris Wilson * that if the read fails because it is outside of the 373c1c82d26SChris Wilson * mmio power well, then it will return 0 -- in which 374c1c82d26SChris Wilson * case we assume the system is running at the intended 375c1c82d26SChris Wilson * frequency. Fortunately, the read should rarely fail! 376c1c82d26SChris Wilson */ 377b66ecd04STvrtko Ursulin val = intel_uncore_read_fw(uncore, GEN6_RPSTAT1); 378b66ecd04STvrtko Ursulin if (val) 379e03512edSAndi Shyti val = intel_rps_get_cagf(rps, val); 380b66ecd04STvrtko Ursulin else 381b66ecd04STvrtko Ursulin val = rps->cur_freq; 382b46a33e2STvrtko Ursulin 38308ce5c64STvrtko Ursulin add_sample_mult(&pmu->sample[__I915_SAMPLE_FREQ_ACT], 384b66ecd04STvrtko Ursulin intel_gpu_freq(rps, val), period_ns / 1000); 385b46a33e2STvrtko Ursulin } 386b46a33e2STvrtko Ursulin 38708ce5c64STvrtko Ursulin if (pmu->enable & config_enabled_mask(I915_PMU_REQUESTED_FREQUENCY)) { 38808ce5c64STvrtko Ursulin add_sample_mult(&pmu->sample[__I915_SAMPLE_FREQ_REQ], 3893e7abf81SAndi Shyti intel_gpu_freq(rps, rps->cur_freq), 3909f473ecfSTvrtko Ursulin period_ns / 1000); 391b46a33e2STvrtko Ursulin } 392b66ecd04STvrtko Ursulin 393b66ecd04STvrtko Ursulin intel_gt_pm_put_async(gt); 394b46a33e2STvrtko Ursulin } 395b46a33e2STvrtko Ursulin 396b46a33e2STvrtko Ursulin static enum hrtimer_restart i915_sample(struct hrtimer *hrtimer) 397b46a33e2STvrtko Ursulin { 398b46a33e2STvrtko Ursulin struct drm_i915_private *i915 = 399b46a33e2STvrtko Ursulin container_of(hrtimer, struct drm_i915_private, pmu.timer); 400908091c8STvrtko Ursulin struct i915_pmu *pmu = &i915->pmu; 40108ce5c64STvrtko Ursulin struct intel_gt *gt = &i915->gt; 4029f473ecfSTvrtko Ursulin unsigned int period_ns; 4039f473ecfSTvrtko Ursulin ktime_t now; 404b46a33e2STvrtko Ursulin 405908091c8STvrtko Ursulin if (!READ_ONCE(pmu->timer_enabled)) 406b46a33e2STvrtko Ursulin return HRTIMER_NORESTART; 407b46a33e2STvrtko Ursulin 4089f473ecfSTvrtko Ursulin now = ktime_get(); 409908091c8STvrtko Ursulin period_ns = ktime_to_ns(ktime_sub(now, pmu->timer_last)); 410908091c8STvrtko Ursulin pmu->timer_last = now; 411b46a33e2STvrtko Ursulin 4129f473ecfSTvrtko Ursulin /* 4139f473ecfSTvrtko Ursulin * Strictly speaking the passed in period may not be 100% accurate for 4149f473ecfSTvrtko Ursulin * all internal calculation, since some amount of time can be spent on 4159f473ecfSTvrtko Ursulin * grabbing the forcewake. However the potential error from timer call- 4169f473ecfSTvrtko Ursulin * back delay greatly dominates this so we keep it simple. 4179f473ecfSTvrtko Ursulin */ 41808ce5c64STvrtko Ursulin engines_sample(gt, period_ns); 41908ce5c64STvrtko Ursulin frequency_sample(gt, period_ns); 4209f473ecfSTvrtko Ursulin 4219f473ecfSTvrtko Ursulin hrtimer_forward(hrtimer, now, ns_to_ktime(PERIOD)); 4229f473ecfSTvrtko Ursulin 423b46a33e2STvrtko Ursulin return HRTIMER_RESTART; 424b46a33e2STvrtko Ursulin } 425b46a33e2STvrtko Ursulin 4260cd4684dSTvrtko Ursulin static u64 count_interrupts(struct drm_i915_private *i915) 4270cd4684dSTvrtko Ursulin { 4280cd4684dSTvrtko Ursulin /* open-coded kstat_irqs() */ 4290cd4684dSTvrtko Ursulin struct irq_desc *desc = irq_to_desc(i915->drm.pdev->irq); 4300cd4684dSTvrtko Ursulin u64 sum = 0; 4310cd4684dSTvrtko Ursulin int cpu; 4320cd4684dSTvrtko Ursulin 4330cd4684dSTvrtko Ursulin if (!desc || !desc->kstat_irqs) 4340cd4684dSTvrtko Ursulin return 0; 4350cd4684dSTvrtko Ursulin 4360cd4684dSTvrtko Ursulin for_each_possible_cpu(cpu) 4370cd4684dSTvrtko Ursulin sum += *per_cpu_ptr(desc->kstat_irqs, cpu); 4380cd4684dSTvrtko Ursulin 4390cd4684dSTvrtko Ursulin return sum; 4400cd4684dSTvrtko Ursulin } 4410cd4684dSTvrtko Ursulin 442b46a33e2STvrtko Ursulin static void i915_pmu_event_destroy(struct perf_event *event) 443b46a33e2STvrtko Ursulin { 444b46a33e2STvrtko Ursulin WARN_ON(event->parent); 445b46a33e2STvrtko Ursulin } 446b46a33e2STvrtko Ursulin 447109ec558STvrtko Ursulin static int 448109ec558STvrtko Ursulin engine_event_status(struct intel_engine_cs *engine, 449109ec558STvrtko Ursulin enum drm_i915_pmu_engine_sample sample) 450b46a33e2STvrtko Ursulin { 451109ec558STvrtko Ursulin switch (sample) { 452b46a33e2STvrtko Ursulin case I915_SAMPLE_BUSY: 453b46a33e2STvrtko Ursulin case I915_SAMPLE_WAIT: 454b46a33e2STvrtko Ursulin break; 455b46a33e2STvrtko Ursulin case I915_SAMPLE_SEMA: 456109ec558STvrtko Ursulin if (INTEL_GEN(engine->i915) < 6) 457b46a33e2STvrtko Ursulin return -ENODEV; 458b46a33e2STvrtko Ursulin break; 459b46a33e2STvrtko Ursulin default: 460b46a33e2STvrtko Ursulin return -ENOENT; 461b46a33e2STvrtko Ursulin } 462b46a33e2STvrtko Ursulin 463b46a33e2STvrtko Ursulin return 0; 464b46a33e2STvrtko Ursulin } 465b46a33e2STvrtko Ursulin 466109ec558STvrtko Ursulin static int 467109ec558STvrtko Ursulin config_status(struct drm_i915_private *i915, u64 config) 468109ec558STvrtko Ursulin { 469109ec558STvrtko Ursulin switch (config) { 470109ec558STvrtko Ursulin case I915_PMU_ACTUAL_FREQUENCY: 471109ec558STvrtko Ursulin if (IS_VALLEYVIEW(i915) || IS_CHERRYVIEW(i915)) 472109ec558STvrtko Ursulin /* Requires a mutex for sampling! */ 473109ec558STvrtko Ursulin return -ENODEV; 474109ec558STvrtko Ursulin /* Fall-through. */ 475109ec558STvrtko Ursulin case I915_PMU_REQUESTED_FREQUENCY: 476109ec558STvrtko Ursulin if (INTEL_GEN(i915) < 6) 477109ec558STvrtko Ursulin return -ENODEV; 478109ec558STvrtko Ursulin break; 479109ec558STvrtko Ursulin case I915_PMU_INTERRUPTS: 480109ec558STvrtko Ursulin break; 481109ec558STvrtko Ursulin case I915_PMU_RC6_RESIDENCY: 482109ec558STvrtko Ursulin if (!HAS_RC6(i915)) 483109ec558STvrtko Ursulin return -ENODEV; 484109ec558STvrtko Ursulin break; 485109ec558STvrtko Ursulin default: 486109ec558STvrtko Ursulin return -ENOENT; 487109ec558STvrtko Ursulin } 488109ec558STvrtko Ursulin 489109ec558STvrtko Ursulin return 0; 490109ec558STvrtko Ursulin } 491109ec558STvrtko Ursulin 492109ec558STvrtko Ursulin static int engine_event_init(struct perf_event *event) 493109ec558STvrtko Ursulin { 494109ec558STvrtko Ursulin struct drm_i915_private *i915 = 495109ec558STvrtko Ursulin container_of(event->pmu, typeof(*i915), pmu.base); 496109ec558STvrtko Ursulin struct intel_engine_cs *engine; 497109ec558STvrtko Ursulin 498109ec558STvrtko Ursulin engine = intel_engine_lookup_user(i915, engine_event_class(event), 499109ec558STvrtko Ursulin engine_event_instance(event)); 500109ec558STvrtko Ursulin if (!engine) 501109ec558STvrtko Ursulin return -ENODEV; 502109ec558STvrtko Ursulin 503*426d0073SChris Wilson return engine_event_status(engine, engine_event_sample(event)); 504109ec558STvrtko Ursulin } 505109ec558STvrtko Ursulin 506b46a33e2STvrtko Ursulin static int i915_pmu_event_init(struct perf_event *event) 507b46a33e2STvrtko Ursulin { 508b46a33e2STvrtko Ursulin struct drm_i915_private *i915 = 509b46a33e2STvrtko Ursulin container_of(event->pmu, typeof(*i915), pmu.base); 5100426c046STvrtko Ursulin int ret; 511b46a33e2STvrtko Ursulin 512b46a33e2STvrtko Ursulin if (event->attr.type != event->pmu->type) 513b46a33e2STvrtko Ursulin return -ENOENT; 514b46a33e2STvrtko Ursulin 515b46a33e2STvrtko Ursulin /* unsupported modes and filters */ 516b46a33e2STvrtko Ursulin if (event->attr.sample_period) /* no sampling */ 517b46a33e2STvrtko Ursulin return -EINVAL; 518b46a33e2STvrtko Ursulin 519b46a33e2STvrtko Ursulin if (has_branch_stack(event)) 520b46a33e2STvrtko Ursulin return -EOPNOTSUPP; 521b46a33e2STvrtko Ursulin 522b46a33e2STvrtko Ursulin if (event->cpu < 0) 523b46a33e2STvrtko Ursulin return -EINVAL; 524b46a33e2STvrtko Ursulin 5250426c046STvrtko Ursulin /* only allow running on one cpu at a time */ 5260426c046STvrtko Ursulin if (!cpumask_test_cpu(event->cpu, &i915_pmu_cpumask)) 52700a79722STvrtko Ursulin return -EINVAL; 528b46a33e2STvrtko Ursulin 529109ec558STvrtko Ursulin if (is_engine_event(event)) 530b46a33e2STvrtko Ursulin ret = engine_event_init(event); 531109ec558STvrtko Ursulin else 532109ec558STvrtko Ursulin ret = config_status(i915, event->attr.config); 533b46a33e2STvrtko Ursulin if (ret) 534b46a33e2STvrtko Ursulin return ret; 535b46a33e2STvrtko Ursulin 536b46a33e2STvrtko Ursulin if (!event->parent) 537b46a33e2STvrtko Ursulin event->destroy = i915_pmu_event_destroy; 538b46a33e2STvrtko Ursulin 539b46a33e2STvrtko Ursulin return 0; 540b46a33e2STvrtko Ursulin } 541b46a33e2STvrtko Ursulin 542ad055fb8STvrtko Ursulin static u64 __i915_pmu_event_read(struct perf_event *event) 543b46a33e2STvrtko Ursulin { 544b46a33e2STvrtko Ursulin struct drm_i915_private *i915 = 545b46a33e2STvrtko Ursulin container_of(event->pmu, typeof(*i915), pmu.base); 546908091c8STvrtko Ursulin struct i915_pmu *pmu = &i915->pmu; 547b46a33e2STvrtko Ursulin u64 val = 0; 548b46a33e2STvrtko Ursulin 549b46a33e2STvrtko Ursulin if (is_engine_event(event)) { 550b46a33e2STvrtko Ursulin u8 sample = engine_event_sample(event); 551b46a33e2STvrtko Ursulin struct intel_engine_cs *engine; 552b46a33e2STvrtko Ursulin 553b46a33e2STvrtko Ursulin engine = intel_engine_lookup_user(i915, 554b46a33e2STvrtko Ursulin engine_event_class(event), 555b46a33e2STvrtko Ursulin engine_event_instance(event)); 556b46a33e2STvrtko Ursulin 55748a1b8d4SPankaj Bharadiya if (drm_WARN_ON_ONCE(&i915->drm, !engine)) { 558b46a33e2STvrtko Ursulin /* Do nothing */ 559b3add01eSTvrtko Ursulin } else if (sample == I915_SAMPLE_BUSY && 560b2f78cdaSTvrtko Ursulin intel_engine_supports_stats(engine)) { 561b3add01eSTvrtko Ursulin val = ktime_to_ns(intel_engine_get_busy_time(engine)); 562b46a33e2STvrtko Ursulin } else { 563b46a33e2STvrtko Ursulin val = engine->pmu.sample[sample].cur; 564b46a33e2STvrtko Ursulin } 565b46a33e2STvrtko Ursulin } else { 566b46a33e2STvrtko Ursulin switch (event->attr.config) { 567b46a33e2STvrtko Ursulin case I915_PMU_ACTUAL_FREQUENCY: 568b46a33e2STvrtko Ursulin val = 569908091c8STvrtko Ursulin div_u64(pmu->sample[__I915_SAMPLE_FREQ_ACT].cur, 5709f473ecfSTvrtko Ursulin USEC_PER_SEC /* to MHz */); 571b46a33e2STvrtko Ursulin break; 572b46a33e2STvrtko Ursulin case I915_PMU_REQUESTED_FREQUENCY: 573b46a33e2STvrtko Ursulin val = 574908091c8STvrtko Ursulin div_u64(pmu->sample[__I915_SAMPLE_FREQ_REQ].cur, 5759f473ecfSTvrtko Ursulin USEC_PER_SEC /* to MHz */); 576b46a33e2STvrtko Ursulin break; 5770cd4684dSTvrtko Ursulin case I915_PMU_INTERRUPTS: 5780cd4684dSTvrtko Ursulin val = count_interrupts(i915); 5790cd4684dSTvrtko Ursulin break; 5806060b6aeSTvrtko Ursulin case I915_PMU_RC6_RESIDENCY: 581518ea582STvrtko Ursulin val = get_rc6(&i915->gt); 5826060b6aeSTvrtko Ursulin break; 583b46a33e2STvrtko Ursulin } 584b46a33e2STvrtko Ursulin } 585b46a33e2STvrtko Ursulin 586b46a33e2STvrtko Ursulin return val; 587b46a33e2STvrtko Ursulin } 588b46a33e2STvrtko Ursulin 589b46a33e2STvrtko Ursulin static void i915_pmu_event_read(struct perf_event *event) 590b46a33e2STvrtko Ursulin { 591b46a33e2STvrtko Ursulin struct hw_perf_event *hwc = &event->hw; 592b46a33e2STvrtko Ursulin u64 prev, new; 593b46a33e2STvrtko Ursulin 594b46a33e2STvrtko Ursulin again: 595b46a33e2STvrtko Ursulin prev = local64_read(&hwc->prev_count); 596ad055fb8STvrtko Ursulin new = __i915_pmu_event_read(event); 597b46a33e2STvrtko Ursulin 598b46a33e2STvrtko Ursulin if (local64_cmpxchg(&hwc->prev_count, prev, new) != prev) 599b46a33e2STvrtko Ursulin goto again; 600b46a33e2STvrtko Ursulin 601b46a33e2STvrtko Ursulin local64_add(new - prev, &event->count); 602b46a33e2STvrtko Ursulin } 603b46a33e2STvrtko Ursulin 604b46a33e2STvrtko Ursulin static void i915_pmu_enable(struct perf_event *event) 605b46a33e2STvrtko Ursulin { 606b46a33e2STvrtko Ursulin struct drm_i915_private *i915 = 607b46a33e2STvrtko Ursulin container_of(event->pmu, typeof(*i915), pmu.base); 608b46a33e2STvrtko Ursulin unsigned int bit = event_enabled_bit(event); 609908091c8STvrtko Ursulin struct i915_pmu *pmu = &i915->pmu; 610f4e9894bSChris Wilson intel_wakeref_t wakeref; 611b46a33e2STvrtko Ursulin unsigned long flags; 612b46a33e2STvrtko Ursulin 613f4e9894bSChris Wilson wakeref = intel_runtime_pm_get(&i915->runtime_pm); 614908091c8STvrtko Ursulin spin_lock_irqsave(&pmu->lock, flags); 615b46a33e2STvrtko Ursulin 616b46a33e2STvrtko Ursulin /* 617b46a33e2STvrtko Ursulin * Update the bitmask of enabled events and increment 618b46a33e2STvrtko Ursulin * the event reference counter. 619b46a33e2STvrtko Ursulin */ 620908091c8STvrtko Ursulin BUILD_BUG_ON(ARRAY_SIZE(pmu->enable_count) != I915_PMU_MASK_BITS); 621908091c8STvrtko Ursulin GEM_BUG_ON(bit >= ARRAY_SIZE(pmu->enable_count)); 622908091c8STvrtko Ursulin GEM_BUG_ON(pmu->enable_count[bit] == ~0); 623f4e9894bSChris Wilson 624f4e9894bSChris Wilson if (pmu->enable_count[bit] == 0 && 625f4e9894bSChris Wilson config_enabled_mask(I915_PMU_RC6_RESIDENCY) & BIT_ULL(bit)) { 626f4e9894bSChris Wilson pmu->sample[__I915_SAMPLE_RC6_LAST_REPORTED].cur = 0; 627f4e9894bSChris Wilson pmu->sample[__I915_SAMPLE_RC6].cur = __get_rc6(&i915->gt); 628f4e9894bSChris Wilson pmu->sleep_last = ktime_get(); 629f4e9894bSChris Wilson } 630f4e9894bSChris Wilson 631908091c8STvrtko Ursulin pmu->enable |= BIT_ULL(bit); 632908091c8STvrtko Ursulin pmu->enable_count[bit]++; 633b46a33e2STvrtko Ursulin 634b46a33e2STvrtko Ursulin /* 635feff0dc6STvrtko Ursulin * Start the sampling timer if needed and not already enabled. 636feff0dc6STvrtko Ursulin */ 637908091c8STvrtko Ursulin __i915_pmu_maybe_start_timer(pmu); 638feff0dc6STvrtko Ursulin 639feff0dc6STvrtko Ursulin /* 640b46a33e2STvrtko Ursulin * For per-engine events the bitmask and reference counting 641b46a33e2STvrtko Ursulin * is stored per engine. 642b46a33e2STvrtko Ursulin */ 643b46a33e2STvrtko Ursulin if (is_engine_event(event)) { 644b46a33e2STvrtko Ursulin u8 sample = engine_event_sample(event); 645b46a33e2STvrtko Ursulin struct intel_engine_cs *engine; 646b46a33e2STvrtko Ursulin 647b46a33e2STvrtko Ursulin engine = intel_engine_lookup_user(i915, 648b46a33e2STvrtko Ursulin engine_event_class(event), 649b46a33e2STvrtko Ursulin engine_event_instance(event)); 650b46a33e2STvrtko Ursulin 65126a11deeSTvrtko Ursulin BUILD_BUG_ON(ARRAY_SIZE(engine->pmu.enable_count) != 65226a11deeSTvrtko Ursulin I915_ENGINE_SAMPLE_COUNT); 65326a11deeSTvrtko Ursulin BUILD_BUG_ON(ARRAY_SIZE(engine->pmu.sample) != 65426a11deeSTvrtko Ursulin I915_ENGINE_SAMPLE_COUNT); 65526a11deeSTvrtko Ursulin GEM_BUG_ON(sample >= ARRAY_SIZE(engine->pmu.enable_count)); 65626a11deeSTvrtko Ursulin GEM_BUG_ON(sample >= ARRAY_SIZE(engine->pmu.sample)); 657b46a33e2STvrtko Ursulin GEM_BUG_ON(engine->pmu.enable_count[sample] == ~0); 65826a11deeSTvrtko Ursulin 65926a11deeSTvrtko Ursulin engine->pmu.enable |= BIT(sample); 660b2f78cdaSTvrtko Ursulin engine->pmu.enable_count[sample]++; 661b46a33e2STvrtko Ursulin } 662b46a33e2STvrtko Ursulin 663908091c8STvrtko Ursulin spin_unlock_irqrestore(&pmu->lock, flags); 664ad055fb8STvrtko Ursulin 665b46a33e2STvrtko Ursulin /* 666b46a33e2STvrtko Ursulin * Store the current counter value so we can report the correct delta 667b46a33e2STvrtko Ursulin * for all listeners. Even when the event was already enabled and has 668b46a33e2STvrtko Ursulin * an existing non-zero value. 669b46a33e2STvrtko Ursulin */ 670ad055fb8STvrtko Ursulin local64_set(&event->hw.prev_count, __i915_pmu_event_read(event)); 671f4e9894bSChris Wilson 672f4e9894bSChris Wilson intel_runtime_pm_put(&i915->runtime_pm, wakeref); 673b46a33e2STvrtko Ursulin } 674b46a33e2STvrtko Ursulin 675b46a33e2STvrtko Ursulin static void i915_pmu_disable(struct perf_event *event) 676b46a33e2STvrtko Ursulin { 677b46a33e2STvrtko Ursulin struct drm_i915_private *i915 = 678b46a33e2STvrtko Ursulin container_of(event->pmu, typeof(*i915), pmu.base); 679b46a33e2STvrtko Ursulin unsigned int bit = event_enabled_bit(event); 680908091c8STvrtko Ursulin struct i915_pmu *pmu = &i915->pmu; 681b46a33e2STvrtko Ursulin unsigned long flags; 682b46a33e2STvrtko Ursulin 683908091c8STvrtko Ursulin spin_lock_irqsave(&pmu->lock, flags); 684b46a33e2STvrtko Ursulin 685b46a33e2STvrtko Ursulin if (is_engine_event(event)) { 686b46a33e2STvrtko Ursulin u8 sample = engine_event_sample(event); 687b46a33e2STvrtko Ursulin struct intel_engine_cs *engine; 688b46a33e2STvrtko Ursulin 689b46a33e2STvrtko Ursulin engine = intel_engine_lookup_user(i915, 690b46a33e2STvrtko Ursulin engine_event_class(event), 691b46a33e2STvrtko Ursulin engine_event_instance(event)); 69226a11deeSTvrtko Ursulin 69326a11deeSTvrtko Ursulin GEM_BUG_ON(sample >= ARRAY_SIZE(engine->pmu.enable_count)); 69426a11deeSTvrtko Ursulin GEM_BUG_ON(sample >= ARRAY_SIZE(engine->pmu.sample)); 695b46a33e2STvrtko Ursulin GEM_BUG_ON(engine->pmu.enable_count[sample] == 0); 69626a11deeSTvrtko Ursulin 697b46a33e2STvrtko Ursulin /* 698b46a33e2STvrtko Ursulin * Decrement the reference count and clear the enabled 699b46a33e2STvrtko Ursulin * bitmask when the last listener on an event goes away. 700b46a33e2STvrtko Ursulin */ 701b2f78cdaSTvrtko Ursulin if (--engine->pmu.enable_count[sample] == 0) 702b46a33e2STvrtko Ursulin engine->pmu.enable &= ~BIT(sample); 703b46a33e2STvrtko Ursulin } 704b46a33e2STvrtko Ursulin 705908091c8STvrtko Ursulin GEM_BUG_ON(bit >= ARRAY_SIZE(pmu->enable_count)); 706908091c8STvrtko Ursulin GEM_BUG_ON(pmu->enable_count[bit] == 0); 707b46a33e2STvrtko Ursulin /* 708b46a33e2STvrtko Ursulin * Decrement the reference count and clear the enabled 709b46a33e2STvrtko Ursulin * bitmask when the last listener on an event goes away. 710b46a33e2STvrtko Ursulin */ 711908091c8STvrtko Ursulin if (--pmu->enable_count[bit] == 0) { 712908091c8STvrtko Ursulin pmu->enable &= ~BIT_ULL(bit); 713908091c8STvrtko Ursulin pmu->timer_enabled &= pmu_needs_timer(pmu, true); 714feff0dc6STvrtko Ursulin } 715b46a33e2STvrtko Ursulin 716908091c8STvrtko Ursulin spin_unlock_irqrestore(&pmu->lock, flags); 717b46a33e2STvrtko Ursulin } 718b46a33e2STvrtko Ursulin 719b46a33e2STvrtko Ursulin static void i915_pmu_event_start(struct perf_event *event, int flags) 720b46a33e2STvrtko Ursulin { 721b46a33e2STvrtko Ursulin i915_pmu_enable(event); 722b46a33e2STvrtko Ursulin event->hw.state = 0; 723b46a33e2STvrtko Ursulin } 724b46a33e2STvrtko Ursulin 725b46a33e2STvrtko Ursulin static void i915_pmu_event_stop(struct perf_event *event, int flags) 726b46a33e2STvrtko Ursulin { 727b46a33e2STvrtko Ursulin if (flags & PERF_EF_UPDATE) 728b46a33e2STvrtko Ursulin i915_pmu_event_read(event); 729b46a33e2STvrtko Ursulin i915_pmu_disable(event); 730b46a33e2STvrtko Ursulin event->hw.state = PERF_HES_STOPPED; 731b46a33e2STvrtko Ursulin } 732b46a33e2STvrtko Ursulin 733b46a33e2STvrtko Ursulin static int i915_pmu_event_add(struct perf_event *event, int flags) 734b46a33e2STvrtko Ursulin { 735b46a33e2STvrtko Ursulin if (flags & PERF_EF_START) 736b46a33e2STvrtko Ursulin i915_pmu_event_start(event, flags); 737b46a33e2STvrtko Ursulin 738b46a33e2STvrtko Ursulin return 0; 739b46a33e2STvrtko Ursulin } 740b46a33e2STvrtko Ursulin 741b46a33e2STvrtko Ursulin static void i915_pmu_event_del(struct perf_event *event, int flags) 742b46a33e2STvrtko Ursulin { 743b46a33e2STvrtko Ursulin i915_pmu_event_stop(event, PERF_EF_UPDATE); 744b46a33e2STvrtko Ursulin } 745b46a33e2STvrtko Ursulin 746b46a33e2STvrtko Ursulin static int i915_pmu_event_event_idx(struct perf_event *event) 747b46a33e2STvrtko Ursulin { 748b46a33e2STvrtko Ursulin return 0; 749b46a33e2STvrtko Ursulin } 750b46a33e2STvrtko Ursulin 751b7d3aabfSChris Wilson struct i915_str_attribute { 752b7d3aabfSChris Wilson struct device_attribute attr; 753b7d3aabfSChris Wilson const char *str; 754b7d3aabfSChris Wilson }; 755b7d3aabfSChris Wilson 756b46a33e2STvrtko Ursulin static ssize_t i915_pmu_format_show(struct device *dev, 757b46a33e2STvrtko Ursulin struct device_attribute *attr, char *buf) 758b46a33e2STvrtko Ursulin { 759b7d3aabfSChris Wilson struct i915_str_attribute *eattr; 760b46a33e2STvrtko Ursulin 761b7d3aabfSChris Wilson eattr = container_of(attr, struct i915_str_attribute, attr); 762b7d3aabfSChris Wilson return sprintf(buf, "%s\n", eattr->str); 763b46a33e2STvrtko Ursulin } 764b46a33e2STvrtko Ursulin 765b46a33e2STvrtko Ursulin #define I915_PMU_FORMAT_ATTR(_name, _config) \ 766b7d3aabfSChris Wilson (&((struct i915_str_attribute[]) { \ 767b46a33e2STvrtko Ursulin { .attr = __ATTR(_name, 0444, i915_pmu_format_show, NULL), \ 768b7d3aabfSChris Wilson .str = _config, } \ 769b46a33e2STvrtko Ursulin })[0].attr.attr) 770b46a33e2STvrtko Ursulin 771b46a33e2STvrtko Ursulin static struct attribute *i915_pmu_format_attrs[] = { 772b46a33e2STvrtko Ursulin I915_PMU_FORMAT_ATTR(i915_eventid, "config:0-20"), 773b46a33e2STvrtko Ursulin NULL, 774b46a33e2STvrtko Ursulin }; 775b46a33e2STvrtko Ursulin 776b46a33e2STvrtko Ursulin static const struct attribute_group i915_pmu_format_attr_group = { 777b46a33e2STvrtko Ursulin .name = "format", 778b46a33e2STvrtko Ursulin .attrs = i915_pmu_format_attrs, 779b46a33e2STvrtko Ursulin }; 780b46a33e2STvrtko Ursulin 781b7d3aabfSChris Wilson struct i915_ext_attribute { 782b7d3aabfSChris Wilson struct device_attribute attr; 783b7d3aabfSChris Wilson unsigned long val; 784b7d3aabfSChris Wilson }; 785b7d3aabfSChris Wilson 786b46a33e2STvrtko Ursulin static ssize_t i915_pmu_event_show(struct device *dev, 787b46a33e2STvrtko Ursulin struct device_attribute *attr, char *buf) 788b46a33e2STvrtko Ursulin { 789b7d3aabfSChris Wilson struct i915_ext_attribute *eattr; 790b46a33e2STvrtko Ursulin 791b7d3aabfSChris Wilson eattr = container_of(attr, struct i915_ext_attribute, attr); 792b7d3aabfSChris Wilson return sprintf(buf, "config=0x%lx\n", eattr->val); 793b46a33e2STvrtko Ursulin } 794b46a33e2STvrtko Ursulin 795b46a33e2STvrtko Ursulin static ssize_t 796b46a33e2STvrtko Ursulin i915_pmu_get_attr_cpumask(struct device *dev, 797b46a33e2STvrtko Ursulin struct device_attribute *attr, 798b46a33e2STvrtko Ursulin char *buf) 799b46a33e2STvrtko Ursulin { 800b46a33e2STvrtko Ursulin return cpumap_print_to_pagebuf(true, buf, &i915_pmu_cpumask); 801b46a33e2STvrtko Ursulin } 802b46a33e2STvrtko Ursulin 803b46a33e2STvrtko Ursulin static DEVICE_ATTR(cpumask, 0444, i915_pmu_get_attr_cpumask, NULL); 804b46a33e2STvrtko Ursulin 805b46a33e2STvrtko Ursulin static struct attribute *i915_cpumask_attrs[] = { 806b46a33e2STvrtko Ursulin &dev_attr_cpumask.attr, 807b46a33e2STvrtko Ursulin NULL, 808b46a33e2STvrtko Ursulin }; 809b46a33e2STvrtko Ursulin 810109ec558STvrtko Ursulin static const struct attribute_group i915_pmu_cpumask_attr_group = { 811b46a33e2STvrtko Ursulin .attrs = i915_cpumask_attrs, 812b46a33e2STvrtko Ursulin }; 813b46a33e2STvrtko Ursulin 814109ec558STvrtko Ursulin #define __event(__config, __name, __unit) \ 815109ec558STvrtko Ursulin { \ 816109ec558STvrtko Ursulin .config = (__config), \ 817109ec558STvrtko Ursulin .name = (__name), \ 818109ec558STvrtko Ursulin .unit = (__unit), \ 819109ec558STvrtko Ursulin } 820109ec558STvrtko Ursulin 821109ec558STvrtko Ursulin #define __engine_event(__sample, __name) \ 822109ec558STvrtko Ursulin { \ 823109ec558STvrtko Ursulin .sample = (__sample), \ 824109ec558STvrtko Ursulin .name = (__name), \ 825109ec558STvrtko Ursulin } 826109ec558STvrtko Ursulin 827109ec558STvrtko Ursulin static struct i915_ext_attribute * 828109ec558STvrtko Ursulin add_i915_attr(struct i915_ext_attribute *attr, const char *name, u64 config) 829109ec558STvrtko Ursulin { 8302bbba4e9SChris Wilson sysfs_attr_init(&attr->attr.attr); 831109ec558STvrtko Ursulin attr->attr.attr.name = name; 832109ec558STvrtko Ursulin attr->attr.attr.mode = 0444; 833109ec558STvrtko Ursulin attr->attr.show = i915_pmu_event_show; 834109ec558STvrtko Ursulin attr->val = config; 835109ec558STvrtko Ursulin 836109ec558STvrtko Ursulin return ++attr; 837109ec558STvrtko Ursulin } 838109ec558STvrtko Ursulin 839109ec558STvrtko Ursulin static struct perf_pmu_events_attr * 840109ec558STvrtko Ursulin add_pmu_attr(struct perf_pmu_events_attr *attr, const char *name, 841109ec558STvrtko Ursulin const char *str) 842109ec558STvrtko Ursulin { 8432bbba4e9SChris Wilson sysfs_attr_init(&attr->attr.attr); 844109ec558STvrtko Ursulin attr->attr.attr.name = name; 845109ec558STvrtko Ursulin attr->attr.attr.mode = 0444; 846109ec558STvrtko Ursulin attr->attr.show = perf_event_sysfs_show; 847109ec558STvrtko Ursulin attr->event_str = str; 848109ec558STvrtko Ursulin 849109ec558STvrtko Ursulin return ++attr; 850109ec558STvrtko Ursulin } 851109ec558STvrtko Ursulin 852109ec558STvrtko Ursulin static struct attribute ** 853908091c8STvrtko Ursulin create_event_attributes(struct i915_pmu *pmu) 854109ec558STvrtko Ursulin { 855908091c8STvrtko Ursulin struct drm_i915_private *i915 = container_of(pmu, typeof(*i915), pmu); 856109ec558STvrtko Ursulin static const struct { 857109ec558STvrtko Ursulin u64 config; 858109ec558STvrtko Ursulin const char *name; 859109ec558STvrtko Ursulin const char *unit; 860109ec558STvrtko Ursulin } events[] = { 861e88866efSChris Wilson __event(I915_PMU_ACTUAL_FREQUENCY, "actual-frequency", "M"), 862e88866efSChris Wilson __event(I915_PMU_REQUESTED_FREQUENCY, "requested-frequency", "M"), 863109ec558STvrtko Ursulin __event(I915_PMU_INTERRUPTS, "interrupts", NULL), 864109ec558STvrtko Ursulin __event(I915_PMU_RC6_RESIDENCY, "rc6-residency", "ns"), 865109ec558STvrtko Ursulin }; 866109ec558STvrtko Ursulin static const struct { 867109ec558STvrtko Ursulin enum drm_i915_pmu_engine_sample sample; 868109ec558STvrtko Ursulin char *name; 869109ec558STvrtko Ursulin } engine_events[] = { 870109ec558STvrtko Ursulin __engine_event(I915_SAMPLE_BUSY, "busy"), 871109ec558STvrtko Ursulin __engine_event(I915_SAMPLE_SEMA, "sema"), 872109ec558STvrtko Ursulin __engine_event(I915_SAMPLE_WAIT, "wait"), 873109ec558STvrtko Ursulin }; 874109ec558STvrtko Ursulin unsigned int count = 0; 875109ec558STvrtko Ursulin struct perf_pmu_events_attr *pmu_attr = NULL, *pmu_iter; 876109ec558STvrtko Ursulin struct i915_ext_attribute *i915_attr = NULL, *i915_iter; 877109ec558STvrtko Ursulin struct attribute **attr = NULL, **attr_iter; 878109ec558STvrtko Ursulin struct intel_engine_cs *engine; 879109ec558STvrtko Ursulin unsigned int i; 880109ec558STvrtko Ursulin 881109ec558STvrtko Ursulin /* Count how many counters we will be exposing. */ 882109ec558STvrtko Ursulin for (i = 0; i < ARRAY_SIZE(events); i++) { 883109ec558STvrtko Ursulin if (!config_status(i915, events[i].config)) 884109ec558STvrtko Ursulin count++; 885109ec558STvrtko Ursulin } 886109ec558STvrtko Ursulin 887750e76b4SChris Wilson for_each_uabi_engine(engine, i915) { 888109ec558STvrtko Ursulin for (i = 0; i < ARRAY_SIZE(engine_events); i++) { 889109ec558STvrtko Ursulin if (!engine_event_status(engine, 890109ec558STvrtko Ursulin engine_events[i].sample)) 891109ec558STvrtko Ursulin count++; 892109ec558STvrtko Ursulin } 893109ec558STvrtko Ursulin } 894109ec558STvrtko Ursulin 895109ec558STvrtko Ursulin /* Allocate attribute objects and table. */ 896dd5fec87STvrtko Ursulin i915_attr = kcalloc(count, sizeof(*i915_attr), GFP_KERNEL); 897109ec558STvrtko Ursulin if (!i915_attr) 898109ec558STvrtko Ursulin goto err_alloc; 899109ec558STvrtko Ursulin 900dd5fec87STvrtko Ursulin pmu_attr = kcalloc(count, sizeof(*pmu_attr), GFP_KERNEL); 901109ec558STvrtko Ursulin if (!pmu_attr) 902109ec558STvrtko Ursulin goto err_alloc; 903109ec558STvrtko Ursulin 904109ec558STvrtko Ursulin /* Max one pointer of each attribute type plus a termination entry. */ 905dd5fec87STvrtko Ursulin attr = kcalloc(count * 2 + 1, sizeof(*attr), GFP_KERNEL); 906109ec558STvrtko Ursulin if (!attr) 907109ec558STvrtko Ursulin goto err_alloc; 908109ec558STvrtko Ursulin 909109ec558STvrtko Ursulin i915_iter = i915_attr; 910109ec558STvrtko Ursulin pmu_iter = pmu_attr; 911109ec558STvrtko Ursulin attr_iter = attr; 912109ec558STvrtko Ursulin 913109ec558STvrtko Ursulin /* Initialize supported non-engine counters. */ 914109ec558STvrtko Ursulin for (i = 0; i < ARRAY_SIZE(events); i++) { 915109ec558STvrtko Ursulin char *str; 916109ec558STvrtko Ursulin 917109ec558STvrtko Ursulin if (config_status(i915, events[i].config)) 918109ec558STvrtko Ursulin continue; 919109ec558STvrtko Ursulin 920109ec558STvrtko Ursulin str = kstrdup(events[i].name, GFP_KERNEL); 921109ec558STvrtko Ursulin if (!str) 922109ec558STvrtko Ursulin goto err; 923109ec558STvrtko Ursulin 924109ec558STvrtko Ursulin *attr_iter++ = &i915_iter->attr.attr; 925109ec558STvrtko Ursulin i915_iter = add_i915_attr(i915_iter, str, events[i].config); 926109ec558STvrtko Ursulin 927109ec558STvrtko Ursulin if (events[i].unit) { 928109ec558STvrtko Ursulin str = kasprintf(GFP_KERNEL, "%s.unit", events[i].name); 929109ec558STvrtko Ursulin if (!str) 930109ec558STvrtko Ursulin goto err; 931109ec558STvrtko Ursulin 932109ec558STvrtko Ursulin *attr_iter++ = &pmu_iter->attr.attr; 933109ec558STvrtko Ursulin pmu_iter = add_pmu_attr(pmu_iter, str, events[i].unit); 934109ec558STvrtko Ursulin } 935109ec558STvrtko Ursulin } 936109ec558STvrtko Ursulin 937109ec558STvrtko Ursulin /* Initialize supported engine counters. */ 938750e76b4SChris Wilson for_each_uabi_engine(engine, i915) { 939109ec558STvrtko Ursulin for (i = 0; i < ARRAY_SIZE(engine_events); i++) { 940109ec558STvrtko Ursulin char *str; 941109ec558STvrtko Ursulin 942109ec558STvrtko Ursulin if (engine_event_status(engine, 943109ec558STvrtko Ursulin engine_events[i].sample)) 944109ec558STvrtko Ursulin continue; 945109ec558STvrtko Ursulin 946109ec558STvrtko Ursulin str = kasprintf(GFP_KERNEL, "%s-%s", 947109ec558STvrtko Ursulin engine->name, engine_events[i].name); 948109ec558STvrtko Ursulin if (!str) 949109ec558STvrtko Ursulin goto err; 950109ec558STvrtko Ursulin 951109ec558STvrtko Ursulin *attr_iter++ = &i915_iter->attr.attr; 952109ec558STvrtko Ursulin i915_iter = 953109ec558STvrtko Ursulin add_i915_attr(i915_iter, str, 9548810bc56STvrtko Ursulin __I915_PMU_ENGINE(engine->uabi_class, 955750e76b4SChris Wilson engine->uabi_instance, 956109ec558STvrtko Ursulin engine_events[i].sample)); 957109ec558STvrtko Ursulin 958109ec558STvrtko Ursulin str = kasprintf(GFP_KERNEL, "%s-%s.unit", 959109ec558STvrtko Ursulin engine->name, engine_events[i].name); 960109ec558STvrtko Ursulin if (!str) 961109ec558STvrtko Ursulin goto err; 962109ec558STvrtko Ursulin 963109ec558STvrtko Ursulin *attr_iter++ = &pmu_iter->attr.attr; 964109ec558STvrtko Ursulin pmu_iter = add_pmu_attr(pmu_iter, str, "ns"); 965109ec558STvrtko Ursulin } 966109ec558STvrtko Ursulin } 967109ec558STvrtko Ursulin 968908091c8STvrtko Ursulin pmu->i915_attr = i915_attr; 969908091c8STvrtko Ursulin pmu->pmu_attr = pmu_attr; 970109ec558STvrtko Ursulin 971109ec558STvrtko Ursulin return attr; 972109ec558STvrtko Ursulin 973109ec558STvrtko Ursulin err:; 974109ec558STvrtko Ursulin for (attr_iter = attr; *attr_iter; attr_iter++) 975109ec558STvrtko Ursulin kfree((*attr_iter)->name); 976109ec558STvrtko Ursulin 977109ec558STvrtko Ursulin err_alloc: 978109ec558STvrtko Ursulin kfree(attr); 979109ec558STvrtko Ursulin kfree(i915_attr); 980109ec558STvrtko Ursulin kfree(pmu_attr); 981109ec558STvrtko Ursulin 982109ec558STvrtko Ursulin return NULL; 983109ec558STvrtko Ursulin } 984109ec558STvrtko Ursulin 985908091c8STvrtko Ursulin static void free_event_attributes(struct i915_pmu *pmu) 986109ec558STvrtko Ursulin { 98746129dc1SMichał Winiarski struct attribute **attr_iter = pmu->events_attr_group.attrs; 988109ec558STvrtko Ursulin 989109ec558STvrtko Ursulin for (; *attr_iter; attr_iter++) 990109ec558STvrtko Ursulin kfree((*attr_iter)->name); 991109ec558STvrtko Ursulin 99246129dc1SMichał Winiarski kfree(pmu->events_attr_group.attrs); 993908091c8STvrtko Ursulin kfree(pmu->i915_attr); 994908091c8STvrtko Ursulin kfree(pmu->pmu_attr); 995109ec558STvrtko Ursulin 99646129dc1SMichał Winiarski pmu->events_attr_group.attrs = NULL; 997908091c8STvrtko Ursulin pmu->i915_attr = NULL; 998908091c8STvrtko Ursulin pmu->pmu_attr = NULL; 999109ec558STvrtko Ursulin } 1000109ec558STvrtko Ursulin 1001b46a33e2STvrtko Ursulin static int i915_pmu_cpu_online(unsigned int cpu, struct hlist_node *node) 1002b46a33e2STvrtko Ursulin { 1003f5a179d4SMichał Winiarski struct i915_pmu *pmu = hlist_entry_safe(node, typeof(*pmu), cpuhp.node); 1004b46a33e2STvrtko Ursulin 1005b46a33e2STvrtko Ursulin GEM_BUG_ON(!pmu->base.event_init); 1006b46a33e2STvrtko Ursulin 1007b46a33e2STvrtko Ursulin /* Select the first online CPU as a designated reader. */ 10080426c046STvrtko Ursulin if (!cpumask_weight(&i915_pmu_cpumask)) 1009b46a33e2STvrtko Ursulin cpumask_set_cpu(cpu, &i915_pmu_cpumask); 1010b46a33e2STvrtko Ursulin 1011b46a33e2STvrtko Ursulin return 0; 1012b46a33e2STvrtko Ursulin } 1013b46a33e2STvrtko Ursulin 1014b46a33e2STvrtko Ursulin static int i915_pmu_cpu_offline(unsigned int cpu, struct hlist_node *node) 1015b46a33e2STvrtko Ursulin { 1016f5a179d4SMichał Winiarski struct i915_pmu *pmu = hlist_entry_safe(node, typeof(*pmu), cpuhp.node); 1017b46a33e2STvrtko Ursulin unsigned int target; 1018b46a33e2STvrtko Ursulin 1019b46a33e2STvrtko Ursulin GEM_BUG_ON(!pmu->base.event_init); 1020b46a33e2STvrtko Ursulin 1021b46a33e2STvrtko Ursulin if (cpumask_test_and_clear_cpu(cpu, &i915_pmu_cpumask)) { 1022b46a33e2STvrtko Ursulin target = cpumask_any_but(topology_sibling_cpumask(cpu), cpu); 1023b46a33e2STvrtko Ursulin /* Migrate events if there is a valid target */ 1024b46a33e2STvrtko Ursulin if (target < nr_cpu_ids) { 1025b46a33e2STvrtko Ursulin cpumask_set_cpu(target, &i915_pmu_cpumask); 1026b46a33e2STvrtko Ursulin perf_pmu_migrate_context(&pmu->base, cpu, target); 1027b46a33e2STvrtko Ursulin } 1028b46a33e2STvrtko Ursulin } 1029b46a33e2STvrtko Ursulin 1030b46a33e2STvrtko Ursulin return 0; 1031b46a33e2STvrtko Ursulin } 1032b46a33e2STvrtko Ursulin 1033908091c8STvrtko Ursulin static int i915_pmu_register_cpuhp_state(struct i915_pmu *pmu) 1034b46a33e2STvrtko Ursulin { 1035b46a33e2STvrtko Ursulin enum cpuhp_state slot; 1036b46a33e2STvrtko Ursulin int ret; 1037b46a33e2STvrtko Ursulin 1038b46a33e2STvrtko Ursulin ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN, 1039b46a33e2STvrtko Ursulin "perf/x86/intel/i915:online", 1040b46a33e2STvrtko Ursulin i915_pmu_cpu_online, 1041b46a33e2STvrtko Ursulin i915_pmu_cpu_offline); 1042b46a33e2STvrtko Ursulin if (ret < 0) 1043b46a33e2STvrtko Ursulin return ret; 1044b46a33e2STvrtko Ursulin 1045b46a33e2STvrtko Ursulin slot = ret; 1046f5a179d4SMichał Winiarski ret = cpuhp_state_add_instance(slot, &pmu->cpuhp.node); 1047b46a33e2STvrtko Ursulin if (ret) { 1048b46a33e2STvrtko Ursulin cpuhp_remove_multi_state(slot); 1049b46a33e2STvrtko Ursulin return ret; 1050b46a33e2STvrtko Ursulin } 1051b46a33e2STvrtko Ursulin 1052f5a179d4SMichał Winiarski pmu->cpuhp.slot = slot; 1053b46a33e2STvrtko Ursulin return 0; 1054b46a33e2STvrtko Ursulin } 1055b46a33e2STvrtko Ursulin 1056908091c8STvrtko Ursulin static void i915_pmu_unregister_cpuhp_state(struct i915_pmu *pmu) 1057b46a33e2STvrtko Ursulin { 1058f5a179d4SMichał Winiarski WARN_ON(pmu->cpuhp.slot == CPUHP_INVALID); 1059f5a179d4SMichał Winiarski WARN_ON(cpuhp_state_remove_instance(pmu->cpuhp.slot, &pmu->cpuhp.node)); 1060f5a179d4SMichał Winiarski cpuhp_remove_multi_state(pmu->cpuhp.slot); 1061f5a179d4SMichał Winiarski pmu->cpuhp.slot = CPUHP_INVALID; 1062b46a33e2STvrtko Ursulin } 1063b46a33e2STvrtko Ursulin 106405488673STvrtko Ursulin static bool is_igp(struct drm_i915_private *i915) 106505488673STvrtko Ursulin { 106605488673STvrtko Ursulin struct pci_dev *pdev = i915->drm.pdev; 106705488673STvrtko Ursulin 106805488673STvrtko Ursulin /* IGP is 0000:00:02.0 */ 106905488673STvrtko Ursulin return pci_domain_nr(pdev->bus) == 0 && 107005488673STvrtko Ursulin pdev->bus->number == 0 && 107105488673STvrtko Ursulin PCI_SLOT(pdev->devfn) == 2 && 107205488673STvrtko Ursulin PCI_FUNC(pdev->devfn) == 0; 107305488673STvrtko Ursulin } 107405488673STvrtko Ursulin 1075b46a33e2STvrtko Ursulin void i915_pmu_register(struct drm_i915_private *i915) 1076b46a33e2STvrtko Ursulin { 1077908091c8STvrtko Ursulin struct i915_pmu *pmu = &i915->pmu; 107846129dc1SMichał Winiarski const struct attribute_group *attr_groups[] = { 107946129dc1SMichał Winiarski &i915_pmu_format_attr_group, 108046129dc1SMichał Winiarski &pmu->events_attr_group, 108146129dc1SMichał Winiarski &i915_pmu_cpumask_attr_group, 108246129dc1SMichał Winiarski NULL 108346129dc1SMichał Winiarski }; 108446129dc1SMichał Winiarski 1085fb26eee0STvrtko Ursulin int ret = -ENOMEM; 1086b46a33e2STvrtko Ursulin 1087b46a33e2STvrtko Ursulin if (INTEL_GEN(i915) <= 2) { 10881900aba5SJani Nikula drm_info(&i915->drm, "PMU not supported for this GPU."); 1089b46a33e2STvrtko Ursulin return; 1090b46a33e2STvrtko Ursulin } 1091b46a33e2STvrtko Ursulin 1092908091c8STvrtko Ursulin spin_lock_init(&pmu->lock); 1093908091c8STvrtko Ursulin hrtimer_init(&pmu->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); 1094908091c8STvrtko Ursulin pmu->timer.function = i915_sample; 1095f5a179d4SMichał Winiarski pmu->cpuhp.slot = CPUHP_INVALID; 1096b46a33e2STvrtko Ursulin 1097aebf3b52STvrtko Ursulin if (!is_igp(i915)) { 109805488673STvrtko Ursulin pmu->name = kasprintf(GFP_KERNEL, 1099aebf3b52STvrtko Ursulin "i915_%s", 110005488673STvrtko Ursulin dev_name(i915->drm.dev)); 1101aebf3b52STvrtko Ursulin if (pmu->name) { 1102aebf3b52STvrtko Ursulin /* tools/perf reserves colons as special. */ 1103aebf3b52STvrtko Ursulin strreplace((char *)pmu->name, ':', '_'); 1104aebf3b52STvrtko Ursulin } 1105aebf3b52STvrtko Ursulin } else { 110605488673STvrtko Ursulin pmu->name = "i915"; 1107aebf3b52STvrtko Ursulin } 110805488673STvrtko Ursulin if (!pmu->name) 1109b46a33e2STvrtko Ursulin goto err; 1110b46a33e2STvrtko Ursulin 111146129dc1SMichał Winiarski pmu->events_attr_group.name = "events"; 111246129dc1SMichał Winiarski pmu->events_attr_group.attrs = create_event_attributes(pmu); 111346129dc1SMichał Winiarski if (!pmu->events_attr_group.attrs) 1114c442292aSChris Wilson goto err_name; 1115c442292aSChris Wilson 111646129dc1SMichał Winiarski pmu->base.attr_groups = kmemdup(attr_groups, sizeof(attr_groups), 111746129dc1SMichał Winiarski GFP_KERNEL); 111846129dc1SMichał Winiarski if (!pmu->base.attr_groups) 111946129dc1SMichał Winiarski goto err_attr; 112046129dc1SMichał Winiarski 1121c442292aSChris Wilson pmu->base.task_ctx_nr = perf_invalid_context; 1122c442292aSChris Wilson pmu->base.event_init = i915_pmu_event_init; 1123c442292aSChris Wilson pmu->base.add = i915_pmu_event_add; 1124c442292aSChris Wilson pmu->base.del = i915_pmu_event_del; 1125c442292aSChris Wilson pmu->base.start = i915_pmu_event_start; 1126c442292aSChris Wilson pmu->base.stop = i915_pmu_event_stop; 1127c442292aSChris Wilson pmu->base.read = i915_pmu_event_read; 1128c442292aSChris Wilson pmu->base.event_idx = i915_pmu_event_event_idx; 1129c442292aSChris Wilson 113005488673STvrtko Ursulin ret = perf_pmu_register(&pmu->base, pmu->name, -1); 113105488673STvrtko Ursulin if (ret) 113246129dc1SMichał Winiarski goto err_groups; 113305488673STvrtko Ursulin 1134908091c8STvrtko Ursulin ret = i915_pmu_register_cpuhp_state(pmu); 1135b46a33e2STvrtko Ursulin if (ret) 1136b46a33e2STvrtko Ursulin goto err_unreg; 1137b46a33e2STvrtko Ursulin 1138b46a33e2STvrtko Ursulin return; 1139b46a33e2STvrtko Ursulin 1140b46a33e2STvrtko Ursulin err_unreg: 1141908091c8STvrtko Ursulin perf_pmu_unregister(&pmu->base); 114246129dc1SMichał Winiarski err_groups: 114346129dc1SMichał Winiarski kfree(pmu->base.attr_groups); 1144c442292aSChris Wilson err_attr: 1145c442292aSChris Wilson pmu->base.event_init = NULL; 1146c442292aSChris Wilson free_event_attributes(pmu); 114705488673STvrtko Ursulin err_name: 114805488673STvrtko Ursulin if (!is_igp(i915)) 114905488673STvrtko Ursulin kfree(pmu->name); 1150b46a33e2STvrtko Ursulin err: 11511900aba5SJani Nikula drm_notice(&i915->drm, "Failed to register PMU!\n"); 1152b46a33e2STvrtko Ursulin } 1153b46a33e2STvrtko Ursulin 1154b46a33e2STvrtko Ursulin void i915_pmu_unregister(struct drm_i915_private *i915) 1155b46a33e2STvrtko Ursulin { 1156908091c8STvrtko Ursulin struct i915_pmu *pmu = &i915->pmu; 1157908091c8STvrtko Ursulin 1158908091c8STvrtko Ursulin if (!pmu->base.event_init) 1159b46a33e2STvrtko Ursulin return; 1160b46a33e2STvrtko Ursulin 116148a1b8d4SPankaj Bharadiya drm_WARN_ON(&i915->drm, pmu->enable); 1162b46a33e2STvrtko Ursulin 1163908091c8STvrtko Ursulin hrtimer_cancel(&pmu->timer); 1164b46a33e2STvrtko Ursulin 1165908091c8STvrtko Ursulin i915_pmu_unregister_cpuhp_state(pmu); 1166b46a33e2STvrtko Ursulin 1167908091c8STvrtko Ursulin perf_pmu_unregister(&pmu->base); 1168908091c8STvrtko Ursulin pmu->base.event_init = NULL; 116946129dc1SMichał Winiarski kfree(pmu->base.attr_groups); 117005488673STvrtko Ursulin if (!is_igp(i915)) 117105488673STvrtko Ursulin kfree(pmu->name); 1172908091c8STvrtko Ursulin free_event_attributes(pmu); 1173b46a33e2STvrtko Ursulin } 1174