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