1dd08ebf6SMatthew Brost // SPDX-License-Identifier: MIT 2dd08ebf6SMatthew Brost /* 3dd08ebf6SMatthew Brost * Copyright © 2022 Intel Corporation 4dd08ebf6SMatthew Brost */ 5dd08ebf6SMatthew Brost 6ea9f879dSLucas De Marchi #include "xe_pm.h" 7ea9f879dSLucas De Marchi 8dd08ebf6SMatthew Brost #include <linux/pm_runtime.h> 9dd08ebf6SMatthew Brost 10b2d75619SAnshuman Gupta #include <drm/drm_managed.h> 11dd08ebf6SMatthew Brost #include <drm/ttm/ttm_placement.h> 12dd08ebf6SMatthew Brost 131e5a4dfeSJani Nikula #include "display/xe_display.h" 14dd08ebf6SMatthew Brost #include "xe_bo.h" 15dd08ebf6SMatthew Brost #include "xe_bo_evict.h" 16dd08ebf6SMatthew Brost #include "xe_device.h" 17b2d75619SAnshuman Gupta #include "xe_device_sysfs.h" 18dd08ebf6SMatthew Brost #include "xe_ggtt.h" 19ea9f879dSLucas De Marchi #include "xe_gt.h" 2009d88e3bSAnshuman Gupta #include "xe_guc.h" 21dd08ebf6SMatthew Brost #include "xe_irq.h" 22dd08ebf6SMatthew Brost #include "xe_pcode.h" 230d053475SMatt Roper #include "xe_wa.h" 24dd08ebf6SMatthew Brost 25dd08ebf6SMatthew Brost /** 26dd08ebf6SMatthew Brost * DOC: Xe Power Management 27dd08ebf6SMatthew Brost * 2830c39952SRodrigo Vivi * Xe PM implements the main routines for both system level suspend states and 2930c39952SRodrigo Vivi * for the opportunistic runtime suspend states. 30dd08ebf6SMatthew Brost * 3130c39952SRodrigo Vivi * System Level Suspend (S-States) - In general this is OS initiated suspend 3230c39952SRodrigo Vivi * driven by ACPI for achieving S0ix (a.k.a. S2idle, freeze), S3 (suspend to ram), 3330c39952SRodrigo Vivi * S4 (disk). The main functions here are `xe_pm_suspend` and `xe_pm_resume`. They 3430c39952SRodrigo Vivi * are the main point for the suspend to and resume from these states. 35dd08ebf6SMatthew Brost * 3630c39952SRodrigo Vivi * PCI Device Suspend (D-States) - This is the opportunistic PCIe device low power 3730c39952SRodrigo Vivi * state D3, controlled by the PCI subsystem and ACPI with the help from the 3830c39952SRodrigo Vivi * runtime_pm infrastructure. 3930c39952SRodrigo Vivi * PCI D3 is special and can mean D3hot, where Vcc power is on for keeping memory 4030c39952SRodrigo Vivi * alive and quicker low latency resume or D3Cold where Vcc power is off for 4130c39952SRodrigo Vivi * better power savings. 4230c39952SRodrigo Vivi * The Vcc control of PCI hierarchy can only be controlled at the PCI root port 4330c39952SRodrigo Vivi * level, while the device driver can be behind multiple bridges/switches and 4430c39952SRodrigo Vivi * paired with other devices. For this reason, the PCI subsystem cannot perform 4530c39952SRodrigo Vivi * the transition towards D3Cold. The lowest runtime PM possible from the PCI 4630c39952SRodrigo Vivi * subsystem is D3hot. Then, if all these paired devices in the same root port 4730c39952SRodrigo Vivi * are in D3hot, ACPI will assist here and run its own methods (_PR3 and _OFF) 4830c39952SRodrigo Vivi * to perform the transition from D3hot to D3cold. Xe may disallow this 4930c39952SRodrigo Vivi * transition by calling pci_d3cold_disable(root_pdev) before going to runtime 5030c39952SRodrigo Vivi * suspend. It will be based on runtime conditions such as VRAM usage for a 5130c39952SRodrigo Vivi * quick and low latency resume for instance. 52dd08ebf6SMatthew Brost * 5330c39952SRodrigo Vivi * Runtime PM - This infrastructure provided by the Linux kernel allows the 5430c39952SRodrigo Vivi * device drivers to indicate when the can be runtime suspended, so the device 5530c39952SRodrigo Vivi * could be put at D3 (if supported), or allow deeper package sleep states 5630c39952SRodrigo Vivi * (PC-states), and/or other low level power states. Xe PM component provides 5730c39952SRodrigo Vivi * `xe_pm_runtime_suspend` and `xe_pm_runtime_resume` functions that PCI 5830c39952SRodrigo Vivi * subsystem will call before transition to/from runtime suspend. 59dd08ebf6SMatthew Brost * 6030c39952SRodrigo Vivi * Also, Xe PM provides get and put functions that Xe driver will use to 6130c39952SRodrigo Vivi * indicate activity. In order to avoid locking complications with the memory 6230c39952SRodrigo Vivi * management, whenever possible, these get and put functions needs to be called 6330c39952SRodrigo Vivi * from the higher/outer levels. 6430c39952SRodrigo Vivi * The main cases that need to be protected from the outer levels are: IOCTL, 6530c39952SRodrigo Vivi * sysfs, debugfs, dma-buf sharing, GPU execution. 6630c39952SRodrigo Vivi * 6730c39952SRodrigo Vivi * This component is not responsible for GT idleness (RC6) nor GT frequency 6830c39952SRodrigo Vivi * management (RPS). 69dd08ebf6SMatthew Brost */ 70dd08ebf6SMatthew Brost 718ae84a27SRodrigo Vivi #ifdef CONFIG_LOCKDEP 72869e54d4SRodrigo Vivi static struct lockdep_map xe_pm_runtime_lockdep_map = { 738ae84a27SRodrigo Vivi .name = "xe_pm_runtime_lockdep_map" 748ae84a27SRodrigo Vivi }; 758ae84a27SRodrigo Vivi #endif 768ae84a27SRodrigo Vivi 77dd08ebf6SMatthew Brost /** 78dd08ebf6SMatthew Brost * xe_pm_suspend - Helper for System suspend, i.e. S0->S3 / S0->S2idle 79dd08ebf6SMatthew Brost * @xe: xe device instance 80dd08ebf6SMatthew Brost * 81dd08ebf6SMatthew Brost * Return: 0 on success 82dd08ebf6SMatthew Brost */ 83dd08ebf6SMatthew Brost int xe_pm_suspend(struct xe_device *xe) 84dd08ebf6SMatthew Brost { 85dd08ebf6SMatthew Brost struct xe_gt *gt; 86dd08ebf6SMatthew Brost u8 id; 87dd08ebf6SMatthew Brost int err; 88dd08ebf6SMatthew Brost 89f7f24b79SRodrigo Vivi drm_dbg(&xe->drm, "Suspending device\n"); 90f7f24b79SRodrigo Vivi 91dd08ebf6SMatthew Brost for_each_gt(gt, xe, id) 92dd08ebf6SMatthew Brost xe_gt_suspend_prepare(gt); 93dd08ebf6SMatthew Brost 94dd08ebf6SMatthew Brost /* FIXME: Super racey... */ 95dd08ebf6SMatthew Brost err = xe_bo_evict_all(xe); 96dd08ebf6SMatthew Brost if (err) 97f7f24b79SRodrigo Vivi goto err; 98dd08ebf6SMatthew Brost 9944e69495SMaarten Lankhorst xe_display_pm_suspend(xe); 10044e69495SMaarten Lankhorst 101dd08ebf6SMatthew Brost for_each_gt(gt, xe, id) { 102dd08ebf6SMatthew Brost err = xe_gt_suspend(gt); 10344e69495SMaarten Lankhorst if (err) { 10444e69495SMaarten Lankhorst xe_display_pm_resume(xe); 105f7f24b79SRodrigo Vivi goto err; 106dd08ebf6SMatthew Brost } 10744e69495SMaarten Lankhorst } 108dd08ebf6SMatthew Brost 109dd08ebf6SMatthew Brost xe_irq_suspend(xe); 110dd08ebf6SMatthew Brost 11144e69495SMaarten Lankhorst xe_display_pm_suspend_late(xe); 11244e69495SMaarten Lankhorst 113f7f24b79SRodrigo Vivi drm_dbg(&xe->drm, "Device suspended\n"); 114dd08ebf6SMatthew Brost return 0; 115f7f24b79SRodrigo Vivi err: 116f7f24b79SRodrigo Vivi drm_dbg(&xe->drm, "Device suspend failed %d\n", err); 117f7f24b79SRodrigo Vivi return err; 118dd08ebf6SMatthew Brost } 119dd08ebf6SMatthew Brost 120dd08ebf6SMatthew Brost /** 121dd08ebf6SMatthew Brost * xe_pm_resume - Helper for System resume S3->S0 / S2idle->S0 122dd08ebf6SMatthew Brost * @xe: xe device instance 123dd08ebf6SMatthew Brost * 124dd08ebf6SMatthew Brost * Return: 0 on success 125dd08ebf6SMatthew Brost */ 126dd08ebf6SMatthew Brost int xe_pm_resume(struct xe_device *xe) 127dd08ebf6SMatthew Brost { 1280d053475SMatt Roper struct xe_tile *tile; 129dd08ebf6SMatthew Brost struct xe_gt *gt; 130dd08ebf6SMatthew Brost u8 id; 131dd08ebf6SMatthew Brost int err; 132dd08ebf6SMatthew Brost 133f7f24b79SRodrigo Vivi drm_dbg(&xe->drm, "Resuming device\n"); 134f7f24b79SRodrigo Vivi 1350d053475SMatt Roper for_each_tile(tile, xe, id) 1360d053475SMatt Roper xe_wa_apply_tile_workarounds(tile); 1370d053475SMatt Roper 138933fd5ffSRiana Tauro err = xe_pcode_ready(xe, true); 139dd08ebf6SMatthew Brost if (err) 140933fd5ffSRiana Tauro return err; 141dd08ebf6SMatthew Brost 14244e69495SMaarten Lankhorst xe_display_pm_resume_early(xe); 14344e69495SMaarten Lankhorst 144dd08ebf6SMatthew Brost /* 145dd08ebf6SMatthew Brost * This only restores pinned memory which is the memory required for the 146dd08ebf6SMatthew Brost * GT(s) to resume. 147dd08ebf6SMatthew Brost */ 148dd08ebf6SMatthew Brost err = xe_bo_restore_kernel(xe); 149dd08ebf6SMatthew Brost if (err) 150f7f24b79SRodrigo Vivi goto err; 151dd08ebf6SMatthew Brost 152dd08ebf6SMatthew Brost xe_irq_resume(xe); 153dd08ebf6SMatthew Brost 15444e69495SMaarten Lankhorst xe_display_pm_resume(xe); 15544e69495SMaarten Lankhorst 156dd08ebf6SMatthew Brost for_each_gt(gt, xe, id) 157dd08ebf6SMatthew Brost xe_gt_resume(gt); 158dd08ebf6SMatthew Brost 159dd08ebf6SMatthew Brost err = xe_bo_restore_user(xe); 160dd08ebf6SMatthew Brost if (err) 161f7f24b79SRodrigo Vivi goto err; 162dd08ebf6SMatthew Brost 163f7f24b79SRodrigo Vivi drm_dbg(&xe->drm, "Device resumed\n"); 164dd08ebf6SMatthew Brost return 0; 165f7f24b79SRodrigo Vivi err: 166f7f24b79SRodrigo Vivi drm_dbg(&xe->drm, "Device resume failed %d\n", err); 167f7f24b79SRodrigo Vivi return err; 168dd08ebf6SMatthew Brost } 169dd08ebf6SMatthew Brost 17095ec8c1dSRiana Tauro static bool xe_pm_pci_d3cold_capable(struct xe_device *xe) 171ac0be3b5SAnshuman Gupta { 17295ec8c1dSRiana Tauro struct pci_dev *pdev = to_pci_dev(xe->drm.dev); 173ac0be3b5SAnshuman Gupta struct pci_dev *root_pdev; 174ac0be3b5SAnshuman Gupta 175ac0be3b5SAnshuman Gupta root_pdev = pcie_find_root_port(pdev); 176ac0be3b5SAnshuman Gupta if (!root_pdev) 177ac0be3b5SAnshuman Gupta return false; 178ac0be3b5SAnshuman Gupta 17995ec8c1dSRiana Tauro /* D3Cold requires PME capability */ 18095ec8c1dSRiana Tauro if (!pci_pme_capable(root_pdev, PCI_D3cold)) { 18195ec8c1dSRiana Tauro drm_dbg(&xe->drm, "d3cold: PME# not supported\n"); 182ac0be3b5SAnshuman Gupta return false; 18395ec8c1dSRiana Tauro } 18495ec8c1dSRiana Tauro 18595ec8c1dSRiana Tauro /* D3Cold requires _PR3 power resource */ 18695ec8c1dSRiana Tauro if (!pci_pr3_present(root_pdev)) { 18795ec8c1dSRiana Tauro drm_dbg(&xe->drm, "d3cold: ACPI _PR3 not present\n"); 18895ec8c1dSRiana Tauro return false; 18995ec8c1dSRiana Tauro } 190ac0be3b5SAnshuman Gupta 191ac0be3b5SAnshuman Gupta return true; 192ac0be3b5SAnshuman Gupta } 193ac0be3b5SAnshuman Gupta 194fddebcbfSAnshuman Gupta static void xe_pm_runtime_init(struct xe_device *xe) 195dd08ebf6SMatthew Brost { 196dd08ebf6SMatthew Brost struct device *dev = xe->drm.dev; 197dd08ebf6SMatthew Brost 198d87c424aSRodrigo Vivi /* 199d87c424aSRodrigo Vivi * Disable the system suspend direct complete optimization. 200d87c424aSRodrigo Vivi * We need to ensure that the regular device suspend/resume functions 201d87c424aSRodrigo Vivi * are called since our runtime_pm cannot guarantee local memory 202d87c424aSRodrigo Vivi * eviction for d3cold. 203d87c424aSRodrigo Vivi * TODO: Check HDA audio dependencies claimed by i915, and then enforce 204d87c424aSRodrigo Vivi * this option to integrated graphics as well. 205d87c424aSRodrigo Vivi */ 206d87c424aSRodrigo Vivi if (IS_DGFX(xe)) 207d87c424aSRodrigo Vivi dev_pm_set_driver_flags(dev, DPM_FLAG_NO_DIRECT_COMPLETE); 208d87c424aSRodrigo Vivi 209dd08ebf6SMatthew Brost pm_runtime_use_autosuspend(dev); 210dd08ebf6SMatthew Brost pm_runtime_set_autosuspend_delay(dev, 1000); 211dd08ebf6SMatthew Brost pm_runtime_set_active(dev); 212dd08ebf6SMatthew Brost pm_runtime_allow(dev); 213dd08ebf6SMatthew Brost pm_runtime_mark_last_busy(dev); 214bba2ec41SRodrigo Vivi pm_runtime_put(dev); 215dd08ebf6SMatthew Brost } 216dd08ebf6SMatthew Brost 217c086bfc6SHimal Prasad Ghimiray int xe_pm_init_early(struct xe_device *xe) 218fa78e188SBadal Nilawar { 219c086bfc6SHimal Prasad Ghimiray int err; 220c086bfc6SHimal Prasad Ghimiray 221fa78e188SBadal Nilawar INIT_LIST_HEAD(&xe->mem_access.vram_userfault.list); 222c086bfc6SHimal Prasad Ghimiray 223c086bfc6SHimal Prasad Ghimiray err = drmm_mutex_init(&xe->drm, &xe->mem_access.vram_userfault.lock); 224c086bfc6SHimal Prasad Ghimiray if (err) 225c086bfc6SHimal Prasad Ghimiray return err; 226c086bfc6SHimal Prasad Ghimiray 227c086bfc6SHimal Prasad Ghimiray err = drmm_mutex_init(&xe->drm, &xe->d3cold.lock); 228c086bfc6SHimal Prasad Ghimiray if (err) 229c086bfc6SHimal Prasad Ghimiray return err; 230c086bfc6SHimal Prasad Ghimiray 231c086bfc6SHimal Prasad Ghimiray return 0; 232fa78e188SBadal Nilawar } 233fa78e188SBadal Nilawar 23430c39952SRodrigo Vivi /** 23530c39952SRodrigo Vivi * xe_pm_init - Initialize Xe Power Management 23630c39952SRodrigo Vivi * @xe: xe device instance 23730c39952SRodrigo Vivi * 23830c39952SRodrigo Vivi * This component is responsible for System and Device sleep states. 239c086bfc6SHimal Prasad Ghimiray * 240c086bfc6SHimal Prasad Ghimiray * Returns 0 for success, negative error code otherwise. 24130c39952SRodrigo Vivi */ 242c086bfc6SHimal Prasad Ghimiray int xe_pm_init(struct xe_device *xe) 243ac0be3b5SAnshuman Gupta { 244c086bfc6SHimal Prasad Ghimiray int err; 245c086bfc6SHimal Prasad Ghimiray 2465349bb76SOhad Sharabi /* For now suspend/resume is only allowed with GuC */ 2475349bb76SOhad Sharabi if (!xe_device_uc_enabled(xe)) 248c086bfc6SHimal Prasad Ghimiray return 0; 249a32d82b4SRodrigo Vivi 25095ec8c1dSRiana Tauro xe->d3cold.capable = xe_pm_pci_d3cold_capable(xe); 2513d4b0bfcSAnshuman Gupta 2523d4b0bfcSAnshuman Gupta if (xe->d3cold.capable) { 253c086bfc6SHimal Prasad Ghimiray err = xe_device_sysfs_init(xe); 254c086bfc6SHimal Prasad Ghimiray if (err) 255c086bfc6SHimal Prasad Ghimiray return err; 256c086bfc6SHimal Prasad Ghimiray 257c086bfc6SHimal Prasad Ghimiray err = xe_pm_set_vram_threshold(xe, DEFAULT_VRAM_THRESHOLD); 258c086bfc6SHimal Prasad Ghimiray if (err) 259c086bfc6SHimal Prasad Ghimiray return err; 2603d4b0bfcSAnshuman Gupta } 261a32d82b4SRodrigo Vivi 262a32d82b4SRodrigo Vivi xe_pm_runtime_init(xe); 263c086bfc6SHimal Prasad Ghimiray 264c086bfc6SHimal Prasad Ghimiray return 0; 265ac0be3b5SAnshuman Gupta } 266ac0be3b5SAnshuman Gupta 26730c39952SRodrigo Vivi /** 26830c39952SRodrigo Vivi * xe_pm_runtime_fini - Finalize Runtime PM 26930c39952SRodrigo Vivi * @xe: xe device instance 27030c39952SRodrigo Vivi */ 2715b7e50e2SMatthew Auld void xe_pm_runtime_fini(struct xe_device *xe) 2725b7e50e2SMatthew Auld { 2735b7e50e2SMatthew Auld struct device *dev = xe->drm.dev; 2745b7e50e2SMatthew Auld 2755b7e50e2SMatthew Auld pm_runtime_get_sync(dev); 2765b7e50e2SMatthew Auld pm_runtime_forbid(dev); 2775b7e50e2SMatthew Auld } 2785b7e50e2SMatthew Auld 279a00b8f1aSMatthew Auld static void xe_pm_write_callback_task(struct xe_device *xe, 280a00b8f1aSMatthew Auld struct task_struct *task) 281a00b8f1aSMatthew Auld { 282a00b8f1aSMatthew Auld WRITE_ONCE(xe->pm_callback_task, task); 283a00b8f1aSMatthew Auld 284a00b8f1aSMatthew Auld /* 285a00b8f1aSMatthew Auld * Just in case it's somehow possible for our writes to be reordered to 286a00b8f1aSMatthew Auld * the extent that something else re-uses the task written in 287a00b8f1aSMatthew Auld * pm_callback_task. For example after returning from the callback, but 288a00b8f1aSMatthew Auld * before the reordered write that resets pm_callback_task back to NULL. 289a00b8f1aSMatthew Auld */ 290a00b8f1aSMatthew Auld smp_mb(); /* pairs with xe_pm_read_callback_task */ 291a00b8f1aSMatthew Auld } 292a00b8f1aSMatthew Auld 293a00b8f1aSMatthew Auld struct task_struct *xe_pm_read_callback_task(struct xe_device *xe) 294a00b8f1aSMatthew Auld { 295a00b8f1aSMatthew Auld smp_mb(); /* pairs with xe_pm_write_callback_task */ 296a00b8f1aSMatthew Auld 297a00b8f1aSMatthew Auld return READ_ONCE(xe->pm_callback_task); 298a00b8f1aSMatthew Auld } 299a00b8f1aSMatthew Auld 30030c39952SRodrigo Vivi /** 3010f9d886fSRodrigo Vivi * xe_pm_runtime_suspended - Check if runtime_pm state is suspended 3020f9d886fSRodrigo Vivi * @xe: xe device instance 3030f9d886fSRodrigo Vivi * 3040f9d886fSRodrigo Vivi * This does not provide any guarantee that the device is going to remain 3050f9d886fSRodrigo Vivi * suspended as it might be racing with the runtime state transitions. 3060f9d886fSRodrigo Vivi * It can be used only as a non-reliable assertion, to ensure that we are not in 3070f9d886fSRodrigo Vivi * the sleep state while trying to access some memory for instance. 3080f9d886fSRodrigo Vivi * 3090f9d886fSRodrigo Vivi * Returns true if PCI device is suspended, false otherwise. 3100f9d886fSRodrigo Vivi */ 3110f9d886fSRodrigo Vivi bool xe_pm_runtime_suspended(struct xe_device *xe) 3120f9d886fSRodrigo Vivi { 3130f9d886fSRodrigo Vivi return pm_runtime_suspended(xe->drm.dev); 3140f9d886fSRodrigo Vivi } 3150f9d886fSRodrigo Vivi 3160f9d886fSRodrigo Vivi /** 31730c39952SRodrigo Vivi * xe_pm_runtime_suspend - Prepare our device for D3hot/D3Cold 31830c39952SRodrigo Vivi * @xe: xe device instance 31930c39952SRodrigo Vivi * 32030c39952SRodrigo Vivi * Returns 0 for success, negative error code otherwise. 32130c39952SRodrigo Vivi */ 322dd08ebf6SMatthew Brost int xe_pm_runtime_suspend(struct xe_device *xe) 323dd08ebf6SMatthew Brost { 324fa78e188SBadal Nilawar struct xe_bo *bo, *on; 325dd08ebf6SMatthew Brost struct xe_gt *gt; 326dd08ebf6SMatthew Brost u8 id; 327a00b8f1aSMatthew Auld int err = 0; 328dd08ebf6SMatthew Brost 329a00b8f1aSMatthew Auld /* Disable access_ongoing asserts and prevent recursive pm calls */ 330a00b8f1aSMatthew Auld xe_pm_write_callback_task(xe, current); 331a00b8f1aSMatthew Auld 3329700a1dfSMatthew Auld /* 3338ae84a27SRodrigo Vivi * The actual xe_pm_runtime_put() is always async underneath, so 3349700a1dfSMatthew Auld * exactly where that is called should makes no difference to us. However 3359700a1dfSMatthew Auld * we still need to be very careful with the locks that this callback 3369700a1dfSMatthew Auld * acquires and the locks that are acquired and held by any callers of 3378ae84a27SRodrigo Vivi * xe_runtime_pm_get(). We already have the matching annotation 3389700a1dfSMatthew Auld * on that side, but we also need it here. For example lockdep should be 3399700a1dfSMatthew Auld * able to tell us if the following scenario is in theory possible: 3409700a1dfSMatthew Auld * 3419700a1dfSMatthew Auld * CPU0 | CPU1 (kworker) 3429700a1dfSMatthew Auld * lock(A) | 3439700a1dfSMatthew Auld * | xe_pm_runtime_suspend() 3449700a1dfSMatthew Auld * | lock(A) 3458ae84a27SRodrigo Vivi * xe_pm_runtime_get() | 3469700a1dfSMatthew Auld * 3479700a1dfSMatthew Auld * This will clearly deadlock since rpm core needs to wait for 3489700a1dfSMatthew Auld * xe_pm_runtime_suspend() to complete, but here we are holding lock(A) 3499700a1dfSMatthew Auld * on CPU0 which prevents CPU1 making forward progress. With the 3508ae84a27SRodrigo Vivi * annotation here and in xe_pm_runtime_get() lockdep will see 3519700a1dfSMatthew Auld * the potential lock inversion and give us a nice splat. 3529700a1dfSMatthew Auld */ 3538ae84a27SRodrigo Vivi lock_map_acquire(&xe_pm_runtime_lockdep_map); 3549700a1dfSMatthew Auld 355fa78e188SBadal Nilawar /* 356fa78e188SBadal Nilawar * Applying lock for entire list op as xe_ttm_bo_destroy and xe_bo_move_notify 357fa78e188SBadal Nilawar * also checks and delets bo entry from user fault list. 358fa78e188SBadal Nilawar */ 359fa78e188SBadal Nilawar mutex_lock(&xe->mem_access.vram_userfault.lock); 360fa78e188SBadal Nilawar list_for_each_entry_safe(bo, on, 361fa78e188SBadal Nilawar &xe->mem_access.vram_userfault.list, vram_userfault_link) 362fa78e188SBadal Nilawar xe_bo_runtime_pm_release_mmap_offset(bo); 363fa78e188SBadal Nilawar mutex_unlock(&xe->mem_access.vram_userfault.lock); 364fa78e188SBadal Nilawar 365a00b8f1aSMatthew Auld if (xe->d3cold.allowed) { 366dd08ebf6SMatthew Brost err = xe_bo_evict_all(xe); 367dd08ebf6SMatthew Brost if (err) 368a00b8f1aSMatthew Auld goto out; 369dd08ebf6SMatthew Brost } 370dd08ebf6SMatthew Brost 371dd08ebf6SMatthew Brost for_each_gt(gt, xe, id) { 372dd08ebf6SMatthew Brost err = xe_gt_suspend(gt); 373dd08ebf6SMatthew Brost if (err) 374a00b8f1aSMatthew Auld goto out; 375dd08ebf6SMatthew Brost } 376dd08ebf6SMatthew Brost 377dd08ebf6SMatthew Brost xe_irq_suspend(xe); 378a00b8f1aSMatthew Auld out: 3798ae84a27SRodrigo Vivi lock_map_release(&xe_pm_runtime_lockdep_map); 380a00b8f1aSMatthew Auld xe_pm_write_callback_task(xe, NULL); 381a00b8f1aSMatthew Auld return err; 382dd08ebf6SMatthew Brost } 383dd08ebf6SMatthew Brost 38430c39952SRodrigo Vivi /** 38530c39952SRodrigo Vivi * xe_pm_runtime_resume - Waking up from D3hot/D3Cold 38630c39952SRodrigo Vivi * @xe: xe device instance 38730c39952SRodrigo Vivi * 38830c39952SRodrigo Vivi * Returns 0 for success, negative error code otherwise. 38930c39952SRodrigo Vivi */ 390dd08ebf6SMatthew Brost int xe_pm_runtime_resume(struct xe_device *xe) 391dd08ebf6SMatthew Brost { 392dd08ebf6SMatthew Brost struct xe_gt *gt; 393dd08ebf6SMatthew Brost u8 id; 394a00b8f1aSMatthew Auld int err = 0; 395a00b8f1aSMatthew Auld 396a00b8f1aSMatthew Auld /* Disable access_ongoing asserts and prevent recursive pm calls */ 397a00b8f1aSMatthew Auld xe_pm_write_callback_task(xe, current); 398dd08ebf6SMatthew Brost 3998ae84a27SRodrigo Vivi lock_map_acquire(&xe_pm_runtime_lockdep_map); 4009700a1dfSMatthew Auld 40109d88e3bSAnshuman Gupta /* 40209d88e3bSAnshuman Gupta * It can be possible that xe has allowed d3cold but other pcie devices 40309d88e3bSAnshuman Gupta * in gfx card soc would have blocked d3cold, therefore card has not 40409d88e3bSAnshuman Gupta * really lost power. Detecting primary Gt power is sufficient. 40509d88e3bSAnshuman Gupta */ 40609d88e3bSAnshuman Gupta gt = xe_device_get_gt(xe, 0); 40709d88e3bSAnshuman Gupta xe->d3cold.power_lost = xe_guc_in_reset(>->uc.guc); 40809d88e3bSAnshuman Gupta 40909d88e3bSAnshuman Gupta if (xe->d3cold.allowed && xe->d3cold.power_lost) { 410933fd5ffSRiana Tauro err = xe_pcode_ready(xe, true); 411dd08ebf6SMatthew Brost if (err) 412a00b8f1aSMatthew Auld goto out; 413dd08ebf6SMatthew Brost 414dd08ebf6SMatthew Brost /* 415dd08ebf6SMatthew Brost * This only restores pinned memory which is the memory 416dd08ebf6SMatthew Brost * required for the GT(s) to resume. 417dd08ebf6SMatthew Brost */ 418dd08ebf6SMatthew Brost err = xe_bo_restore_kernel(xe); 419dd08ebf6SMatthew Brost if (err) 420a00b8f1aSMatthew Auld goto out; 421dd08ebf6SMatthew Brost } 422dd08ebf6SMatthew Brost 423dd08ebf6SMatthew Brost xe_irq_resume(xe); 424dd08ebf6SMatthew Brost 425dd08ebf6SMatthew Brost for_each_gt(gt, xe, id) 426dd08ebf6SMatthew Brost xe_gt_resume(gt); 427dd08ebf6SMatthew Brost 42809d88e3bSAnshuman Gupta if (xe->d3cold.allowed && xe->d3cold.power_lost) { 429dd08ebf6SMatthew Brost err = xe_bo_restore_user(xe); 430dd08ebf6SMatthew Brost if (err) 431a00b8f1aSMatthew Auld goto out; 432dd08ebf6SMatthew Brost } 433a00b8f1aSMatthew Auld out: 4348ae84a27SRodrigo Vivi lock_map_release(&xe_pm_runtime_lockdep_map); 435a00b8f1aSMatthew Auld xe_pm_write_callback_task(xe, NULL); 436a00b8f1aSMatthew Auld return err; 437dd08ebf6SMatthew Brost } 438dd08ebf6SMatthew Brost 4398ae84a27SRodrigo Vivi /* 4408ae84a27SRodrigo Vivi * For places where resume is synchronous it can be quite easy to deadlock 4418ae84a27SRodrigo Vivi * if we are not careful. Also in practice it might be quite timing 4428ae84a27SRodrigo Vivi * sensitive to ever see the 0 -> 1 transition with the callers locks 4438ae84a27SRodrigo Vivi * held, so deadlocks might exist but are hard for lockdep to ever see. 4448ae84a27SRodrigo Vivi * With this in mind, help lockdep learn about the potentially scary 4458ae84a27SRodrigo Vivi * stuff that can happen inside the runtime_resume callback by acquiring 4468ae84a27SRodrigo Vivi * a dummy lock (it doesn't protect anything and gets compiled out on 4478ae84a27SRodrigo Vivi * non-debug builds). Lockdep then only needs to see the 4488ae84a27SRodrigo Vivi * xe_pm_runtime_lockdep_map -> runtime_resume callback once, and then can 4498ae84a27SRodrigo Vivi * hopefully validate all the (callers_locks) -> xe_pm_runtime_lockdep_map. 4508ae84a27SRodrigo Vivi * For example if the (callers_locks) are ever grabbed in the 4518ae84a27SRodrigo Vivi * runtime_resume callback, lockdep should give us a nice splat. 4528ae84a27SRodrigo Vivi */ 4538ae84a27SRodrigo Vivi static void pm_runtime_lockdep_prime(void) 4548ae84a27SRodrigo Vivi { 4558ae84a27SRodrigo Vivi lock_map_acquire(&xe_pm_runtime_lockdep_map); 4568ae84a27SRodrigo Vivi lock_map_release(&xe_pm_runtime_lockdep_map); 4578ae84a27SRodrigo Vivi } 4588ae84a27SRodrigo Vivi 45930c39952SRodrigo Vivi /** 46030c39952SRodrigo Vivi * xe_pm_runtime_get - Get a runtime_pm reference and resume synchronously 46130c39952SRodrigo Vivi * @xe: xe device instance 46230c39952SRodrigo Vivi */ 4635c9da9fcSRodrigo Vivi void xe_pm_runtime_get(struct xe_device *xe) 464dd08ebf6SMatthew Brost { 4655c9da9fcSRodrigo Vivi pm_runtime_get_noresume(xe->drm.dev); 4665c9da9fcSRodrigo Vivi 4675c9da9fcSRodrigo Vivi if (xe_pm_read_callback_task(xe) == current) 4685c9da9fcSRodrigo Vivi return; 4695c9da9fcSRodrigo Vivi 4708ae84a27SRodrigo Vivi pm_runtime_lockdep_prime(); 4715c9da9fcSRodrigo Vivi pm_runtime_resume(xe->drm.dev); 472dd08ebf6SMatthew Brost } 473dd08ebf6SMatthew Brost 47430c39952SRodrigo Vivi /** 47530c39952SRodrigo Vivi * xe_pm_runtime_put - Put the runtime_pm reference back and mark as idle 47630c39952SRodrigo Vivi * @xe: xe device instance 47730c39952SRodrigo Vivi */ 4785c9da9fcSRodrigo Vivi void xe_pm_runtime_put(struct xe_device *xe) 479dd08ebf6SMatthew Brost { 4805c9da9fcSRodrigo Vivi if (xe_pm_read_callback_task(xe) == current) { 4815c9da9fcSRodrigo Vivi pm_runtime_put_noidle(xe->drm.dev); 4825c9da9fcSRodrigo Vivi } else { 483dd08ebf6SMatthew Brost pm_runtime_mark_last_busy(xe->drm.dev); 4845c9da9fcSRodrigo Vivi pm_runtime_put(xe->drm.dev); 4855c9da9fcSRodrigo Vivi } 486dd08ebf6SMatthew Brost } 487dd08ebf6SMatthew Brost 48830c39952SRodrigo Vivi /** 48923cf006bSRodrigo Vivi * xe_pm_runtime_get_ioctl - Get a runtime_pm reference before ioctl 49023cf006bSRodrigo Vivi * @xe: xe device instance 49123cf006bSRodrigo Vivi * 49223cf006bSRodrigo Vivi * Returns: Any number greater than or equal to 0 for success, negative error 49323cf006bSRodrigo Vivi * code otherwise. 49423cf006bSRodrigo Vivi */ 49523cf006bSRodrigo Vivi int xe_pm_runtime_get_ioctl(struct xe_device *xe) 49623cf006bSRodrigo Vivi { 49723cf006bSRodrigo Vivi if (WARN_ON(xe_pm_read_callback_task(xe) == current)) 49823cf006bSRodrigo Vivi return -ELOOP; 49923cf006bSRodrigo Vivi 5008ae84a27SRodrigo Vivi pm_runtime_lockdep_prime(); 50123cf006bSRodrigo Vivi return pm_runtime_get_sync(xe->drm.dev); 50223cf006bSRodrigo Vivi } 50323cf006bSRodrigo Vivi 50423cf006bSRodrigo Vivi /** 50530c39952SRodrigo Vivi * xe_pm_runtime_get_if_active - Get a runtime_pm reference if device active 50630c39952SRodrigo Vivi * @xe: xe device instance 50730c39952SRodrigo Vivi * 508*46edb0a3SRodrigo Vivi * Return: True if device is awake (regardless the previous number of references) 509*46edb0a3SRodrigo Vivi * and a new reference was taken, false otherwise. 51030c39952SRodrigo Vivi */ 511*46edb0a3SRodrigo Vivi bool xe_pm_runtime_get_if_active(struct xe_device *xe) 512dd08ebf6SMatthew Brost { 513*46edb0a3SRodrigo Vivi return pm_runtime_get_if_active(xe->drm.dev) > 0; 514dd08ebf6SMatthew Brost } 515c8a74077SAnshuman Gupta 51630c39952SRodrigo Vivi /** 5173b85b7bcSRodrigo Vivi * xe_pm_runtime_get_if_in_use - Get a runtime_pm reference and resume if needed 5183b85b7bcSRodrigo Vivi * @xe: xe device instance 5193b85b7bcSRodrigo Vivi * 5203b85b7bcSRodrigo Vivi * Returns: True if device is awake and the reference was taken, false otherwise. 5213b85b7bcSRodrigo Vivi */ 5223b85b7bcSRodrigo Vivi bool xe_pm_runtime_get_if_in_use(struct xe_device *xe) 5233b85b7bcSRodrigo Vivi { 5243b85b7bcSRodrigo Vivi if (xe_pm_read_callback_task(xe) == current) { 5253b85b7bcSRodrigo Vivi /* The device is awake, grab the ref and move on */ 5263b85b7bcSRodrigo Vivi pm_runtime_get_noresume(xe->drm.dev); 5273b85b7bcSRodrigo Vivi return true; 5283b85b7bcSRodrigo Vivi } 5293b85b7bcSRodrigo Vivi 5303b85b7bcSRodrigo Vivi return pm_runtime_get_if_in_use(xe->drm.dev) > 0; 5313b85b7bcSRodrigo Vivi } 5323b85b7bcSRodrigo Vivi 5333b85b7bcSRodrigo Vivi /** 534cbb6a741SRodrigo Vivi * xe_pm_runtime_get_noresume - Bump runtime PM usage counter without resuming 535cbb6a741SRodrigo Vivi * @xe: xe device instance 536cbb6a741SRodrigo Vivi * 537cbb6a741SRodrigo Vivi * This function should be used in inner places where it is surely already 538cbb6a741SRodrigo Vivi * protected by outer-bound callers of `xe_pm_runtime_get`. 539cbb6a741SRodrigo Vivi * It will warn if not protected. 540cbb6a741SRodrigo Vivi * The reference should be put back after this function regardless, since it 541cbb6a741SRodrigo Vivi * will always bump the usage counter, regardless. 542cbb6a741SRodrigo Vivi */ 543cbb6a741SRodrigo Vivi void xe_pm_runtime_get_noresume(struct xe_device *xe) 544cbb6a741SRodrigo Vivi { 545cbb6a741SRodrigo Vivi bool ref; 546cbb6a741SRodrigo Vivi 547cbb6a741SRodrigo Vivi ref = xe_pm_runtime_get_if_in_use(xe); 548cbb6a741SRodrigo Vivi 549cbb6a741SRodrigo Vivi if (drm_WARN(&xe->drm, !ref, "Missing outer runtime PM protection\n")) 550cbb6a741SRodrigo Vivi pm_runtime_get_noresume(xe->drm.dev); 551cbb6a741SRodrigo Vivi } 552cbb6a741SRodrigo Vivi 553cbb6a741SRodrigo Vivi /** 554d6b41378SRodrigo Vivi * xe_pm_runtime_resume_and_get - Resume, then get a runtime_pm ref if awake. 555d6b41378SRodrigo Vivi * @xe: xe device instance 556d6b41378SRodrigo Vivi * 557d6b41378SRodrigo Vivi * Returns: True if device is awake and the reference was taken, false otherwise. 558d6b41378SRodrigo Vivi */ 559d6b41378SRodrigo Vivi bool xe_pm_runtime_resume_and_get(struct xe_device *xe) 560d6b41378SRodrigo Vivi { 561d6b41378SRodrigo Vivi if (xe_pm_read_callback_task(xe) == current) { 562d6b41378SRodrigo Vivi /* The device is awake, grab the ref and move on */ 563d6b41378SRodrigo Vivi pm_runtime_get_noresume(xe->drm.dev); 564d6b41378SRodrigo Vivi return true; 565d6b41378SRodrigo Vivi } 566d6b41378SRodrigo Vivi 5678ae84a27SRodrigo Vivi pm_runtime_lockdep_prime(); 568d6b41378SRodrigo Vivi return pm_runtime_resume_and_get(xe->drm.dev) >= 0; 569d6b41378SRodrigo Vivi } 570d6b41378SRodrigo Vivi 571d6b41378SRodrigo Vivi /** 57230c39952SRodrigo Vivi * xe_pm_assert_unbounded_bridge - Disable PM on unbounded pcie parent bridge 57330c39952SRodrigo Vivi * @xe: xe device instance 57430c39952SRodrigo Vivi */ 575c8a74077SAnshuman Gupta void xe_pm_assert_unbounded_bridge(struct xe_device *xe) 576c8a74077SAnshuman Gupta { 577c8a74077SAnshuman Gupta struct pci_dev *pdev = to_pci_dev(xe->drm.dev); 578c8a74077SAnshuman Gupta struct pci_dev *bridge = pci_upstream_bridge(pdev); 579c8a74077SAnshuman Gupta 580c8a74077SAnshuman Gupta if (!bridge) 581c8a74077SAnshuman Gupta return; 582c8a74077SAnshuman Gupta 583c8a74077SAnshuman Gupta if (!bridge->driver) { 584c8a74077SAnshuman Gupta drm_warn(&xe->drm, "unbounded parent pci bridge, device won't support any PM support.\n"); 585c8a74077SAnshuman Gupta device_set_pm_not_required(&pdev->dev); 586c8a74077SAnshuman Gupta } 587c8a74077SAnshuman Gupta } 588b2d75619SAnshuman Gupta 58930c39952SRodrigo Vivi /** 59030c39952SRodrigo Vivi * xe_pm_set_vram_threshold - Set a vram threshold for allowing/blocking D3Cold 59130c39952SRodrigo Vivi * @xe: xe device instance 59230c39952SRodrigo Vivi * @threshold: VRAM size in bites for the D3cold threshold 59330c39952SRodrigo Vivi * 59430c39952SRodrigo Vivi * Returns 0 for success, negative error code otherwise. 59530c39952SRodrigo Vivi */ 596b2d75619SAnshuman Gupta int xe_pm_set_vram_threshold(struct xe_device *xe, u32 threshold) 597b2d75619SAnshuman Gupta { 598b2d75619SAnshuman Gupta struct ttm_resource_manager *man; 599b2d75619SAnshuman Gupta u32 vram_total_mb = 0; 600b2d75619SAnshuman Gupta int i; 601b2d75619SAnshuman Gupta 602b2d75619SAnshuman Gupta for (i = XE_PL_VRAM0; i <= XE_PL_VRAM1; ++i) { 603b2d75619SAnshuman Gupta man = ttm_manager_type(&xe->ttm, i); 604b2d75619SAnshuman Gupta if (man) 605b2d75619SAnshuman Gupta vram_total_mb += DIV_ROUND_UP_ULL(man->size, 1024 * 1024); 606b2d75619SAnshuman Gupta } 607b2d75619SAnshuman Gupta 608b2d75619SAnshuman Gupta drm_dbg(&xe->drm, "Total vram %u mb\n", vram_total_mb); 609b2d75619SAnshuman Gupta 610b2d75619SAnshuman Gupta if (threshold > vram_total_mb) 611b2d75619SAnshuman Gupta return -EINVAL; 612b2d75619SAnshuman Gupta 613b2d75619SAnshuman Gupta mutex_lock(&xe->d3cold.lock); 614b2d75619SAnshuman Gupta xe->d3cold.vram_threshold = threshold; 615b2d75619SAnshuman Gupta mutex_unlock(&xe->d3cold.lock); 616b2d75619SAnshuman Gupta 617b2d75619SAnshuman Gupta return 0; 618b2d75619SAnshuman Gupta } 6192ef08b98SAnshuman Gupta 62030c39952SRodrigo Vivi /** 62130c39952SRodrigo Vivi * xe_pm_d3cold_allowed_toggle - Check conditions to toggle d3cold.allowed 62230c39952SRodrigo Vivi * @xe: xe device instance 62330c39952SRodrigo Vivi * 62430c39952SRodrigo Vivi * To be called during runtime_pm idle callback. 62530c39952SRodrigo Vivi * Check for all the D3Cold conditions ahead of runtime suspend. 62630c39952SRodrigo Vivi */ 6272ef08b98SAnshuman Gupta void xe_pm_d3cold_allowed_toggle(struct xe_device *xe) 6282ef08b98SAnshuman Gupta { 6292ef08b98SAnshuman Gupta struct ttm_resource_manager *man; 6302ef08b98SAnshuman Gupta u32 total_vram_used_mb = 0; 6312ef08b98SAnshuman Gupta u64 vram_used; 6322ef08b98SAnshuman Gupta int i; 6332ef08b98SAnshuman Gupta 634e07aa913SRodrigo Vivi if (!xe->d3cold.capable) { 635e07aa913SRodrigo Vivi xe->d3cold.allowed = false; 636e07aa913SRodrigo Vivi return; 637e07aa913SRodrigo Vivi } 638e07aa913SRodrigo Vivi 6392ef08b98SAnshuman Gupta for (i = XE_PL_VRAM0; i <= XE_PL_VRAM1; ++i) { 6402ef08b98SAnshuman Gupta man = ttm_manager_type(&xe->ttm, i); 6412ef08b98SAnshuman Gupta if (man) { 6422ef08b98SAnshuman Gupta vram_used = ttm_resource_manager_usage(man); 6432ef08b98SAnshuman Gupta total_vram_used_mb += DIV_ROUND_UP_ULL(vram_used, 1024 * 1024); 6442ef08b98SAnshuman Gupta } 6452ef08b98SAnshuman Gupta } 6462ef08b98SAnshuman Gupta 6472ef08b98SAnshuman Gupta mutex_lock(&xe->d3cold.lock); 6482ef08b98SAnshuman Gupta 6492ef08b98SAnshuman Gupta if (total_vram_used_mb < xe->d3cold.vram_threshold) 6502ef08b98SAnshuman Gupta xe->d3cold.allowed = true; 6512ef08b98SAnshuman Gupta else 6522ef08b98SAnshuman Gupta xe->d3cold.allowed = false; 6532ef08b98SAnshuman Gupta 6542ef08b98SAnshuman Gupta mutex_unlock(&xe->d3cold.lock); 655ff765b77SMatthew Auld 656ff765b77SMatthew Auld drm_dbg(&xe->drm, 657ff765b77SMatthew Auld "d3cold: allowed=%s\n", str_yes_no(xe->d3cold.allowed)); 6582ef08b98SAnshuman Gupta } 659