xref: /linux/drivers/gpu/drm/xe/xe_pm.c (revision e7b180b22022f52e3f5fca695cc75d63bddc5a1c)
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 
99*e7b180b2SRodrigo Vivi 	xe_display_pm_suspend(xe, false);
10044e69495SMaarten Lankhorst 
101dd08ebf6SMatthew Brost 	for_each_gt(gt, xe, id) {
102dd08ebf6SMatthew Brost 		err = xe_gt_suspend(gt);
10344e69495SMaarten Lankhorst 		if (err) {
104*e7b180b2SRodrigo Vivi 			xe_display_pm_resume(xe, false);
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 
154*e7b180b2SRodrigo Vivi 	xe_display_pm_resume(xe, false);
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;
369*e7b180b2SRodrigo Vivi 		xe_display_pm_suspend(xe, true);
370dd08ebf6SMatthew Brost 	}
371dd08ebf6SMatthew Brost 
372dd08ebf6SMatthew Brost 	for_each_gt(gt, xe, id) {
373dd08ebf6SMatthew Brost 		err = xe_gt_suspend(gt);
374dd08ebf6SMatthew Brost 		if (err)
375a00b8f1aSMatthew Auld 			goto out;
376dd08ebf6SMatthew Brost 	}
377dd08ebf6SMatthew Brost 
378dd08ebf6SMatthew Brost 	xe_irq_suspend(xe);
379*e7b180b2SRodrigo Vivi 
380*e7b180b2SRodrigo Vivi 	if (xe->d3cold.allowed)
381*e7b180b2SRodrigo Vivi 		xe_display_pm_suspend_late(xe);
382a00b8f1aSMatthew Auld out:
383*e7b180b2SRodrigo Vivi 	if (err)
384*e7b180b2SRodrigo Vivi 		xe_display_pm_resume(xe, true);
3858ae84a27SRodrigo Vivi 	lock_map_release(&xe_pm_runtime_lockdep_map);
386a00b8f1aSMatthew Auld 	xe_pm_write_callback_task(xe, NULL);
387a00b8f1aSMatthew Auld 	return err;
388dd08ebf6SMatthew Brost }
389dd08ebf6SMatthew Brost 
39030c39952SRodrigo Vivi /**
39130c39952SRodrigo Vivi  * xe_pm_runtime_resume - Waking up from D3hot/D3Cold
39230c39952SRodrigo Vivi  * @xe: xe device instance
39330c39952SRodrigo Vivi  *
39430c39952SRodrigo Vivi  * Returns 0 for success, negative error code otherwise.
39530c39952SRodrigo Vivi  */
396dd08ebf6SMatthew Brost int xe_pm_runtime_resume(struct xe_device *xe)
397dd08ebf6SMatthew Brost {
398dd08ebf6SMatthew Brost 	struct xe_gt *gt;
399dd08ebf6SMatthew Brost 	u8 id;
400a00b8f1aSMatthew Auld 	int err = 0;
401a00b8f1aSMatthew Auld 
402a00b8f1aSMatthew Auld 	/* Disable access_ongoing asserts and prevent recursive pm calls */
403a00b8f1aSMatthew Auld 	xe_pm_write_callback_task(xe, current);
404dd08ebf6SMatthew Brost 
4058ae84a27SRodrigo Vivi 	lock_map_acquire(&xe_pm_runtime_lockdep_map);
4069700a1dfSMatthew Auld 
40709d88e3bSAnshuman Gupta 	/*
40809d88e3bSAnshuman Gupta 	 * It can be possible that xe has allowed d3cold but other pcie devices
40909d88e3bSAnshuman Gupta 	 * in gfx card soc would have blocked d3cold, therefore card has not
41009d88e3bSAnshuman Gupta 	 * really lost power. Detecting primary Gt power is sufficient.
41109d88e3bSAnshuman Gupta 	 */
41209d88e3bSAnshuman Gupta 	gt = xe_device_get_gt(xe, 0);
41309d88e3bSAnshuman Gupta 	xe->d3cold.power_lost = xe_guc_in_reset(&gt->uc.guc);
41409d88e3bSAnshuman Gupta 
41509d88e3bSAnshuman Gupta 	if (xe->d3cold.allowed && xe->d3cold.power_lost) {
416933fd5ffSRiana Tauro 		err = xe_pcode_ready(xe, true);
417dd08ebf6SMatthew Brost 		if (err)
418a00b8f1aSMatthew Auld 			goto out;
419dd08ebf6SMatthew Brost 
420*e7b180b2SRodrigo Vivi 		xe_display_pm_resume_early(xe);
421*e7b180b2SRodrigo Vivi 
422dd08ebf6SMatthew Brost 		/*
423dd08ebf6SMatthew Brost 		 * This only restores pinned memory which is the memory
424dd08ebf6SMatthew Brost 		 * required for the GT(s) to resume.
425dd08ebf6SMatthew Brost 		 */
426dd08ebf6SMatthew Brost 		err = xe_bo_restore_kernel(xe);
427dd08ebf6SMatthew Brost 		if (err)
428a00b8f1aSMatthew Auld 			goto out;
429dd08ebf6SMatthew Brost 	}
430dd08ebf6SMatthew Brost 
431dd08ebf6SMatthew Brost 	xe_irq_resume(xe);
432dd08ebf6SMatthew Brost 
433dd08ebf6SMatthew Brost 	for_each_gt(gt, xe, id)
434dd08ebf6SMatthew Brost 		xe_gt_resume(gt);
435dd08ebf6SMatthew Brost 
43609d88e3bSAnshuman Gupta 	if (xe->d3cold.allowed && xe->d3cold.power_lost) {
437*e7b180b2SRodrigo Vivi 		xe_display_pm_resume(xe, true);
438dd08ebf6SMatthew Brost 		err = xe_bo_restore_user(xe);
439dd08ebf6SMatthew Brost 		if (err)
440a00b8f1aSMatthew Auld 			goto out;
441dd08ebf6SMatthew Brost 	}
442a00b8f1aSMatthew Auld out:
4438ae84a27SRodrigo Vivi 	lock_map_release(&xe_pm_runtime_lockdep_map);
444a00b8f1aSMatthew Auld 	xe_pm_write_callback_task(xe, NULL);
445a00b8f1aSMatthew Auld 	return err;
446dd08ebf6SMatthew Brost }
447dd08ebf6SMatthew Brost 
4488ae84a27SRodrigo Vivi /*
4498ae84a27SRodrigo Vivi  * For places where resume is synchronous it can be quite easy to deadlock
4508ae84a27SRodrigo Vivi  * if we are not careful. Also in practice it might be quite timing
4518ae84a27SRodrigo Vivi  * sensitive to ever see the 0 -> 1 transition with the callers locks
4528ae84a27SRodrigo Vivi  * held, so deadlocks might exist but are hard for lockdep to ever see.
4538ae84a27SRodrigo Vivi  * With this in mind, help lockdep learn about the potentially scary
4548ae84a27SRodrigo Vivi  * stuff that can happen inside the runtime_resume callback by acquiring
4558ae84a27SRodrigo Vivi  * a dummy lock (it doesn't protect anything and gets compiled out on
4568ae84a27SRodrigo Vivi  * non-debug builds).  Lockdep then only needs to see the
4578ae84a27SRodrigo Vivi  * xe_pm_runtime_lockdep_map -> runtime_resume callback once, and then can
4588ae84a27SRodrigo Vivi  * hopefully validate all the (callers_locks) -> xe_pm_runtime_lockdep_map.
4598ae84a27SRodrigo Vivi  * For example if the (callers_locks) are ever grabbed in the
4608ae84a27SRodrigo Vivi  * runtime_resume callback, lockdep should give us a nice splat.
4618ae84a27SRodrigo Vivi  */
4628ae84a27SRodrigo Vivi static void pm_runtime_lockdep_prime(void)
4638ae84a27SRodrigo Vivi {
4648ae84a27SRodrigo Vivi 	lock_map_acquire(&xe_pm_runtime_lockdep_map);
4658ae84a27SRodrigo Vivi 	lock_map_release(&xe_pm_runtime_lockdep_map);
4668ae84a27SRodrigo Vivi }
4678ae84a27SRodrigo Vivi 
46830c39952SRodrigo Vivi /**
46930c39952SRodrigo Vivi  * xe_pm_runtime_get - Get a runtime_pm reference and resume synchronously
47030c39952SRodrigo Vivi  * @xe: xe device instance
47130c39952SRodrigo Vivi  */
4725c9da9fcSRodrigo Vivi void xe_pm_runtime_get(struct xe_device *xe)
473dd08ebf6SMatthew Brost {
4745c9da9fcSRodrigo Vivi 	pm_runtime_get_noresume(xe->drm.dev);
4755c9da9fcSRodrigo Vivi 
4765c9da9fcSRodrigo Vivi 	if (xe_pm_read_callback_task(xe) == current)
4775c9da9fcSRodrigo Vivi 		return;
4785c9da9fcSRodrigo Vivi 
4798ae84a27SRodrigo Vivi 	pm_runtime_lockdep_prime();
4805c9da9fcSRodrigo Vivi 	pm_runtime_resume(xe->drm.dev);
481dd08ebf6SMatthew Brost }
482dd08ebf6SMatthew Brost 
48330c39952SRodrigo Vivi /**
48430c39952SRodrigo Vivi  * xe_pm_runtime_put - Put the runtime_pm reference back and mark as idle
48530c39952SRodrigo Vivi  * @xe: xe device instance
48630c39952SRodrigo Vivi  */
4875c9da9fcSRodrigo Vivi void xe_pm_runtime_put(struct xe_device *xe)
488dd08ebf6SMatthew Brost {
4895c9da9fcSRodrigo Vivi 	if (xe_pm_read_callback_task(xe) == current) {
4905c9da9fcSRodrigo Vivi 		pm_runtime_put_noidle(xe->drm.dev);
4915c9da9fcSRodrigo Vivi 	} else {
492dd08ebf6SMatthew Brost 		pm_runtime_mark_last_busy(xe->drm.dev);
4935c9da9fcSRodrigo Vivi 		pm_runtime_put(xe->drm.dev);
4945c9da9fcSRodrigo Vivi 	}
495dd08ebf6SMatthew Brost }
496dd08ebf6SMatthew Brost 
49730c39952SRodrigo Vivi /**
49823cf006bSRodrigo Vivi  * xe_pm_runtime_get_ioctl - Get a runtime_pm reference before ioctl
49923cf006bSRodrigo Vivi  * @xe: xe device instance
50023cf006bSRodrigo Vivi  *
50123cf006bSRodrigo Vivi  * Returns: Any number greater than or equal to 0 for success, negative error
50223cf006bSRodrigo Vivi  * code otherwise.
50323cf006bSRodrigo Vivi  */
50423cf006bSRodrigo Vivi int xe_pm_runtime_get_ioctl(struct xe_device *xe)
50523cf006bSRodrigo Vivi {
50623cf006bSRodrigo Vivi 	if (WARN_ON(xe_pm_read_callback_task(xe) == current))
50723cf006bSRodrigo Vivi 		return -ELOOP;
50823cf006bSRodrigo Vivi 
5098ae84a27SRodrigo Vivi 	pm_runtime_lockdep_prime();
51023cf006bSRodrigo Vivi 	return pm_runtime_get_sync(xe->drm.dev);
51123cf006bSRodrigo Vivi }
51223cf006bSRodrigo Vivi 
51323cf006bSRodrigo Vivi /**
51430c39952SRodrigo Vivi  * xe_pm_runtime_get_if_active - Get a runtime_pm reference if device active
51530c39952SRodrigo Vivi  * @xe: xe device instance
51630c39952SRodrigo Vivi  *
51746edb0a3SRodrigo Vivi  * Return: True if device is awake (regardless the previous number of references)
51846edb0a3SRodrigo Vivi  * and a new reference was taken, false otherwise.
51930c39952SRodrigo Vivi  */
52046edb0a3SRodrigo Vivi bool xe_pm_runtime_get_if_active(struct xe_device *xe)
521dd08ebf6SMatthew Brost {
52246edb0a3SRodrigo Vivi 	return pm_runtime_get_if_active(xe->drm.dev) > 0;
523dd08ebf6SMatthew Brost }
524c8a74077SAnshuman Gupta 
52530c39952SRodrigo Vivi /**
526967c5d7cSRodrigo Vivi  * xe_pm_runtime_get_if_in_use - Get a new reference if device is active with previous ref taken
5273b85b7bcSRodrigo Vivi  * @xe: xe device instance
5283b85b7bcSRodrigo Vivi  *
529967c5d7cSRodrigo Vivi  * Return: True if device is awake, a previous reference had been already taken,
530967c5d7cSRodrigo Vivi  * and a new reference was now taken, false otherwise.
5313b85b7bcSRodrigo Vivi  */
5323b85b7bcSRodrigo Vivi bool xe_pm_runtime_get_if_in_use(struct xe_device *xe)
5333b85b7bcSRodrigo Vivi {
5343b85b7bcSRodrigo Vivi 	if (xe_pm_read_callback_task(xe) == current) {
5353b85b7bcSRodrigo Vivi 		/* The device is awake, grab the ref and move on */
5363b85b7bcSRodrigo Vivi 		pm_runtime_get_noresume(xe->drm.dev);
5373b85b7bcSRodrigo Vivi 		return true;
5383b85b7bcSRodrigo Vivi 	}
5393b85b7bcSRodrigo Vivi 
5403b85b7bcSRodrigo Vivi 	return pm_runtime_get_if_in_use(xe->drm.dev) > 0;
5413b85b7bcSRodrigo Vivi }
5423b85b7bcSRodrigo Vivi 
5433b85b7bcSRodrigo Vivi /**
544cbb6a741SRodrigo Vivi  * xe_pm_runtime_get_noresume - Bump runtime PM usage counter without resuming
545cbb6a741SRodrigo Vivi  * @xe: xe device instance
546cbb6a741SRodrigo Vivi  *
547cbb6a741SRodrigo Vivi  * This function should be used in inner places where it is surely already
548cbb6a741SRodrigo Vivi  * protected by outer-bound callers of `xe_pm_runtime_get`.
549cbb6a741SRodrigo Vivi  * It will warn if not protected.
550cbb6a741SRodrigo Vivi  * The reference should be put back after this function regardless, since it
551cbb6a741SRodrigo Vivi  * will always bump the usage counter, regardless.
552cbb6a741SRodrigo Vivi  */
553cbb6a741SRodrigo Vivi void xe_pm_runtime_get_noresume(struct xe_device *xe)
554cbb6a741SRodrigo Vivi {
555cbb6a741SRodrigo Vivi 	bool ref;
556cbb6a741SRodrigo Vivi 
557cbb6a741SRodrigo Vivi 	ref = xe_pm_runtime_get_if_in_use(xe);
558cbb6a741SRodrigo Vivi 
559cbb6a741SRodrigo Vivi 	if (drm_WARN(&xe->drm, !ref, "Missing outer runtime PM protection\n"))
560cbb6a741SRodrigo Vivi 		pm_runtime_get_noresume(xe->drm.dev);
561cbb6a741SRodrigo Vivi }
562cbb6a741SRodrigo Vivi 
563cbb6a741SRodrigo Vivi /**
564d6b41378SRodrigo Vivi  * xe_pm_runtime_resume_and_get - Resume, then get a runtime_pm ref if awake.
565d6b41378SRodrigo Vivi  * @xe: xe device instance
566d6b41378SRodrigo Vivi  *
567d6b41378SRodrigo Vivi  * Returns: True if device is awake and the reference was taken, false otherwise.
568d6b41378SRodrigo Vivi  */
569d6b41378SRodrigo Vivi bool xe_pm_runtime_resume_and_get(struct xe_device *xe)
570d6b41378SRodrigo Vivi {
571d6b41378SRodrigo Vivi 	if (xe_pm_read_callback_task(xe) == current) {
572d6b41378SRodrigo Vivi 		/* The device is awake, grab the ref and move on */
573d6b41378SRodrigo Vivi 		pm_runtime_get_noresume(xe->drm.dev);
574d6b41378SRodrigo Vivi 		return true;
575d6b41378SRodrigo Vivi 	}
576d6b41378SRodrigo Vivi 
5778ae84a27SRodrigo Vivi 	pm_runtime_lockdep_prime();
578d6b41378SRodrigo Vivi 	return pm_runtime_resume_and_get(xe->drm.dev) >= 0;
579d6b41378SRodrigo Vivi }
580d6b41378SRodrigo Vivi 
581d6b41378SRodrigo Vivi /**
58230c39952SRodrigo Vivi  * xe_pm_assert_unbounded_bridge - Disable PM on unbounded pcie parent bridge
58330c39952SRodrigo Vivi  * @xe: xe device instance
58430c39952SRodrigo Vivi  */
585c8a74077SAnshuman Gupta void xe_pm_assert_unbounded_bridge(struct xe_device *xe)
586c8a74077SAnshuman Gupta {
587c8a74077SAnshuman Gupta 	struct pci_dev *pdev = to_pci_dev(xe->drm.dev);
588c8a74077SAnshuman Gupta 	struct pci_dev *bridge = pci_upstream_bridge(pdev);
589c8a74077SAnshuman Gupta 
590c8a74077SAnshuman Gupta 	if (!bridge)
591c8a74077SAnshuman Gupta 		return;
592c8a74077SAnshuman Gupta 
593c8a74077SAnshuman Gupta 	if (!bridge->driver) {
594c8a74077SAnshuman Gupta 		drm_warn(&xe->drm, "unbounded parent pci bridge, device won't support any PM support.\n");
595c8a74077SAnshuman Gupta 		device_set_pm_not_required(&pdev->dev);
596c8a74077SAnshuman Gupta 	}
597c8a74077SAnshuman Gupta }
598b2d75619SAnshuman Gupta 
59930c39952SRodrigo Vivi /**
60030c39952SRodrigo Vivi  * xe_pm_set_vram_threshold - Set a vram threshold for allowing/blocking D3Cold
60130c39952SRodrigo Vivi  * @xe: xe device instance
60230c39952SRodrigo Vivi  * @threshold: VRAM size in bites for the D3cold threshold
60330c39952SRodrigo Vivi  *
60430c39952SRodrigo Vivi  * Returns 0 for success, negative error code otherwise.
60530c39952SRodrigo Vivi  */
606b2d75619SAnshuman Gupta int xe_pm_set_vram_threshold(struct xe_device *xe, u32 threshold)
607b2d75619SAnshuman Gupta {
608b2d75619SAnshuman Gupta 	struct ttm_resource_manager *man;
609b2d75619SAnshuman Gupta 	u32 vram_total_mb = 0;
610b2d75619SAnshuman Gupta 	int i;
611b2d75619SAnshuman Gupta 
612b2d75619SAnshuman Gupta 	for (i = XE_PL_VRAM0; i <= XE_PL_VRAM1; ++i) {
613b2d75619SAnshuman Gupta 		man = ttm_manager_type(&xe->ttm, i);
614b2d75619SAnshuman Gupta 		if (man)
615b2d75619SAnshuman Gupta 			vram_total_mb += DIV_ROUND_UP_ULL(man->size, 1024 * 1024);
616b2d75619SAnshuman Gupta 	}
617b2d75619SAnshuman Gupta 
618b2d75619SAnshuman Gupta 	drm_dbg(&xe->drm, "Total vram %u mb\n", vram_total_mb);
619b2d75619SAnshuman Gupta 
620b2d75619SAnshuman Gupta 	if (threshold > vram_total_mb)
621b2d75619SAnshuman Gupta 		return -EINVAL;
622b2d75619SAnshuman Gupta 
623b2d75619SAnshuman Gupta 	mutex_lock(&xe->d3cold.lock);
624b2d75619SAnshuman Gupta 	xe->d3cold.vram_threshold = threshold;
625b2d75619SAnshuman Gupta 	mutex_unlock(&xe->d3cold.lock);
626b2d75619SAnshuman Gupta 
627b2d75619SAnshuman Gupta 	return 0;
628b2d75619SAnshuman Gupta }
6292ef08b98SAnshuman Gupta 
63030c39952SRodrigo Vivi /**
63130c39952SRodrigo Vivi  * xe_pm_d3cold_allowed_toggle - Check conditions to toggle d3cold.allowed
63230c39952SRodrigo Vivi  * @xe: xe device instance
63330c39952SRodrigo Vivi  *
63430c39952SRodrigo Vivi  * To be called during runtime_pm idle callback.
63530c39952SRodrigo Vivi  * Check for all the D3Cold conditions ahead of runtime suspend.
63630c39952SRodrigo Vivi  */
6372ef08b98SAnshuman Gupta void xe_pm_d3cold_allowed_toggle(struct xe_device *xe)
6382ef08b98SAnshuman Gupta {
6392ef08b98SAnshuman Gupta 	struct ttm_resource_manager *man;
6402ef08b98SAnshuman Gupta 	u32 total_vram_used_mb = 0;
6412ef08b98SAnshuman Gupta 	u64 vram_used;
6422ef08b98SAnshuman Gupta 	int i;
6432ef08b98SAnshuman Gupta 
644e07aa913SRodrigo Vivi 	if (!xe->d3cold.capable) {
645e07aa913SRodrigo Vivi 		xe->d3cold.allowed = false;
646e07aa913SRodrigo Vivi 		return;
647e07aa913SRodrigo Vivi 	}
648e07aa913SRodrigo Vivi 
6492ef08b98SAnshuman Gupta 	for (i = XE_PL_VRAM0; i <= XE_PL_VRAM1; ++i) {
6502ef08b98SAnshuman Gupta 		man = ttm_manager_type(&xe->ttm, i);
6512ef08b98SAnshuman Gupta 		if (man) {
6522ef08b98SAnshuman Gupta 			vram_used = ttm_resource_manager_usage(man);
6532ef08b98SAnshuman Gupta 			total_vram_used_mb += DIV_ROUND_UP_ULL(vram_used, 1024 * 1024);
6542ef08b98SAnshuman Gupta 		}
6552ef08b98SAnshuman Gupta 	}
6562ef08b98SAnshuman Gupta 
6572ef08b98SAnshuman Gupta 	mutex_lock(&xe->d3cold.lock);
6582ef08b98SAnshuman Gupta 
6592ef08b98SAnshuman Gupta 	if (total_vram_used_mb < xe->d3cold.vram_threshold)
6602ef08b98SAnshuman Gupta 		xe->d3cold.allowed = true;
6612ef08b98SAnshuman Gupta 	else
6622ef08b98SAnshuman Gupta 		xe->d3cold.allowed = false;
6632ef08b98SAnshuman Gupta 
6642ef08b98SAnshuman Gupta 	mutex_unlock(&xe->d3cold.lock);
665ff765b77SMatthew Auld 
666ff765b77SMatthew Auld 	drm_dbg(&xe->drm,
667ff765b77SMatthew Auld 		"d3cold: allowed=%s\n", str_yes_no(xe->d3cold.allowed));
6682ef08b98SAnshuman Gupta }
669