Tvrtko Ursulin | b46a33e | 2017-11-21 18:18:45 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 2017 Intel Corporation |
| 3 | * |
| 4 | * Permission is hereby granted, free of charge, to any person obtaining a |
| 5 | * copy of this software and associated documentation files (the "Software"), |
| 6 | * to deal in the Software without restriction, including without limitation |
| 7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 8 | * and/or sell copies of the Software, and to permit persons to whom the |
| 9 | * Software is furnished to do so, subject to the following conditions: |
| 10 | * |
| 11 | * The above copyright notice and this permission notice (including the next |
| 12 | * paragraph) shall be included in all copies or substantial portions of the |
| 13 | * Software. |
| 14 | * |
| 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 18 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 20 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
| 21 | * IN THE SOFTWARE. |
| 22 | * |
| 23 | */ |
| 24 | |
| 25 | #include <linux/perf_event.h> |
| 26 | #include <linux/pm_runtime.h> |
| 27 | |
| 28 | #include "i915_drv.h" |
| 29 | #include "i915_pmu.h" |
| 30 | #include "intel_ringbuffer.h" |
| 31 | |
| 32 | /* Frequency for the sampling timer for events which need it. */ |
| 33 | #define FREQUENCY 200 |
| 34 | #define PERIOD max_t(u64, 10000, NSEC_PER_SEC / FREQUENCY) |
| 35 | |
| 36 | #define ENGINE_SAMPLE_MASK \ |
| 37 | (BIT(I915_SAMPLE_BUSY) | \ |
| 38 | BIT(I915_SAMPLE_WAIT) | \ |
| 39 | BIT(I915_SAMPLE_SEMA)) |
| 40 | |
| 41 | #define ENGINE_SAMPLE_BITS (1 << I915_PMU_SAMPLE_BITS) |
| 42 | |
| 43 | static cpumask_t i915_pmu_cpumask = CPU_MASK_NONE; |
| 44 | |
| 45 | static u8 engine_config_sample(u64 config) |
| 46 | { |
| 47 | return config & I915_PMU_SAMPLE_MASK; |
| 48 | } |
| 49 | |
| 50 | static u8 engine_event_sample(struct perf_event *event) |
| 51 | { |
| 52 | return engine_config_sample(event->attr.config); |
| 53 | } |
| 54 | |
| 55 | static u8 engine_event_class(struct perf_event *event) |
| 56 | { |
| 57 | return (event->attr.config >> I915_PMU_CLASS_SHIFT) & 0xff; |
| 58 | } |
| 59 | |
| 60 | static u8 engine_event_instance(struct perf_event *event) |
| 61 | { |
| 62 | return (event->attr.config >> I915_PMU_SAMPLE_BITS) & 0xff; |
| 63 | } |
| 64 | |
| 65 | static bool is_engine_config(u64 config) |
| 66 | { |
| 67 | return config < __I915_PMU_OTHER(0); |
| 68 | } |
| 69 | |
| 70 | static unsigned int config_enabled_bit(u64 config) |
| 71 | { |
| 72 | if (is_engine_config(config)) |
| 73 | return engine_config_sample(config); |
| 74 | else |
| 75 | return ENGINE_SAMPLE_BITS + (config - __I915_PMU_OTHER(0)); |
| 76 | } |
| 77 | |
| 78 | static u64 config_enabled_mask(u64 config) |
| 79 | { |
| 80 | return BIT_ULL(config_enabled_bit(config)); |
| 81 | } |
| 82 | |
| 83 | static bool is_engine_event(struct perf_event *event) |
| 84 | { |
| 85 | return is_engine_config(event->attr.config); |
| 86 | } |
| 87 | |
| 88 | static unsigned int event_enabled_bit(struct perf_event *event) |
| 89 | { |
| 90 | return config_enabled_bit(event->attr.config); |
| 91 | } |
| 92 | |
Tvrtko Ursulin | b3add01 | 2017-11-21 18:18:49 +0000 | [diff] [blame] | 93 | static bool supports_busy_stats(struct drm_i915_private *i915) |
| 94 | { |
| 95 | return INTEL_GEN(i915) >= 8; |
| 96 | } |
| 97 | |
Tvrtko Ursulin | feff0dc | 2017-11-21 18:18:46 +0000 | [diff] [blame] | 98 | static bool pmu_needs_timer(struct drm_i915_private *i915, bool gpu_active) |
| 99 | { |
| 100 | u64 enable; |
| 101 | |
| 102 | /* |
| 103 | * Only some counters need the sampling timer. |
| 104 | * |
| 105 | * We start with a bitmask of all currently enabled events. |
| 106 | */ |
| 107 | enable = i915->pmu.enable; |
| 108 | |
| 109 | /* |
| 110 | * Mask out all the ones which do not need the timer, or in |
| 111 | * other words keep all the ones that could need the timer. |
| 112 | */ |
| 113 | enable &= config_enabled_mask(I915_PMU_ACTUAL_FREQUENCY) | |
| 114 | config_enabled_mask(I915_PMU_REQUESTED_FREQUENCY) | |
| 115 | ENGINE_SAMPLE_MASK; |
| 116 | |
| 117 | /* |
| 118 | * When the GPU is idle per-engine counters do not need to be |
| 119 | * running so clear those bits out. |
| 120 | */ |
| 121 | if (!gpu_active) |
| 122 | enable &= ~ENGINE_SAMPLE_MASK; |
Tvrtko Ursulin | b3add01 | 2017-11-21 18:18:49 +0000 | [diff] [blame] | 123 | /* |
| 124 | * Also there is software busyness tracking available we do not |
| 125 | * need the timer for I915_SAMPLE_BUSY counter. |
| 126 | */ |
| 127 | else if (supports_busy_stats(i915)) |
| 128 | enable &= ~BIT(I915_SAMPLE_BUSY); |
Tvrtko Ursulin | feff0dc | 2017-11-21 18:18:46 +0000 | [diff] [blame] | 129 | |
| 130 | /* |
| 131 | * If some bits remain it means we need the sampling timer running. |
| 132 | */ |
| 133 | return enable; |
| 134 | } |
| 135 | |
| 136 | void i915_pmu_gt_parked(struct drm_i915_private *i915) |
| 137 | { |
| 138 | if (!i915->pmu.base.event_init) |
| 139 | return; |
| 140 | |
| 141 | spin_lock_irq(&i915->pmu.lock); |
| 142 | /* |
| 143 | * Signal sampling timer to stop if only engine events are enabled and |
| 144 | * GPU went idle. |
| 145 | */ |
| 146 | i915->pmu.timer_enabled = pmu_needs_timer(i915, false); |
| 147 | spin_unlock_irq(&i915->pmu.lock); |
| 148 | } |
| 149 | |
| 150 | static void __i915_pmu_maybe_start_timer(struct drm_i915_private *i915) |
| 151 | { |
| 152 | if (!i915->pmu.timer_enabled && pmu_needs_timer(i915, true)) { |
| 153 | i915->pmu.timer_enabled = true; |
| 154 | hrtimer_start_range_ns(&i915->pmu.timer, |
| 155 | ns_to_ktime(PERIOD), 0, |
| 156 | HRTIMER_MODE_REL_PINNED); |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | void i915_pmu_gt_unparked(struct drm_i915_private *i915) |
| 161 | { |
| 162 | if (!i915->pmu.base.event_init) |
| 163 | return; |
| 164 | |
| 165 | spin_lock_irq(&i915->pmu.lock); |
| 166 | /* |
| 167 | * Re-enable sampling timer when GPU goes active. |
| 168 | */ |
| 169 | __i915_pmu_maybe_start_timer(i915); |
| 170 | spin_unlock_irq(&i915->pmu.lock); |
| 171 | } |
| 172 | |
Tvrtko Ursulin | b46a33e | 2017-11-21 18:18:45 +0000 | [diff] [blame] | 173 | static bool grab_forcewake(struct drm_i915_private *i915, bool fw) |
| 174 | { |
| 175 | if (!fw) |
| 176 | intel_uncore_forcewake_get(i915, FORCEWAKE_ALL); |
| 177 | |
| 178 | return true; |
| 179 | } |
| 180 | |
| 181 | static void |
| 182 | update_sample(struct i915_pmu_sample *sample, u32 unit, u32 val) |
| 183 | { |
| 184 | /* |
| 185 | * Since we are doing stochastic sampling for these counters, |
| 186 | * average the delta with the previous value for better accuracy. |
| 187 | */ |
| 188 | sample->cur += div_u64(mul_u32_u32(sample->prev + val, unit), 2); |
| 189 | sample->prev = val; |
| 190 | } |
| 191 | |
| 192 | static void engines_sample(struct drm_i915_private *dev_priv) |
| 193 | { |
| 194 | struct intel_engine_cs *engine; |
| 195 | enum intel_engine_id id; |
| 196 | bool fw = false; |
| 197 | |
| 198 | if ((dev_priv->pmu.enable & ENGINE_SAMPLE_MASK) == 0) |
| 199 | return; |
| 200 | |
| 201 | if (!dev_priv->gt.awake) |
| 202 | return; |
| 203 | |
| 204 | if (!intel_runtime_pm_get_if_in_use(dev_priv)) |
| 205 | return; |
| 206 | |
| 207 | for_each_engine(engine, dev_priv, id) { |
| 208 | u32 current_seqno = intel_engine_get_seqno(engine); |
| 209 | u32 last_seqno = intel_engine_last_submit(engine); |
| 210 | u32 val; |
| 211 | |
| 212 | val = !i915_seqno_passed(current_seqno, last_seqno); |
| 213 | |
| 214 | update_sample(&engine->pmu.sample[I915_SAMPLE_BUSY], |
| 215 | PERIOD, val); |
| 216 | |
| 217 | if (val && (engine->pmu.enable & |
| 218 | (BIT(I915_SAMPLE_WAIT) | BIT(I915_SAMPLE_SEMA)))) { |
| 219 | fw = grab_forcewake(dev_priv, fw); |
| 220 | |
| 221 | val = I915_READ_FW(RING_CTL(engine->mmio_base)); |
| 222 | } else { |
| 223 | val = 0; |
| 224 | } |
| 225 | |
| 226 | update_sample(&engine->pmu.sample[I915_SAMPLE_WAIT], |
| 227 | PERIOD, !!(val & RING_WAIT)); |
| 228 | |
| 229 | update_sample(&engine->pmu.sample[I915_SAMPLE_SEMA], |
| 230 | PERIOD, !!(val & RING_WAIT_SEMAPHORE)); |
| 231 | } |
| 232 | |
| 233 | if (fw) |
| 234 | intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL); |
| 235 | |
| 236 | intel_runtime_pm_put(dev_priv); |
| 237 | } |
| 238 | |
| 239 | static void frequency_sample(struct drm_i915_private *dev_priv) |
| 240 | { |
| 241 | if (dev_priv->pmu.enable & |
| 242 | config_enabled_mask(I915_PMU_ACTUAL_FREQUENCY)) { |
| 243 | u32 val; |
| 244 | |
| 245 | val = dev_priv->gt_pm.rps.cur_freq; |
| 246 | if (dev_priv->gt.awake && |
| 247 | intel_runtime_pm_get_if_in_use(dev_priv)) { |
| 248 | val = intel_get_cagf(dev_priv, |
| 249 | I915_READ_NOTRACE(GEN6_RPSTAT1)); |
| 250 | intel_runtime_pm_put(dev_priv); |
| 251 | } |
| 252 | |
| 253 | update_sample(&dev_priv->pmu.sample[__I915_SAMPLE_FREQ_ACT], |
| 254 | 1, intel_gpu_freq(dev_priv, val)); |
| 255 | } |
| 256 | |
| 257 | if (dev_priv->pmu.enable & |
| 258 | config_enabled_mask(I915_PMU_REQUESTED_FREQUENCY)) { |
| 259 | update_sample(&dev_priv->pmu.sample[__I915_SAMPLE_FREQ_REQ], 1, |
| 260 | intel_gpu_freq(dev_priv, |
| 261 | dev_priv->gt_pm.rps.cur_freq)); |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | static enum hrtimer_restart i915_sample(struct hrtimer *hrtimer) |
| 266 | { |
| 267 | struct drm_i915_private *i915 = |
| 268 | container_of(hrtimer, struct drm_i915_private, pmu.timer); |
| 269 | |
Tvrtko Ursulin | feff0dc | 2017-11-21 18:18:46 +0000 | [diff] [blame] | 270 | if (!READ_ONCE(i915->pmu.timer_enabled)) |
Tvrtko Ursulin | b46a33e | 2017-11-21 18:18:45 +0000 | [diff] [blame] | 271 | return HRTIMER_NORESTART; |
| 272 | |
| 273 | engines_sample(i915); |
| 274 | frequency_sample(i915); |
| 275 | |
| 276 | hrtimer_forward_now(hrtimer, ns_to_ktime(PERIOD)); |
| 277 | return HRTIMER_RESTART; |
| 278 | } |
| 279 | |
Tvrtko Ursulin | 0cd4684 | 2017-11-21 18:18:50 +0000 | [diff] [blame^] | 280 | static u64 count_interrupts(struct drm_i915_private *i915) |
| 281 | { |
| 282 | /* open-coded kstat_irqs() */ |
| 283 | struct irq_desc *desc = irq_to_desc(i915->drm.pdev->irq); |
| 284 | u64 sum = 0; |
| 285 | int cpu; |
| 286 | |
| 287 | if (!desc || !desc->kstat_irqs) |
| 288 | return 0; |
| 289 | |
| 290 | for_each_possible_cpu(cpu) |
| 291 | sum += *per_cpu_ptr(desc->kstat_irqs, cpu); |
| 292 | |
| 293 | return sum; |
| 294 | } |
| 295 | |
Tvrtko Ursulin | b46a33e | 2017-11-21 18:18:45 +0000 | [diff] [blame] | 296 | static void i915_pmu_event_destroy(struct perf_event *event) |
| 297 | { |
| 298 | WARN_ON(event->parent); |
| 299 | } |
| 300 | |
| 301 | static int engine_event_init(struct perf_event *event) |
| 302 | { |
| 303 | struct drm_i915_private *i915 = |
| 304 | container_of(event->pmu, typeof(*i915), pmu.base); |
| 305 | |
| 306 | if (!intel_engine_lookup_user(i915, engine_event_class(event), |
| 307 | engine_event_instance(event))) |
| 308 | return -ENODEV; |
| 309 | |
| 310 | switch (engine_event_sample(event)) { |
| 311 | case I915_SAMPLE_BUSY: |
| 312 | case I915_SAMPLE_WAIT: |
| 313 | break; |
| 314 | case I915_SAMPLE_SEMA: |
| 315 | if (INTEL_GEN(i915) < 6) |
| 316 | return -ENODEV; |
| 317 | break; |
| 318 | default: |
| 319 | return -ENOENT; |
| 320 | } |
| 321 | |
| 322 | return 0; |
| 323 | } |
| 324 | |
| 325 | static int i915_pmu_event_init(struct perf_event *event) |
| 326 | { |
| 327 | struct drm_i915_private *i915 = |
| 328 | container_of(event->pmu, typeof(*i915), pmu.base); |
| 329 | int cpu, ret; |
| 330 | |
| 331 | if (event->attr.type != event->pmu->type) |
| 332 | return -ENOENT; |
| 333 | |
| 334 | /* unsupported modes and filters */ |
| 335 | if (event->attr.sample_period) /* no sampling */ |
| 336 | return -EINVAL; |
| 337 | |
| 338 | if (has_branch_stack(event)) |
| 339 | return -EOPNOTSUPP; |
| 340 | |
| 341 | if (event->cpu < 0) |
| 342 | return -EINVAL; |
| 343 | |
| 344 | cpu = cpumask_any_and(&i915_pmu_cpumask, |
| 345 | topology_sibling_cpumask(event->cpu)); |
| 346 | if (cpu >= nr_cpu_ids) |
| 347 | return -ENODEV; |
| 348 | |
| 349 | if (is_engine_event(event)) { |
| 350 | ret = engine_event_init(event); |
| 351 | } else { |
| 352 | ret = 0; |
| 353 | switch (event->attr.config) { |
| 354 | case I915_PMU_ACTUAL_FREQUENCY: |
| 355 | if (IS_VALLEYVIEW(i915) || IS_CHERRYVIEW(i915)) |
| 356 | /* Requires a mutex for sampling! */ |
| 357 | ret = -ENODEV; |
| 358 | case I915_PMU_REQUESTED_FREQUENCY: |
| 359 | if (INTEL_GEN(i915) < 6) |
| 360 | ret = -ENODEV; |
| 361 | break; |
Tvrtko Ursulin | 0cd4684 | 2017-11-21 18:18:50 +0000 | [diff] [blame^] | 362 | case I915_PMU_INTERRUPTS: |
| 363 | break; |
Tvrtko Ursulin | b46a33e | 2017-11-21 18:18:45 +0000 | [diff] [blame] | 364 | default: |
| 365 | ret = -ENOENT; |
| 366 | break; |
| 367 | } |
| 368 | } |
| 369 | if (ret) |
| 370 | return ret; |
| 371 | |
| 372 | event->cpu = cpu; |
| 373 | if (!event->parent) |
| 374 | event->destroy = i915_pmu_event_destroy; |
| 375 | |
| 376 | return 0; |
| 377 | } |
| 378 | |
| 379 | static u64 __i915_pmu_event_read(struct perf_event *event) |
| 380 | { |
| 381 | struct drm_i915_private *i915 = |
| 382 | container_of(event->pmu, typeof(*i915), pmu.base); |
| 383 | u64 val = 0; |
| 384 | |
| 385 | if (is_engine_event(event)) { |
| 386 | u8 sample = engine_event_sample(event); |
| 387 | struct intel_engine_cs *engine; |
| 388 | |
| 389 | engine = intel_engine_lookup_user(i915, |
| 390 | engine_event_class(event), |
| 391 | engine_event_instance(event)); |
| 392 | |
| 393 | if (WARN_ON_ONCE(!engine)) { |
| 394 | /* Do nothing */ |
Tvrtko Ursulin | b3add01 | 2017-11-21 18:18:49 +0000 | [diff] [blame] | 395 | } else if (sample == I915_SAMPLE_BUSY && |
| 396 | engine->pmu.busy_stats) { |
| 397 | val = ktime_to_ns(intel_engine_get_busy_time(engine)); |
Tvrtko Ursulin | b46a33e | 2017-11-21 18:18:45 +0000 | [diff] [blame] | 398 | } else { |
| 399 | val = engine->pmu.sample[sample].cur; |
| 400 | } |
| 401 | } else { |
| 402 | switch (event->attr.config) { |
| 403 | case I915_PMU_ACTUAL_FREQUENCY: |
| 404 | val = |
| 405 | div_u64(i915->pmu.sample[__I915_SAMPLE_FREQ_ACT].cur, |
| 406 | FREQUENCY); |
| 407 | break; |
| 408 | case I915_PMU_REQUESTED_FREQUENCY: |
| 409 | val = |
| 410 | div_u64(i915->pmu.sample[__I915_SAMPLE_FREQ_REQ].cur, |
| 411 | FREQUENCY); |
| 412 | break; |
Tvrtko Ursulin | 0cd4684 | 2017-11-21 18:18:50 +0000 | [diff] [blame^] | 413 | case I915_PMU_INTERRUPTS: |
| 414 | val = count_interrupts(i915); |
| 415 | break; |
Tvrtko Ursulin | b46a33e | 2017-11-21 18:18:45 +0000 | [diff] [blame] | 416 | } |
| 417 | } |
| 418 | |
| 419 | return val; |
| 420 | } |
| 421 | |
| 422 | static void i915_pmu_event_read(struct perf_event *event) |
| 423 | { |
| 424 | struct hw_perf_event *hwc = &event->hw; |
| 425 | u64 prev, new; |
| 426 | |
| 427 | again: |
| 428 | prev = local64_read(&hwc->prev_count); |
| 429 | new = __i915_pmu_event_read(event); |
| 430 | |
| 431 | if (local64_cmpxchg(&hwc->prev_count, prev, new) != prev) |
| 432 | goto again; |
| 433 | |
| 434 | local64_add(new - prev, &event->count); |
| 435 | } |
| 436 | |
Tvrtko Ursulin | b3add01 | 2017-11-21 18:18:49 +0000 | [diff] [blame] | 437 | static bool engine_needs_busy_stats(struct intel_engine_cs *engine) |
| 438 | { |
| 439 | return supports_busy_stats(engine->i915) && |
| 440 | (engine->pmu.enable & BIT(I915_SAMPLE_BUSY)); |
| 441 | } |
| 442 | |
Tvrtko Ursulin | b46a33e | 2017-11-21 18:18:45 +0000 | [diff] [blame] | 443 | static void i915_pmu_enable(struct perf_event *event) |
| 444 | { |
| 445 | struct drm_i915_private *i915 = |
| 446 | container_of(event->pmu, typeof(*i915), pmu.base); |
| 447 | unsigned int bit = event_enabled_bit(event); |
| 448 | unsigned long flags; |
| 449 | |
| 450 | spin_lock_irqsave(&i915->pmu.lock, flags); |
| 451 | |
| 452 | /* |
Tvrtko Ursulin | b46a33e | 2017-11-21 18:18:45 +0000 | [diff] [blame] | 453 | * Update the bitmask of enabled events and increment |
| 454 | * the event reference counter. |
| 455 | */ |
| 456 | GEM_BUG_ON(bit >= I915_PMU_MASK_BITS); |
| 457 | GEM_BUG_ON(i915->pmu.enable_count[bit] == ~0); |
| 458 | i915->pmu.enable |= BIT_ULL(bit); |
| 459 | i915->pmu.enable_count[bit]++; |
| 460 | |
| 461 | /* |
Tvrtko Ursulin | feff0dc | 2017-11-21 18:18:46 +0000 | [diff] [blame] | 462 | * Start the sampling timer if needed and not already enabled. |
| 463 | */ |
| 464 | __i915_pmu_maybe_start_timer(i915); |
| 465 | |
| 466 | /* |
Tvrtko Ursulin | b46a33e | 2017-11-21 18:18:45 +0000 | [diff] [blame] | 467 | * For per-engine events the bitmask and reference counting |
| 468 | * is stored per engine. |
| 469 | */ |
| 470 | if (is_engine_event(event)) { |
| 471 | u8 sample = engine_event_sample(event); |
| 472 | struct intel_engine_cs *engine; |
| 473 | |
| 474 | engine = intel_engine_lookup_user(i915, |
| 475 | engine_event_class(event), |
| 476 | engine_event_instance(event)); |
| 477 | GEM_BUG_ON(!engine); |
| 478 | engine->pmu.enable |= BIT(sample); |
| 479 | |
| 480 | GEM_BUG_ON(sample >= I915_PMU_SAMPLE_BITS); |
| 481 | GEM_BUG_ON(engine->pmu.enable_count[sample] == ~0); |
Tvrtko Ursulin | b3add01 | 2017-11-21 18:18:49 +0000 | [diff] [blame] | 482 | if (engine->pmu.enable_count[sample]++ == 0) { |
| 483 | /* |
| 484 | * Enable engine busy stats tracking if needed or |
| 485 | * alternatively cancel the scheduled disable. |
| 486 | * |
| 487 | * If the delayed disable was pending, cancel it and |
| 488 | * in this case do not enable since it already is. |
| 489 | */ |
| 490 | if (engine_needs_busy_stats(engine) && |
| 491 | !engine->pmu.busy_stats) { |
| 492 | engine->pmu.busy_stats = true; |
| 493 | if (!cancel_delayed_work(&engine->pmu.disable_busy_stats)) |
| 494 | intel_enable_engine_stats(engine); |
| 495 | } |
| 496 | } |
Tvrtko Ursulin | b46a33e | 2017-11-21 18:18:45 +0000 | [diff] [blame] | 497 | } |
| 498 | |
| 499 | /* |
| 500 | * Store the current counter value so we can report the correct delta |
| 501 | * for all listeners. Even when the event was already enabled and has |
| 502 | * an existing non-zero value. |
| 503 | */ |
| 504 | local64_set(&event->hw.prev_count, __i915_pmu_event_read(event)); |
| 505 | |
| 506 | spin_unlock_irqrestore(&i915->pmu.lock, flags); |
| 507 | } |
| 508 | |
Tvrtko Ursulin | b3add01 | 2017-11-21 18:18:49 +0000 | [diff] [blame] | 509 | static void __disable_busy_stats(struct work_struct *work) |
| 510 | { |
| 511 | struct intel_engine_cs *engine = |
| 512 | container_of(work, typeof(*engine), pmu.disable_busy_stats.work); |
| 513 | |
| 514 | intel_disable_engine_stats(engine); |
| 515 | } |
| 516 | |
Tvrtko Ursulin | b46a33e | 2017-11-21 18:18:45 +0000 | [diff] [blame] | 517 | static void i915_pmu_disable(struct perf_event *event) |
| 518 | { |
| 519 | struct drm_i915_private *i915 = |
| 520 | container_of(event->pmu, typeof(*i915), pmu.base); |
| 521 | unsigned int bit = event_enabled_bit(event); |
| 522 | unsigned long flags; |
| 523 | |
| 524 | spin_lock_irqsave(&i915->pmu.lock, flags); |
| 525 | |
| 526 | if (is_engine_event(event)) { |
| 527 | u8 sample = engine_event_sample(event); |
| 528 | struct intel_engine_cs *engine; |
| 529 | |
| 530 | engine = intel_engine_lookup_user(i915, |
| 531 | engine_event_class(event), |
| 532 | engine_event_instance(event)); |
| 533 | GEM_BUG_ON(!engine); |
| 534 | GEM_BUG_ON(sample >= I915_PMU_SAMPLE_BITS); |
| 535 | GEM_BUG_ON(engine->pmu.enable_count[sample] == 0); |
| 536 | /* |
| 537 | * Decrement the reference count and clear the enabled |
| 538 | * bitmask when the last listener on an event goes away. |
| 539 | */ |
Tvrtko Ursulin | b3add01 | 2017-11-21 18:18:49 +0000 | [diff] [blame] | 540 | if (--engine->pmu.enable_count[sample] == 0) { |
Tvrtko Ursulin | b46a33e | 2017-11-21 18:18:45 +0000 | [diff] [blame] | 541 | engine->pmu.enable &= ~BIT(sample); |
Tvrtko Ursulin | b3add01 | 2017-11-21 18:18:49 +0000 | [diff] [blame] | 542 | if (!engine_needs_busy_stats(engine) && |
| 543 | engine->pmu.busy_stats) { |
| 544 | engine->pmu.busy_stats = false; |
| 545 | /* |
| 546 | * We request a delayed disable to handle the |
| 547 | * rapid on/off cycles on events, which can |
| 548 | * happen when tools like perf stat start, in a |
| 549 | * nicer way. |
| 550 | * |
| 551 | * In addition, this also helps with busy stats |
| 552 | * accuracy with background CPU offline/online |
| 553 | * migration events. |
| 554 | */ |
| 555 | queue_delayed_work(system_wq, |
| 556 | &engine->pmu.disable_busy_stats, |
| 557 | round_jiffies_up_relative(HZ)); |
| 558 | } |
| 559 | } |
Tvrtko Ursulin | b46a33e | 2017-11-21 18:18:45 +0000 | [diff] [blame] | 560 | } |
| 561 | |
| 562 | GEM_BUG_ON(bit >= I915_PMU_MASK_BITS); |
| 563 | GEM_BUG_ON(i915->pmu.enable_count[bit] == 0); |
| 564 | /* |
| 565 | * Decrement the reference count and clear the enabled |
| 566 | * bitmask when the last listener on an event goes away. |
| 567 | */ |
Tvrtko Ursulin | feff0dc | 2017-11-21 18:18:46 +0000 | [diff] [blame] | 568 | if (--i915->pmu.enable_count[bit] == 0) { |
Tvrtko Ursulin | b46a33e | 2017-11-21 18:18:45 +0000 | [diff] [blame] | 569 | i915->pmu.enable &= ~BIT_ULL(bit); |
Tvrtko Ursulin | feff0dc | 2017-11-21 18:18:46 +0000 | [diff] [blame] | 570 | i915->pmu.timer_enabled &= pmu_needs_timer(i915, true); |
| 571 | } |
Tvrtko Ursulin | b46a33e | 2017-11-21 18:18:45 +0000 | [diff] [blame] | 572 | |
| 573 | spin_unlock_irqrestore(&i915->pmu.lock, flags); |
| 574 | } |
| 575 | |
| 576 | static void i915_pmu_event_start(struct perf_event *event, int flags) |
| 577 | { |
| 578 | i915_pmu_enable(event); |
| 579 | event->hw.state = 0; |
| 580 | } |
| 581 | |
| 582 | static void i915_pmu_event_stop(struct perf_event *event, int flags) |
| 583 | { |
| 584 | if (flags & PERF_EF_UPDATE) |
| 585 | i915_pmu_event_read(event); |
| 586 | i915_pmu_disable(event); |
| 587 | event->hw.state = PERF_HES_STOPPED; |
| 588 | } |
| 589 | |
| 590 | static int i915_pmu_event_add(struct perf_event *event, int flags) |
| 591 | { |
| 592 | if (flags & PERF_EF_START) |
| 593 | i915_pmu_event_start(event, flags); |
| 594 | |
| 595 | return 0; |
| 596 | } |
| 597 | |
| 598 | static void i915_pmu_event_del(struct perf_event *event, int flags) |
| 599 | { |
| 600 | i915_pmu_event_stop(event, PERF_EF_UPDATE); |
| 601 | } |
| 602 | |
| 603 | static int i915_pmu_event_event_idx(struct perf_event *event) |
| 604 | { |
| 605 | return 0; |
| 606 | } |
| 607 | |
| 608 | static ssize_t i915_pmu_format_show(struct device *dev, |
| 609 | struct device_attribute *attr, char *buf) |
| 610 | { |
| 611 | struct dev_ext_attribute *eattr; |
| 612 | |
| 613 | eattr = container_of(attr, struct dev_ext_attribute, attr); |
| 614 | return sprintf(buf, "%s\n", (char *)eattr->var); |
| 615 | } |
| 616 | |
| 617 | #define I915_PMU_FORMAT_ATTR(_name, _config) \ |
| 618 | (&((struct dev_ext_attribute[]) { \ |
| 619 | { .attr = __ATTR(_name, 0444, i915_pmu_format_show, NULL), \ |
| 620 | .var = (void *)_config, } \ |
| 621 | })[0].attr.attr) |
| 622 | |
| 623 | static struct attribute *i915_pmu_format_attrs[] = { |
| 624 | I915_PMU_FORMAT_ATTR(i915_eventid, "config:0-20"), |
| 625 | NULL, |
| 626 | }; |
| 627 | |
| 628 | static const struct attribute_group i915_pmu_format_attr_group = { |
| 629 | .name = "format", |
| 630 | .attrs = i915_pmu_format_attrs, |
| 631 | }; |
| 632 | |
| 633 | static ssize_t i915_pmu_event_show(struct device *dev, |
| 634 | struct device_attribute *attr, char *buf) |
| 635 | { |
| 636 | struct dev_ext_attribute *eattr; |
| 637 | |
| 638 | eattr = container_of(attr, struct dev_ext_attribute, attr); |
| 639 | return sprintf(buf, "config=0x%lx\n", (unsigned long)eattr->var); |
| 640 | } |
| 641 | |
| 642 | #define I915_EVENT_ATTR(_name, _config) \ |
| 643 | (&((struct dev_ext_attribute[]) { \ |
| 644 | { .attr = __ATTR(_name, 0444, i915_pmu_event_show, NULL), \ |
| 645 | .var = (void *)_config, } \ |
| 646 | })[0].attr.attr) |
| 647 | |
| 648 | #define I915_EVENT_STR(_name, _str) \ |
| 649 | (&((struct perf_pmu_events_attr[]) { \ |
| 650 | { .attr = __ATTR(_name, 0444, perf_event_sysfs_show, NULL), \ |
| 651 | .id = 0, \ |
| 652 | .event_str = _str, } \ |
| 653 | })[0].attr.attr) |
| 654 | |
| 655 | #define I915_EVENT(_name, _config, _unit) \ |
| 656 | I915_EVENT_ATTR(_name, _config), \ |
| 657 | I915_EVENT_STR(_name.unit, _unit) |
| 658 | |
| 659 | #define I915_ENGINE_EVENT(_name, _class, _instance, _sample) \ |
| 660 | I915_EVENT_ATTR(_name, __I915_PMU_ENGINE(_class, _instance, _sample)), \ |
| 661 | I915_EVENT_STR(_name.unit, "ns") |
| 662 | |
| 663 | #define I915_ENGINE_EVENTS(_name, _class, _instance) \ |
| 664 | I915_ENGINE_EVENT(_name##_instance-busy, _class, _instance, I915_SAMPLE_BUSY), \ |
| 665 | I915_ENGINE_EVENT(_name##_instance-sema, _class, _instance, I915_SAMPLE_SEMA), \ |
| 666 | I915_ENGINE_EVENT(_name##_instance-wait, _class, _instance, I915_SAMPLE_WAIT) |
| 667 | |
| 668 | static struct attribute *i915_pmu_events_attrs[] = { |
| 669 | I915_ENGINE_EVENTS(rcs, I915_ENGINE_CLASS_RENDER, 0), |
| 670 | I915_ENGINE_EVENTS(bcs, I915_ENGINE_CLASS_COPY, 0), |
| 671 | I915_ENGINE_EVENTS(vcs, I915_ENGINE_CLASS_VIDEO, 0), |
| 672 | I915_ENGINE_EVENTS(vcs, I915_ENGINE_CLASS_VIDEO, 1), |
| 673 | I915_ENGINE_EVENTS(vecs, I915_ENGINE_CLASS_VIDEO_ENHANCE, 0), |
| 674 | |
| 675 | I915_EVENT(actual-frequency, I915_PMU_ACTUAL_FREQUENCY, "MHz"), |
| 676 | I915_EVENT(requested-frequency, I915_PMU_REQUESTED_FREQUENCY, "MHz"), |
| 677 | |
Tvrtko Ursulin | 0cd4684 | 2017-11-21 18:18:50 +0000 | [diff] [blame^] | 678 | I915_EVENT_ATTR(interrupts, I915_PMU_INTERRUPTS), |
| 679 | |
Tvrtko Ursulin | b46a33e | 2017-11-21 18:18:45 +0000 | [diff] [blame] | 680 | NULL, |
| 681 | }; |
| 682 | |
| 683 | static const struct attribute_group i915_pmu_events_attr_group = { |
| 684 | .name = "events", |
| 685 | .attrs = i915_pmu_events_attrs, |
| 686 | }; |
| 687 | |
| 688 | static ssize_t |
| 689 | i915_pmu_get_attr_cpumask(struct device *dev, |
| 690 | struct device_attribute *attr, |
| 691 | char *buf) |
| 692 | { |
| 693 | return cpumap_print_to_pagebuf(true, buf, &i915_pmu_cpumask); |
| 694 | } |
| 695 | |
| 696 | static DEVICE_ATTR(cpumask, 0444, i915_pmu_get_attr_cpumask, NULL); |
| 697 | |
| 698 | static struct attribute *i915_cpumask_attrs[] = { |
| 699 | &dev_attr_cpumask.attr, |
| 700 | NULL, |
| 701 | }; |
| 702 | |
| 703 | static struct attribute_group i915_pmu_cpumask_attr_group = { |
| 704 | .attrs = i915_cpumask_attrs, |
| 705 | }; |
| 706 | |
| 707 | static const struct attribute_group *i915_pmu_attr_groups[] = { |
| 708 | &i915_pmu_format_attr_group, |
| 709 | &i915_pmu_events_attr_group, |
| 710 | &i915_pmu_cpumask_attr_group, |
| 711 | NULL |
| 712 | }; |
| 713 | |
| 714 | #ifdef CONFIG_HOTPLUG_CPU |
| 715 | static int i915_pmu_cpu_online(unsigned int cpu, struct hlist_node *node) |
| 716 | { |
| 717 | struct i915_pmu *pmu = hlist_entry_safe(node, typeof(*pmu), node); |
| 718 | unsigned int target; |
| 719 | |
| 720 | GEM_BUG_ON(!pmu->base.event_init); |
| 721 | |
| 722 | target = cpumask_any_and(&i915_pmu_cpumask, &i915_pmu_cpumask); |
| 723 | /* Select the first online CPU as a designated reader. */ |
| 724 | if (target >= nr_cpu_ids) |
| 725 | cpumask_set_cpu(cpu, &i915_pmu_cpumask); |
| 726 | |
| 727 | return 0; |
| 728 | } |
| 729 | |
| 730 | static int i915_pmu_cpu_offline(unsigned int cpu, struct hlist_node *node) |
| 731 | { |
| 732 | struct i915_pmu *pmu = hlist_entry_safe(node, typeof(*pmu), node); |
| 733 | unsigned int target; |
| 734 | |
| 735 | GEM_BUG_ON(!pmu->base.event_init); |
| 736 | |
| 737 | if (cpumask_test_and_clear_cpu(cpu, &i915_pmu_cpumask)) { |
| 738 | target = cpumask_any_but(topology_sibling_cpumask(cpu), cpu); |
| 739 | /* Migrate events if there is a valid target */ |
| 740 | if (target < nr_cpu_ids) { |
| 741 | cpumask_set_cpu(target, &i915_pmu_cpumask); |
| 742 | perf_pmu_migrate_context(&pmu->base, cpu, target); |
| 743 | } |
| 744 | } |
| 745 | |
| 746 | return 0; |
| 747 | } |
| 748 | |
| 749 | static enum cpuhp_state cpuhp_slot = CPUHP_INVALID; |
| 750 | #endif |
| 751 | |
| 752 | static int i915_pmu_register_cpuhp_state(struct drm_i915_private *i915) |
| 753 | { |
| 754 | #ifdef CONFIG_HOTPLUG_CPU |
| 755 | enum cpuhp_state slot; |
| 756 | int ret; |
| 757 | |
| 758 | ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN, |
| 759 | "perf/x86/intel/i915:online", |
| 760 | i915_pmu_cpu_online, |
| 761 | i915_pmu_cpu_offline); |
| 762 | if (ret < 0) |
| 763 | return ret; |
| 764 | |
| 765 | slot = ret; |
| 766 | ret = cpuhp_state_add_instance(slot, &i915->pmu.node); |
| 767 | if (ret) { |
| 768 | cpuhp_remove_multi_state(slot); |
| 769 | return ret; |
| 770 | } |
| 771 | |
| 772 | cpuhp_slot = slot; |
| 773 | #endif |
| 774 | return 0; |
| 775 | } |
| 776 | |
| 777 | static void i915_pmu_unregister_cpuhp_state(struct drm_i915_private *i915) |
| 778 | { |
| 779 | #ifdef CONFIG_HOTPLUG_CPU |
| 780 | WARN_ON(cpuhp_slot == CPUHP_INVALID); |
| 781 | WARN_ON(cpuhp_state_remove_instance(cpuhp_slot, &i915->pmu.node)); |
| 782 | cpuhp_remove_multi_state(cpuhp_slot); |
| 783 | #endif |
| 784 | } |
| 785 | |
| 786 | void i915_pmu_register(struct drm_i915_private *i915) |
| 787 | { |
Tvrtko Ursulin | b3add01 | 2017-11-21 18:18:49 +0000 | [diff] [blame] | 788 | struct intel_engine_cs *engine; |
| 789 | enum intel_engine_id id; |
Tvrtko Ursulin | b46a33e | 2017-11-21 18:18:45 +0000 | [diff] [blame] | 790 | int ret; |
| 791 | |
| 792 | if (INTEL_GEN(i915) <= 2) { |
| 793 | DRM_INFO("PMU not supported for this GPU."); |
| 794 | return; |
| 795 | } |
| 796 | |
| 797 | i915->pmu.base.attr_groups = i915_pmu_attr_groups; |
| 798 | i915->pmu.base.task_ctx_nr = perf_invalid_context; |
| 799 | i915->pmu.base.event_init = i915_pmu_event_init; |
| 800 | i915->pmu.base.add = i915_pmu_event_add; |
| 801 | i915->pmu.base.del = i915_pmu_event_del; |
| 802 | i915->pmu.base.start = i915_pmu_event_start; |
| 803 | i915->pmu.base.stop = i915_pmu_event_stop; |
| 804 | i915->pmu.base.read = i915_pmu_event_read; |
| 805 | i915->pmu.base.event_idx = i915_pmu_event_event_idx; |
| 806 | |
| 807 | spin_lock_init(&i915->pmu.lock); |
| 808 | hrtimer_init(&i915->pmu.timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); |
| 809 | i915->pmu.timer.function = i915_sample; |
| 810 | |
Tvrtko Ursulin | b3add01 | 2017-11-21 18:18:49 +0000 | [diff] [blame] | 811 | for_each_engine(engine, i915, id) |
| 812 | INIT_DELAYED_WORK(&engine->pmu.disable_busy_stats, |
| 813 | __disable_busy_stats); |
| 814 | |
Tvrtko Ursulin | b46a33e | 2017-11-21 18:18:45 +0000 | [diff] [blame] | 815 | ret = perf_pmu_register(&i915->pmu.base, "i915", -1); |
| 816 | if (ret) |
| 817 | goto err; |
| 818 | |
| 819 | ret = i915_pmu_register_cpuhp_state(i915); |
| 820 | if (ret) |
| 821 | goto err_unreg; |
| 822 | |
| 823 | return; |
| 824 | |
| 825 | err_unreg: |
| 826 | perf_pmu_unregister(&i915->pmu.base); |
| 827 | err: |
| 828 | i915->pmu.base.event_init = NULL; |
| 829 | DRM_NOTE("Failed to register PMU! (err=%d)\n", ret); |
| 830 | } |
| 831 | |
| 832 | void i915_pmu_unregister(struct drm_i915_private *i915) |
| 833 | { |
Tvrtko Ursulin | b3add01 | 2017-11-21 18:18:49 +0000 | [diff] [blame] | 834 | struct intel_engine_cs *engine; |
| 835 | enum intel_engine_id id; |
| 836 | |
Tvrtko Ursulin | b46a33e | 2017-11-21 18:18:45 +0000 | [diff] [blame] | 837 | if (!i915->pmu.base.event_init) |
| 838 | return; |
| 839 | |
| 840 | WARN_ON(i915->pmu.enable); |
| 841 | |
| 842 | hrtimer_cancel(&i915->pmu.timer); |
| 843 | |
Tvrtko Ursulin | b3add01 | 2017-11-21 18:18:49 +0000 | [diff] [blame] | 844 | for_each_engine(engine, i915, id) { |
| 845 | GEM_BUG_ON(engine->pmu.busy_stats); |
| 846 | flush_delayed_work(&engine->pmu.disable_busy_stats); |
| 847 | } |
| 848 | |
Tvrtko Ursulin | b46a33e | 2017-11-21 18:18:45 +0000 | [diff] [blame] | 849 | i915_pmu_unregister_cpuhp_state(i915); |
| 850 | |
| 851 | perf_pmu_unregister(&i915->pmu.base); |
| 852 | i915->pmu.base.event_init = NULL; |
| 853 | } |