Tvrtko Ursulin | b46a33e | 2017-11-21 18:18:45 +0000 | [diff] [blame] | 1 | /* |
Michal Wajdeczko | 058a9b4 | 2018-03-08 09:50:36 +0000 | [diff] [blame] | 2 | * SPDX-License-Identifier: MIT |
Tvrtko Ursulin | b46a33e | 2017-11-21 18:18:45 +0000 | [diff] [blame] | 3 | * |
Michal Wajdeczko | 058a9b4 | 2018-03-08 09:50:36 +0000 | [diff] [blame] | 4 | * Copyright © 2017-2018 Intel Corporation |
Tvrtko Ursulin | b46a33e | 2017-11-21 18:18:45 +0000 | [diff] [blame] | 5 | */ |
| 6 | |
Nicolai Stange | 447ae31 | 2018-07-29 12:15:33 +0200 | [diff] [blame] | 7 | #include <linux/irq.h> |
Vincent Guittot | 3b4ed2e | 2018-12-21 11:33:55 +0100 | [diff] [blame] | 8 | #include <linux/pm_runtime.h> |
Chris Wilson | 112ed2d | 2019-04-24 18:48:39 +0100 | [diff] [blame] | 9 | |
| 10 | #include "gt/intel_engine.h" |
Chris Wilson | 51fbd8de | 2019-08-02 00:36:16 +0100 | [diff] [blame] | 11 | #include "gt/intel_engine_pm.h" |
Chris Wilson | 750e76b | 2019-08-06 13:43:00 +0100 | [diff] [blame] | 12 | #include "gt/intel_engine_user.h" |
Chris Wilson | 51fbd8de | 2019-08-02 00:36:16 +0100 | [diff] [blame] | 13 | #include "gt/intel_gt_pm.h" |
Andi Shyti | c113236 | 2019-09-27 12:08:49 +0100 | [diff] [blame] | 14 | #include "gt/intel_rc6.h" |
Andi Shyti | 3e7abf8 | 2019-10-24 22:16:41 +0100 | [diff] [blame] | 15 | #include "gt/intel_rps.h" |
Chris Wilson | 112ed2d | 2019-04-24 18:48:39 +0100 | [diff] [blame] | 16 | |
Michal Wajdeczko | 058a9b4 | 2018-03-08 09:50:36 +0000 | [diff] [blame] | 17 | #include "i915_drv.h" |
Jani Nikula | ecbb5fb | 2019-04-29 15:29:37 +0300 | [diff] [blame] | 18 | #include "i915_pmu.h" |
| 19 | #include "intel_pm.h" |
Tvrtko Ursulin | b46a33e | 2017-11-21 18:18:45 +0000 | [diff] [blame] | 20 | |
| 21 | /* Frequency for the sampling timer for events which need it. */ |
| 22 | #define FREQUENCY 200 |
| 23 | #define PERIOD max_t(u64, 10000, NSEC_PER_SEC / FREQUENCY) |
| 24 | |
| 25 | #define ENGINE_SAMPLE_MASK \ |
| 26 | (BIT(I915_SAMPLE_BUSY) | \ |
| 27 | BIT(I915_SAMPLE_WAIT) | \ |
| 28 | BIT(I915_SAMPLE_SEMA)) |
| 29 | |
| 30 | #define ENGINE_SAMPLE_BITS (1 << I915_PMU_SAMPLE_BITS) |
| 31 | |
Chris Wilson | 141a089 | 2017-11-23 12:34:31 +0000 | [diff] [blame] | 32 | static cpumask_t i915_pmu_cpumask; |
Tvrtko Ursulin | b46a33e | 2017-11-21 18:18:45 +0000 | [diff] [blame] | 33 | |
| 34 | static u8 engine_config_sample(u64 config) |
| 35 | { |
| 36 | return config & I915_PMU_SAMPLE_MASK; |
| 37 | } |
| 38 | |
| 39 | static u8 engine_event_sample(struct perf_event *event) |
| 40 | { |
| 41 | return engine_config_sample(event->attr.config); |
| 42 | } |
| 43 | |
| 44 | static u8 engine_event_class(struct perf_event *event) |
| 45 | { |
| 46 | return (event->attr.config >> I915_PMU_CLASS_SHIFT) & 0xff; |
| 47 | } |
| 48 | |
| 49 | static u8 engine_event_instance(struct perf_event *event) |
| 50 | { |
| 51 | return (event->attr.config >> I915_PMU_SAMPLE_BITS) & 0xff; |
| 52 | } |
| 53 | |
| 54 | static bool is_engine_config(u64 config) |
| 55 | { |
| 56 | return config < __I915_PMU_OTHER(0); |
| 57 | } |
| 58 | |
| 59 | static unsigned int config_enabled_bit(u64 config) |
| 60 | { |
| 61 | if (is_engine_config(config)) |
| 62 | return engine_config_sample(config); |
| 63 | else |
| 64 | return ENGINE_SAMPLE_BITS + (config - __I915_PMU_OTHER(0)); |
| 65 | } |
| 66 | |
| 67 | static u64 config_enabled_mask(u64 config) |
| 68 | { |
| 69 | return BIT_ULL(config_enabled_bit(config)); |
| 70 | } |
| 71 | |
| 72 | static bool is_engine_event(struct perf_event *event) |
| 73 | { |
| 74 | return is_engine_config(event->attr.config); |
| 75 | } |
| 76 | |
| 77 | static unsigned int event_enabled_bit(struct perf_event *event) |
| 78 | { |
| 79 | return config_enabled_bit(event->attr.config); |
| 80 | } |
| 81 | |
Tvrtko Ursulin | 908091c | 2019-08-01 17:23:27 +0100 | [diff] [blame] | 82 | static bool pmu_needs_timer(struct i915_pmu *pmu, bool gpu_active) |
Tvrtko Ursulin | feff0dc | 2017-11-21 18:18:46 +0000 | [diff] [blame] | 83 | { |
Tvrtko Ursulin | 908091c | 2019-08-01 17:23:27 +0100 | [diff] [blame] | 84 | struct drm_i915_private *i915 = container_of(pmu, typeof(*i915), pmu); |
Tvrtko Ursulin | feff0dc | 2017-11-21 18:18:46 +0000 | [diff] [blame] | 85 | u64 enable; |
| 86 | |
| 87 | /* |
| 88 | * Only some counters need the sampling timer. |
| 89 | * |
| 90 | * We start with a bitmask of all currently enabled events. |
| 91 | */ |
Tvrtko Ursulin | 908091c | 2019-08-01 17:23:27 +0100 | [diff] [blame] | 92 | enable = pmu->enable; |
Tvrtko Ursulin | feff0dc | 2017-11-21 18:18:46 +0000 | [diff] [blame] | 93 | |
| 94 | /* |
| 95 | * Mask out all the ones which do not need the timer, or in |
| 96 | * other words keep all the ones that could need the timer. |
| 97 | */ |
| 98 | enable &= config_enabled_mask(I915_PMU_ACTUAL_FREQUENCY) | |
| 99 | config_enabled_mask(I915_PMU_REQUESTED_FREQUENCY) | |
| 100 | ENGINE_SAMPLE_MASK; |
| 101 | |
| 102 | /* |
| 103 | * When the GPU is idle per-engine counters do not need to be |
| 104 | * running so clear those bits out. |
| 105 | */ |
| 106 | if (!gpu_active) |
| 107 | enable &= ~ENGINE_SAMPLE_MASK; |
Tvrtko Ursulin | b3add01 | 2017-11-21 18:18:49 +0000 | [diff] [blame] | 108 | /* |
| 109 | * Also there is software busyness tracking available we do not |
| 110 | * need the timer for I915_SAMPLE_BUSY counter. |
| 111 | */ |
Chris Wilson | bf73fc0 | 2019-07-03 15:37:02 +0100 | [diff] [blame] | 112 | else if (i915->caps.scheduler & I915_SCHEDULER_CAP_ENGINE_BUSY_STATS) |
Tvrtko Ursulin | b3add01 | 2017-11-21 18:18:49 +0000 | [diff] [blame] | 113 | enable &= ~BIT(I915_SAMPLE_BUSY); |
Tvrtko Ursulin | feff0dc | 2017-11-21 18:18:46 +0000 | [diff] [blame] | 114 | |
| 115 | /* |
| 116 | * If some bits remain it means we need the sampling timer running. |
| 117 | */ |
| 118 | return enable; |
| 119 | } |
| 120 | |
Andi Shyti | c113236 | 2019-09-27 12:08:49 +0100 | [diff] [blame] | 121 | static u64 __get_rc6(struct intel_gt *gt) |
Chris Wilson | 16ffe73 | 2019-09-12 13:48:13 +0100 | [diff] [blame] | 122 | { |
| 123 | struct drm_i915_private *i915 = gt->i915; |
| 124 | u64 val; |
| 125 | |
Andi Shyti | c113236 | 2019-09-27 12:08:49 +0100 | [diff] [blame] | 126 | val = intel_rc6_residency_ns(>->rc6, |
Chris Wilson | 16ffe73 | 2019-09-12 13:48:13 +0100 | [diff] [blame] | 127 | IS_VALLEYVIEW(i915) ? |
| 128 | VLV_GT_RENDER_RC6 : |
| 129 | GEN6_GT_GFX_RC6); |
| 130 | |
| 131 | if (HAS_RC6p(i915)) |
Andi Shyti | c113236 | 2019-09-27 12:08:49 +0100 | [diff] [blame] | 132 | val += intel_rc6_residency_ns(>->rc6, GEN6_GT_GFX_RC6p); |
Chris Wilson | 16ffe73 | 2019-09-12 13:48:13 +0100 | [diff] [blame] | 133 | |
| 134 | if (HAS_RC6pp(i915)) |
Andi Shyti | c113236 | 2019-09-27 12:08:49 +0100 | [diff] [blame] | 135 | val += intel_rc6_residency_ns(>->rc6, GEN6_GT_GFX_RC6pp); |
Chris Wilson | 16ffe73 | 2019-09-12 13:48:13 +0100 | [diff] [blame] | 136 | |
| 137 | return val; |
| 138 | } |
| 139 | |
| 140 | #if IS_ENABLED(CONFIG_PM) |
| 141 | |
| 142 | static inline s64 ktime_since(const ktime_t kt) |
| 143 | { |
| 144 | return ktime_to_ns(ktime_sub(ktime_get(), kt)); |
| 145 | } |
| 146 | |
Chris Wilson | 16ffe73 | 2019-09-12 13:48:13 +0100 | [diff] [blame] | 147 | static u64 get_rc6(struct intel_gt *gt) |
| 148 | { |
| 149 | struct drm_i915_private *i915 = gt->i915; |
| 150 | struct i915_pmu *pmu = &i915->pmu; |
| 151 | unsigned long flags; |
Tvrtko Ursulin | df6a420 | 2019-12-17 14:20:57 +0000 | [diff] [blame] | 152 | bool awake = false; |
Chris Wilson | 16ffe73 | 2019-09-12 13:48:13 +0100 | [diff] [blame] | 153 | u64 val; |
| 154 | |
Chris Wilson | 16ffe73 | 2019-09-12 13:48:13 +0100 | [diff] [blame] | 155 | if (intel_gt_pm_get_if_awake(gt)) { |
| 156 | val = __get_rc6(gt); |
Chris Wilson | 07779a7 | 2019-11-20 12:54:33 +0000 | [diff] [blame] | 157 | intel_gt_pm_put_async(gt); |
Tvrtko Ursulin | df6a420 | 2019-12-17 14:20:57 +0000 | [diff] [blame] | 158 | awake = true; |
Chris Wilson | 16ffe73 | 2019-09-12 13:48:13 +0100 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | spin_lock_irqsave(&pmu->lock, flags); |
| 162 | |
Tvrtko Ursulin | df6a420 | 2019-12-17 14:20:57 +0000 | [diff] [blame] | 163 | if (awake) { |
| 164 | pmu->sample[__I915_SAMPLE_RC6].cur = val; |
| 165 | } else { |
| 166 | /* |
| 167 | * We think we are runtime suspended. |
| 168 | * |
| 169 | * Report the delta from when the device was suspended to now, |
| 170 | * on top of the last known real value, as the approximated RC6 |
| 171 | * counter value. |
| 172 | */ |
| 173 | val = ktime_since(pmu->sleep_last); |
| 174 | val += pmu->sample[__I915_SAMPLE_RC6].cur; |
| 175 | } |
| 176 | |
| 177 | if (val < pmu->sample[__I915_SAMPLE_RC6_LAST_REPORTED].cur) |
| 178 | val = pmu->sample[__I915_SAMPLE_RC6_LAST_REPORTED].cur; |
Chris Wilson | 16ffe73 | 2019-09-12 13:48:13 +0100 | [diff] [blame] | 179 | else |
Tvrtko Ursulin | df6a420 | 2019-12-17 14:20:57 +0000 | [diff] [blame] | 180 | pmu->sample[__I915_SAMPLE_RC6_LAST_REPORTED].cur = val; |
Chris Wilson | 16ffe73 | 2019-09-12 13:48:13 +0100 | [diff] [blame] | 181 | |
| 182 | spin_unlock_irqrestore(&pmu->lock, flags); |
| 183 | |
| 184 | return val; |
| 185 | } |
| 186 | |
| 187 | static void park_rc6(struct drm_i915_private *i915) |
Tvrtko Ursulin | feff0dc | 2017-11-21 18:18:46 +0000 | [diff] [blame] | 188 | { |
Tvrtko Ursulin | 908091c | 2019-08-01 17:23:27 +0100 | [diff] [blame] | 189 | struct i915_pmu *pmu = &i915->pmu; |
| 190 | |
Chris Wilson | 16ffe73 | 2019-09-12 13:48:13 +0100 | [diff] [blame] | 191 | if (pmu->enable & config_enabled_mask(I915_PMU_RC6_RESIDENCY)) |
Tvrtko Ursulin | df6a420 | 2019-12-17 14:20:57 +0000 | [diff] [blame] | 192 | pmu->sample[__I915_SAMPLE_RC6].cur = __get_rc6(&i915->gt); |
Tvrtko Ursulin | feff0dc | 2017-11-21 18:18:46 +0000 | [diff] [blame] | 193 | |
Chris Wilson | 16ffe73 | 2019-09-12 13:48:13 +0100 | [diff] [blame] | 194 | pmu->sleep_last = ktime_get(); |
Tvrtko Ursulin | feff0dc | 2017-11-21 18:18:46 +0000 | [diff] [blame] | 195 | } |
| 196 | |
Chris Wilson | 16ffe73 | 2019-09-12 13:48:13 +0100 | [diff] [blame] | 197 | #else |
| 198 | |
| 199 | static u64 get_rc6(struct intel_gt *gt) |
| 200 | { |
| 201 | return __get_rc6(gt); |
| 202 | } |
| 203 | |
| 204 | static void park_rc6(struct drm_i915_private *i915) {} |
Chris Wilson | 16ffe73 | 2019-09-12 13:48:13 +0100 | [diff] [blame] | 205 | |
| 206 | #endif |
| 207 | |
Tvrtko Ursulin | 908091c | 2019-08-01 17:23:27 +0100 | [diff] [blame] | 208 | static void __i915_pmu_maybe_start_timer(struct i915_pmu *pmu) |
Tvrtko Ursulin | feff0dc | 2017-11-21 18:18:46 +0000 | [diff] [blame] | 209 | { |
Tvrtko Ursulin | 908091c | 2019-08-01 17:23:27 +0100 | [diff] [blame] | 210 | if (!pmu->timer_enabled && pmu_needs_timer(pmu, true)) { |
| 211 | pmu->timer_enabled = true; |
| 212 | pmu->timer_last = ktime_get(); |
| 213 | hrtimer_start_range_ns(&pmu->timer, |
Tvrtko Ursulin | feff0dc | 2017-11-21 18:18:46 +0000 | [diff] [blame] | 214 | ns_to_ktime(PERIOD), 0, |
| 215 | HRTIMER_MODE_REL_PINNED); |
| 216 | } |
| 217 | } |
| 218 | |
Chris Wilson | 16ffe73 | 2019-09-12 13:48:13 +0100 | [diff] [blame] | 219 | void i915_pmu_gt_parked(struct drm_i915_private *i915) |
| 220 | { |
| 221 | struct i915_pmu *pmu = &i915->pmu; |
| 222 | |
| 223 | if (!pmu->base.event_init) |
| 224 | return; |
| 225 | |
| 226 | spin_lock_irq(&pmu->lock); |
| 227 | |
| 228 | park_rc6(i915); |
| 229 | |
| 230 | /* |
| 231 | * Signal sampling timer to stop if only engine events are enabled and |
| 232 | * GPU went idle. |
| 233 | */ |
| 234 | pmu->timer_enabled = pmu_needs_timer(pmu, false); |
| 235 | |
| 236 | spin_unlock_irq(&pmu->lock); |
| 237 | } |
| 238 | |
Tvrtko Ursulin | feff0dc | 2017-11-21 18:18:46 +0000 | [diff] [blame] | 239 | void i915_pmu_gt_unparked(struct drm_i915_private *i915) |
| 240 | { |
Tvrtko Ursulin | 908091c | 2019-08-01 17:23:27 +0100 | [diff] [blame] | 241 | struct i915_pmu *pmu = &i915->pmu; |
| 242 | |
| 243 | if (!pmu->base.event_init) |
Tvrtko Ursulin | feff0dc | 2017-11-21 18:18:46 +0000 | [diff] [blame] | 244 | return; |
| 245 | |
Tvrtko Ursulin | 908091c | 2019-08-01 17:23:27 +0100 | [diff] [blame] | 246 | spin_lock_irq(&pmu->lock); |
Chris Wilson | 16ffe73 | 2019-09-12 13:48:13 +0100 | [diff] [blame] | 247 | |
Tvrtko Ursulin | feff0dc | 2017-11-21 18:18:46 +0000 | [diff] [blame] | 248 | /* |
| 249 | * Re-enable sampling timer when GPU goes active. |
| 250 | */ |
Tvrtko Ursulin | 908091c | 2019-08-01 17:23:27 +0100 | [diff] [blame] | 251 | __i915_pmu_maybe_start_timer(pmu); |
Chris Wilson | 16ffe73 | 2019-09-12 13:48:13 +0100 | [diff] [blame] | 252 | |
Tvrtko Ursulin | 908091c | 2019-08-01 17:23:27 +0100 | [diff] [blame] | 253 | spin_unlock_irq(&pmu->lock); |
Tvrtko Ursulin | feff0dc | 2017-11-21 18:18:46 +0000 | [diff] [blame] | 254 | } |
| 255 | |
Tvrtko Ursulin | b46a33e | 2017-11-21 18:18:45 +0000 | [diff] [blame] | 256 | static void |
Tvrtko Ursulin | 9f473ec | 2018-06-05 15:02:53 +0100 | [diff] [blame] | 257 | add_sample(struct i915_pmu_sample *sample, u32 val) |
Tvrtko Ursulin | b46a33e | 2017-11-21 18:18:45 +0000 | [diff] [blame] | 258 | { |
Tvrtko Ursulin | 9f473ec | 2018-06-05 15:02:53 +0100 | [diff] [blame] | 259 | sample->cur += val; |
Tvrtko Ursulin | b46a33e | 2017-11-21 18:18:45 +0000 | [diff] [blame] | 260 | } |
| 261 | |
Chris Wilson | d79e1bd | 2019-11-08 10:35:11 +0000 | [diff] [blame] | 262 | static bool exclusive_mmio_access(const struct drm_i915_private *i915) |
| 263 | { |
| 264 | /* |
| 265 | * We have to avoid concurrent mmio cache line access on gen7 or |
| 266 | * risk a machine hang. For a fun history lesson dig out the old |
| 267 | * userspace intel_gpu_top and run it on Ivybridge or Haswell! |
| 268 | */ |
| 269 | return IS_GEN(i915, 7); |
| 270 | } |
| 271 | |
Tvrtko Ursulin | 9f473ec | 2018-06-05 15:02:53 +0100 | [diff] [blame] | 272 | static void |
Tvrtko Ursulin | 08ce5c6 | 2019-08-01 17:23:29 +0100 | [diff] [blame] | 273 | engines_sample(struct intel_gt *gt, unsigned int period_ns) |
Tvrtko Ursulin | b46a33e | 2017-11-21 18:18:45 +0000 | [diff] [blame] | 274 | { |
Tvrtko Ursulin | 08ce5c6 | 2019-08-01 17:23:29 +0100 | [diff] [blame] | 275 | struct drm_i915_private *i915 = gt->i915; |
Tvrtko Ursulin | b46a33e | 2017-11-21 18:18:45 +0000 | [diff] [blame] | 276 | struct intel_engine_cs *engine; |
| 277 | enum intel_engine_id id; |
Tvrtko Ursulin | b46a33e | 2017-11-21 18:18:45 +0000 | [diff] [blame] | 278 | |
Tvrtko Ursulin | 28fba09 | 2019-08-01 17:23:28 +0100 | [diff] [blame] | 279 | if ((i915->pmu.enable & ENGINE_SAMPLE_MASK) == 0) |
Tvrtko Ursulin | b46a33e | 2017-11-21 18:18:45 +0000 | [diff] [blame] | 280 | return; |
| 281 | |
Chris Wilson | edb1eca | 2019-12-18 00:07:56 +0000 | [diff] [blame] | 282 | if (!intel_gt_pm_is_awake(gt)) |
| 283 | return; |
| 284 | |
Chris Wilson | c6e07ad | 2019-10-18 12:53:31 +0100 | [diff] [blame] | 285 | for_each_engine(engine, gt, id) { |
Chris Wilson | d0aa694 | 2019-02-23 00:01:02 +0000 | [diff] [blame] | 286 | struct intel_engine_pmu *pmu = &engine->pmu; |
Chris Wilson | d79e1bd | 2019-11-08 10:35:11 +0000 | [diff] [blame] | 287 | spinlock_t *mmio_lock; |
Chris Wilson | 51fbd8de | 2019-08-02 00:36:16 +0100 | [diff] [blame] | 288 | unsigned long flags; |
Chris Wilson | d0aa694 | 2019-02-23 00:01:02 +0000 | [diff] [blame] | 289 | bool busy; |
Tvrtko Ursulin | b46a33e | 2017-11-21 18:18:45 +0000 | [diff] [blame] | 290 | u32 val; |
| 291 | |
Chris Wilson | 51fbd8de | 2019-08-02 00:36:16 +0100 | [diff] [blame] | 292 | if (!intel_engine_pm_get_if_awake(engine)) |
| 293 | continue; |
| 294 | |
Chris Wilson | d79e1bd | 2019-11-08 10:35:11 +0000 | [diff] [blame] | 295 | mmio_lock = NULL; |
| 296 | if (exclusive_mmio_access(i915)) |
| 297 | mmio_lock = &engine->uncore->lock; |
| 298 | |
| 299 | if (unlikely(mmio_lock)) |
| 300 | spin_lock_irqsave(mmio_lock, flags); |
Chris Wilson | 51fbd8de | 2019-08-02 00:36:16 +0100 | [diff] [blame] | 301 | |
Tvrtko Ursulin | 28fba09 | 2019-08-01 17:23:28 +0100 | [diff] [blame] | 302 | val = ENGINE_READ_FW(engine, RING_CTL); |
Chris Wilson | d0aa694 | 2019-02-23 00:01:02 +0000 | [diff] [blame] | 303 | if (val == 0) /* powerwell off => engine idle */ |
Chris Wilson | 51fbd8de | 2019-08-02 00:36:16 +0100 | [diff] [blame] | 304 | goto skip; |
Tvrtko Ursulin | b46a33e | 2017-11-21 18:18:45 +0000 | [diff] [blame] | 305 | |
Tvrtko Ursulin | 9f473ec | 2018-06-05 15:02:53 +0100 | [diff] [blame] | 306 | if (val & RING_WAIT) |
Chris Wilson | d0aa694 | 2019-02-23 00:01:02 +0000 | [diff] [blame] | 307 | add_sample(&pmu->sample[I915_SAMPLE_WAIT], period_ns); |
Tvrtko Ursulin | 9f473ec | 2018-06-05 15:02:53 +0100 | [diff] [blame] | 308 | if (val & RING_WAIT_SEMAPHORE) |
Chris Wilson | d0aa694 | 2019-02-23 00:01:02 +0000 | [diff] [blame] | 309 | add_sample(&pmu->sample[I915_SAMPLE_SEMA], period_ns); |
Tvrtko Ursulin | b46a33e | 2017-11-21 18:18:45 +0000 | [diff] [blame] | 310 | |
Tvrtko Ursulin | 54fc577 | 2019-09-11 17:07:30 +0100 | [diff] [blame] | 311 | /* No need to sample when busy stats are supported. */ |
| 312 | if (intel_engine_supports_stats(engine)) |
| 313 | goto skip; |
| 314 | |
Chris Wilson | d0aa694 | 2019-02-23 00:01:02 +0000 | [diff] [blame] | 315 | /* |
| 316 | * While waiting on a semaphore or event, MI_MODE reports the |
| 317 | * ring as idle. However, previously using the seqno, and with |
| 318 | * execlists sampling, we account for the ring waiting as the |
| 319 | * engine being busy. Therefore, we record the sample as being |
| 320 | * busy if either waiting or !idle. |
| 321 | */ |
| 322 | busy = val & (RING_WAIT_SEMAPHORE | RING_WAIT); |
| 323 | if (!busy) { |
Tvrtko Ursulin | 28fba09 | 2019-08-01 17:23:28 +0100 | [diff] [blame] | 324 | val = ENGINE_READ_FW(engine, RING_MI_MODE); |
Chris Wilson | d0aa694 | 2019-02-23 00:01:02 +0000 | [diff] [blame] | 325 | busy = !(val & MODE_IDLE); |
| 326 | } |
| 327 | if (busy) |
| 328 | add_sample(&pmu->sample[I915_SAMPLE_BUSY], period_ns); |
Tvrtko Ursulin | b46a33e | 2017-11-21 18:18:45 +0000 | [diff] [blame] | 329 | |
Chris Wilson | 51fbd8de | 2019-08-02 00:36:16 +0100 | [diff] [blame] | 330 | skip: |
Chris Wilson | d79e1bd | 2019-11-08 10:35:11 +0000 | [diff] [blame] | 331 | if (unlikely(mmio_lock)) |
| 332 | spin_unlock_irqrestore(mmio_lock, flags); |
Chris Wilson | 07779a7 | 2019-11-20 12:54:33 +0000 | [diff] [blame] | 333 | intel_engine_pm_put_async(engine); |
Chris Wilson | 51fbd8de | 2019-08-02 00:36:16 +0100 | [diff] [blame] | 334 | } |
Tvrtko Ursulin | b46a33e | 2017-11-21 18:18:45 +0000 | [diff] [blame] | 335 | } |
| 336 | |
Tvrtko Ursulin | 9f473ec | 2018-06-05 15:02:53 +0100 | [diff] [blame] | 337 | static void |
| 338 | add_sample_mult(struct i915_pmu_sample *sample, u32 val, u32 mul) |
| 339 | { |
| 340 | sample->cur += mul_u32_u32(val, mul); |
| 341 | } |
| 342 | |
Tvrtko Ursulin | b66ecd0 | 2019-11-29 10:54:36 +0000 | [diff] [blame] | 343 | static bool frequency_sampling_enabled(struct i915_pmu *pmu) |
| 344 | { |
| 345 | return pmu->enable & |
| 346 | (config_enabled_mask(I915_PMU_ACTUAL_FREQUENCY) | |
| 347 | config_enabled_mask(I915_PMU_REQUESTED_FREQUENCY)); |
| 348 | } |
| 349 | |
Tvrtko Ursulin | 9f473ec | 2018-06-05 15:02:53 +0100 | [diff] [blame] | 350 | static void |
Tvrtko Ursulin | 08ce5c6 | 2019-08-01 17:23:29 +0100 | [diff] [blame] | 351 | frequency_sample(struct intel_gt *gt, unsigned int period_ns) |
Tvrtko Ursulin | b46a33e | 2017-11-21 18:18:45 +0000 | [diff] [blame] | 352 | { |
Tvrtko Ursulin | 08ce5c6 | 2019-08-01 17:23:29 +0100 | [diff] [blame] | 353 | struct drm_i915_private *i915 = gt->i915; |
| 354 | struct intel_uncore *uncore = gt->uncore; |
| 355 | struct i915_pmu *pmu = &i915->pmu; |
Andi Shyti | 3e7abf8 | 2019-10-24 22:16:41 +0100 | [diff] [blame] | 356 | struct intel_rps *rps = >->rps; |
Tvrtko Ursulin | 08ce5c6 | 2019-08-01 17:23:29 +0100 | [diff] [blame] | 357 | |
Tvrtko Ursulin | b66ecd0 | 2019-11-29 10:54:36 +0000 | [diff] [blame] | 358 | if (!frequency_sampling_enabled(pmu)) |
| 359 | return; |
| 360 | |
| 361 | /* Report 0/0 (actual/requested) frequency while parked. */ |
| 362 | if (!intel_gt_pm_get_if_awake(gt)) |
| 363 | return; |
| 364 | |
Tvrtko Ursulin | 08ce5c6 | 2019-08-01 17:23:29 +0100 | [diff] [blame] | 365 | if (pmu->enable & config_enabled_mask(I915_PMU_ACTUAL_FREQUENCY)) { |
Tvrtko Ursulin | b46a33e | 2017-11-21 18:18:45 +0000 | [diff] [blame] | 366 | u32 val; |
| 367 | |
Tvrtko Ursulin | b66ecd0 | 2019-11-29 10:54:36 +0000 | [diff] [blame] | 368 | /* |
| 369 | * We take a quick peek here without using forcewake |
| 370 | * so that we don't perturb the system under observation |
| 371 | * (forcewake => !rc6 => increased power use). We expect |
| 372 | * that if the read fails because it is outside of the |
| 373 | * mmio power well, then it will return 0 -- in which |
| 374 | * case we assume the system is running at the intended |
| 375 | * frequency. Fortunately, the read should rarely fail! |
| 376 | */ |
| 377 | val = intel_uncore_read_fw(uncore, GEN6_RPSTAT1); |
| 378 | if (val) |
Andi Shyti | e03512e | 2019-12-13 20:37:35 +0200 | [diff] [blame] | 379 | val = intel_rps_get_cagf(rps, val); |
Tvrtko Ursulin | b66ecd0 | 2019-11-29 10:54:36 +0000 | [diff] [blame] | 380 | else |
| 381 | val = rps->cur_freq; |
Tvrtko Ursulin | b46a33e | 2017-11-21 18:18:45 +0000 | [diff] [blame] | 382 | |
Tvrtko Ursulin | 08ce5c6 | 2019-08-01 17:23:29 +0100 | [diff] [blame] | 383 | add_sample_mult(&pmu->sample[__I915_SAMPLE_FREQ_ACT], |
Tvrtko Ursulin | b66ecd0 | 2019-11-29 10:54:36 +0000 | [diff] [blame] | 384 | intel_gpu_freq(rps, val), period_ns / 1000); |
Tvrtko Ursulin | b46a33e | 2017-11-21 18:18:45 +0000 | [diff] [blame] | 385 | } |
| 386 | |
Tvrtko Ursulin | 08ce5c6 | 2019-08-01 17:23:29 +0100 | [diff] [blame] | 387 | if (pmu->enable & config_enabled_mask(I915_PMU_REQUESTED_FREQUENCY)) { |
| 388 | add_sample_mult(&pmu->sample[__I915_SAMPLE_FREQ_REQ], |
Andi Shyti | 3e7abf8 | 2019-10-24 22:16:41 +0100 | [diff] [blame] | 389 | intel_gpu_freq(rps, rps->cur_freq), |
Tvrtko Ursulin | 9f473ec | 2018-06-05 15:02:53 +0100 | [diff] [blame] | 390 | period_ns / 1000); |
Tvrtko Ursulin | b46a33e | 2017-11-21 18:18:45 +0000 | [diff] [blame] | 391 | } |
Tvrtko Ursulin | b66ecd0 | 2019-11-29 10:54:36 +0000 | [diff] [blame] | 392 | |
| 393 | intel_gt_pm_put_async(gt); |
Tvrtko Ursulin | b46a33e | 2017-11-21 18:18:45 +0000 | [diff] [blame] | 394 | } |
| 395 | |
| 396 | static enum hrtimer_restart i915_sample(struct hrtimer *hrtimer) |
| 397 | { |
| 398 | struct drm_i915_private *i915 = |
| 399 | container_of(hrtimer, struct drm_i915_private, pmu.timer); |
Tvrtko Ursulin | 908091c | 2019-08-01 17:23:27 +0100 | [diff] [blame] | 400 | struct i915_pmu *pmu = &i915->pmu; |
Tvrtko Ursulin | 08ce5c6 | 2019-08-01 17:23:29 +0100 | [diff] [blame] | 401 | struct intel_gt *gt = &i915->gt; |
Tvrtko Ursulin | 9f473ec | 2018-06-05 15:02:53 +0100 | [diff] [blame] | 402 | unsigned int period_ns; |
| 403 | ktime_t now; |
Tvrtko Ursulin | b46a33e | 2017-11-21 18:18:45 +0000 | [diff] [blame] | 404 | |
Tvrtko Ursulin | 908091c | 2019-08-01 17:23:27 +0100 | [diff] [blame] | 405 | if (!READ_ONCE(pmu->timer_enabled)) |
Tvrtko Ursulin | b46a33e | 2017-11-21 18:18:45 +0000 | [diff] [blame] | 406 | return HRTIMER_NORESTART; |
| 407 | |
Tvrtko Ursulin | 9f473ec | 2018-06-05 15:02:53 +0100 | [diff] [blame] | 408 | now = ktime_get(); |
Tvrtko Ursulin | 908091c | 2019-08-01 17:23:27 +0100 | [diff] [blame] | 409 | period_ns = ktime_to_ns(ktime_sub(now, pmu->timer_last)); |
| 410 | pmu->timer_last = now; |
Tvrtko Ursulin | b46a33e | 2017-11-21 18:18:45 +0000 | [diff] [blame] | 411 | |
Tvrtko Ursulin | 9f473ec | 2018-06-05 15:02:53 +0100 | [diff] [blame] | 412 | /* |
| 413 | * Strictly speaking the passed in period may not be 100% accurate for |
| 414 | * all internal calculation, since some amount of time can be spent on |
| 415 | * grabbing the forcewake. However the potential error from timer call- |
| 416 | * back delay greatly dominates this so we keep it simple. |
| 417 | */ |
Tvrtko Ursulin | 08ce5c6 | 2019-08-01 17:23:29 +0100 | [diff] [blame] | 418 | engines_sample(gt, period_ns); |
| 419 | frequency_sample(gt, period_ns); |
Tvrtko Ursulin | 9f473ec | 2018-06-05 15:02:53 +0100 | [diff] [blame] | 420 | |
| 421 | hrtimer_forward(hrtimer, now, ns_to_ktime(PERIOD)); |
| 422 | |
Tvrtko Ursulin | b46a33e | 2017-11-21 18:18:45 +0000 | [diff] [blame] | 423 | return HRTIMER_RESTART; |
| 424 | } |
| 425 | |
Tvrtko Ursulin | 0cd4684 | 2017-11-21 18:18:50 +0000 | [diff] [blame] | 426 | static u64 count_interrupts(struct drm_i915_private *i915) |
| 427 | { |
| 428 | /* open-coded kstat_irqs() */ |
| 429 | struct irq_desc *desc = irq_to_desc(i915->drm.pdev->irq); |
| 430 | u64 sum = 0; |
| 431 | int cpu; |
| 432 | |
| 433 | if (!desc || !desc->kstat_irqs) |
| 434 | return 0; |
| 435 | |
| 436 | for_each_possible_cpu(cpu) |
| 437 | sum += *per_cpu_ptr(desc->kstat_irqs, cpu); |
| 438 | |
| 439 | return sum; |
| 440 | } |
| 441 | |
Tvrtko Ursulin | b2f78cd | 2018-02-05 09:34:48 +0000 | [diff] [blame] | 442 | static void engine_event_destroy(struct perf_event *event) |
| 443 | { |
| 444 | struct drm_i915_private *i915 = |
| 445 | container_of(event->pmu, typeof(*i915), pmu.base); |
| 446 | struct intel_engine_cs *engine; |
| 447 | |
| 448 | engine = intel_engine_lookup_user(i915, |
| 449 | engine_event_class(event), |
| 450 | engine_event_instance(event)); |
Pankaj Bharadiya | 48a1b8d | 2020-01-15 09:14:53 +0530 | [diff] [blame] | 451 | if (drm_WARN_ON_ONCE(&i915->drm, !engine)) |
Tvrtko Ursulin | b2f78cd | 2018-02-05 09:34:48 +0000 | [diff] [blame] | 452 | return; |
| 453 | |
| 454 | if (engine_event_sample(event) == I915_SAMPLE_BUSY && |
| 455 | intel_engine_supports_stats(engine)) |
| 456 | intel_disable_engine_stats(engine); |
| 457 | } |
| 458 | |
Tvrtko Ursulin | b46a33e | 2017-11-21 18:18:45 +0000 | [diff] [blame] | 459 | static void i915_pmu_event_destroy(struct perf_event *event) |
| 460 | { |
| 461 | WARN_ON(event->parent); |
Tvrtko Ursulin | b2f78cd | 2018-02-05 09:34:48 +0000 | [diff] [blame] | 462 | |
| 463 | if (is_engine_event(event)) |
| 464 | engine_event_destroy(event); |
Tvrtko Ursulin | b46a33e | 2017-11-21 18:18:45 +0000 | [diff] [blame] | 465 | } |
| 466 | |
Tvrtko Ursulin | 109ec55 | 2018-01-11 08:35:25 +0000 | [diff] [blame] | 467 | static int |
| 468 | engine_event_status(struct intel_engine_cs *engine, |
| 469 | enum drm_i915_pmu_engine_sample sample) |
Tvrtko Ursulin | b46a33e | 2017-11-21 18:18:45 +0000 | [diff] [blame] | 470 | { |
Tvrtko Ursulin | 109ec55 | 2018-01-11 08:35:25 +0000 | [diff] [blame] | 471 | switch (sample) { |
Tvrtko Ursulin | b46a33e | 2017-11-21 18:18:45 +0000 | [diff] [blame] | 472 | case I915_SAMPLE_BUSY: |
| 473 | case I915_SAMPLE_WAIT: |
| 474 | break; |
| 475 | case I915_SAMPLE_SEMA: |
Tvrtko Ursulin | 109ec55 | 2018-01-11 08:35:25 +0000 | [diff] [blame] | 476 | if (INTEL_GEN(engine->i915) < 6) |
Tvrtko Ursulin | b46a33e | 2017-11-21 18:18:45 +0000 | [diff] [blame] | 477 | return -ENODEV; |
| 478 | break; |
| 479 | default: |
| 480 | return -ENOENT; |
| 481 | } |
| 482 | |
| 483 | return 0; |
| 484 | } |
| 485 | |
Tvrtko Ursulin | 109ec55 | 2018-01-11 08:35:25 +0000 | [diff] [blame] | 486 | static int |
| 487 | config_status(struct drm_i915_private *i915, u64 config) |
| 488 | { |
| 489 | switch (config) { |
| 490 | case I915_PMU_ACTUAL_FREQUENCY: |
| 491 | if (IS_VALLEYVIEW(i915) || IS_CHERRYVIEW(i915)) |
| 492 | /* Requires a mutex for sampling! */ |
| 493 | return -ENODEV; |
| 494 | /* Fall-through. */ |
| 495 | case I915_PMU_REQUESTED_FREQUENCY: |
| 496 | if (INTEL_GEN(i915) < 6) |
| 497 | return -ENODEV; |
| 498 | break; |
| 499 | case I915_PMU_INTERRUPTS: |
| 500 | break; |
| 501 | case I915_PMU_RC6_RESIDENCY: |
| 502 | if (!HAS_RC6(i915)) |
| 503 | return -ENODEV; |
| 504 | break; |
| 505 | default: |
| 506 | return -ENOENT; |
| 507 | } |
| 508 | |
| 509 | return 0; |
| 510 | } |
| 511 | |
| 512 | static int engine_event_init(struct perf_event *event) |
| 513 | { |
| 514 | struct drm_i915_private *i915 = |
| 515 | container_of(event->pmu, typeof(*i915), pmu.base); |
| 516 | struct intel_engine_cs *engine; |
Tvrtko Ursulin | b2f78cd | 2018-02-05 09:34:48 +0000 | [diff] [blame] | 517 | u8 sample; |
| 518 | int ret; |
Tvrtko Ursulin | 109ec55 | 2018-01-11 08:35:25 +0000 | [diff] [blame] | 519 | |
| 520 | engine = intel_engine_lookup_user(i915, engine_event_class(event), |
| 521 | engine_event_instance(event)); |
| 522 | if (!engine) |
| 523 | return -ENODEV; |
| 524 | |
Tvrtko Ursulin | b2f78cd | 2018-02-05 09:34:48 +0000 | [diff] [blame] | 525 | sample = engine_event_sample(event); |
| 526 | ret = engine_event_status(engine, sample); |
| 527 | if (ret) |
| 528 | return ret; |
| 529 | |
| 530 | if (sample == I915_SAMPLE_BUSY && intel_engine_supports_stats(engine)) |
| 531 | ret = intel_enable_engine_stats(engine); |
| 532 | |
| 533 | return ret; |
Tvrtko Ursulin | 109ec55 | 2018-01-11 08:35:25 +0000 | [diff] [blame] | 534 | } |
| 535 | |
Tvrtko Ursulin | b46a33e | 2017-11-21 18:18:45 +0000 | [diff] [blame] | 536 | static int i915_pmu_event_init(struct perf_event *event) |
| 537 | { |
| 538 | struct drm_i915_private *i915 = |
| 539 | container_of(event->pmu, typeof(*i915), pmu.base); |
Tvrtko Ursulin | 0426c04 | 2017-11-23 12:34:32 +0000 | [diff] [blame] | 540 | int ret; |
Tvrtko Ursulin | b46a33e | 2017-11-21 18:18:45 +0000 | [diff] [blame] | 541 | |
| 542 | if (event->attr.type != event->pmu->type) |
| 543 | return -ENOENT; |
| 544 | |
| 545 | /* unsupported modes and filters */ |
| 546 | if (event->attr.sample_period) /* no sampling */ |
| 547 | return -EINVAL; |
| 548 | |
| 549 | if (has_branch_stack(event)) |
| 550 | return -EOPNOTSUPP; |
| 551 | |
| 552 | if (event->cpu < 0) |
| 553 | return -EINVAL; |
| 554 | |
Tvrtko Ursulin | 0426c04 | 2017-11-23 12:34:32 +0000 | [diff] [blame] | 555 | /* only allow running on one cpu at a time */ |
| 556 | if (!cpumask_test_cpu(event->cpu, &i915_pmu_cpumask)) |
Tvrtko Ursulin | 00a7972 | 2017-11-28 10:55:15 +0000 | [diff] [blame] | 557 | return -EINVAL; |
Tvrtko Ursulin | b46a33e | 2017-11-21 18:18:45 +0000 | [diff] [blame] | 558 | |
Tvrtko Ursulin | 109ec55 | 2018-01-11 08:35:25 +0000 | [diff] [blame] | 559 | if (is_engine_event(event)) |
Tvrtko Ursulin | b46a33e | 2017-11-21 18:18:45 +0000 | [diff] [blame] | 560 | ret = engine_event_init(event); |
Tvrtko Ursulin | 109ec55 | 2018-01-11 08:35:25 +0000 | [diff] [blame] | 561 | else |
| 562 | ret = config_status(i915, event->attr.config); |
Tvrtko Ursulin | b46a33e | 2017-11-21 18:18:45 +0000 | [diff] [blame] | 563 | if (ret) |
| 564 | return ret; |
| 565 | |
Tvrtko Ursulin | b46a33e | 2017-11-21 18:18:45 +0000 | [diff] [blame] | 566 | if (!event->parent) |
| 567 | event->destroy = i915_pmu_event_destroy; |
| 568 | |
| 569 | return 0; |
| 570 | } |
| 571 | |
Tvrtko Ursulin | ad055fb | 2018-03-14 08:05:35 +0000 | [diff] [blame] | 572 | static u64 __i915_pmu_event_read(struct perf_event *event) |
Tvrtko Ursulin | b46a33e | 2017-11-21 18:18:45 +0000 | [diff] [blame] | 573 | { |
| 574 | struct drm_i915_private *i915 = |
| 575 | container_of(event->pmu, typeof(*i915), pmu.base); |
Tvrtko Ursulin | 908091c | 2019-08-01 17:23:27 +0100 | [diff] [blame] | 576 | struct i915_pmu *pmu = &i915->pmu; |
Tvrtko Ursulin | b46a33e | 2017-11-21 18:18:45 +0000 | [diff] [blame] | 577 | u64 val = 0; |
| 578 | |
| 579 | if (is_engine_event(event)) { |
| 580 | u8 sample = engine_event_sample(event); |
| 581 | struct intel_engine_cs *engine; |
| 582 | |
| 583 | engine = intel_engine_lookup_user(i915, |
| 584 | engine_event_class(event), |
| 585 | engine_event_instance(event)); |
| 586 | |
Pankaj Bharadiya | 48a1b8d | 2020-01-15 09:14:53 +0530 | [diff] [blame] | 587 | if (drm_WARN_ON_ONCE(&i915->drm, !engine)) { |
Tvrtko Ursulin | b46a33e | 2017-11-21 18:18:45 +0000 | [diff] [blame] | 588 | /* Do nothing */ |
Tvrtko Ursulin | b3add01 | 2017-11-21 18:18:49 +0000 | [diff] [blame] | 589 | } else if (sample == I915_SAMPLE_BUSY && |
Tvrtko Ursulin | b2f78cd | 2018-02-05 09:34:48 +0000 | [diff] [blame] | 590 | intel_engine_supports_stats(engine)) { |
Tvrtko Ursulin | b3add01 | 2017-11-21 18:18:49 +0000 | [diff] [blame] | 591 | val = ktime_to_ns(intel_engine_get_busy_time(engine)); |
Tvrtko Ursulin | b46a33e | 2017-11-21 18:18:45 +0000 | [diff] [blame] | 592 | } else { |
| 593 | val = engine->pmu.sample[sample].cur; |
| 594 | } |
| 595 | } else { |
| 596 | switch (event->attr.config) { |
| 597 | case I915_PMU_ACTUAL_FREQUENCY: |
| 598 | val = |
Tvrtko Ursulin | 908091c | 2019-08-01 17:23:27 +0100 | [diff] [blame] | 599 | div_u64(pmu->sample[__I915_SAMPLE_FREQ_ACT].cur, |
Tvrtko Ursulin | 9f473ec | 2018-06-05 15:02:53 +0100 | [diff] [blame] | 600 | USEC_PER_SEC /* to MHz */); |
Tvrtko Ursulin | b46a33e | 2017-11-21 18:18:45 +0000 | [diff] [blame] | 601 | break; |
| 602 | case I915_PMU_REQUESTED_FREQUENCY: |
| 603 | val = |
Tvrtko Ursulin | 908091c | 2019-08-01 17:23:27 +0100 | [diff] [blame] | 604 | div_u64(pmu->sample[__I915_SAMPLE_FREQ_REQ].cur, |
Tvrtko Ursulin | 9f473ec | 2018-06-05 15:02:53 +0100 | [diff] [blame] | 605 | USEC_PER_SEC /* to MHz */); |
Tvrtko Ursulin | b46a33e | 2017-11-21 18:18:45 +0000 | [diff] [blame] | 606 | break; |
Tvrtko Ursulin | 0cd4684 | 2017-11-21 18:18:50 +0000 | [diff] [blame] | 607 | case I915_PMU_INTERRUPTS: |
| 608 | val = count_interrupts(i915); |
| 609 | break; |
Tvrtko Ursulin | 6060b6a | 2017-11-21 18:18:52 +0000 | [diff] [blame] | 610 | case I915_PMU_RC6_RESIDENCY: |
Tvrtko Ursulin | 518ea58 | 2019-08-01 17:23:30 +0100 | [diff] [blame] | 611 | val = get_rc6(&i915->gt); |
Tvrtko Ursulin | 6060b6a | 2017-11-21 18:18:52 +0000 | [diff] [blame] | 612 | break; |
Tvrtko Ursulin | b46a33e | 2017-11-21 18:18:45 +0000 | [diff] [blame] | 613 | } |
| 614 | } |
| 615 | |
| 616 | return val; |
| 617 | } |
| 618 | |
| 619 | static void i915_pmu_event_read(struct perf_event *event) |
| 620 | { |
| 621 | struct hw_perf_event *hwc = &event->hw; |
| 622 | u64 prev, new; |
| 623 | |
| 624 | again: |
| 625 | prev = local64_read(&hwc->prev_count); |
Tvrtko Ursulin | ad055fb | 2018-03-14 08:05:35 +0000 | [diff] [blame] | 626 | new = __i915_pmu_event_read(event); |
Tvrtko Ursulin | b46a33e | 2017-11-21 18:18:45 +0000 | [diff] [blame] | 627 | |
| 628 | if (local64_cmpxchg(&hwc->prev_count, prev, new) != prev) |
| 629 | goto again; |
| 630 | |
| 631 | local64_add(new - prev, &event->count); |
| 632 | } |
| 633 | |
| 634 | static void i915_pmu_enable(struct perf_event *event) |
| 635 | { |
| 636 | struct drm_i915_private *i915 = |
| 637 | container_of(event->pmu, typeof(*i915), pmu.base); |
| 638 | unsigned int bit = event_enabled_bit(event); |
Tvrtko Ursulin | 908091c | 2019-08-01 17:23:27 +0100 | [diff] [blame] | 639 | struct i915_pmu *pmu = &i915->pmu; |
Chris Wilson | f4e9894 | 2020-01-14 10:56:47 +0000 | [diff] [blame] | 640 | intel_wakeref_t wakeref; |
Tvrtko Ursulin | b46a33e | 2017-11-21 18:18:45 +0000 | [diff] [blame] | 641 | unsigned long flags; |
| 642 | |
Chris Wilson | f4e9894 | 2020-01-14 10:56:47 +0000 | [diff] [blame] | 643 | wakeref = intel_runtime_pm_get(&i915->runtime_pm); |
Tvrtko Ursulin | 908091c | 2019-08-01 17:23:27 +0100 | [diff] [blame] | 644 | spin_lock_irqsave(&pmu->lock, flags); |
Tvrtko Ursulin | b46a33e | 2017-11-21 18:18:45 +0000 | [diff] [blame] | 645 | |
| 646 | /* |
Tvrtko Ursulin | b46a33e | 2017-11-21 18:18:45 +0000 | [diff] [blame] | 647 | * Update the bitmask of enabled events and increment |
| 648 | * the event reference counter. |
| 649 | */ |
Tvrtko Ursulin | 908091c | 2019-08-01 17:23:27 +0100 | [diff] [blame] | 650 | BUILD_BUG_ON(ARRAY_SIZE(pmu->enable_count) != I915_PMU_MASK_BITS); |
| 651 | GEM_BUG_ON(bit >= ARRAY_SIZE(pmu->enable_count)); |
| 652 | GEM_BUG_ON(pmu->enable_count[bit] == ~0); |
Chris Wilson | f4e9894 | 2020-01-14 10:56:47 +0000 | [diff] [blame] | 653 | |
| 654 | if (pmu->enable_count[bit] == 0 && |
| 655 | config_enabled_mask(I915_PMU_RC6_RESIDENCY) & BIT_ULL(bit)) { |
| 656 | pmu->sample[__I915_SAMPLE_RC6_LAST_REPORTED].cur = 0; |
| 657 | pmu->sample[__I915_SAMPLE_RC6].cur = __get_rc6(&i915->gt); |
| 658 | pmu->sleep_last = ktime_get(); |
| 659 | } |
| 660 | |
Tvrtko Ursulin | 908091c | 2019-08-01 17:23:27 +0100 | [diff] [blame] | 661 | pmu->enable |= BIT_ULL(bit); |
| 662 | pmu->enable_count[bit]++; |
Tvrtko Ursulin | b46a33e | 2017-11-21 18:18:45 +0000 | [diff] [blame] | 663 | |
| 664 | /* |
Tvrtko Ursulin | feff0dc | 2017-11-21 18:18:46 +0000 | [diff] [blame] | 665 | * Start the sampling timer if needed and not already enabled. |
| 666 | */ |
Tvrtko Ursulin | 908091c | 2019-08-01 17:23:27 +0100 | [diff] [blame] | 667 | __i915_pmu_maybe_start_timer(pmu); |
Tvrtko Ursulin | feff0dc | 2017-11-21 18:18:46 +0000 | [diff] [blame] | 668 | |
| 669 | /* |
Tvrtko Ursulin | b46a33e | 2017-11-21 18:18:45 +0000 | [diff] [blame] | 670 | * For per-engine events the bitmask and reference counting |
| 671 | * is stored per engine. |
| 672 | */ |
| 673 | if (is_engine_event(event)) { |
| 674 | u8 sample = engine_event_sample(event); |
| 675 | struct intel_engine_cs *engine; |
| 676 | |
| 677 | engine = intel_engine_lookup_user(i915, |
| 678 | engine_event_class(event), |
| 679 | engine_event_instance(event)); |
Tvrtko Ursulin | b46a33e | 2017-11-21 18:18:45 +0000 | [diff] [blame] | 680 | |
Tvrtko Ursulin | 26a11de | 2019-02-05 13:03:53 +0000 | [diff] [blame] | 681 | BUILD_BUG_ON(ARRAY_SIZE(engine->pmu.enable_count) != |
| 682 | I915_ENGINE_SAMPLE_COUNT); |
| 683 | BUILD_BUG_ON(ARRAY_SIZE(engine->pmu.sample) != |
| 684 | I915_ENGINE_SAMPLE_COUNT); |
| 685 | GEM_BUG_ON(sample >= ARRAY_SIZE(engine->pmu.enable_count)); |
| 686 | GEM_BUG_ON(sample >= ARRAY_SIZE(engine->pmu.sample)); |
Tvrtko Ursulin | b46a33e | 2017-11-21 18:18:45 +0000 | [diff] [blame] | 687 | GEM_BUG_ON(engine->pmu.enable_count[sample] == ~0); |
Tvrtko Ursulin | 26a11de | 2019-02-05 13:03:53 +0000 | [diff] [blame] | 688 | |
| 689 | engine->pmu.enable |= BIT(sample); |
Tvrtko Ursulin | b2f78cd | 2018-02-05 09:34:48 +0000 | [diff] [blame] | 690 | engine->pmu.enable_count[sample]++; |
Tvrtko Ursulin | b46a33e | 2017-11-21 18:18:45 +0000 | [diff] [blame] | 691 | } |
| 692 | |
Tvrtko Ursulin | 908091c | 2019-08-01 17:23:27 +0100 | [diff] [blame] | 693 | spin_unlock_irqrestore(&pmu->lock, flags); |
Tvrtko Ursulin | ad055fb | 2018-03-14 08:05:35 +0000 | [diff] [blame] | 694 | |
Tvrtko Ursulin | b46a33e | 2017-11-21 18:18:45 +0000 | [diff] [blame] | 695 | /* |
| 696 | * Store the current counter value so we can report the correct delta |
| 697 | * for all listeners. Even when the event was already enabled and has |
| 698 | * an existing non-zero value. |
| 699 | */ |
Tvrtko Ursulin | ad055fb | 2018-03-14 08:05:35 +0000 | [diff] [blame] | 700 | local64_set(&event->hw.prev_count, __i915_pmu_event_read(event)); |
Chris Wilson | f4e9894 | 2020-01-14 10:56:47 +0000 | [diff] [blame] | 701 | |
| 702 | intel_runtime_pm_put(&i915->runtime_pm, wakeref); |
Tvrtko Ursulin | b46a33e | 2017-11-21 18:18:45 +0000 | [diff] [blame] | 703 | } |
| 704 | |
| 705 | static void i915_pmu_disable(struct perf_event *event) |
| 706 | { |
| 707 | struct drm_i915_private *i915 = |
| 708 | container_of(event->pmu, typeof(*i915), pmu.base); |
| 709 | unsigned int bit = event_enabled_bit(event); |
Tvrtko Ursulin | 908091c | 2019-08-01 17:23:27 +0100 | [diff] [blame] | 710 | struct i915_pmu *pmu = &i915->pmu; |
Tvrtko Ursulin | b46a33e | 2017-11-21 18:18:45 +0000 | [diff] [blame] | 711 | unsigned long flags; |
| 712 | |
Tvrtko Ursulin | 908091c | 2019-08-01 17:23:27 +0100 | [diff] [blame] | 713 | spin_lock_irqsave(&pmu->lock, flags); |
Tvrtko Ursulin | b46a33e | 2017-11-21 18:18:45 +0000 | [diff] [blame] | 714 | |
| 715 | if (is_engine_event(event)) { |
| 716 | u8 sample = engine_event_sample(event); |
| 717 | struct intel_engine_cs *engine; |
| 718 | |
| 719 | engine = intel_engine_lookup_user(i915, |
| 720 | engine_event_class(event), |
| 721 | engine_event_instance(event)); |
Tvrtko Ursulin | 26a11de | 2019-02-05 13:03:53 +0000 | [diff] [blame] | 722 | |
| 723 | GEM_BUG_ON(sample >= ARRAY_SIZE(engine->pmu.enable_count)); |
| 724 | GEM_BUG_ON(sample >= ARRAY_SIZE(engine->pmu.sample)); |
Tvrtko Ursulin | b46a33e | 2017-11-21 18:18:45 +0000 | [diff] [blame] | 725 | GEM_BUG_ON(engine->pmu.enable_count[sample] == 0); |
Tvrtko Ursulin | 26a11de | 2019-02-05 13:03:53 +0000 | [diff] [blame] | 726 | |
Tvrtko Ursulin | b46a33e | 2017-11-21 18:18:45 +0000 | [diff] [blame] | 727 | /* |
| 728 | * Decrement the reference count and clear the enabled |
| 729 | * bitmask when the last listener on an event goes away. |
| 730 | */ |
Tvrtko Ursulin | b2f78cd | 2018-02-05 09:34:48 +0000 | [diff] [blame] | 731 | if (--engine->pmu.enable_count[sample] == 0) |
Tvrtko Ursulin | b46a33e | 2017-11-21 18:18:45 +0000 | [diff] [blame] | 732 | engine->pmu.enable &= ~BIT(sample); |
| 733 | } |
| 734 | |
Tvrtko Ursulin | 908091c | 2019-08-01 17:23:27 +0100 | [diff] [blame] | 735 | GEM_BUG_ON(bit >= ARRAY_SIZE(pmu->enable_count)); |
| 736 | GEM_BUG_ON(pmu->enable_count[bit] == 0); |
Tvrtko Ursulin | b46a33e | 2017-11-21 18:18:45 +0000 | [diff] [blame] | 737 | /* |
| 738 | * Decrement the reference count and clear the enabled |
| 739 | * bitmask when the last listener on an event goes away. |
| 740 | */ |
Tvrtko Ursulin | 908091c | 2019-08-01 17:23:27 +0100 | [diff] [blame] | 741 | if (--pmu->enable_count[bit] == 0) { |
| 742 | pmu->enable &= ~BIT_ULL(bit); |
| 743 | pmu->timer_enabled &= pmu_needs_timer(pmu, true); |
Tvrtko Ursulin | feff0dc | 2017-11-21 18:18:46 +0000 | [diff] [blame] | 744 | } |
Tvrtko Ursulin | b46a33e | 2017-11-21 18:18:45 +0000 | [diff] [blame] | 745 | |
Tvrtko Ursulin | 908091c | 2019-08-01 17:23:27 +0100 | [diff] [blame] | 746 | spin_unlock_irqrestore(&pmu->lock, flags); |
Tvrtko Ursulin | b46a33e | 2017-11-21 18:18:45 +0000 | [diff] [blame] | 747 | } |
| 748 | |
| 749 | static void i915_pmu_event_start(struct perf_event *event, int flags) |
| 750 | { |
| 751 | i915_pmu_enable(event); |
| 752 | event->hw.state = 0; |
| 753 | } |
| 754 | |
| 755 | static void i915_pmu_event_stop(struct perf_event *event, int flags) |
| 756 | { |
| 757 | if (flags & PERF_EF_UPDATE) |
| 758 | i915_pmu_event_read(event); |
| 759 | i915_pmu_disable(event); |
| 760 | event->hw.state = PERF_HES_STOPPED; |
| 761 | } |
| 762 | |
| 763 | static int i915_pmu_event_add(struct perf_event *event, int flags) |
| 764 | { |
| 765 | if (flags & PERF_EF_START) |
| 766 | i915_pmu_event_start(event, flags); |
| 767 | |
| 768 | return 0; |
| 769 | } |
| 770 | |
| 771 | static void i915_pmu_event_del(struct perf_event *event, int flags) |
| 772 | { |
| 773 | i915_pmu_event_stop(event, PERF_EF_UPDATE); |
| 774 | } |
| 775 | |
| 776 | static int i915_pmu_event_event_idx(struct perf_event *event) |
| 777 | { |
| 778 | return 0; |
| 779 | } |
| 780 | |
Chris Wilson | b7d3aab | 2017-11-23 21:17:51 +0000 | [diff] [blame] | 781 | struct i915_str_attribute { |
| 782 | struct device_attribute attr; |
| 783 | const char *str; |
| 784 | }; |
| 785 | |
Tvrtko Ursulin | b46a33e | 2017-11-21 18:18:45 +0000 | [diff] [blame] | 786 | static ssize_t i915_pmu_format_show(struct device *dev, |
| 787 | struct device_attribute *attr, char *buf) |
| 788 | { |
Chris Wilson | b7d3aab | 2017-11-23 21:17:51 +0000 | [diff] [blame] | 789 | struct i915_str_attribute *eattr; |
Tvrtko Ursulin | b46a33e | 2017-11-21 18:18:45 +0000 | [diff] [blame] | 790 | |
Chris Wilson | b7d3aab | 2017-11-23 21:17:51 +0000 | [diff] [blame] | 791 | eattr = container_of(attr, struct i915_str_attribute, attr); |
| 792 | return sprintf(buf, "%s\n", eattr->str); |
Tvrtko Ursulin | b46a33e | 2017-11-21 18:18:45 +0000 | [diff] [blame] | 793 | } |
| 794 | |
| 795 | #define I915_PMU_FORMAT_ATTR(_name, _config) \ |
Chris Wilson | b7d3aab | 2017-11-23 21:17:51 +0000 | [diff] [blame] | 796 | (&((struct i915_str_attribute[]) { \ |
Tvrtko Ursulin | b46a33e | 2017-11-21 18:18:45 +0000 | [diff] [blame] | 797 | { .attr = __ATTR(_name, 0444, i915_pmu_format_show, NULL), \ |
Chris Wilson | b7d3aab | 2017-11-23 21:17:51 +0000 | [diff] [blame] | 798 | .str = _config, } \ |
Tvrtko Ursulin | b46a33e | 2017-11-21 18:18:45 +0000 | [diff] [blame] | 799 | })[0].attr.attr) |
| 800 | |
| 801 | static struct attribute *i915_pmu_format_attrs[] = { |
| 802 | I915_PMU_FORMAT_ATTR(i915_eventid, "config:0-20"), |
| 803 | NULL, |
| 804 | }; |
| 805 | |
| 806 | static const struct attribute_group i915_pmu_format_attr_group = { |
| 807 | .name = "format", |
| 808 | .attrs = i915_pmu_format_attrs, |
| 809 | }; |
| 810 | |
Chris Wilson | b7d3aab | 2017-11-23 21:17:51 +0000 | [diff] [blame] | 811 | struct i915_ext_attribute { |
| 812 | struct device_attribute attr; |
| 813 | unsigned long val; |
| 814 | }; |
| 815 | |
Tvrtko Ursulin | b46a33e | 2017-11-21 18:18:45 +0000 | [diff] [blame] | 816 | static ssize_t i915_pmu_event_show(struct device *dev, |
| 817 | struct device_attribute *attr, char *buf) |
| 818 | { |
Chris Wilson | b7d3aab | 2017-11-23 21:17:51 +0000 | [diff] [blame] | 819 | struct i915_ext_attribute *eattr; |
Tvrtko Ursulin | b46a33e | 2017-11-21 18:18:45 +0000 | [diff] [blame] | 820 | |
Chris Wilson | b7d3aab | 2017-11-23 21:17:51 +0000 | [diff] [blame] | 821 | eattr = container_of(attr, struct i915_ext_attribute, attr); |
| 822 | return sprintf(buf, "config=0x%lx\n", eattr->val); |
Tvrtko Ursulin | b46a33e | 2017-11-21 18:18:45 +0000 | [diff] [blame] | 823 | } |
| 824 | |
Tvrtko Ursulin | 109ec55 | 2018-01-11 08:35:25 +0000 | [diff] [blame] | 825 | static struct attribute_group i915_pmu_events_attr_group = { |
Tvrtko Ursulin | b46a33e | 2017-11-21 18:18:45 +0000 | [diff] [blame] | 826 | .name = "events", |
Tvrtko Ursulin | 109ec55 | 2018-01-11 08:35:25 +0000 | [diff] [blame] | 827 | /* Patch in attrs at runtime. */ |
Tvrtko Ursulin | b46a33e | 2017-11-21 18:18:45 +0000 | [diff] [blame] | 828 | }; |
| 829 | |
| 830 | static ssize_t |
| 831 | i915_pmu_get_attr_cpumask(struct device *dev, |
| 832 | struct device_attribute *attr, |
| 833 | char *buf) |
| 834 | { |
| 835 | return cpumap_print_to_pagebuf(true, buf, &i915_pmu_cpumask); |
| 836 | } |
| 837 | |
| 838 | static DEVICE_ATTR(cpumask, 0444, i915_pmu_get_attr_cpumask, NULL); |
| 839 | |
| 840 | static struct attribute *i915_cpumask_attrs[] = { |
| 841 | &dev_attr_cpumask.attr, |
| 842 | NULL, |
| 843 | }; |
| 844 | |
Tvrtko Ursulin | 109ec55 | 2018-01-11 08:35:25 +0000 | [diff] [blame] | 845 | static const struct attribute_group i915_pmu_cpumask_attr_group = { |
Tvrtko Ursulin | b46a33e | 2017-11-21 18:18:45 +0000 | [diff] [blame] | 846 | .attrs = i915_cpumask_attrs, |
| 847 | }; |
| 848 | |
| 849 | static const struct attribute_group *i915_pmu_attr_groups[] = { |
| 850 | &i915_pmu_format_attr_group, |
| 851 | &i915_pmu_events_attr_group, |
| 852 | &i915_pmu_cpumask_attr_group, |
| 853 | NULL |
| 854 | }; |
| 855 | |
Tvrtko Ursulin | 109ec55 | 2018-01-11 08:35:25 +0000 | [diff] [blame] | 856 | #define __event(__config, __name, __unit) \ |
| 857 | { \ |
| 858 | .config = (__config), \ |
| 859 | .name = (__name), \ |
| 860 | .unit = (__unit), \ |
| 861 | } |
| 862 | |
| 863 | #define __engine_event(__sample, __name) \ |
| 864 | { \ |
| 865 | .sample = (__sample), \ |
| 866 | .name = (__name), \ |
| 867 | } |
| 868 | |
| 869 | static struct i915_ext_attribute * |
| 870 | add_i915_attr(struct i915_ext_attribute *attr, const char *name, u64 config) |
| 871 | { |
Chris Wilson | 2bbba4e | 2018-01-11 14:04:02 +0000 | [diff] [blame] | 872 | sysfs_attr_init(&attr->attr.attr); |
Tvrtko Ursulin | 109ec55 | 2018-01-11 08:35:25 +0000 | [diff] [blame] | 873 | attr->attr.attr.name = name; |
| 874 | attr->attr.attr.mode = 0444; |
| 875 | attr->attr.show = i915_pmu_event_show; |
| 876 | attr->val = config; |
| 877 | |
| 878 | return ++attr; |
| 879 | } |
| 880 | |
| 881 | static struct perf_pmu_events_attr * |
| 882 | add_pmu_attr(struct perf_pmu_events_attr *attr, const char *name, |
| 883 | const char *str) |
| 884 | { |
Chris Wilson | 2bbba4e | 2018-01-11 14:04:02 +0000 | [diff] [blame] | 885 | sysfs_attr_init(&attr->attr.attr); |
Tvrtko Ursulin | 109ec55 | 2018-01-11 08:35:25 +0000 | [diff] [blame] | 886 | attr->attr.attr.name = name; |
| 887 | attr->attr.attr.mode = 0444; |
| 888 | attr->attr.show = perf_event_sysfs_show; |
| 889 | attr->event_str = str; |
| 890 | |
| 891 | return ++attr; |
| 892 | } |
| 893 | |
| 894 | static struct attribute ** |
Tvrtko Ursulin | 908091c | 2019-08-01 17:23:27 +0100 | [diff] [blame] | 895 | create_event_attributes(struct i915_pmu *pmu) |
Tvrtko Ursulin | 109ec55 | 2018-01-11 08:35:25 +0000 | [diff] [blame] | 896 | { |
Tvrtko Ursulin | 908091c | 2019-08-01 17:23:27 +0100 | [diff] [blame] | 897 | struct drm_i915_private *i915 = container_of(pmu, typeof(*i915), pmu); |
Tvrtko Ursulin | 109ec55 | 2018-01-11 08:35:25 +0000 | [diff] [blame] | 898 | static const struct { |
| 899 | u64 config; |
| 900 | const char *name; |
| 901 | const char *unit; |
| 902 | } events[] = { |
Chris Wilson | e88866e | 2019-11-09 10:53:56 +0000 | [diff] [blame] | 903 | __event(I915_PMU_ACTUAL_FREQUENCY, "actual-frequency", "M"), |
| 904 | __event(I915_PMU_REQUESTED_FREQUENCY, "requested-frequency", "M"), |
Tvrtko Ursulin | 109ec55 | 2018-01-11 08:35:25 +0000 | [diff] [blame] | 905 | __event(I915_PMU_INTERRUPTS, "interrupts", NULL), |
| 906 | __event(I915_PMU_RC6_RESIDENCY, "rc6-residency", "ns"), |
| 907 | }; |
| 908 | static const struct { |
| 909 | enum drm_i915_pmu_engine_sample sample; |
| 910 | char *name; |
| 911 | } engine_events[] = { |
| 912 | __engine_event(I915_SAMPLE_BUSY, "busy"), |
| 913 | __engine_event(I915_SAMPLE_SEMA, "sema"), |
| 914 | __engine_event(I915_SAMPLE_WAIT, "wait"), |
| 915 | }; |
| 916 | unsigned int count = 0; |
| 917 | struct perf_pmu_events_attr *pmu_attr = NULL, *pmu_iter; |
| 918 | struct i915_ext_attribute *i915_attr = NULL, *i915_iter; |
| 919 | struct attribute **attr = NULL, **attr_iter; |
| 920 | struct intel_engine_cs *engine; |
Tvrtko Ursulin | 109ec55 | 2018-01-11 08:35:25 +0000 | [diff] [blame] | 921 | unsigned int i; |
| 922 | |
| 923 | /* Count how many counters we will be exposing. */ |
| 924 | for (i = 0; i < ARRAY_SIZE(events); i++) { |
| 925 | if (!config_status(i915, events[i].config)) |
| 926 | count++; |
| 927 | } |
| 928 | |
Chris Wilson | 750e76b | 2019-08-06 13:43:00 +0100 | [diff] [blame] | 929 | for_each_uabi_engine(engine, i915) { |
Tvrtko Ursulin | 109ec55 | 2018-01-11 08:35:25 +0000 | [diff] [blame] | 930 | for (i = 0; i < ARRAY_SIZE(engine_events); i++) { |
| 931 | if (!engine_event_status(engine, |
| 932 | engine_events[i].sample)) |
| 933 | count++; |
| 934 | } |
| 935 | } |
| 936 | |
| 937 | /* Allocate attribute objects and table. */ |
Tvrtko Ursulin | dd5fec8 | 2018-01-12 17:03:40 +0000 | [diff] [blame] | 938 | i915_attr = kcalloc(count, sizeof(*i915_attr), GFP_KERNEL); |
Tvrtko Ursulin | 109ec55 | 2018-01-11 08:35:25 +0000 | [diff] [blame] | 939 | if (!i915_attr) |
| 940 | goto err_alloc; |
| 941 | |
Tvrtko Ursulin | dd5fec8 | 2018-01-12 17:03:40 +0000 | [diff] [blame] | 942 | pmu_attr = kcalloc(count, sizeof(*pmu_attr), GFP_KERNEL); |
Tvrtko Ursulin | 109ec55 | 2018-01-11 08:35:25 +0000 | [diff] [blame] | 943 | if (!pmu_attr) |
| 944 | goto err_alloc; |
| 945 | |
| 946 | /* Max one pointer of each attribute type plus a termination entry. */ |
Tvrtko Ursulin | dd5fec8 | 2018-01-12 17:03:40 +0000 | [diff] [blame] | 947 | attr = kcalloc(count * 2 + 1, sizeof(*attr), GFP_KERNEL); |
Tvrtko Ursulin | 109ec55 | 2018-01-11 08:35:25 +0000 | [diff] [blame] | 948 | if (!attr) |
| 949 | goto err_alloc; |
| 950 | |
| 951 | i915_iter = i915_attr; |
| 952 | pmu_iter = pmu_attr; |
| 953 | attr_iter = attr; |
| 954 | |
| 955 | /* Initialize supported non-engine counters. */ |
| 956 | for (i = 0; i < ARRAY_SIZE(events); i++) { |
| 957 | char *str; |
| 958 | |
| 959 | if (config_status(i915, events[i].config)) |
| 960 | continue; |
| 961 | |
| 962 | str = kstrdup(events[i].name, GFP_KERNEL); |
| 963 | if (!str) |
| 964 | goto err; |
| 965 | |
| 966 | *attr_iter++ = &i915_iter->attr.attr; |
| 967 | i915_iter = add_i915_attr(i915_iter, str, events[i].config); |
| 968 | |
| 969 | if (events[i].unit) { |
| 970 | str = kasprintf(GFP_KERNEL, "%s.unit", events[i].name); |
| 971 | if (!str) |
| 972 | goto err; |
| 973 | |
| 974 | *attr_iter++ = &pmu_iter->attr.attr; |
| 975 | pmu_iter = add_pmu_attr(pmu_iter, str, events[i].unit); |
| 976 | } |
| 977 | } |
| 978 | |
| 979 | /* Initialize supported engine counters. */ |
Chris Wilson | 750e76b | 2019-08-06 13:43:00 +0100 | [diff] [blame] | 980 | for_each_uabi_engine(engine, i915) { |
Tvrtko Ursulin | 109ec55 | 2018-01-11 08:35:25 +0000 | [diff] [blame] | 981 | for (i = 0; i < ARRAY_SIZE(engine_events); i++) { |
| 982 | char *str; |
| 983 | |
| 984 | if (engine_event_status(engine, |
| 985 | engine_events[i].sample)) |
| 986 | continue; |
| 987 | |
| 988 | str = kasprintf(GFP_KERNEL, "%s-%s", |
| 989 | engine->name, engine_events[i].name); |
| 990 | if (!str) |
| 991 | goto err; |
| 992 | |
| 993 | *attr_iter++ = &i915_iter->attr.attr; |
| 994 | i915_iter = |
| 995 | add_i915_attr(i915_iter, str, |
Tvrtko Ursulin | 8810bc5 | 2018-01-23 13:45:58 +0000 | [diff] [blame] | 996 | __I915_PMU_ENGINE(engine->uabi_class, |
Chris Wilson | 750e76b | 2019-08-06 13:43:00 +0100 | [diff] [blame] | 997 | engine->uabi_instance, |
Tvrtko Ursulin | 109ec55 | 2018-01-11 08:35:25 +0000 | [diff] [blame] | 998 | engine_events[i].sample)); |
| 999 | |
| 1000 | str = kasprintf(GFP_KERNEL, "%s-%s.unit", |
| 1001 | engine->name, engine_events[i].name); |
| 1002 | if (!str) |
| 1003 | goto err; |
| 1004 | |
| 1005 | *attr_iter++ = &pmu_iter->attr.attr; |
| 1006 | pmu_iter = add_pmu_attr(pmu_iter, str, "ns"); |
| 1007 | } |
| 1008 | } |
| 1009 | |
Tvrtko Ursulin | 908091c | 2019-08-01 17:23:27 +0100 | [diff] [blame] | 1010 | pmu->i915_attr = i915_attr; |
| 1011 | pmu->pmu_attr = pmu_attr; |
Tvrtko Ursulin | 109ec55 | 2018-01-11 08:35:25 +0000 | [diff] [blame] | 1012 | |
| 1013 | return attr; |
| 1014 | |
| 1015 | err:; |
| 1016 | for (attr_iter = attr; *attr_iter; attr_iter++) |
| 1017 | kfree((*attr_iter)->name); |
| 1018 | |
| 1019 | err_alloc: |
| 1020 | kfree(attr); |
| 1021 | kfree(i915_attr); |
| 1022 | kfree(pmu_attr); |
| 1023 | |
| 1024 | return NULL; |
| 1025 | } |
| 1026 | |
Tvrtko Ursulin | 908091c | 2019-08-01 17:23:27 +0100 | [diff] [blame] | 1027 | static void free_event_attributes(struct i915_pmu *pmu) |
Tvrtko Ursulin | 109ec55 | 2018-01-11 08:35:25 +0000 | [diff] [blame] | 1028 | { |
| 1029 | struct attribute **attr_iter = i915_pmu_events_attr_group.attrs; |
| 1030 | |
| 1031 | for (; *attr_iter; attr_iter++) |
| 1032 | kfree((*attr_iter)->name); |
| 1033 | |
| 1034 | kfree(i915_pmu_events_attr_group.attrs); |
Tvrtko Ursulin | 908091c | 2019-08-01 17:23:27 +0100 | [diff] [blame] | 1035 | kfree(pmu->i915_attr); |
| 1036 | kfree(pmu->pmu_attr); |
Tvrtko Ursulin | 109ec55 | 2018-01-11 08:35:25 +0000 | [diff] [blame] | 1037 | |
| 1038 | i915_pmu_events_attr_group.attrs = NULL; |
Tvrtko Ursulin | 908091c | 2019-08-01 17:23:27 +0100 | [diff] [blame] | 1039 | pmu->i915_attr = NULL; |
| 1040 | pmu->pmu_attr = NULL; |
Tvrtko Ursulin | 109ec55 | 2018-01-11 08:35:25 +0000 | [diff] [blame] | 1041 | } |
| 1042 | |
Tvrtko Ursulin | b46a33e | 2017-11-21 18:18:45 +0000 | [diff] [blame] | 1043 | static int i915_pmu_cpu_online(unsigned int cpu, struct hlist_node *node) |
| 1044 | { |
| 1045 | struct i915_pmu *pmu = hlist_entry_safe(node, typeof(*pmu), node); |
Tvrtko Ursulin | b46a33e | 2017-11-21 18:18:45 +0000 | [diff] [blame] | 1046 | |
| 1047 | GEM_BUG_ON(!pmu->base.event_init); |
| 1048 | |
Tvrtko Ursulin | b46a33e | 2017-11-21 18:18:45 +0000 | [diff] [blame] | 1049 | /* Select the first online CPU as a designated reader. */ |
Tvrtko Ursulin | 0426c04 | 2017-11-23 12:34:32 +0000 | [diff] [blame] | 1050 | if (!cpumask_weight(&i915_pmu_cpumask)) |
Tvrtko Ursulin | b46a33e | 2017-11-21 18:18:45 +0000 | [diff] [blame] | 1051 | cpumask_set_cpu(cpu, &i915_pmu_cpumask); |
| 1052 | |
| 1053 | return 0; |
| 1054 | } |
| 1055 | |
| 1056 | static int i915_pmu_cpu_offline(unsigned int cpu, struct hlist_node *node) |
| 1057 | { |
| 1058 | struct i915_pmu *pmu = hlist_entry_safe(node, typeof(*pmu), node); |
| 1059 | unsigned int target; |
| 1060 | |
| 1061 | GEM_BUG_ON(!pmu->base.event_init); |
| 1062 | |
| 1063 | if (cpumask_test_and_clear_cpu(cpu, &i915_pmu_cpumask)) { |
| 1064 | target = cpumask_any_but(topology_sibling_cpumask(cpu), cpu); |
| 1065 | /* Migrate events if there is a valid target */ |
| 1066 | if (target < nr_cpu_ids) { |
| 1067 | cpumask_set_cpu(target, &i915_pmu_cpumask); |
| 1068 | perf_pmu_migrate_context(&pmu->base, cpu, target); |
| 1069 | } |
| 1070 | } |
| 1071 | |
| 1072 | return 0; |
| 1073 | } |
| 1074 | |
| 1075 | static enum cpuhp_state cpuhp_slot = CPUHP_INVALID; |
Tvrtko Ursulin | b46a33e | 2017-11-21 18:18:45 +0000 | [diff] [blame] | 1076 | |
Tvrtko Ursulin | 908091c | 2019-08-01 17:23:27 +0100 | [diff] [blame] | 1077 | static int i915_pmu_register_cpuhp_state(struct i915_pmu *pmu) |
Tvrtko Ursulin | b46a33e | 2017-11-21 18:18:45 +0000 | [diff] [blame] | 1078 | { |
Tvrtko Ursulin | b46a33e | 2017-11-21 18:18:45 +0000 | [diff] [blame] | 1079 | enum cpuhp_state slot; |
| 1080 | int ret; |
| 1081 | |
| 1082 | ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN, |
| 1083 | "perf/x86/intel/i915:online", |
| 1084 | i915_pmu_cpu_online, |
| 1085 | i915_pmu_cpu_offline); |
| 1086 | if (ret < 0) |
| 1087 | return ret; |
| 1088 | |
| 1089 | slot = ret; |
Tvrtko Ursulin | 908091c | 2019-08-01 17:23:27 +0100 | [diff] [blame] | 1090 | ret = cpuhp_state_add_instance(slot, &pmu->node); |
Tvrtko Ursulin | b46a33e | 2017-11-21 18:18:45 +0000 | [diff] [blame] | 1091 | if (ret) { |
| 1092 | cpuhp_remove_multi_state(slot); |
| 1093 | return ret; |
| 1094 | } |
| 1095 | |
| 1096 | cpuhp_slot = slot; |
Tvrtko Ursulin | b46a33e | 2017-11-21 18:18:45 +0000 | [diff] [blame] | 1097 | return 0; |
| 1098 | } |
| 1099 | |
Tvrtko Ursulin | 908091c | 2019-08-01 17:23:27 +0100 | [diff] [blame] | 1100 | static void i915_pmu_unregister_cpuhp_state(struct i915_pmu *pmu) |
Tvrtko Ursulin | b46a33e | 2017-11-21 18:18:45 +0000 | [diff] [blame] | 1101 | { |
Tvrtko Ursulin | b46a33e | 2017-11-21 18:18:45 +0000 | [diff] [blame] | 1102 | WARN_ON(cpuhp_slot == CPUHP_INVALID); |
Tvrtko Ursulin | 908091c | 2019-08-01 17:23:27 +0100 | [diff] [blame] | 1103 | WARN_ON(cpuhp_state_remove_instance(cpuhp_slot, &pmu->node)); |
Tvrtko Ursulin | b46a33e | 2017-11-21 18:18:45 +0000 | [diff] [blame] | 1104 | cpuhp_remove_multi_state(cpuhp_slot); |
Tvrtko Ursulin | b46a33e | 2017-11-21 18:18:45 +0000 | [diff] [blame] | 1105 | } |
| 1106 | |
Tvrtko Ursulin | 0548867 | 2019-10-16 10:38:02 +0100 | [diff] [blame] | 1107 | static bool is_igp(struct drm_i915_private *i915) |
| 1108 | { |
| 1109 | struct pci_dev *pdev = i915->drm.pdev; |
| 1110 | |
| 1111 | /* IGP is 0000:00:02.0 */ |
| 1112 | return pci_domain_nr(pdev->bus) == 0 && |
| 1113 | pdev->bus->number == 0 && |
| 1114 | PCI_SLOT(pdev->devfn) == 2 && |
| 1115 | PCI_FUNC(pdev->devfn) == 0; |
| 1116 | } |
| 1117 | |
Tvrtko Ursulin | b46a33e | 2017-11-21 18:18:45 +0000 | [diff] [blame] | 1118 | void i915_pmu_register(struct drm_i915_private *i915) |
| 1119 | { |
Tvrtko Ursulin | 908091c | 2019-08-01 17:23:27 +0100 | [diff] [blame] | 1120 | struct i915_pmu *pmu = &i915->pmu; |
Tvrtko Ursulin | fb26eee | 2019-10-18 10:05:14 +0100 | [diff] [blame] | 1121 | int ret = -ENOMEM; |
Tvrtko Ursulin | b46a33e | 2017-11-21 18:18:45 +0000 | [diff] [blame] | 1122 | |
| 1123 | if (INTEL_GEN(i915) <= 2) { |
Chris Wilson | 88f8065 | 2019-08-15 10:36:04 +0100 | [diff] [blame] | 1124 | dev_info(i915->drm.dev, "PMU not supported for this GPU."); |
Tvrtko Ursulin | b46a33e | 2017-11-21 18:18:45 +0000 | [diff] [blame] | 1125 | return; |
| 1126 | } |
| 1127 | |
Tvrtko Ursulin | 908091c | 2019-08-01 17:23:27 +0100 | [diff] [blame] | 1128 | spin_lock_init(&pmu->lock); |
| 1129 | hrtimer_init(&pmu->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); |
| 1130 | pmu->timer.function = i915_sample; |
Tvrtko Ursulin | b46a33e | 2017-11-21 18:18:45 +0000 | [diff] [blame] | 1131 | |
Tvrtko Ursulin | aebf3b5 | 2020-01-10 11:32:53 +0000 | [diff] [blame] | 1132 | if (!is_igp(i915)) { |
Tvrtko Ursulin | 0548867 | 2019-10-16 10:38:02 +0100 | [diff] [blame] | 1133 | pmu->name = kasprintf(GFP_KERNEL, |
Tvrtko Ursulin | aebf3b5 | 2020-01-10 11:32:53 +0000 | [diff] [blame] | 1134 | "i915_%s", |
Tvrtko Ursulin | 0548867 | 2019-10-16 10:38:02 +0100 | [diff] [blame] | 1135 | dev_name(i915->drm.dev)); |
Tvrtko Ursulin | aebf3b5 | 2020-01-10 11:32:53 +0000 | [diff] [blame] | 1136 | if (pmu->name) { |
| 1137 | /* tools/perf reserves colons as special. */ |
| 1138 | strreplace((char *)pmu->name, ':', '_'); |
| 1139 | } |
| 1140 | } else { |
Tvrtko Ursulin | 0548867 | 2019-10-16 10:38:02 +0100 | [diff] [blame] | 1141 | pmu->name = "i915"; |
Tvrtko Ursulin | aebf3b5 | 2020-01-10 11:32:53 +0000 | [diff] [blame] | 1142 | } |
Tvrtko Ursulin | 0548867 | 2019-10-16 10:38:02 +0100 | [diff] [blame] | 1143 | if (!pmu->name) |
Tvrtko Ursulin | b46a33e | 2017-11-21 18:18:45 +0000 | [diff] [blame] | 1144 | goto err; |
| 1145 | |
Chris Wilson | c442292 | 2019-10-25 17:54:42 +0100 | [diff] [blame] | 1146 | i915_pmu_events_attr_group.attrs = create_event_attributes(pmu); |
| 1147 | if (!i915_pmu_events_attr_group.attrs) |
| 1148 | goto err_name; |
| 1149 | |
| 1150 | pmu->base.attr_groups = i915_pmu_attr_groups; |
| 1151 | pmu->base.task_ctx_nr = perf_invalid_context; |
| 1152 | pmu->base.event_init = i915_pmu_event_init; |
| 1153 | pmu->base.add = i915_pmu_event_add; |
| 1154 | pmu->base.del = i915_pmu_event_del; |
| 1155 | pmu->base.start = i915_pmu_event_start; |
| 1156 | pmu->base.stop = i915_pmu_event_stop; |
| 1157 | pmu->base.read = i915_pmu_event_read; |
| 1158 | pmu->base.event_idx = i915_pmu_event_event_idx; |
| 1159 | |
Tvrtko Ursulin | 0548867 | 2019-10-16 10:38:02 +0100 | [diff] [blame] | 1160 | ret = perf_pmu_register(&pmu->base, pmu->name, -1); |
| 1161 | if (ret) |
Chris Wilson | c442292 | 2019-10-25 17:54:42 +0100 | [diff] [blame] | 1162 | goto err_attr; |
Tvrtko Ursulin | 0548867 | 2019-10-16 10:38:02 +0100 | [diff] [blame] | 1163 | |
Tvrtko Ursulin | 908091c | 2019-08-01 17:23:27 +0100 | [diff] [blame] | 1164 | ret = i915_pmu_register_cpuhp_state(pmu); |
Tvrtko Ursulin | b46a33e | 2017-11-21 18:18:45 +0000 | [diff] [blame] | 1165 | if (ret) |
| 1166 | goto err_unreg; |
| 1167 | |
| 1168 | return; |
| 1169 | |
| 1170 | err_unreg: |
Tvrtko Ursulin | 908091c | 2019-08-01 17:23:27 +0100 | [diff] [blame] | 1171 | perf_pmu_unregister(&pmu->base); |
Chris Wilson | c442292 | 2019-10-25 17:54:42 +0100 | [diff] [blame] | 1172 | err_attr: |
| 1173 | pmu->base.event_init = NULL; |
| 1174 | free_event_attributes(pmu); |
Tvrtko Ursulin | 0548867 | 2019-10-16 10:38:02 +0100 | [diff] [blame] | 1175 | err_name: |
| 1176 | if (!is_igp(i915)) |
| 1177 | kfree(pmu->name); |
Tvrtko Ursulin | b46a33e | 2017-11-21 18:18:45 +0000 | [diff] [blame] | 1178 | err: |
Chris Wilson | c442292 | 2019-10-25 17:54:42 +0100 | [diff] [blame] | 1179 | dev_notice(i915->drm.dev, "Failed to register PMU!\n"); |
Tvrtko Ursulin | b46a33e | 2017-11-21 18:18:45 +0000 | [diff] [blame] | 1180 | } |
| 1181 | |
| 1182 | void i915_pmu_unregister(struct drm_i915_private *i915) |
| 1183 | { |
Tvrtko Ursulin | 908091c | 2019-08-01 17:23:27 +0100 | [diff] [blame] | 1184 | struct i915_pmu *pmu = &i915->pmu; |
| 1185 | |
| 1186 | if (!pmu->base.event_init) |
Tvrtko Ursulin | b46a33e | 2017-11-21 18:18:45 +0000 | [diff] [blame] | 1187 | return; |
| 1188 | |
Pankaj Bharadiya | 48a1b8d | 2020-01-15 09:14:53 +0530 | [diff] [blame] | 1189 | drm_WARN_ON(&i915->drm, pmu->enable); |
Tvrtko Ursulin | b46a33e | 2017-11-21 18:18:45 +0000 | [diff] [blame] | 1190 | |
Tvrtko Ursulin | 908091c | 2019-08-01 17:23:27 +0100 | [diff] [blame] | 1191 | hrtimer_cancel(&pmu->timer); |
Tvrtko Ursulin | b46a33e | 2017-11-21 18:18:45 +0000 | [diff] [blame] | 1192 | |
Tvrtko Ursulin | 908091c | 2019-08-01 17:23:27 +0100 | [diff] [blame] | 1193 | i915_pmu_unregister_cpuhp_state(pmu); |
Tvrtko Ursulin | b46a33e | 2017-11-21 18:18:45 +0000 | [diff] [blame] | 1194 | |
Tvrtko Ursulin | 908091c | 2019-08-01 17:23:27 +0100 | [diff] [blame] | 1195 | perf_pmu_unregister(&pmu->base); |
| 1196 | pmu->base.event_init = NULL; |
Tvrtko Ursulin | 0548867 | 2019-10-16 10:38:02 +0100 | [diff] [blame] | 1197 | if (!is_igp(i915)) |
| 1198 | kfree(pmu->name); |
Tvrtko Ursulin | 908091c | 2019-08-01 17:23:27 +0100 | [diff] [blame] | 1199 | free_event_attributes(pmu); |
Tvrtko Ursulin | b46a33e | 2017-11-21 18:18:45 +0000 | [diff] [blame] | 1200 | } |