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