1 // SPDX-License-Identifier: MIT 2 /* 3 * Copyright © 2022 Intel Corporation 4 */ 5 6 #include "xe_pm.h" 7 8 #include <linux/error-injection.h> 9 #include <linux/pm_runtime.h> 10 #include <linux/suspend.h> 11 #include <linux/dmi.h> 12 13 #include <drm/drm_managed.h> 14 #include <drm/ttm/ttm_placement.h> 15 16 #include "display/xe_display.h" 17 #include "xe_bo.h" 18 #include "xe_bo_evict.h" 19 #include "xe_device.h" 20 #include "xe_ggtt.h" 21 #include "xe_gt.h" 22 #include "xe_gt_idle.h" 23 #include "xe_i2c.h" 24 #include "xe_irq.h" 25 #include "xe_late_bind_fw.h" 26 #include "xe_pcode.h" 27 #include "xe_printk.h" 28 #include "xe_pxp.h" 29 #include "xe_sriov_vf_ccs.h" 30 #include "xe_sysctrl.h" 31 #include "xe_trace.h" 32 #include "xe_vm.h" 33 #include "xe_wa.h" 34 35 /** 36 * DOC: Xe Power Management 37 * 38 * Xe PM implements the main routines for both system level suspend states and 39 * for the opportunistic runtime suspend states. 40 * 41 * System Level Suspend (S-States) - In general this is OS initiated suspend 42 * driven by ACPI for achieving S0ix (a.k.a. S2idle, freeze), S3 (suspend to ram), 43 * S4 (disk). The main functions here are `xe_pm_suspend` and `xe_pm_resume`. They 44 * are the main point for the suspend to and resume from these states. 45 * 46 * PCI Device Suspend (D-States) - This is the opportunistic PCIe device low power 47 * state D3, controlled by the PCI subsystem and ACPI with the help from the 48 * runtime_pm infrastructure. 49 * PCI D3 is special and can mean D3hot, where Vcc power is on for keeping memory 50 * alive and quicker low latency resume or D3Cold where Vcc power is off for 51 * better power savings. 52 * The Vcc control of PCI hierarchy can only be controlled at the PCI root port 53 * level, while the device driver can be behind multiple bridges/switches and 54 * paired with other devices. For this reason, the PCI subsystem cannot perform 55 * the transition towards D3Cold. The lowest runtime PM possible from the PCI 56 * subsystem is D3hot. Then, if all these paired devices in the same root port 57 * are in D3hot, ACPI will assist here and run its own methods (_PR3 and _OFF) 58 * to perform the transition from D3hot to D3cold. Xe may disallow this 59 * transition by calling pci_d3cold_disable(root_pdev) before going to runtime 60 * suspend. It will be based on runtime conditions such as VRAM usage for a 61 * quick and low latency resume for instance. 62 * 63 * Runtime PM - This infrastructure provided by the Linux kernel allows the 64 * device drivers to indicate when the can be runtime suspended, so the device 65 * could be put at D3 (if supported), or allow deeper package sleep states 66 * (PC-states), and/or other low level power states. Xe PM component provides 67 * `xe_pm_runtime_suspend` and `xe_pm_runtime_resume` functions that PCI 68 * subsystem will call before transition to/from runtime suspend. 69 * 70 * Also, Xe PM provides get and put functions that Xe driver will use to 71 * indicate activity. In order to avoid locking complications with the memory 72 * management, whenever possible, these get and put functions needs to be called 73 * from the higher/outer levels. 74 * The main cases that need to be protected from the outer levels are: IOCTL, 75 * sysfs, debugfs, dma-buf sharing, GPU execution. 76 * 77 * This component is not responsible for GT idleness (RC6) nor GT frequency 78 * management (RPS). 79 */ 80 81 #define HAS_PM_PME_SUPPORT(xe) (GRAPHICS_VERx100(xe) >= 3500) 82 83 #ifdef CONFIG_LOCKDEP 84 static struct lockdep_map xe_pm_runtime_d3cold_map = { 85 .name = "xe_rpm_d3cold_map" 86 }; 87 88 static struct lockdep_map xe_pm_runtime_nod3cold_map = { 89 .name = "xe_rpm_nod3cold_map" 90 }; 91 92 static struct lockdep_map xe_pm_block_lockdep_map = { 93 .name = "xe_pm_block_map", 94 }; 95 #endif 96 97 static void xe_pm_block_begin_signalling(void) 98 { 99 lock_acquire_shared_recursive(&xe_pm_block_lockdep_map, 0, 1, NULL, _RET_IP_); 100 } 101 102 static void xe_pm_block_end_signalling(void) 103 { 104 lock_release(&xe_pm_block_lockdep_map, _RET_IP_); 105 } 106 107 /** 108 * xe_pm_might_block_on_suspend() - Annotate that the code might block on suspend 109 * 110 * Annotation to use where the code might block or seize to make 111 * progress pending resume completion. 112 */ 113 void xe_pm_might_block_on_suspend(void) 114 { 115 lock_map_acquire(&xe_pm_block_lockdep_map); 116 lock_map_release(&xe_pm_block_lockdep_map); 117 } 118 119 /** 120 * xe_pm_block_on_suspend() - Block pending suspend. 121 * @xe: The xe device about to be suspended. 122 * 123 * Block if the pm notifier has start evicting bos, to avoid 124 * racing and validating those bos back. The function is 125 * annotated to ensure no locks are held that are also grabbed 126 * in the pm notifier or the device suspend / resume. 127 * This is intended to be used by freezable tasks only. 128 * (Not freezable workqueues), with the intention that the function 129 * returns %-ERESTARTSYS when tasks are frozen during suspend, 130 * and allows the task to freeze. The caller must be able to 131 * handle the %-ERESTARTSYS. 132 * 133 * Return: %0 on success, %-ERESTARTSYS on signal pending or 134 * if freezing requested. 135 */ 136 int xe_pm_block_on_suspend(struct xe_device *xe) 137 { 138 xe_pm_might_block_on_suspend(); 139 140 return wait_for_completion_interruptible(&xe->pm_block); 141 } 142 143 /** 144 * xe_rpm_reclaim_safe() - Whether runtime resume can be done from reclaim context 145 * @xe: The xe device. 146 * 147 * Return: true if it is safe to runtime resume from reclaim context. 148 * false otherwise. 149 */ 150 bool xe_rpm_reclaim_safe(const struct xe_device *xe) 151 { 152 return !xe->d3cold.capable; 153 } 154 155 static void xe_rpm_lockmap_acquire(const struct xe_device *xe) 156 { 157 lock_map_acquire(xe_rpm_reclaim_safe(xe) ? 158 &xe_pm_runtime_nod3cold_map : 159 &xe_pm_runtime_d3cold_map); 160 } 161 162 static void xe_rpm_lockmap_release(const struct xe_device *xe) 163 { 164 lock_map_release(xe_rpm_reclaim_safe(xe) ? 165 &xe_pm_runtime_nod3cold_map : 166 &xe_pm_runtime_d3cold_map); 167 } 168 169 /** 170 * xe_pm_suspend - Helper for System suspend, i.e. S0->S3 / S0->S2idle 171 * @xe: xe device instance 172 * 173 * Return: 0 on success 174 */ 175 int xe_pm_suspend(struct xe_device *xe) 176 { 177 struct xe_gt *gt; 178 u8 id; 179 int err; 180 181 drm_dbg(&xe->drm, "Suspending device\n"); 182 xe_pm_block_begin_signalling(); 183 trace_xe_pm_suspend(xe, __builtin_return_address(0)); 184 185 err = xe_pxp_pm_suspend(xe->pxp); 186 if (err) 187 goto err; 188 189 xe_late_bind_wait_for_worker_completion(&xe->late_bind); 190 191 for_each_gt(gt, xe, id) 192 xe_gt_suspend_prepare(gt); 193 194 xe_display_pm_suspend(xe); 195 196 /* FIXME: Super racey... */ 197 err = xe_bo_evict_all(xe); 198 if (err) 199 goto err_display; 200 201 for_each_gt(gt, xe, id) { 202 err = xe_gt_suspend(gt); 203 if (err) 204 goto err_display; 205 } 206 207 xe_irq_suspend(xe); 208 209 xe_display_pm_suspend_late(xe); 210 211 xe_i2c_pm_suspend(xe); 212 213 drm_dbg(&xe->drm, "Device suspended\n"); 214 xe_pm_block_end_signalling(); 215 216 return 0; 217 218 err_display: 219 xe_display_pm_resume(xe); 220 xe_pxp_pm_resume(xe->pxp); 221 err: 222 drm_dbg(&xe->drm, "Device suspend failed %d\n", err); 223 xe_pm_block_end_signalling(); 224 return err; 225 } 226 227 /** 228 * xe_pm_resume - Helper for System resume S3->S0 / S2idle->S0 229 * @xe: xe device instance 230 * 231 * Return: 0 on success 232 */ 233 int xe_pm_resume(struct xe_device *xe) 234 { 235 struct xe_tile *tile; 236 struct xe_gt *gt; 237 u8 id; 238 int err; 239 240 xe_pm_block_begin_signalling(); 241 drm_dbg(&xe->drm, "Resuming device\n"); 242 trace_xe_pm_resume(xe, __builtin_return_address(0)); 243 244 err = xe_pcode_ready(xe, true); 245 if (err) 246 return err; 247 248 for_each_tile(tile, xe, id) 249 xe_wa_apply_tile_workarounds(tile); 250 251 for_each_gt(gt, xe, id) 252 xe_gt_idle_disable_c6(gt); 253 254 xe_display_pm_resume_early(xe); 255 256 /* 257 * This only restores pinned memory which is the memory required for the 258 * GT(s) to resume. 259 */ 260 err = xe_bo_restore_early(xe); 261 if (err) 262 goto err; 263 264 xe_i2c_pm_resume(xe, true); 265 266 xe_sysctrl_pm_resume(xe); 267 268 xe_irq_resume(xe); 269 270 for_each_gt(gt, xe, id) { 271 err = xe_gt_resume(gt); 272 if (err) 273 break; 274 } 275 276 /* 277 * Try to bring up display before bailing from GT resume failure, 278 * so we don't leave the user clueless with a blank screen. 279 */ 280 xe_display_pm_resume(xe); 281 if (err) 282 goto err; 283 284 err = xe_bo_restore_late(xe); 285 if (err) 286 goto err; 287 288 xe_pxp_pm_resume(xe->pxp); 289 290 if (IS_VF_CCS_READY(xe)) 291 xe_sriov_vf_ccs_register_context(xe); 292 293 xe_late_bind_fw_load(&xe->late_bind); 294 295 drm_dbg(&xe->drm, "Device resumed\n"); 296 xe_pm_block_end_signalling(); 297 return 0; 298 err: 299 drm_dbg(&xe->drm, "Device resume failed %d\n", err); 300 xe_pm_block_end_signalling(); 301 return err; 302 } 303 304 static bool xe_pm_pci_d3cold_capable(struct xe_device *xe) 305 { 306 struct pci_dev *pdev = to_pci_dev(xe->drm.dev); 307 struct pci_dev *root_pdev; 308 309 root_pdev = pcie_find_root_port(pdev); 310 if (!root_pdev) 311 return false; 312 313 /* D3Cold requires PME capability */ 314 if (!pci_pme_capable(root_pdev, PCI_D3cold)) { 315 drm_dbg(&xe->drm, "d3cold: PME# not supported\n"); 316 return false; 317 } 318 319 /* D3Cold requires _PR3 power resource */ 320 if (!pci_pr3_present(root_pdev)) { 321 drm_dbg(&xe->drm, "d3cold: ACPI _PR3 not present\n"); 322 return false; 323 } 324 325 return true; 326 } 327 328 static void xe_pm_runtime_init(struct xe_device *xe) 329 { 330 struct device *dev = xe->drm.dev; 331 332 /* Our current VFs do not support RPM. so, disable it */ 333 if (IS_SRIOV_VF(xe)) 334 return; 335 336 /* 337 * Disable the system suspend direct complete optimization. 338 * We need to ensure that the regular device suspend/resume functions 339 * are called since our runtime_pm cannot guarantee local memory 340 * eviction for d3cold. 341 * TODO: Check HDA audio dependencies claimed by i915, and then enforce 342 * this option to integrated graphics as well. 343 */ 344 if (IS_DGFX(xe)) 345 dev_pm_set_driver_flags(dev, DPM_FLAG_NO_DIRECT_COMPLETE); 346 347 pm_runtime_use_autosuspend(dev); 348 pm_runtime_set_autosuspend_delay(dev, 1000); 349 pm_runtime_set_active(dev); 350 pm_runtime_allow(dev); 351 pm_runtime_mark_last_busy(dev); 352 pm_runtime_put(dev); 353 } 354 355 /** 356 * xe_pm_init_early() - Initialize Xe Power Management 357 * @xe: the &xe_device instance 358 * 359 * Initialize everything that is a "software-only" state that does not 360 * require access to any of the device's hardware data. 361 * 362 * Return: 0 on success or a negative error code on failure. 363 */ 364 int xe_pm_init_early(struct xe_device *xe) 365 { 366 int err; 367 368 init_completion(&xe->pm_block); 369 complete_all(&xe->pm_block); 370 INIT_LIST_HEAD(&xe->rebind_resume_list); 371 INIT_LIST_HEAD(&xe->mem_access.vram_userfault.list); 372 373 err = drmm_mutex_init(&xe->drm, &xe->mem_access.vram_userfault.lock); 374 if (err) 375 return err; 376 377 err = drmm_mutex_init(&xe->drm, &xe->d3cold.lock); 378 if (err) 379 return err; 380 381 err = drmm_mutex_init(&xe->drm, &xe->rebind_resume_lock); 382 if (err) 383 return err; 384 385 return 0; 386 } 387 ALLOW_ERROR_INJECTION(xe_pm_init_early, ERRNO); /* See xe_pci_probe() */ 388 389 static bool xe_pm_pci_pme_capable(struct xe_device *xe) 390 { 391 struct pci_dev *pdev = to_pci_dev(xe->drm.dev); 392 393 return HAS_PM_PME_SUPPORT(xe) ? 394 pci_pme_capable(pdev, PCI_D3hot) : false; 395 } 396 397 /** 398 * xe_pm_probe() - Initialize Xe Power Management 399 * @xe: the &xe_device instance 400 * 401 * Check d3cold capability. 402 * 403 * Return: 0 on success or a negative error code on failure. 404 */ 405 int xe_pm_probe(struct xe_device *xe) 406 { 407 xe->d3cold.capable = xe_pm_pci_d3cold_capable(xe); 408 xe_dbg(xe, "d3cold: capable=%s\n", str_yes_no(xe->d3cold.capable)); 409 410 xe->pme.capable = xe_pm_pci_pme_capable(xe); 411 xe_dbg(xe, "pme: capable=%s\n", str_yes_no(xe->pme.capable)); 412 413 if (xe->pme.capable) { 414 int err = devm_device_init_wakeup(xe->drm.dev); 415 416 if (err) 417 return err; 418 } 419 420 return 0; 421 } 422 423 static u32 vram_threshold_value(struct xe_device *xe) 424 { 425 if (xe->info.platform == XE_BATTLEMAGE) { 426 const char *product_name; 427 428 product_name = dmi_get_system_info(DMI_PRODUCT_NAME); 429 if (product_name && strstr(product_name, "NUC13RNG")) { 430 drm_warn(&xe->drm, "BMG + D3Cold not supported on this platform\n"); 431 return 0; 432 } 433 } 434 435 return DEFAULT_VRAM_THRESHOLD; 436 } 437 438 static void xe_pm_wake_rebind_workers(struct xe_device *xe) 439 { 440 struct xe_vm *vm, *next; 441 442 mutex_lock(&xe->rebind_resume_lock); 443 list_for_each_entry_safe(vm, next, &xe->rebind_resume_list, 444 preempt.pm_activate_link) { 445 list_del_init(&vm->preempt.pm_activate_link); 446 xe_vm_resume_rebind_worker(vm); 447 } 448 mutex_unlock(&xe->rebind_resume_lock); 449 } 450 451 static int xe_pm_notifier_callback(struct notifier_block *nb, 452 unsigned long action, void *data) 453 { 454 struct xe_device *xe = container_of(nb, struct xe_device, pm_notifier); 455 int err = 0; 456 457 switch (action) { 458 case PM_HIBERNATION_PREPARE: 459 case PM_SUSPEND_PREPARE: 460 { 461 struct xe_validation_ctx ctx; 462 463 reinit_completion(&xe->pm_block); 464 xe_pm_block_begin_signalling(); 465 xe_pm_runtime_get(xe); 466 (void)xe_validation_ctx_init(&ctx, &xe->val, NULL, 467 (struct xe_val_flags) {.exclusive = true}); 468 err = xe_bo_evict_all_user(xe); 469 xe_validation_ctx_fini(&ctx); 470 if (err) 471 drm_dbg(&xe->drm, "Notifier evict user failed (%d)\n", err); 472 473 err = xe_bo_notifier_prepare_all_pinned(xe); 474 if (err) 475 drm_dbg(&xe->drm, "Notifier prepare pin failed (%d)\n", err); 476 /* 477 * Keep the runtime pm reference until post hibernation / post suspend to 478 * avoid a runtime suspend interfering with evicted objects or backup 479 * allocations. 480 */ 481 xe_pm_block_end_signalling(); 482 break; 483 } 484 case PM_POST_HIBERNATION: 485 case PM_POST_SUSPEND: 486 complete_all(&xe->pm_block); 487 xe_pm_wake_rebind_workers(xe); 488 xe_bo_notifier_unprepare_all_pinned(xe); 489 xe_pm_runtime_put(xe); 490 break; 491 } 492 493 return NOTIFY_DONE; 494 } 495 496 /** 497 * xe_pm_init - Initialize Xe Power Management 498 * @xe: xe device instance 499 * 500 * This component is responsible for System and Device sleep states. 501 * 502 * Returns 0 for success, negative error code otherwise. 503 */ 504 int xe_pm_init(struct xe_device *xe) 505 { 506 u32 vram_threshold; 507 int err; 508 509 xe->pm_notifier.notifier_call = xe_pm_notifier_callback; 510 err = register_pm_notifier(&xe->pm_notifier); 511 if (err) 512 return err; 513 514 /* For now suspend/resume is only allowed with GuC */ 515 if (!xe_device_uc_enabled(xe)) 516 return 0; 517 518 if (xe->d3cold.capable) { 519 vram_threshold = vram_threshold_value(xe); 520 err = xe_pm_set_vram_threshold(xe, vram_threshold); 521 if (err) 522 goto err_unregister; 523 } 524 525 xe_pm_runtime_init(xe); 526 return 0; 527 528 err_unregister: 529 unregister_pm_notifier(&xe->pm_notifier); 530 return err; 531 } 532 533 static void xe_pm_runtime_fini(struct xe_device *xe) 534 { 535 struct device *dev = xe->drm.dev; 536 537 /* Our current VFs do not support RPM. so, disable it */ 538 if (IS_SRIOV_VF(xe)) 539 return; 540 541 pm_runtime_get_sync(dev); 542 pm_runtime_forbid(dev); 543 } 544 545 /** 546 * xe_pm_fini - Finalize PM 547 * @xe: xe device instance 548 */ 549 void xe_pm_fini(struct xe_device *xe) 550 { 551 if (xe_device_uc_enabled(xe)) 552 xe_pm_runtime_fini(xe); 553 554 unregister_pm_notifier(&xe->pm_notifier); 555 } 556 557 static void xe_pm_write_callback_task(struct xe_device *xe, 558 struct task_struct *task) 559 { 560 WRITE_ONCE(xe->pm_callback_task, task); 561 562 /* 563 * Just in case it's somehow possible for our writes to be reordered to 564 * the extent that something else re-uses the task written in 565 * pm_callback_task. For example after returning from the callback, but 566 * before the reordered write that resets pm_callback_task back to NULL. 567 */ 568 smp_mb(); /* pairs with xe_pm_read_callback_task */ 569 } 570 571 struct task_struct *xe_pm_read_callback_task(struct xe_device *xe) 572 { 573 smp_mb(); /* pairs with xe_pm_write_callback_task */ 574 575 return READ_ONCE(xe->pm_callback_task); 576 } 577 578 /** 579 * xe_pm_runtime_suspended - Check if runtime_pm state is suspended 580 * @xe: xe device instance 581 * 582 * This does not provide any guarantee that the device is going to remain 583 * suspended as it might be racing with the runtime state transitions. 584 * It can be used only as a non-reliable assertion, to ensure that we are not in 585 * the sleep state while trying to access some memory for instance. 586 * 587 * Returns true if PCI device is suspended, false otherwise. 588 */ 589 bool xe_pm_runtime_suspended(struct xe_device *xe) 590 { 591 return pm_runtime_suspended(xe->drm.dev); 592 } 593 594 /** 595 * xe_pm_runtime_suspend - Prepare our device for D3hot/D3Cold 596 * @xe: xe device instance 597 * 598 * Returns 0 for success, negative error code otherwise. 599 */ 600 int xe_pm_runtime_suspend(struct xe_device *xe) 601 { 602 struct xe_bo *bo, *on; 603 struct xe_gt *gt; 604 u8 id; 605 int err = 0; 606 607 trace_xe_pm_runtime_suspend(xe, __builtin_return_address(0)); 608 /* Disable access_ongoing asserts and prevent recursive pm calls */ 609 xe_pm_write_callback_task(xe, current); 610 611 /* 612 * The actual xe_pm_runtime_put() is always async underneath, so 613 * exactly where that is called should makes no difference to us. However 614 * we still need to be very careful with the locks that this callback 615 * acquires and the locks that are acquired and held by any callers of 616 * xe_runtime_pm_get(). We already have the matching annotation 617 * on that side, but we also need it here. For example lockdep should be 618 * able to tell us if the following scenario is in theory possible: 619 * 620 * CPU0 | CPU1 (kworker) 621 * lock(A) | 622 * | xe_pm_runtime_suspend() 623 * | lock(A) 624 * xe_pm_runtime_get() | 625 * 626 * This will clearly deadlock since rpm core needs to wait for 627 * xe_pm_runtime_suspend() to complete, but here we are holding lock(A) 628 * on CPU0 which prevents CPU1 making forward progress. With the 629 * annotation here and in xe_pm_runtime_get() lockdep will see 630 * the potential lock inversion and give us a nice splat. 631 */ 632 xe_rpm_lockmap_acquire(xe); 633 634 err = xe_pxp_pm_suspend(xe->pxp); 635 if (err) 636 goto out; 637 638 /* 639 * Applying lock for entire list op as xe_ttm_bo_destroy and xe_bo_move_notify 640 * also checks and deletes bo entry from user fault list. 641 */ 642 mutex_lock(&xe->mem_access.vram_userfault.lock); 643 list_for_each_entry_safe(bo, on, 644 &xe->mem_access.vram_userfault.list, vram_userfault_link) 645 xe_bo_runtime_pm_release_mmap_offset(bo); 646 mutex_unlock(&xe->mem_access.vram_userfault.lock); 647 648 xe_display_pm_runtime_suspend(xe); 649 650 if (xe->d3cold.allowed) { 651 err = xe_bo_evict_all(xe); 652 if (err) 653 goto out_resume; 654 } 655 656 for_each_gt(gt, xe, id) { 657 err = xe->d3cold.allowed ? xe_gt_suspend(gt) : xe_gt_runtime_suspend(gt); 658 if (err) 659 goto out_resume; 660 } 661 662 xe_irq_suspend(xe); 663 664 xe_display_pm_runtime_suspend_late(xe); 665 666 xe_i2c_pm_suspend(xe); 667 668 xe_rpm_lockmap_release(xe); 669 xe_pm_write_callback_task(xe, NULL); 670 return 0; 671 672 out_resume: 673 xe_pm_update_pme_enabled(xe, false); 674 xe_display_pm_runtime_resume(xe); 675 xe_pxp_pm_resume(xe->pxp); 676 out: 677 xe_rpm_lockmap_release(xe); 678 xe_pm_write_callback_task(xe, NULL); 679 return err; 680 } 681 682 /** 683 * xe_pm_runtime_resume - Waking up from D3hot/D3Cold 684 * @xe: xe device instance 685 * 686 * Returns 0 for success, negative error code otherwise. 687 */ 688 int xe_pm_runtime_resume(struct xe_device *xe) 689 { 690 struct xe_tile *tile; 691 struct xe_gt *gt; 692 u8 id; 693 int err = 0; 694 695 trace_xe_pm_runtime_resume(xe, __builtin_return_address(0)); 696 /* Disable access_ongoing asserts and prevent recursive pm calls */ 697 xe_pm_write_callback_task(xe, current); 698 699 xe_rpm_lockmap_acquire(xe); 700 701 if (xe->d3cold.allowed) { 702 err = xe_pcode_ready(xe, true); 703 if (err) 704 goto out; 705 706 for_each_tile(tile, xe, id) 707 xe_wa_apply_tile_workarounds(tile); 708 709 for_each_gt(gt, xe, id) 710 xe_gt_idle_disable_c6(gt); 711 712 xe_display_pm_resume_early(xe); 713 714 /* 715 * This only restores pinned memory which is the memory 716 * required for the GT(s) to resume. 717 */ 718 err = xe_bo_restore_early(xe); 719 if (err) 720 goto out; 721 } 722 723 xe_i2c_pm_resume(xe, xe->d3cold.allowed); 724 725 if (xe->d3cold.allowed) 726 xe_sysctrl_pm_resume(xe); 727 728 xe_display_pm_runtime_resume_early(xe); 729 730 xe_irq_resume(xe); 731 732 for_each_gt(gt, xe, id) { 733 err = xe->d3cold.allowed ? xe_gt_resume(gt) : xe_gt_runtime_resume(gt); 734 if (err) 735 break; 736 } 737 738 /* 739 * Try to bring up display before bailing from GT resume failure, 740 * so we don't leave the user clueless with a blank screen. 741 */ 742 xe_display_pm_runtime_resume(xe); 743 if (err) 744 goto out; 745 746 if (xe->d3cold.allowed) { 747 err = xe_bo_restore_late(xe); 748 if (err) 749 goto out; 750 } 751 752 xe_pxp_pm_resume(xe->pxp); 753 754 if (IS_VF_CCS_READY(xe)) 755 xe_sriov_vf_ccs_register_context(xe); 756 757 if (xe->d3cold.allowed) 758 xe_late_bind_fw_load(&xe->late_bind); 759 760 out: 761 xe_rpm_lockmap_release(xe); 762 xe_pm_write_callback_task(xe, NULL); 763 return err; 764 } 765 766 /* 767 * For places where resume is synchronous it can be quite easy to deadlock 768 * if we are not careful. Also in practice it might be quite timing 769 * sensitive to ever see the 0 -> 1 transition with the callers locks 770 * held, so deadlocks might exist but are hard for lockdep to ever see. 771 * With this in mind, help lockdep learn about the potentially scary 772 * stuff that can happen inside the runtime_resume callback by acquiring 773 * a dummy lock (it doesn't protect anything and gets compiled out on 774 * non-debug builds). Lockdep then only needs to see the 775 * xe_pm_runtime_xxx_map -> runtime_resume callback once, and then can 776 * hopefully validate all the (callers_locks) -> xe_pm_runtime_xxx_map. 777 * For example if the (callers_locks) are ever grabbed in the 778 * runtime_resume callback, lockdep should give us a nice splat. 779 */ 780 static void xe_rpm_might_enter_cb(const struct xe_device *xe) 781 { 782 xe_rpm_lockmap_acquire(xe); 783 xe_rpm_lockmap_release(xe); 784 } 785 786 /* 787 * Prime the lockdep maps for known locking orders that need to 788 * be supported but that may not always occur on all systems. 789 */ 790 static void xe_pm_runtime_lockdep_prime(void) 791 { 792 struct dma_resv lockdep_resv; 793 794 dma_resv_init(&lockdep_resv); 795 lock_map_acquire(&xe_pm_runtime_d3cold_map); 796 /* D3Cold takes the dma_resv locks to evict bos */ 797 dma_resv_lock(&lockdep_resv, NULL); 798 dma_resv_unlock(&lockdep_resv); 799 lock_map_release(&xe_pm_runtime_d3cold_map); 800 801 /* Shrinkers might like to wake up the device under reclaim. */ 802 fs_reclaim_acquire(GFP_KERNEL); 803 lock_map_acquire(&xe_pm_runtime_nod3cold_map); 804 lock_map_release(&xe_pm_runtime_nod3cold_map); 805 fs_reclaim_release(GFP_KERNEL); 806 } 807 808 /** 809 * xe_pm_runtime_get - Get a runtime_pm reference and resume synchronously 810 * @xe: xe device instance 811 * 812 * When possible, scope-based runtime PM (through guard(xe_pm_runtime)) is 813 * be preferred over direct usage of this function. Manual get/put handling 814 * should only be used when the function contains goto-based logic which 815 * can break scope-based handling, or when the lifetime of the runtime PM 816 * reference does not match a specific scope (e.g., runtime PM obtained in one 817 * function and released in a different one). 818 */ 819 void xe_pm_runtime_get(struct xe_device *xe) 820 { 821 trace_xe_pm_runtime_get(xe, __builtin_return_address(0)); 822 pm_runtime_get_noresume(xe->drm.dev); 823 824 if (xe_pm_read_callback_task(xe) == current) 825 return; 826 827 xe_rpm_might_enter_cb(xe); 828 pm_runtime_resume(xe->drm.dev); 829 } 830 831 /** 832 * xe_pm_runtime_put - Put the runtime_pm reference back and mark as idle 833 * @xe: xe device instance 834 */ 835 void xe_pm_runtime_put(struct xe_device *xe) 836 { 837 trace_xe_pm_runtime_put(xe, __builtin_return_address(0)); 838 if (xe_pm_read_callback_task(xe) == current) { 839 pm_runtime_put_noidle(xe->drm.dev); 840 } else { 841 pm_runtime_mark_last_busy(xe->drm.dev); 842 pm_runtime_put(xe->drm.dev); 843 } 844 } 845 846 /** 847 * xe_pm_runtime_get_ioctl - Get a runtime_pm reference before ioctl 848 * @xe: xe device instance 849 * 850 * When possible, scope-based runtime PM (through 851 * ACQUIRE(xe_pm_runtime_ioctl, ...)) is be preferred over direct usage of this 852 * function. Manual get/put handling should only be used when the function 853 * contains goto-based logic which can break scope-based handling, or when the 854 * lifetime of the runtime PM reference does not match a specific scope (e.g., 855 * runtime PM obtained in one function and released in a different one). 856 * 857 * Returns: Any number greater than or equal to 0 for success, negative error 858 * code otherwise. 859 */ 860 int xe_pm_runtime_get_ioctl(struct xe_device *xe) 861 { 862 trace_xe_pm_runtime_get_ioctl(xe, __builtin_return_address(0)); 863 if (WARN_ON(xe_pm_read_callback_task(xe) == current)) 864 return -ELOOP; 865 866 xe_rpm_might_enter_cb(xe); 867 return pm_runtime_get_sync(xe->drm.dev); 868 } 869 870 /** 871 * xe_pm_runtime_get_if_active - Get a runtime_pm reference if device active 872 * @xe: xe device instance 873 * 874 * Return: True if device is awake (regardless the previous number of references) 875 * and a new reference was taken, false otherwise. 876 */ 877 bool xe_pm_runtime_get_if_active(struct xe_device *xe) 878 { 879 return pm_runtime_get_if_active(xe->drm.dev) > 0; 880 } 881 882 /** 883 * xe_pm_runtime_get_if_in_use - Get a new reference if device is active with previous ref taken 884 * @xe: xe device instance 885 * 886 * Return: True if device is awake, a previous reference had been already taken, 887 * and a new reference was now taken, false otherwise. 888 */ 889 bool xe_pm_runtime_get_if_in_use(struct xe_device *xe) 890 { 891 if (xe_pm_read_callback_task(xe) == current) { 892 /* The device is awake, grab the ref and move on */ 893 pm_runtime_get_noresume(xe->drm.dev); 894 return true; 895 } 896 897 return pm_runtime_get_if_in_use(xe->drm.dev) > 0; 898 } 899 900 /* 901 * Very unreliable! Should only be used to suppress the false positive case 902 * in the missing outer rpm protection warning. 903 */ 904 static bool xe_pm_suspending_or_resuming(struct xe_device *xe) 905 { 906 #ifdef CONFIG_PM 907 struct device *dev = xe->drm.dev; 908 909 return dev->power.runtime_status == RPM_SUSPENDING || 910 dev->power.runtime_status == RPM_RESUMING || 911 pm_suspend_in_progress(); 912 #else 913 return false; 914 #endif 915 } 916 917 /** 918 * xe_pm_runtime_get_noresume - Bump runtime PM usage counter without resuming 919 * @xe: xe device instance 920 * 921 * This function should be used in inner places where it is surely already 922 * protected by outer-bound callers of `xe_pm_runtime_get`. 923 * It will warn if not protected. 924 * The reference should be put back after this function regardless, since it 925 * will always bump the usage counter, regardless. 926 * 927 * When possible, scope-based runtime PM (through guard(xe_pm_runtime_noresume)) 928 * is be preferred over direct usage of this function. Manual get/put handling 929 * should only be used when the function contains goto-based logic which can 930 * break scope-based handling, or when the lifetime of the runtime PM reference 931 * does not match a specific scope (e.g., runtime PM obtained in one function 932 * and released in a different one). 933 * 934 * This helper assumes the caller already holds a runtime PM reference and 935 * only warns when it cannot see one. After hot-unplug runtime PM is disabled 936 * and the check fails even when a reference is held, so callers that may run 937 * after unplug must guard it with drm_dev_enter()/drm_dev_exit() instead. 938 */ 939 void xe_pm_runtime_get_noresume(struct xe_device *xe) 940 { 941 bool ref; 942 943 ref = xe_pm_runtime_get_if_in_use(xe); 944 945 if (!ref) { 946 pm_runtime_get_noresume(xe->drm.dev); 947 drm_WARN(&xe->drm, !xe_pm_suspending_or_resuming(xe), 948 "Missing outer runtime PM protection\n"); 949 } 950 } 951 952 /** 953 * xe_pm_runtime_resume_and_get - Resume, then get a runtime_pm ref if awake. 954 * @xe: xe device instance 955 * 956 * Returns: True if device is awake and the reference was taken, false otherwise. 957 */ 958 bool xe_pm_runtime_resume_and_get(struct xe_device *xe) 959 { 960 if (xe_pm_read_callback_task(xe) == current) { 961 /* The device is awake, grab the ref and move on */ 962 pm_runtime_get_noresume(xe->drm.dev); 963 return true; 964 } 965 966 xe_rpm_might_enter_cb(xe); 967 return pm_runtime_resume_and_get(xe->drm.dev) >= 0; 968 } 969 970 /** 971 * xe_pm_assert_unbounded_bridge - Disable PM on unbounded pcie parent bridge 972 * @xe: xe device instance 973 */ 974 void xe_pm_assert_unbounded_bridge(struct xe_device *xe) 975 { 976 struct pci_dev *pdev = to_pci_dev(xe->drm.dev); 977 struct pci_dev *bridge = pci_upstream_bridge(pdev); 978 979 if (!bridge) 980 return; 981 982 if (!bridge->driver) { 983 drm_warn(&xe->drm, "unbounded parent pci bridge, device won't support any PM support.\n"); 984 device_set_pm_not_required(&pdev->dev); 985 } 986 } 987 988 /** 989 * xe_pm_set_vram_threshold - Set a VRAM threshold for allowing/blocking D3Cold 990 * @xe: xe device instance 991 * @threshold: VRAM size in MiB for the D3cold threshold 992 * 993 * Return: 994 * * 0 - success 995 * * -EINVAL - invalid argument 996 */ 997 int xe_pm_set_vram_threshold(struct xe_device *xe, u32 threshold) 998 { 999 struct ttm_resource_manager *man; 1000 u32 vram_total_mb = 0; 1001 int i; 1002 1003 for (i = XE_PL_VRAM0; i <= XE_PL_VRAM1; ++i) { 1004 man = ttm_manager_type(&xe->ttm, i); 1005 if (man) 1006 vram_total_mb += DIV_ROUND_UP_ULL(man->size, 1024 * 1024); 1007 } 1008 1009 drm_dbg(&xe->drm, "Total vram %u mb\n", vram_total_mb); 1010 1011 if (threshold > vram_total_mb) 1012 return -EINVAL; 1013 1014 mutex_lock(&xe->d3cold.lock); 1015 xe->d3cold.vram_threshold = threshold; 1016 mutex_unlock(&xe->d3cold.lock); 1017 1018 return 0; 1019 } 1020 1021 /** 1022 * xe_pm_pme_enabled - get the current status of PME enabled 1023 * @xe: xe device instance 1024 * 1025 * Returns: True if PME is enabled, false otherwise. 1026 */ 1027 bool xe_pm_pme_enabled(struct xe_device *xe) 1028 { 1029 return xe->pme.enabled; 1030 } 1031 1032 /** 1033 * xe_pm_update_pme_enabled - Update the PME enabled state 1034 * @xe: xe device instance 1035 * @status: New PME enabled status 1036 * 1037 * Called during runtime suspend / resume. Status is set to True if PME is 1038 * enabled during runtime_suspend. Cleared on runtime_resume. 1039 */ 1040 void xe_pm_update_pme_enabled(struct xe_device *xe, bool status) 1041 { 1042 xe->pme.enabled = status; 1043 } 1044 1045 /** 1046 * xe_pm_d3cold_allowed_toggle - Check conditions to toggle d3cold.allowed 1047 * @xe: xe device instance 1048 * 1049 * To be called during runtime_pm idle callback. 1050 * Check for all the D3Cold conditions ahead of runtime suspend. 1051 */ 1052 void xe_pm_d3cold_allowed_toggle(struct xe_device *xe) 1053 { 1054 struct ttm_resource_manager *man; 1055 u32 total_vram_used_mb = 0; 1056 u64 vram_used; 1057 int i; 1058 1059 if (!xe->d3cold.capable) { 1060 xe->d3cold.allowed = false; 1061 return; 1062 } 1063 1064 for (i = XE_PL_VRAM0; i <= XE_PL_VRAM1; ++i) { 1065 man = ttm_manager_type(&xe->ttm, i); 1066 if (man) { 1067 vram_used = ttm_resource_manager_usage(man); 1068 total_vram_used_mb += DIV_ROUND_UP_ULL(vram_used, 1024 * 1024); 1069 } 1070 } 1071 1072 mutex_lock(&xe->d3cold.lock); 1073 1074 if (total_vram_used_mb < xe->d3cold.vram_threshold) 1075 xe->d3cold.allowed = true; 1076 else 1077 xe->d3cold.allowed = false; 1078 1079 mutex_unlock(&xe->d3cold.lock); 1080 } 1081 1082 /** 1083 * xe_pm_wait_all_c6() - Wait for all GTs to enter C6. 1084 * @xe: xe device instance 1085 * 1086 * Return: 0 on success, -EAGAIN on failure 1087 */ 1088 int xe_pm_wait_all_c6(struct xe_device *xe) 1089 { 1090 struct xe_gt *gt; 1091 u8 id; 1092 1093 for_each_gt(gt, xe, id) 1094 if (xe_gt_idle_wait_for_c6(gt, 200)) 1095 return -EAGAIN; 1096 1097 return 0; 1098 } 1099 1100 /** 1101 * xe_pm_module_init() - Perform xe_pm specific module initialization. 1102 * 1103 * Return: 0 on success. Currently doesn't fail. 1104 */ 1105 int __init xe_pm_module_init(void) 1106 { 1107 xe_pm_runtime_lockdep_prime(); 1108 return 0; 1109 } 1110