xref: /linux/drivers/gpu/drm/xe/xe_pm.c (revision b1aa0491fad27f030c94ed42c873c3f46f5e7364)
1 // SPDX-License-Identifier: MIT
2 /*
3  * Copyright © 2022 Intel Corporation
4  */
5 
6 #include "xe_pm.h"
7 
8 #include <linux/pm_runtime.h>
9 
10 #include <drm/drm_managed.h>
11 #include <drm/ttm/ttm_placement.h>
12 
13 #include "display/xe_display.h"
14 #include "xe_bo.h"
15 #include "xe_bo_evict.h"
16 #include "xe_device.h"
17 #include "xe_device_sysfs.h"
18 #include "xe_ggtt.h"
19 #include "xe_gt.h"
20 #include "xe_guc.h"
21 #include "xe_irq.h"
22 #include "xe_pcode.h"
23 #include "xe_trace.h"
24 #include "xe_wa.h"
25 
26 /**
27  * DOC: Xe Power Management
28  *
29  * Xe PM implements the main routines for both system level suspend states and
30  * for the opportunistic runtime suspend states.
31  *
32  * System Level Suspend (S-States) - In general this is OS initiated suspend
33  * driven by ACPI for achieving S0ix (a.k.a. S2idle, freeze), S3 (suspend to ram),
34  * S4 (disk). The main functions here are `xe_pm_suspend` and `xe_pm_resume`. They
35  * are the main point for the suspend to and resume from these states.
36  *
37  * PCI Device Suspend (D-States) - This is the opportunistic PCIe device low power
38  * state D3, controlled by the PCI subsystem and ACPI with the help from the
39  * runtime_pm infrastructure.
40  * PCI D3 is special and can mean D3hot, where Vcc power is on for keeping memory
41  * alive and quicker low latency resume or D3Cold where Vcc power is off for
42  * better power savings.
43  * The Vcc control of PCI hierarchy can only be controlled at the PCI root port
44  * level, while the device driver can be behind multiple bridges/switches and
45  * paired with other devices. For this reason, the PCI subsystem cannot perform
46  * the transition towards D3Cold. The lowest runtime PM possible from the PCI
47  * subsystem is D3hot. Then, if all these paired devices in the same root port
48  * are in D3hot, ACPI will assist here and run its own methods (_PR3 and _OFF)
49  * to perform the transition from D3hot to D3cold. Xe may disallow this
50  * transition by calling pci_d3cold_disable(root_pdev) before going to runtime
51  * suspend. It will be based on runtime conditions such as VRAM usage for a
52  * quick and low latency resume for instance.
53  *
54  * Runtime PM - This infrastructure provided by the Linux kernel allows the
55  * device drivers to indicate when the can be runtime suspended, so the device
56  * could be put at D3 (if supported), or allow deeper package sleep states
57  * (PC-states), and/or other low level power states. Xe PM component provides
58  * `xe_pm_runtime_suspend` and `xe_pm_runtime_resume` functions that PCI
59  * subsystem will call before transition to/from runtime suspend.
60  *
61  * Also, Xe PM provides get and put functions that Xe driver will use to
62  * indicate activity. In order to avoid locking complications with the memory
63  * management, whenever possible, these get and put functions needs to be called
64  * from the higher/outer levels.
65  * The main cases that need to be protected from the outer levels are: IOCTL,
66  * sysfs, debugfs, dma-buf sharing, GPU execution.
67  *
68  * This component is not responsible for GT idleness (RC6) nor GT frequency
69  * management (RPS).
70  */
71 
72 #ifdef CONFIG_LOCKDEP
73 static struct lockdep_map xe_pm_runtime_d3cold_map = {
74 	.name = "xe_rpm_d3cold_map"
75 };
76 
77 static struct lockdep_map xe_pm_runtime_nod3cold_map = {
78 	.name = "xe_rpm_nod3cold_map"
79 };
80 #endif
81 
82 static bool __maybe_unused xe_rpm_reclaim_safe(const struct xe_device *xe)
83 {
84 	return !xe->d3cold.capable && !xe->info.has_sriov;
85 }
86 
87 static void xe_rpm_lockmap_acquire(const struct xe_device *xe)
88 {
89 	lock_map_acquire(xe_rpm_reclaim_safe(xe) ?
90 			 &xe_pm_runtime_nod3cold_map :
91 			 &xe_pm_runtime_d3cold_map);
92 }
93 
94 static void xe_rpm_lockmap_release(const struct xe_device *xe)
95 {
96 	lock_map_release(xe_rpm_reclaim_safe(xe) ?
97 			 &xe_pm_runtime_nod3cold_map :
98 			 &xe_pm_runtime_d3cold_map);
99 }
100 
101 /**
102  * xe_pm_suspend - Helper for System suspend, i.e. S0->S3 / S0->S2idle
103  * @xe: xe device instance
104  *
105  * Return: 0 on success
106  */
107 int xe_pm_suspend(struct xe_device *xe)
108 {
109 	struct xe_gt *gt;
110 	u8 id;
111 	int err;
112 
113 	drm_dbg(&xe->drm, "Suspending device\n");
114 	trace_xe_pm_suspend(xe, __builtin_return_address(0));
115 
116 	for_each_gt(gt, xe, id)
117 		xe_gt_suspend_prepare(gt);
118 
119 	xe_display_pm_suspend(xe, false);
120 
121 	/* FIXME: Super racey... */
122 	err = xe_bo_evict_all(xe);
123 	if (err)
124 		goto err;
125 
126 	for_each_gt(gt, xe, id) {
127 		err = xe_gt_suspend(gt);
128 		if (err) {
129 			xe_display_pm_resume(xe, false);
130 			goto err;
131 		}
132 	}
133 
134 	xe_irq_suspend(xe);
135 
136 	xe_display_pm_suspend_late(xe);
137 
138 	drm_dbg(&xe->drm, "Device suspended\n");
139 	return 0;
140 err:
141 	drm_dbg(&xe->drm, "Device suspend failed %d\n", err);
142 	return err;
143 }
144 
145 /**
146  * xe_pm_resume - Helper for System resume S3->S0 / S2idle->S0
147  * @xe: xe device instance
148  *
149  * Return: 0 on success
150  */
151 int xe_pm_resume(struct xe_device *xe)
152 {
153 	struct xe_tile *tile;
154 	struct xe_gt *gt;
155 	u8 id;
156 	int err;
157 
158 	drm_dbg(&xe->drm, "Resuming device\n");
159 	trace_xe_pm_resume(xe, __builtin_return_address(0));
160 
161 	for_each_tile(tile, xe, id)
162 		xe_wa_apply_tile_workarounds(tile);
163 
164 	err = xe_pcode_ready(xe, true);
165 	if (err)
166 		return err;
167 
168 	xe_display_pm_resume_early(xe);
169 
170 	/*
171 	 * This only restores pinned memory which is the memory required for the
172 	 * GT(s) to resume.
173 	 */
174 	err = xe_bo_restore_kernel(xe);
175 	if (err)
176 		goto err;
177 
178 	xe_irq_resume(xe);
179 
180 	for_each_gt(gt, xe, id)
181 		xe_gt_resume(gt);
182 
183 	xe_display_pm_resume(xe, false);
184 
185 	err = xe_bo_restore_user(xe);
186 	if (err)
187 		goto err;
188 
189 	drm_dbg(&xe->drm, "Device resumed\n");
190 	return 0;
191 err:
192 	drm_dbg(&xe->drm, "Device resume failed %d\n", err);
193 	return err;
194 }
195 
196 static bool xe_pm_pci_d3cold_capable(struct xe_device *xe)
197 {
198 	struct pci_dev *pdev = to_pci_dev(xe->drm.dev);
199 	struct pci_dev *root_pdev;
200 
201 	root_pdev = pcie_find_root_port(pdev);
202 	if (!root_pdev)
203 		return false;
204 
205 	/* D3Cold requires PME capability */
206 	if (!pci_pme_capable(root_pdev, PCI_D3cold)) {
207 		drm_dbg(&xe->drm, "d3cold: PME# not supported\n");
208 		return false;
209 	}
210 
211 	/* D3Cold requires _PR3 power resource */
212 	if (!pci_pr3_present(root_pdev)) {
213 		drm_dbg(&xe->drm, "d3cold: ACPI _PR3 not present\n");
214 		return false;
215 	}
216 
217 	return true;
218 }
219 
220 static void xe_pm_runtime_init(struct xe_device *xe)
221 {
222 	struct device *dev = xe->drm.dev;
223 
224 	/*
225 	 * Disable the system suspend direct complete optimization.
226 	 * We need to ensure that the regular device suspend/resume functions
227 	 * are called since our runtime_pm cannot guarantee local memory
228 	 * eviction for d3cold.
229 	 * TODO: Check HDA audio dependencies claimed by i915, and then enforce
230 	 *       this option to integrated graphics as well.
231 	 */
232 	if (IS_DGFX(xe))
233 		dev_pm_set_driver_flags(dev, DPM_FLAG_NO_DIRECT_COMPLETE);
234 
235 	pm_runtime_use_autosuspend(dev);
236 	pm_runtime_set_autosuspend_delay(dev, 1000);
237 	pm_runtime_set_active(dev);
238 	pm_runtime_allow(dev);
239 	pm_runtime_mark_last_busy(dev);
240 	pm_runtime_put(dev);
241 }
242 
243 int xe_pm_init_early(struct xe_device *xe)
244 {
245 	int err;
246 
247 	INIT_LIST_HEAD(&xe->mem_access.vram_userfault.list);
248 
249 	err = drmm_mutex_init(&xe->drm, &xe->mem_access.vram_userfault.lock);
250 	if (err)
251 		return err;
252 
253 	err = drmm_mutex_init(&xe->drm, &xe->d3cold.lock);
254 	if (err)
255 		return err;
256 
257 	return 0;
258 }
259 
260 /**
261  * xe_pm_init - Initialize Xe Power Management
262  * @xe: xe device instance
263  *
264  * This component is responsible for System and Device sleep states.
265  *
266  * Returns 0 for success, negative error code otherwise.
267  */
268 int xe_pm_init(struct xe_device *xe)
269 {
270 	int err;
271 
272 	/* For now suspend/resume is only allowed with GuC */
273 	if (!xe_device_uc_enabled(xe))
274 		return 0;
275 
276 	xe->d3cold.capable = xe_pm_pci_d3cold_capable(xe);
277 
278 	if (xe->d3cold.capable) {
279 		err = xe_device_sysfs_init(xe);
280 		if (err)
281 			return err;
282 
283 		err = xe_pm_set_vram_threshold(xe, DEFAULT_VRAM_THRESHOLD);
284 		if (err)
285 			return err;
286 	}
287 
288 	xe_pm_runtime_init(xe);
289 
290 	return 0;
291 }
292 
293 /**
294  * xe_pm_runtime_fini - Finalize Runtime PM
295  * @xe: xe device instance
296  */
297 void xe_pm_runtime_fini(struct xe_device *xe)
298 {
299 	struct device *dev = xe->drm.dev;
300 
301 	pm_runtime_get_sync(dev);
302 	pm_runtime_forbid(dev);
303 }
304 
305 static void xe_pm_write_callback_task(struct xe_device *xe,
306 				      struct task_struct *task)
307 {
308 	WRITE_ONCE(xe->pm_callback_task, task);
309 
310 	/*
311 	 * Just in case it's somehow possible for our writes to be reordered to
312 	 * the extent that something else re-uses the task written in
313 	 * pm_callback_task. For example after returning from the callback, but
314 	 * before the reordered write that resets pm_callback_task back to NULL.
315 	 */
316 	smp_mb(); /* pairs with xe_pm_read_callback_task */
317 }
318 
319 struct task_struct *xe_pm_read_callback_task(struct xe_device *xe)
320 {
321 	smp_mb(); /* pairs with xe_pm_write_callback_task */
322 
323 	return READ_ONCE(xe->pm_callback_task);
324 }
325 
326 /**
327  * xe_pm_runtime_suspended - Check if runtime_pm state is suspended
328  * @xe: xe device instance
329  *
330  * This does not provide any guarantee that the device is going to remain
331  * suspended as it might be racing with the runtime state transitions.
332  * It can be used only as a non-reliable assertion, to ensure that we are not in
333  * the sleep state while trying to access some memory for instance.
334  *
335  * Returns true if PCI device is suspended, false otherwise.
336  */
337 bool xe_pm_runtime_suspended(struct xe_device *xe)
338 {
339 	return pm_runtime_suspended(xe->drm.dev);
340 }
341 
342 /**
343  * xe_pm_runtime_suspend - Prepare our device for D3hot/D3Cold
344  * @xe: xe device instance
345  *
346  * Returns 0 for success, negative error code otherwise.
347  */
348 int xe_pm_runtime_suspend(struct xe_device *xe)
349 {
350 	struct xe_bo *bo, *on;
351 	struct xe_gt *gt;
352 	u8 id;
353 	int err = 0;
354 
355 	trace_xe_pm_runtime_suspend(xe, __builtin_return_address(0));
356 	/* Disable access_ongoing asserts and prevent recursive pm calls */
357 	xe_pm_write_callback_task(xe, current);
358 
359 	/*
360 	 * The actual xe_pm_runtime_put() is always async underneath, so
361 	 * exactly where that is called should makes no difference to us. However
362 	 * we still need to be very careful with the locks that this callback
363 	 * acquires and the locks that are acquired and held by any callers of
364 	 * xe_runtime_pm_get(). We already have the matching annotation
365 	 * on that side, but we also need it here. For example lockdep should be
366 	 * able to tell us if the following scenario is in theory possible:
367 	 *
368 	 * CPU0                          | CPU1 (kworker)
369 	 * lock(A)                       |
370 	 *                               | xe_pm_runtime_suspend()
371 	 *                               |      lock(A)
372 	 * xe_pm_runtime_get()           |
373 	 *
374 	 * This will clearly deadlock since rpm core needs to wait for
375 	 * xe_pm_runtime_suspend() to complete, but here we are holding lock(A)
376 	 * on CPU0 which prevents CPU1 making forward progress.  With the
377 	 * annotation here and in xe_pm_runtime_get() lockdep will see
378 	 * the potential lock inversion and give us a nice splat.
379 	 */
380 	xe_rpm_lockmap_acquire(xe);
381 
382 	/*
383 	 * Applying lock for entire list op as xe_ttm_bo_destroy and xe_bo_move_notify
384 	 * also checks and delets bo entry from user fault list.
385 	 */
386 	mutex_lock(&xe->mem_access.vram_userfault.lock);
387 	list_for_each_entry_safe(bo, on,
388 				 &xe->mem_access.vram_userfault.list, vram_userfault_link)
389 		xe_bo_runtime_pm_release_mmap_offset(bo);
390 	mutex_unlock(&xe->mem_access.vram_userfault.lock);
391 
392 	xe_display_pm_runtime_suspend(xe);
393 
394 	if (xe->d3cold.allowed) {
395 		err = xe_bo_evict_all(xe);
396 		if (err)
397 			goto out;
398 	}
399 
400 	for_each_gt(gt, xe, id) {
401 		err = xe_gt_suspend(gt);
402 		if (err)
403 			goto out;
404 	}
405 
406 	xe_irq_suspend(xe);
407 
408 	if (xe->d3cold.allowed)
409 		xe_display_pm_suspend_late(xe);
410 out:
411 	if (err)
412 		xe_display_pm_resume(xe, true);
413 	xe_rpm_lockmap_release(xe);
414 	xe_pm_write_callback_task(xe, NULL);
415 	return err;
416 }
417 
418 /**
419  * xe_pm_runtime_resume - Waking up from D3hot/D3Cold
420  * @xe: xe device instance
421  *
422  * Returns 0 for success, negative error code otherwise.
423  */
424 int xe_pm_runtime_resume(struct xe_device *xe)
425 {
426 	struct xe_gt *gt;
427 	u8 id;
428 	int err = 0;
429 
430 	trace_xe_pm_runtime_resume(xe, __builtin_return_address(0));
431 	/* Disable access_ongoing asserts and prevent recursive pm calls */
432 	xe_pm_write_callback_task(xe, current);
433 
434 	xe_rpm_lockmap_acquire(xe);
435 
436 	if (xe->d3cold.allowed) {
437 		err = xe_pcode_ready(xe, true);
438 		if (err)
439 			goto out;
440 
441 		xe_display_pm_resume_early(xe);
442 
443 		/*
444 		 * This only restores pinned memory which is the memory
445 		 * required for the GT(s) to resume.
446 		 */
447 		err = xe_bo_restore_kernel(xe);
448 		if (err)
449 			goto out;
450 	}
451 
452 	xe_irq_resume(xe);
453 
454 	for_each_gt(gt, xe, id)
455 		xe_gt_resume(gt);
456 
457 	xe_display_pm_runtime_resume(xe);
458 
459 	if (xe->d3cold.allowed) {
460 		err = xe_bo_restore_user(xe);
461 		if (err)
462 			goto out;
463 	}
464 
465 out:
466 	xe_rpm_lockmap_release(xe);
467 	xe_pm_write_callback_task(xe, NULL);
468 	return err;
469 }
470 
471 /*
472  * For places where resume is synchronous it can be quite easy to deadlock
473  * if we are not careful. Also in practice it might be quite timing
474  * sensitive to ever see the 0 -> 1 transition with the callers locks
475  * held, so deadlocks might exist but are hard for lockdep to ever see.
476  * With this in mind, help lockdep learn about the potentially scary
477  * stuff that can happen inside the runtime_resume callback by acquiring
478  * a dummy lock (it doesn't protect anything and gets compiled out on
479  * non-debug builds).  Lockdep then only needs to see the
480  * xe_pm_runtime_xxx_map -> runtime_resume callback once, and then can
481  * hopefully validate all the (callers_locks) -> xe_pm_runtime_xxx_map.
482  * For example if the (callers_locks) are ever grabbed in the
483  * runtime_resume callback, lockdep should give us a nice splat.
484  */
485 static void xe_rpm_might_enter_cb(const struct xe_device *xe)
486 {
487 	xe_rpm_lockmap_acquire(xe);
488 	xe_rpm_lockmap_release(xe);
489 }
490 
491 /*
492  * Prime the lockdep maps for known locking orders that need to
493  * be supported but that may not always occur on all systems.
494  */
495 static void xe_pm_runtime_lockdep_prime(void)
496 {
497 	struct dma_resv lockdep_resv;
498 
499 	dma_resv_init(&lockdep_resv);
500 	lock_map_acquire(&xe_pm_runtime_d3cold_map);
501 	/* D3Cold takes the dma_resv locks to evict bos */
502 	dma_resv_lock(&lockdep_resv, NULL);
503 	dma_resv_unlock(&lockdep_resv);
504 	lock_map_release(&xe_pm_runtime_d3cold_map);
505 
506 	/* Shrinkers might like to wake up the device under reclaim. */
507 	fs_reclaim_acquire(GFP_KERNEL);
508 	lock_map_acquire(&xe_pm_runtime_nod3cold_map);
509 	lock_map_release(&xe_pm_runtime_nod3cold_map);
510 	fs_reclaim_release(GFP_KERNEL);
511 }
512 
513 /**
514  * xe_pm_runtime_get - Get a runtime_pm reference and resume synchronously
515  * @xe: xe device instance
516  */
517 void xe_pm_runtime_get(struct xe_device *xe)
518 {
519 	trace_xe_pm_runtime_get(xe, __builtin_return_address(0));
520 	pm_runtime_get_noresume(xe->drm.dev);
521 
522 	if (xe_pm_read_callback_task(xe) == current)
523 		return;
524 
525 	xe_rpm_might_enter_cb(xe);
526 	pm_runtime_resume(xe->drm.dev);
527 }
528 
529 /**
530  * xe_pm_runtime_put - Put the runtime_pm reference back and mark as idle
531  * @xe: xe device instance
532  */
533 void xe_pm_runtime_put(struct xe_device *xe)
534 {
535 	trace_xe_pm_runtime_put(xe, __builtin_return_address(0));
536 	if (xe_pm_read_callback_task(xe) == current) {
537 		pm_runtime_put_noidle(xe->drm.dev);
538 	} else {
539 		pm_runtime_mark_last_busy(xe->drm.dev);
540 		pm_runtime_put(xe->drm.dev);
541 	}
542 }
543 
544 /**
545  * xe_pm_runtime_get_ioctl - Get a runtime_pm reference before ioctl
546  * @xe: xe device instance
547  *
548  * Returns: Any number greater than or equal to 0 for success, negative error
549  * code otherwise.
550  */
551 int xe_pm_runtime_get_ioctl(struct xe_device *xe)
552 {
553 	trace_xe_pm_runtime_get_ioctl(xe, __builtin_return_address(0));
554 	if (WARN_ON(xe_pm_read_callback_task(xe) == current))
555 		return -ELOOP;
556 
557 	xe_rpm_might_enter_cb(xe);
558 	return pm_runtime_get_sync(xe->drm.dev);
559 }
560 
561 /**
562  * xe_pm_runtime_get_if_active - Get a runtime_pm reference if device active
563  * @xe: xe device instance
564  *
565  * Return: True if device is awake (regardless the previous number of references)
566  * and a new reference was taken, false otherwise.
567  */
568 bool xe_pm_runtime_get_if_active(struct xe_device *xe)
569 {
570 	return pm_runtime_get_if_active(xe->drm.dev) > 0;
571 }
572 
573 /**
574  * xe_pm_runtime_get_if_in_use - Get a new reference if device is active with previous ref taken
575  * @xe: xe device instance
576  *
577  * Return: True if device is awake, a previous reference had been already taken,
578  * and a new reference was now taken, false otherwise.
579  */
580 bool xe_pm_runtime_get_if_in_use(struct xe_device *xe)
581 {
582 	if (xe_pm_read_callback_task(xe) == current) {
583 		/* The device is awake, grab the ref and move on */
584 		pm_runtime_get_noresume(xe->drm.dev);
585 		return true;
586 	}
587 
588 	return pm_runtime_get_if_in_use(xe->drm.dev) > 0;
589 }
590 
591 /**
592  * xe_pm_runtime_get_noresume - Bump runtime PM usage counter without resuming
593  * @xe: xe device instance
594  *
595  * This function should be used in inner places where it is surely already
596  * protected by outer-bound callers of `xe_pm_runtime_get`.
597  * It will warn if not protected.
598  * The reference should be put back after this function regardless, since it
599  * will always bump the usage counter, regardless.
600  */
601 void xe_pm_runtime_get_noresume(struct xe_device *xe)
602 {
603 	bool ref;
604 
605 	ref = xe_pm_runtime_get_if_in_use(xe);
606 
607 	if (drm_WARN(&xe->drm, !ref, "Missing outer runtime PM protection\n"))
608 		pm_runtime_get_noresume(xe->drm.dev);
609 }
610 
611 /**
612  * xe_pm_runtime_resume_and_get - Resume, then get a runtime_pm ref if awake.
613  * @xe: xe device instance
614  *
615  * Returns: True if device is awake and the reference was taken, false otherwise.
616  */
617 bool xe_pm_runtime_resume_and_get(struct xe_device *xe)
618 {
619 	if (xe_pm_read_callback_task(xe) == current) {
620 		/* The device is awake, grab the ref and move on */
621 		pm_runtime_get_noresume(xe->drm.dev);
622 		return true;
623 	}
624 
625 	xe_rpm_might_enter_cb(xe);
626 	return pm_runtime_resume_and_get(xe->drm.dev) >= 0;
627 }
628 
629 /**
630  * xe_pm_assert_unbounded_bridge - Disable PM on unbounded pcie parent bridge
631  * @xe: xe device instance
632  */
633 void xe_pm_assert_unbounded_bridge(struct xe_device *xe)
634 {
635 	struct pci_dev *pdev = to_pci_dev(xe->drm.dev);
636 	struct pci_dev *bridge = pci_upstream_bridge(pdev);
637 
638 	if (!bridge)
639 		return;
640 
641 	if (!bridge->driver) {
642 		drm_warn(&xe->drm, "unbounded parent pci bridge, device won't support any PM support.\n");
643 		device_set_pm_not_required(&pdev->dev);
644 	}
645 }
646 
647 /**
648  * xe_pm_set_vram_threshold - Set a vram threshold for allowing/blocking D3Cold
649  * @xe: xe device instance
650  * @threshold: VRAM size in bites for the D3cold threshold
651  *
652  * Returns 0 for success, negative error code otherwise.
653  */
654 int xe_pm_set_vram_threshold(struct xe_device *xe, u32 threshold)
655 {
656 	struct ttm_resource_manager *man;
657 	u32 vram_total_mb = 0;
658 	int i;
659 
660 	for (i = XE_PL_VRAM0; i <= XE_PL_VRAM1; ++i) {
661 		man = ttm_manager_type(&xe->ttm, i);
662 		if (man)
663 			vram_total_mb += DIV_ROUND_UP_ULL(man->size, 1024 * 1024);
664 	}
665 
666 	drm_dbg(&xe->drm, "Total vram %u mb\n", vram_total_mb);
667 
668 	if (threshold > vram_total_mb)
669 		return -EINVAL;
670 
671 	mutex_lock(&xe->d3cold.lock);
672 	xe->d3cold.vram_threshold = threshold;
673 	mutex_unlock(&xe->d3cold.lock);
674 
675 	return 0;
676 }
677 
678 /**
679  * xe_pm_d3cold_allowed_toggle - Check conditions to toggle d3cold.allowed
680  * @xe: xe device instance
681  *
682  * To be called during runtime_pm idle callback.
683  * Check for all the D3Cold conditions ahead of runtime suspend.
684  */
685 void xe_pm_d3cold_allowed_toggle(struct xe_device *xe)
686 {
687 	struct ttm_resource_manager *man;
688 	u32 total_vram_used_mb = 0;
689 	u64 vram_used;
690 	int i;
691 
692 	if (!xe->d3cold.capable) {
693 		xe->d3cold.allowed = false;
694 		return;
695 	}
696 
697 	for (i = XE_PL_VRAM0; i <= XE_PL_VRAM1; ++i) {
698 		man = ttm_manager_type(&xe->ttm, i);
699 		if (man) {
700 			vram_used = ttm_resource_manager_usage(man);
701 			total_vram_used_mb += DIV_ROUND_UP_ULL(vram_used, 1024 * 1024);
702 		}
703 	}
704 
705 	mutex_lock(&xe->d3cold.lock);
706 
707 	if (total_vram_used_mb < xe->d3cold.vram_threshold)
708 		xe->d3cold.allowed = true;
709 	else
710 		xe->d3cold.allowed = false;
711 
712 	mutex_unlock(&xe->d3cold.lock);
713 
714 	drm_dbg(&xe->drm,
715 		"d3cold: allowed=%s\n", str_yes_no(xe->d3cold.allowed));
716 }
717 
718 /**
719  * xe_pm_module_init() - Perform xe_pm specific module initialization.
720  *
721  * Return: 0 on success. Currently doesn't fail.
722  */
723 int __init xe_pm_module_init(void)
724 {
725 	xe_pm_runtime_lockdep_prime();
726 	return 0;
727 }
728