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 | feff0dc | 2017-11-21 18:18:46 +0000 | [diff] [blame] | 93 | static bool pmu_needs_timer(struct drm_i915_private *i915, bool gpu_active) |
| 94 | { |
| 95 | u64 enable; |
| 96 | |
| 97 | /* |
| 98 | * Only some counters need the sampling timer. |
| 99 | * |
| 100 | * We start with a bitmask of all currently enabled events. |
| 101 | */ |
| 102 | enable = i915->pmu.enable; |
| 103 | |
| 104 | /* |
| 105 | * Mask out all the ones which do not need the timer, or in |
| 106 | * other words keep all the ones that could need the timer. |
| 107 | */ |
| 108 | enable &= config_enabled_mask(I915_PMU_ACTUAL_FREQUENCY) | |
| 109 | config_enabled_mask(I915_PMU_REQUESTED_FREQUENCY) | |
| 110 | ENGINE_SAMPLE_MASK; |
| 111 | |
| 112 | /* |
| 113 | * When the GPU is idle per-engine counters do not need to be |
| 114 | * running so clear those bits out. |
| 115 | */ |
| 116 | if (!gpu_active) |
| 117 | enable &= ~ENGINE_SAMPLE_MASK; |
| 118 | |
| 119 | /* |
| 120 | * If some bits remain it means we need the sampling timer running. |
| 121 | */ |
| 122 | return enable; |
| 123 | } |
| 124 | |
| 125 | void i915_pmu_gt_parked(struct drm_i915_private *i915) |
| 126 | { |
| 127 | if (!i915->pmu.base.event_init) |
| 128 | return; |
| 129 | |
| 130 | spin_lock_irq(&i915->pmu.lock); |
| 131 | /* |
| 132 | * Signal sampling timer to stop if only engine events are enabled and |
| 133 | * GPU went idle. |
| 134 | */ |
| 135 | i915->pmu.timer_enabled = pmu_needs_timer(i915, false); |
| 136 | spin_unlock_irq(&i915->pmu.lock); |
| 137 | } |
| 138 | |
| 139 | static void __i915_pmu_maybe_start_timer(struct drm_i915_private *i915) |
| 140 | { |
| 141 | if (!i915->pmu.timer_enabled && pmu_needs_timer(i915, true)) { |
| 142 | i915->pmu.timer_enabled = true; |
| 143 | hrtimer_start_range_ns(&i915->pmu.timer, |
| 144 | ns_to_ktime(PERIOD), 0, |
| 145 | HRTIMER_MODE_REL_PINNED); |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | void i915_pmu_gt_unparked(struct drm_i915_private *i915) |
| 150 | { |
| 151 | if (!i915->pmu.base.event_init) |
| 152 | return; |
| 153 | |
| 154 | spin_lock_irq(&i915->pmu.lock); |
| 155 | /* |
| 156 | * Re-enable sampling timer when GPU goes active. |
| 157 | */ |
| 158 | __i915_pmu_maybe_start_timer(i915); |
| 159 | spin_unlock_irq(&i915->pmu.lock); |
| 160 | } |
| 161 | |
Tvrtko Ursulin | b46a33e | 2017-11-21 18:18:45 +0000 | [diff] [blame] | 162 | static bool grab_forcewake(struct drm_i915_private *i915, bool fw) |
| 163 | { |
| 164 | if (!fw) |
| 165 | intel_uncore_forcewake_get(i915, FORCEWAKE_ALL); |
| 166 | |
| 167 | return true; |
| 168 | } |
| 169 | |
| 170 | static void |
| 171 | update_sample(struct i915_pmu_sample *sample, u32 unit, u32 val) |
| 172 | { |
| 173 | /* |
| 174 | * Since we are doing stochastic sampling for these counters, |
| 175 | * average the delta with the previous value for better accuracy. |
| 176 | */ |
| 177 | sample->cur += div_u64(mul_u32_u32(sample->prev + val, unit), 2); |
| 178 | sample->prev = val; |
| 179 | } |
| 180 | |
| 181 | static void engines_sample(struct drm_i915_private *dev_priv) |
| 182 | { |
| 183 | struct intel_engine_cs *engine; |
| 184 | enum intel_engine_id id; |
| 185 | bool fw = false; |
| 186 | |
| 187 | if ((dev_priv->pmu.enable & ENGINE_SAMPLE_MASK) == 0) |
| 188 | return; |
| 189 | |
| 190 | if (!dev_priv->gt.awake) |
| 191 | return; |
| 192 | |
| 193 | if (!intel_runtime_pm_get_if_in_use(dev_priv)) |
| 194 | return; |
| 195 | |
| 196 | for_each_engine(engine, dev_priv, id) { |
| 197 | u32 current_seqno = intel_engine_get_seqno(engine); |
| 198 | u32 last_seqno = intel_engine_last_submit(engine); |
| 199 | u32 val; |
| 200 | |
| 201 | val = !i915_seqno_passed(current_seqno, last_seqno); |
| 202 | |
| 203 | update_sample(&engine->pmu.sample[I915_SAMPLE_BUSY], |
| 204 | PERIOD, val); |
| 205 | |
| 206 | if (val && (engine->pmu.enable & |
| 207 | (BIT(I915_SAMPLE_WAIT) | BIT(I915_SAMPLE_SEMA)))) { |
| 208 | fw = grab_forcewake(dev_priv, fw); |
| 209 | |
| 210 | val = I915_READ_FW(RING_CTL(engine->mmio_base)); |
| 211 | } else { |
| 212 | val = 0; |
| 213 | } |
| 214 | |
| 215 | update_sample(&engine->pmu.sample[I915_SAMPLE_WAIT], |
| 216 | PERIOD, !!(val & RING_WAIT)); |
| 217 | |
| 218 | update_sample(&engine->pmu.sample[I915_SAMPLE_SEMA], |
| 219 | PERIOD, !!(val & RING_WAIT_SEMAPHORE)); |
| 220 | } |
| 221 | |
| 222 | if (fw) |
| 223 | intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL); |
| 224 | |
| 225 | intel_runtime_pm_put(dev_priv); |
| 226 | } |
| 227 | |
| 228 | static void frequency_sample(struct drm_i915_private *dev_priv) |
| 229 | { |
| 230 | if (dev_priv->pmu.enable & |
| 231 | config_enabled_mask(I915_PMU_ACTUAL_FREQUENCY)) { |
| 232 | u32 val; |
| 233 | |
| 234 | val = dev_priv->gt_pm.rps.cur_freq; |
| 235 | if (dev_priv->gt.awake && |
| 236 | intel_runtime_pm_get_if_in_use(dev_priv)) { |
| 237 | val = intel_get_cagf(dev_priv, |
| 238 | I915_READ_NOTRACE(GEN6_RPSTAT1)); |
| 239 | intel_runtime_pm_put(dev_priv); |
| 240 | } |
| 241 | |
| 242 | update_sample(&dev_priv->pmu.sample[__I915_SAMPLE_FREQ_ACT], |
| 243 | 1, intel_gpu_freq(dev_priv, val)); |
| 244 | } |
| 245 | |
| 246 | if (dev_priv->pmu.enable & |
| 247 | config_enabled_mask(I915_PMU_REQUESTED_FREQUENCY)) { |
| 248 | update_sample(&dev_priv->pmu.sample[__I915_SAMPLE_FREQ_REQ], 1, |
| 249 | intel_gpu_freq(dev_priv, |
| 250 | dev_priv->gt_pm.rps.cur_freq)); |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | static enum hrtimer_restart i915_sample(struct hrtimer *hrtimer) |
| 255 | { |
| 256 | struct drm_i915_private *i915 = |
| 257 | container_of(hrtimer, struct drm_i915_private, pmu.timer); |
| 258 | |
Tvrtko Ursulin | feff0dc | 2017-11-21 18:18:46 +0000 | [diff] [blame] | 259 | if (!READ_ONCE(i915->pmu.timer_enabled)) |
Tvrtko Ursulin | b46a33e | 2017-11-21 18:18:45 +0000 | [diff] [blame] | 260 | return HRTIMER_NORESTART; |
| 261 | |
| 262 | engines_sample(i915); |
| 263 | frequency_sample(i915); |
| 264 | |
| 265 | hrtimer_forward_now(hrtimer, ns_to_ktime(PERIOD)); |
| 266 | return HRTIMER_RESTART; |
| 267 | } |
| 268 | |
| 269 | static void i915_pmu_event_destroy(struct perf_event *event) |
| 270 | { |
| 271 | WARN_ON(event->parent); |
| 272 | } |
| 273 | |
| 274 | static int engine_event_init(struct perf_event *event) |
| 275 | { |
| 276 | struct drm_i915_private *i915 = |
| 277 | container_of(event->pmu, typeof(*i915), pmu.base); |
| 278 | |
| 279 | if (!intel_engine_lookup_user(i915, engine_event_class(event), |
| 280 | engine_event_instance(event))) |
| 281 | return -ENODEV; |
| 282 | |
| 283 | switch (engine_event_sample(event)) { |
| 284 | case I915_SAMPLE_BUSY: |
| 285 | case I915_SAMPLE_WAIT: |
| 286 | break; |
| 287 | case I915_SAMPLE_SEMA: |
| 288 | if (INTEL_GEN(i915) < 6) |
| 289 | return -ENODEV; |
| 290 | break; |
| 291 | default: |
| 292 | return -ENOENT; |
| 293 | } |
| 294 | |
| 295 | return 0; |
| 296 | } |
| 297 | |
| 298 | static int i915_pmu_event_init(struct perf_event *event) |
| 299 | { |
| 300 | struct drm_i915_private *i915 = |
| 301 | container_of(event->pmu, typeof(*i915), pmu.base); |
| 302 | int cpu, ret; |
| 303 | |
| 304 | if (event->attr.type != event->pmu->type) |
| 305 | return -ENOENT; |
| 306 | |
| 307 | /* unsupported modes and filters */ |
| 308 | if (event->attr.sample_period) /* no sampling */ |
| 309 | return -EINVAL; |
| 310 | |
| 311 | if (has_branch_stack(event)) |
| 312 | return -EOPNOTSUPP; |
| 313 | |
| 314 | if (event->cpu < 0) |
| 315 | return -EINVAL; |
| 316 | |
| 317 | cpu = cpumask_any_and(&i915_pmu_cpumask, |
| 318 | topology_sibling_cpumask(event->cpu)); |
| 319 | if (cpu >= nr_cpu_ids) |
| 320 | return -ENODEV; |
| 321 | |
| 322 | if (is_engine_event(event)) { |
| 323 | ret = engine_event_init(event); |
| 324 | } else { |
| 325 | ret = 0; |
| 326 | switch (event->attr.config) { |
| 327 | case I915_PMU_ACTUAL_FREQUENCY: |
| 328 | if (IS_VALLEYVIEW(i915) || IS_CHERRYVIEW(i915)) |
| 329 | /* Requires a mutex for sampling! */ |
| 330 | ret = -ENODEV; |
| 331 | case I915_PMU_REQUESTED_FREQUENCY: |
| 332 | if (INTEL_GEN(i915) < 6) |
| 333 | ret = -ENODEV; |
| 334 | break; |
| 335 | default: |
| 336 | ret = -ENOENT; |
| 337 | break; |
| 338 | } |
| 339 | } |
| 340 | if (ret) |
| 341 | return ret; |
| 342 | |
| 343 | event->cpu = cpu; |
| 344 | if (!event->parent) |
| 345 | event->destroy = i915_pmu_event_destroy; |
| 346 | |
| 347 | return 0; |
| 348 | } |
| 349 | |
| 350 | static u64 __i915_pmu_event_read(struct perf_event *event) |
| 351 | { |
| 352 | struct drm_i915_private *i915 = |
| 353 | container_of(event->pmu, typeof(*i915), pmu.base); |
| 354 | u64 val = 0; |
| 355 | |
| 356 | if (is_engine_event(event)) { |
| 357 | u8 sample = engine_event_sample(event); |
| 358 | struct intel_engine_cs *engine; |
| 359 | |
| 360 | engine = intel_engine_lookup_user(i915, |
| 361 | engine_event_class(event), |
| 362 | engine_event_instance(event)); |
| 363 | |
| 364 | if (WARN_ON_ONCE(!engine)) { |
| 365 | /* Do nothing */ |
| 366 | } else { |
| 367 | val = engine->pmu.sample[sample].cur; |
| 368 | } |
| 369 | } else { |
| 370 | switch (event->attr.config) { |
| 371 | case I915_PMU_ACTUAL_FREQUENCY: |
| 372 | val = |
| 373 | div_u64(i915->pmu.sample[__I915_SAMPLE_FREQ_ACT].cur, |
| 374 | FREQUENCY); |
| 375 | break; |
| 376 | case I915_PMU_REQUESTED_FREQUENCY: |
| 377 | val = |
| 378 | div_u64(i915->pmu.sample[__I915_SAMPLE_FREQ_REQ].cur, |
| 379 | FREQUENCY); |
| 380 | break; |
| 381 | } |
| 382 | } |
| 383 | |
| 384 | return val; |
| 385 | } |
| 386 | |
| 387 | static void i915_pmu_event_read(struct perf_event *event) |
| 388 | { |
| 389 | struct hw_perf_event *hwc = &event->hw; |
| 390 | u64 prev, new; |
| 391 | |
| 392 | again: |
| 393 | prev = local64_read(&hwc->prev_count); |
| 394 | new = __i915_pmu_event_read(event); |
| 395 | |
| 396 | if (local64_cmpxchg(&hwc->prev_count, prev, new) != prev) |
| 397 | goto again; |
| 398 | |
| 399 | local64_add(new - prev, &event->count); |
| 400 | } |
| 401 | |
| 402 | static void i915_pmu_enable(struct perf_event *event) |
| 403 | { |
| 404 | struct drm_i915_private *i915 = |
| 405 | container_of(event->pmu, typeof(*i915), pmu.base); |
| 406 | unsigned int bit = event_enabled_bit(event); |
| 407 | unsigned long flags; |
| 408 | |
| 409 | spin_lock_irqsave(&i915->pmu.lock, flags); |
| 410 | |
| 411 | /* |
Tvrtko Ursulin | b46a33e | 2017-11-21 18:18:45 +0000 | [diff] [blame] | 412 | * Update the bitmask of enabled events and increment |
| 413 | * the event reference counter. |
| 414 | */ |
| 415 | GEM_BUG_ON(bit >= I915_PMU_MASK_BITS); |
| 416 | GEM_BUG_ON(i915->pmu.enable_count[bit] == ~0); |
| 417 | i915->pmu.enable |= BIT_ULL(bit); |
| 418 | i915->pmu.enable_count[bit]++; |
| 419 | |
| 420 | /* |
Tvrtko Ursulin | feff0dc | 2017-11-21 18:18:46 +0000 | [diff] [blame] | 421 | * Start the sampling timer if needed and not already enabled. |
| 422 | */ |
| 423 | __i915_pmu_maybe_start_timer(i915); |
| 424 | |
| 425 | /* |
Tvrtko Ursulin | b46a33e | 2017-11-21 18:18:45 +0000 | [diff] [blame] | 426 | * For per-engine events the bitmask and reference counting |
| 427 | * is stored per engine. |
| 428 | */ |
| 429 | if (is_engine_event(event)) { |
| 430 | u8 sample = engine_event_sample(event); |
| 431 | struct intel_engine_cs *engine; |
| 432 | |
| 433 | engine = intel_engine_lookup_user(i915, |
| 434 | engine_event_class(event), |
| 435 | engine_event_instance(event)); |
| 436 | GEM_BUG_ON(!engine); |
| 437 | engine->pmu.enable |= BIT(sample); |
| 438 | |
| 439 | GEM_BUG_ON(sample >= I915_PMU_SAMPLE_BITS); |
| 440 | GEM_BUG_ON(engine->pmu.enable_count[sample] == ~0); |
| 441 | engine->pmu.enable_count[sample]++; |
| 442 | } |
| 443 | |
| 444 | /* |
| 445 | * Store the current counter value so we can report the correct delta |
| 446 | * for all listeners. Even when the event was already enabled and has |
| 447 | * an existing non-zero value. |
| 448 | */ |
| 449 | local64_set(&event->hw.prev_count, __i915_pmu_event_read(event)); |
| 450 | |
| 451 | spin_unlock_irqrestore(&i915->pmu.lock, flags); |
| 452 | } |
| 453 | |
| 454 | static void i915_pmu_disable(struct perf_event *event) |
| 455 | { |
| 456 | struct drm_i915_private *i915 = |
| 457 | container_of(event->pmu, typeof(*i915), pmu.base); |
| 458 | unsigned int bit = event_enabled_bit(event); |
| 459 | unsigned long flags; |
| 460 | |
| 461 | spin_lock_irqsave(&i915->pmu.lock, flags); |
| 462 | |
| 463 | if (is_engine_event(event)) { |
| 464 | u8 sample = engine_event_sample(event); |
| 465 | struct intel_engine_cs *engine; |
| 466 | |
| 467 | engine = intel_engine_lookup_user(i915, |
| 468 | engine_event_class(event), |
| 469 | engine_event_instance(event)); |
| 470 | GEM_BUG_ON(!engine); |
| 471 | GEM_BUG_ON(sample >= I915_PMU_SAMPLE_BITS); |
| 472 | GEM_BUG_ON(engine->pmu.enable_count[sample] == 0); |
| 473 | /* |
| 474 | * Decrement the reference count and clear the enabled |
| 475 | * bitmask when the last listener on an event goes away. |
| 476 | */ |
| 477 | if (--engine->pmu.enable_count[sample] == 0) |
| 478 | engine->pmu.enable &= ~BIT(sample); |
| 479 | } |
| 480 | |
| 481 | GEM_BUG_ON(bit >= I915_PMU_MASK_BITS); |
| 482 | GEM_BUG_ON(i915->pmu.enable_count[bit] == 0); |
| 483 | /* |
| 484 | * Decrement the reference count and clear the enabled |
| 485 | * bitmask when the last listener on an event goes away. |
| 486 | */ |
Tvrtko Ursulin | feff0dc | 2017-11-21 18:18:46 +0000 | [diff] [blame] | 487 | if (--i915->pmu.enable_count[bit] == 0) { |
Tvrtko Ursulin | b46a33e | 2017-11-21 18:18:45 +0000 | [diff] [blame] | 488 | i915->pmu.enable &= ~BIT_ULL(bit); |
Tvrtko Ursulin | feff0dc | 2017-11-21 18:18:46 +0000 | [diff] [blame] | 489 | i915->pmu.timer_enabled &= pmu_needs_timer(i915, true); |
| 490 | } |
Tvrtko Ursulin | b46a33e | 2017-11-21 18:18:45 +0000 | [diff] [blame] | 491 | |
| 492 | spin_unlock_irqrestore(&i915->pmu.lock, flags); |
| 493 | } |
| 494 | |
| 495 | static void i915_pmu_event_start(struct perf_event *event, int flags) |
| 496 | { |
| 497 | i915_pmu_enable(event); |
| 498 | event->hw.state = 0; |
| 499 | } |
| 500 | |
| 501 | static void i915_pmu_event_stop(struct perf_event *event, int flags) |
| 502 | { |
| 503 | if (flags & PERF_EF_UPDATE) |
| 504 | i915_pmu_event_read(event); |
| 505 | i915_pmu_disable(event); |
| 506 | event->hw.state = PERF_HES_STOPPED; |
| 507 | } |
| 508 | |
| 509 | static int i915_pmu_event_add(struct perf_event *event, int flags) |
| 510 | { |
| 511 | if (flags & PERF_EF_START) |
| 512 | i915_pmu_event_start(event, flags); |
| 513 | |
| 514 | return 0; |
| 515 | } |
| 516 | |
| 517 | static void i915_pmu_event_del(struct perf_event *event, int flags) |
| 518 | { |
| 519 | i915_pmu_event_stop(event, PERF_EF_UPDATE); |
| 520 | } |
| 521 | |
| 522 | static int i915_pmu_event_event_idx(struct perf_event *event) |
| 523 | { |
| 524 | return 0; |
| 525 | } |
| 526 | |
| 527 | static ssize_t i915_pmu_format_show(struct device *dev, |
| 528 | struct device_attribute *attr, char *buf) |
| 529 | { |
| 530 | struct dev_ext_attribute *eattr; |
| 531 | |
| 532 | eattr = container_of(attr, struct dev_ext_attribute, attr); |
| 533 | return sprintf(buf, "%s\n", (char *)eattr->var); |
| 534 | } |
| 535 | |
| 536 | #define I915_PMU_FORMAT_ATTR(_name, _config) \ |
| 537 | (&((struct dev_ext_attribute[]) { \ |
| 538 | { .attr = __ATTR(_name, 0444, i915_pmu_format_show, NULL), \ |
| 539 | .var = (void *)_config, } \ |
| 540 | })[0].attr.attr) |
| 541 | |
| 542 | static struct attribute *i915_pmu_format_attrs[] = { |
| 543 | I915_PMU_FORMAT_ATTR(i915_eventid, "config:0-20"), |
| 544 | NULL, |
| 545 | }; |
| 546 | |
| 547 | static const struct attribute_group i915_pmu_format_attr_group = { |
| 548 | .name = "format", |
| 549 | .attrs = i915_pmu_format_attrs, |
| 550 | }; |
| 551 | |
| 552 | static ssize_t i915_pmu_event_show(struct device *dev, |
| 553 | struct device_attribute *attr, char *buf) |
| 554 | { |
| 555 | struct dev_ext_attribute *eattr; |
| 556 | |
| 557 | eattr = container_of(attr, struct dev_ext_attribute, attr); |
| 558 | return sprintf(buf, "config=0x%lx\n", (unsigned long)eattr->var); |
| 559 | } |
| 560 | |
| 561 | #define I915_EVENT_ATTR(_name, _config) \ |
| 562 | (&((struct dev_ext_attribute[]) { \ |
| 563 | { .attr = __ATTR(_name, 0444, i915_pmu_event_show, NULL), \ |
| 564 | .var = (void *)_config, } \ |
| 565 | })[0].attr.attr) |
| 566 | |
| 567 | #define I915_EVENT_STR(_name, _str) \ |
| 568 | (&((struct perf_pmu_events_attr[]) { \ |
| 569 | { .attr = __ATTR(_name, 0444, perf_event_sysfs_show, NULL), \ |
| 570 | .id = 0, \ |
| 571 | .event_str = _str, } \ |
| 572 | })[0].attr.attr) |
| 573 | |
| 574 | #define I915_EVENT(_name, _config, _unit) \ |
| 575 | I915_EVENT_ATTR(_name, _config), \ |
| 576 | I915_EVENT_STR(_name.unit, _unit) |
| 577 | |
| 578 | #define I915_ENGINE_EVENT(_name, _class, _instance, _sample) \ |
| 579 | I915_EVENT_ATTR(_name, __I915_PMU_ENGINE(_class, _instance, _sample)), \ |
| 580 | I915_EVENT_STR(_name.unit, "ns") |
| 581 | |
| 582 | #define I915_ENGINE_EVENTS(_name, _class, _instance) \ |
| 583 | I915_ENGINE_EVENT(_name##_instance-busy, _class, _instance, I915_SAMPLE_BUSY), \ |
| 584 | I915_ENGINE_EVENT(_name##_instance-sema, _class, _instance, I915_SAMPLE_SEMA), \ |
| 585 | I915_ENGINE_EVENT(_name##_instance-wait, _class, _instance, I915_SAMPLE_WAIT) |
| 586 | |
| 587 | static struct attribute *i915_pmu_events_attrs[] = { |
| 588 | I915_ENGINE_EVENTS(rcs, I915_ENGINE_CLASS_RENDER, 0), |
| 589 | I915_ENGINE_EVENTS(bcs, I915_ENGINE_CLASS_COPY, 0), |
| 590 | I915_ENGINE_EVENTS(vcs, I915_ENGINE_CLASS_VIDEO, 0), |
| 591 | I915_ENGINE_EVENTS(vcs, I915_ENGINE_CLASS_VIDEO, 1), |
| 592 | I915_ENGINE_EVENTS(vecs, I915_ENGINE_CLASS_VIDEO_ENHANCE, 0), |
| 593 | |
| 594 | I915_EVENT(actual-frequency, I915_PMU_ACTUAL_FREQUENCY, "MHz"), |
| 595 | I915_EVENT(requested-frequency, I915_PMU_REQUESTED_FREQUENCY, "MHz"), |
| 596 | |
| 597 | NULL, |
| 598 | }; |
| 599 | |
| 600 | static const struct attribute_group i915_pmu_events_attr_group = { |
| 601 | .name = "events", |
| 602 | .attrs = i915_pmu_events_attrs, |
| 603 | }; |
| 604 | |
| 605 | static ssize_t |
| 606 | i915_pmu_get_attr_cpumask(struct device *dev, |
| 607 | struct device_attribute *attr, |
| 608 | char *buf) |
| 609 | { |
| 610 | return cpumap_print_to_pagebuf(true, buf, &i915_pmu_cpumask); |
| 611 | } |
| 612 | |
| 613 | static DEVICE_ATTR(cpumask, 0444, i915_pmu_get_attr_cpumask, NULL); |
| 614 | |
| 615 | static struct attribute *i915_cpumask_attrs[] = { |
| 616 | &dev_attr_cpumask.attr, |
| 617 | NULL, |
| 618 | }; |
| 619 | |
| 620 | static struct attribute_group i915_pmu_cpumask_attr_group = { |
| 621 | .attrs = i915_cpumask_attrs, |
| 622 | }; |
| 623 | |
| 624 | static const struct attribute_group *i915_pmu_attr_groups[] = { |
| 625 | &i915_pmu_format_attr_group, |
| 626 | &i915_pmu_events_attr_group, |
| 627 | &i915_pmu_cpumask_attr_group, |
| 628 | NULL |
| 629 | }; |
| 630 | |
| 631 | #ifdef CONFIG_HOTPLUG_CPU |
| 632 | static int i915_pmu_cpu_online(unsigned int cpu, struct hlist_node *node) |
| 633 | { |
| 634 | struct i915_pmu *pmu = hlist_entry_safe(node, typeof(*pmu), node); |
| 635 | unsigned int target; |
| 636 | |
| 637 | GEM_BUG_ON(!pmu->base.event_init); |
| 638 | |
| 639 | target = cpumask_any_and(&i915_pmu_cpumask, &i915_pmu_cpumask); |
| 640 | /* Select the first online CPU as a designated reader. */ |
| 641 | if (target >= nr_cpu_ids) |
| 642 | cpumask_set_cpu(cpu, &i915_pmu_cpumask); |
| 643 | |
| 644 | return 0; |
| 645 | } |
| 646 | |
| 647 | static int i915_pmu_cpu_offline(unsigned int cpu, struct hlist_node *node) |
| 648 | { |
| 649 | struct i915_pmu *pmu = hlist_entry_safe(node, typeof(*pmu), node); |
| 650 | unsigned int target; |
| 651 | |
| 652 | GEM_BUG_ON(!pmu->base.event_init); |
| 653 | |
| 654 | if (cpumask_test_and_clear_cpu(cpu, &i915_pmu_cpumask)) { |
| 655 | target = cpumask_any_but(topology_sibling_cpumask(cpu), cpu); |
| 656 | /* Migrate events if there is a valid target */ |
| 657 | if (target < nr_cpu_ids) { |
| 658 | cpumask_set_cpu(target, &i915_pmu_cpumask); |
| 659 | perf_pmu_migrate_context(&pmu->base, cpu, target); |
| 660 | } |
| 661 | } |
| 662 | |
| 663 | return 0; |
| 664 | } |
| 665 | |
| 666 | static enum cpuhp_state cpuhp_slot = CPUHP_INVALID; |
| 667 | #endif |
| 668 | |
| 669 | static int i915_pmu_register_cpuhp_state(struct drm_i915_private *i915) |
| 670 | { |
| 671 | #ifdef CONFIG_HOTPLUG_CPU |
| 672 | enum cpuhp_state slot; |
| 673 | int ret; |
| 674 | |
| 675 | ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN, |
| 676 | "perf/x86/intel/i915:online", |
| 677 | i915_pmu_cpu_online, |
| 678 | i915_pmu_cpu_offline); |
| 679 | if (ret < 0) |
| 680 | return ret; |
| 681 | |
| 682 | slot = ret; |
| 683 | ret = cpuhp_state_add_instance(slot, &i915->pmu.node); |
| 684 | if (ret) { |
| 685 | cpuhp_remove_multi_state(slot); |
| 686 | return ret; |
| 687 | } |
| 688 | |
| 689 | cpuhp_slot = slot; |
| 690 | #endif |
| 691 | return 0; |
| 692 | } |
| 693 | |
| 694 | static void i915_pmu_unregister_cpuhp_state(struct drm_i915_private *i915) |
| 695 | { |
| 696 | #ifdef CONFIG_HOTPLUG_CPU |
| 697 | WARN_ON(cpuhp_slot == CPUHP_INVALID); |
| 698 | WARN_ON(cpuhp_state_remove_instance(cpuhp_slot, &i915->pmu.node)); |
| 699 | cpuhp_remove_multi_state(cpuhp_slot); |
| 700 | #endif |
| 701 | } |
| 702 | |
| 703 | void i915_pmu_register(struct drm_i915_private *i915) |
| 704 | { |
| 705 | int ret; |
| 706 | |
| 707 | if (INTEL_GEN(i915) <= 2) { |
| 708 | DRM_INFO("PMU not supported for this GPU."); |
| 709 | return; |
| 710 | } |
| 711 | |
| 712 | i915->pmu.base.attr_groups = i915_pmu_attr_groups; |
| 713 | i915->pmu.base.task_ctx_nr = perf_invalid_context; |
| 714 | i915->pmu.base.event_init = i915_pmu_event_init; |
| 715 | i915->pmu.base.add = i915_pmu_event_add; |
| 716 | i915->pmu.base.del = i915_pmu_event_del; |
| 717 | i915->pmu.base.start = i915_pmu_event_start; |
| 718 | i915->pmu.base.stop = i915_pmu_event_stop; |
| 719 | i915->pmu.base.read = i915_pmu_event_read; |
| 720 | i915->pmu.base.event_idx = i915_pmu_event_event_idx; |
| 721 | |
| 722 | spin_lock_init(&i915->pmu.lock); |
| 723 | hrtimer_init(&i915->pmu.timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); |
| 724 | i915->pmu.timer.function = i915_sample; |
| 725 | |
| 726 | ret = perf_pmu_register(&i915->pmu.base, "i915", -1); |
| 727 | if (ret) |
| 728 | goto err; |
| 729 | |
| 730 | ret = i915_pmu_register_cpuhp_state(i915); |
| 731 | if (ret) |
| 732 | goto err_unreg; |
| 733 | |
| 734 | return; |
| 735 | |
| 736 | err_unreg: |
| 737 | perf_pmu_unregister(&i915->pmu.base); |
| 738 | err: |
| 739 | i915->pmu.base.event_init = NULL; |
| 740 | DRM_NOTE("Failed to register PMU! (err=%d)\n", ret); |
| 741 | } |
| 742 | |
| 743 | void i915_pmu_unregister(struct drm_i915_private *i915) |
| 744 | { |
| 745 | if (!i915->pmu.base.event_init) |
| 746 | return; |
| 747 | |
| 748 | WARN_ON(i915->pmu.enable); |
| 749 | |
| 750 | hrtimer_cancel(&i915->pmu.timer); |
| 751 | |
| 752 | i915_pmu_unregister_cpuhp_state(i915); |
| 753 | |
| 754 | perf_pmu_unregister(&i915->pmu.base); |
| 755 | i915->pmu.base.event_init = NULL; |
| 756 | } |